Episode 46: Catch-all Route

Posted almost 7 years back at Railscasts

Sometimes you need to add complex/dynamic routes. This is often impossible to do in routes.rb, but do not worry. It can be accomplished with a catch-all route. See how in this episode.

Tumbleranting

Posted almost 7 years back at zerosum dirt(nap) - Home

Seth Godin is a nicer guy than I am (he’s probably better dressed, too): I would have ballparked his quote a little higher. Explanation: there’s a “don’t waste my time” fee.

As a freelancer, this happens to me all the time and it’s muy frustrating. I like hearing ideas, I like helping you structure your approach, I love developing solutions, applications, tools for you. I don’t even mind giving estimates and free advice. But in order to do that, you need to tell me what it is you want.

And no, “just like digg but with/for xxx” isn’t what I’m talking about :-).

Code Digest #1

Posted almost 7 years back at Revolution On Rails

When you program for a living, you write lots of code. There is often some code that you are fond of. We start the Code Digest series to present such code written by the RHG developers. We encourage other teams and individual developers to share similar snippets in their blogs so we all can learn from each other and become better rails developers.

Daniel Silva



<%= link_to(image_tag("/images/icon.gif", :style=>"padding-right:0px;"), {:controller => '/registration', :action => 'login', :dest => request.request_uri}, {:style => ""}) %> 

With this line, I can create an image link using Rails code, to take advantage of the power of routes in Rails while still creating this kind of structure: <a href="#"><img border="0"/></a>



Jack Dempsey



Ok, so here's something useful for using keyboard short cuts to navigate through a small supporting application:
def include_keyboard_shortcuts

hotkey = 'Ctrl+Shift'
code_string = ''
keys = {
'm'=>'/account_info/show',
's'=>'/account_info/index',
'c'=>'/menu/go?menu_action=new_customer',
'p'=>'/products',
'o'=>'/account_info/orders',
't'=>'/order_tracker',
'x'=>'/auth_sessions/destroy',
'r'=>'/current_users/reset_password'
}

current_user_needed = %w{m o r}

keys.each_pair do |k,v|
next if current_user_needed.include?(k) && current_user.nil?
code_string << "shortcut('#{hotkey}+hm#{k}',function() { document.location.href='#{v}' });\n"
end

javascript_tag(code_string)

end



Then in a layout file just:
<%= javascript_include_tag 'shortcuts' %>
<%= include_keyboard_shortcuts %>



Makes use of a nice shortcuts.js lib.


Val Aleksenko


This is a fragment of a conversation on our internal IRC with my solution for refactoring a piece of code:
May 07 14:07:56 <Anthro> There has to be a better way to do this (x and y are non-negative integers): increment = (x!=0) ? ( (y!=0) ? 0 : 1 ) : -1
May 07 14:08:22 <Anthro> I think it can be done with math instead of logic.
May 07 14:16:56 <jack> Anthro: have you thought about using sin and cos? ;-)
May 07 14:18:42 <muzzy> (x <=> 0) <=> (y <=> 0)



Granted, it is not as clear as the original, but it is hard to resist it from the aesthetic point of view.

Val Aleksenko


I love writing dynamically generated methods and classes in Ruby as the next guy. When I was writing acts_as_readonlyable, I needed to manage multiple connections. I found that the easiest solution for managing active connections was generation of an AR class for each read-only definition and borrowing the connection from it. The usage is acts_as_readonlyable :read_only_entry_in_database_config.
def acts_as_readonlyable(readonly_db)
define_readonly_class(readonly_db) unless ActiveRecord.const_defined?(readonly_class_name(readonly_db))
end

def readonly_class_name(db)
"Generated#{ db.camelize }"
end

def define_readonly_class(db)
ActiveRecord.module_eval %Q!
class #{ readonly_class_name(db) } < Base
self.abstract_class = true
establish_connection configurations[RAILS_ENV]['#{ db }']
end
!

end



Warren Konkel


The ConfigFile class is a quick and easy way to load YAML files located in your Rails application's config directory. Simply place a YAML file into your config directory and then access it like a hash. For example this will read your config/database.yml: ConfigFile['database']['production']['adapter'].
class ConfigFile
def self.[](arg)
@@cached_configs ||= {}
@@cached_config_mtimes ||= {}

base_name = "config/#{arg}.yml"
filename = File.join(RAILS_ROOT, base_name)
raise "ERROR: Config not found: #{base_name}" unless File.exists?(base_name)

if @@cached_configs[arg].nil? || @@cached_config_mtimes[arg] < File.stat(filename).mtime.to_i
@@cached_configs[arg] = YAML.load(File.open(filename))
@@cached_config_mtimes[arg] = File.stat(filename).mtime.to_i
end

@@cached_configs[arg]
end
end

Code Digest #1

Posted almost 7 years back at Revolution On Rails

When you program for a living, you write lots of code. There is often some code that you are fond of. We start the Code Digest series to present such code written by the RHG developers. We encourage other teams and individual developers to share similar snippets in their blogs so we all can learn from each other and become better rails developers.

Daniel Silva



<%= link_to(image_tag("/images/icon.gif", :style=>"padding-right:0px;"), {:controller => '/registration', :action => 'login', :dest => request.request_uri}, {:style => ""}) %> 

With this line, I can create an image link using Rails code, to take advantage of the power of routes in Rails while still creating this kind of structure: <a href="#"><img border="0"/></a>



Jack Dempsey



Ok, so here's something useful for using keyboard short cuts to navigate through a small supporting application:
def include_keyboard_shortcuts

hotkey = 'Ctrl+Shift'
code_string = ''
keys = {
'm'=>'/account_info/show',
's'=>'/account_info/index',
'c'=>'/menu/go?menu_action=new_customer',
'p'=>'/products',
'o'=>'/account_info/orders',
't'=>'/order_tracker',
'x'=>'/auth_sessions/destroy',
'r'=>'/current_users/reset_password'
}

current_user_needed = %w{m o r}

keys.each_pair do |k,v|
next if current_user_needed.include?(k) && current_user.nil?
code_string << "shortcut('#{hotkey}+hm#{k}',function() { document.location.href='#{v}' });\n"
end

javascript_tag(code_string)

end



Then in a layout file just:
<%= javascript_include_tag 'shortcuts' %>
<%= include_keyboard_shortcuts %>



Makes use of a nice shortcuts.js lib.


Val Aleksenko


This is a fragment of a conversation on our internal IRC with my solution for refactoring a piece of code:
May 07 14:07:56 <Anthro> There has to be a better way to do this (x and y are non-negative integers): increment = (x!=0) ? ( (y!=0) ? 0 : 1 ) : -1
May 07 14:08:22 <Anthro> I think it can be done with math instead of logic.
May 07 14:16:56 <jack> Anthro: have you thought about using sin and cos? ;-)
May 07 14:18:42 <muzzy> (x <=> 0) <=> (y <=> 0)



Granted, it is not as clear as the original, but it is hard to resist it from the aesthetic point of view.

Val Aleksenko


I love writing dynamically generated methods and classes in Ruby as the next guy. When I was writing acts_as_readonlyable, I needed to manage multiple connections. I found that the easiest solution for managing active connections was generation of an AR class for each read-only definition and borrowing the connection from it. The usage is acts_as_readonlyable :read_only_entry_in_database_config.
def acts_as_readonlyable(readonly_db)
define_readonly_class(readonly_db) unless ActiveRecord.const_defined?(readonly_class_name(readonly_db))
end

def readonly_class_name(db)
"Generated#{ db.camelize }"
end

def define_readonly_class(db)
ActiveRecord.module_eval %Q!
class #{ readonly_class_name(db) } < Base
self.abstract_class = true
establish_connection configurations[RAILS_ENV]['#{ db }']
end
!

end



Warren Konkel


The ConfigFile class is a quick and easy way to load YAML files located in your Rails application's config directory. Simply place a YAML file into your config directory and then access it like a hash. For example this will read your config/database.yml: ConfigFile['database']['production']['adapter'].
class ConfigFile
def self.[](arg)
@@cached_configs ||= {}
@@cached_config_mtimes ||= {}

base_name = "config/#{arg}.yml"
filename = File.join(RAILS_ROOT, base_name)
raise "ERROR: Config not found: #{base_name}" unless File.exists?(base_name)

if @@cached_configs[arg].nil? || @@cached_config_mtimes[arg] < File.stat(filename).mtime.to_i
@@cached_configs[arg] = YAML.load(File.open(filename))
@@cached_config_mtimes[arg] = File.stat(filename).mtime.to_i
end

@@cached_configs[arg]
end
end

Ticketish

Posted almost 7 years back at benmyles.com - Home

It has been a while since I've posted on my personal blog. In fact, I've decided to start fresh. Lately I've been very busy at Integral Impressions working on some great Ruby on Rails projects.

One such project is Ticketish. Just a few days ago we started sending out invitations for a private beta program. Ticketish is a simple issue/bug/ticket management web app, designed to do what it needs to do and then get out of the way.

We built Ticketish for internal use, and have been using it for some months now. We've come to really appreciate it, and decided to add some polish and release it to the world. If you're interested, go sign up for the beta and I'll get an invitation to you within a day or two (probably quicker).

The State (And Future) Of The UJS Plugin

Posted almost 7 years back at danwebb.net - Home

Over the past few weeks loads of people have been asking me about what’s going on the UJS plugin. It’s obviously fallen in to disrepair so people, understandably, have been concerned. There has been a reason for this aside from the fact that I am a lazy barsteward (which of course I am, but that’s beside the point). Here is a letter I’ve just written to the UJS mailing list that I’d thought I’d post here to try to get a little more feedback. It’s a little bit long but bear with me…

I’ve been chatting to Luke and users of UJS about what to do with it and still haven’t quiet decided hence the lack of news but below is a rundown of where we are at on the whole thing. However, this is definitely personal opinion and doesn’t necessarily represent Luke’s opinion on the matter.

Essentially, the status is that, of late, I personally have not used UJS at all and have found a much better process by using Low Pro on its own without all the Ruby scaffolding of the UJS plugin. Secondarily, after talking to lots of developers at RailsConf it seems that the UJS plugin has failed to truly achieve it’s main goal which is to get Rails developers to write JavaScript using progressive enhancement. Many people seem to mainly use the plugin to get their JavaScript in to a separate file which is actually not even essential to progressive enhancement and I think this is a failing in the design of UJS itself. To achieve progressive enhancement you really need to think of JavaScript as a separate layer on top of a working HTML application but UJS lets you get away with keeping behavior in your views and hence leads many developers to think in the same way as they did before but think they are unobtrusive because they don’t see any JavaScript in their HTML – which is obviously not what we wanted to achieve. While many people can and do successfully use UJS for progressive enhancement even more seem not to – UJS has not been the ‘angel on your shoulder’ that I originally wanted it to be.

On top of this, the method by which the generated JavaScript is kept in the session has many limitations which myself and Luke have been aware of from the start. It’s not generally a good idea to keep this much information in the session (in fact, normally I never store more than a user ID if I can help it) and while acceptable for light to medium use it does have an upper limit depending on the type of session storage you are using. Rails edge now uses cookies to store session info by default which have a very very low limit which will cripple UJS completely. We have considered other alternatives such as some kind of file based storage but every time it just strikes me as too much scaffolding just to allow developers to put behavior in their view files which, as a said above, I’ve come to believe is a really bad idea anyway.

One of the things that I do personally like and something that has received the most positive feedback are the behavior helpers (the make_*) stuff which essentially encapsulate common tasks with sensible defaults in a very Rails like way. It’s a real time saver and the conventions provided mean that the best path (such as using this.href for the Ajax url) is the easiest. Recently, I’ve come to do this in my own projects via Low Pro and it’s behavior ‘classes’ (although they need a better name!). Now via Low Pro I can write stuff like:

Event.addBehavior({
  '.product a.description' : Remote.Link({ update : 'product_description' }),
  '.product' : Draggable({ revert : true }),
  '#basket' : Droppable
});

I really like this and am slowly building up a library (which you can see if you look at the Low Pro trunk) of common behaviors. There’s a date picker, a drag/drop implementation and the remote stuff I’ve illustrated above. I’m planning on writing autocompleters and in-place editors as behaviors as well. But behaviors have proven really easy to write and I love them as a tool for building site specific components. In fact, as I’ve worked with Low Pro it’s become apparent to me that behaviors are by far the killer feature which is interesting as they were just an experiment I hacked together one day without much thought.

So what to do? Well, there’s two ways to go as far as I can see. The first is to shut down development on UJS completely (or hand it over to another party if anyone is interested) and go on to promote the techniques of implementing progressive enhancement using Low Pro that I’ve found to be so successful recently. This could possibly be via the UJS4Rails site or through my own site – I’m not sure which would be a better platform right now.

The other would be to re-think the UJS plugin totally and go for some kind of 2.0 release that would take a completely different tack. However, all of the ideas for this I’ve thought of or heard so far don’t really compel me to write them. I think to work on this myself I’d need to be sure that I’d want to use it and so far this is not the case but any ideas and feedback are very welcome so please do drop me a mail or feedback on this list.

Either way, we need to make fixes to the current plugin to make it work with Rails 1.2.3 which I’ve been working on recently but I’d love patches if you’ve already solved these issues yourself (which it appears many of you have).

So yes, that’s it. Let me know what you think, I’d appreciate any feedback you have.

Large Mephisto Deployment

Posted almost 7 years back at Mephisto - Home

I’m not sure how many other large sites use Mephisto, but I managed to deploy it to the-leaky-cauldron.org the other day (after lots of heavy modding to get things like polls and article ratings working). Leaky gets about 3 million unique visitors a month – and with 9,000 articles and over 300,000 comments (not all of which have converted yet) I thought I’d let you know. —Mephisto group message by Nick Poulden.

I’m not sure, but I think that’s one of the largest Mephisto installations around. Great job, Nick Poulden!

New DRYML getting closer

Posted almost 7 years back at The Hobo Blog

If you’re wondering why things have been a bit quiet around here of late, it’s because we’ve been taking a pretty hard look at where we’ve got so far with DRYML, and figuring out where exactly we want to be.

We really wanted to raise our game and make DRYML even simpler and more elegant than it is already. The good news is, things are starting to come into focus.

During this process we’ve considered a lot of out-there ideas, and have even considered a complete re-write with an entirely new approach. Fortunately, and as so often seems to be the case, the journey has taken us full circle, and we’ve ended up pretty close to where DRYML is right now. It’s just we now have a much better understanding of why it works so well :-). All we really need is a bit of polish here and there and a couple of tweaks.

Just today James and I have had a great day nailing all this down, and it looks like we’re ready to start implementing the new features. We’re really excited about this new stuff and are really looking forward to getting it ready for you all to start hacking with.

I’m afraid your existing DRYML pages won’t work with the new syntax, but the overall semantics is hardly changed so it should be a no-brainer to port everything over. It will be well worth it!

New DRYML getting closer

Posted almost 7 years back at The Hobo Blog

If you’re wondering why things have been a bit quiet around here of late, it’s because we’ve been taking a pretty hard look at where we’ve got so far with DRYML, and figuring out where exactly we want to be.

We really wanted to raise our game and make DRYML even simpler and more elegant than it is already. The good news is, things are starting to come into focus.

During this process we’ve considered a lot of out-there ideas, and have even considered a complete re-write with an entirely new approach. Fortunately, and as so often seems to be the case, the journey has taken us full circle, and we’ve ended up pretty close to where DRYML is right now. It’s just we now have a much better understanding of why it works so well :-). All we really need is a bit of polish here and there and a couple of tweaks.

Just today James and I have had a great day nailing all this down, and it looks like we’re ready to start implementing the new features. We’re really excited about this new stuff and are really looking forward to getting it ready for you all to start hacking with.

I’m afraid your existing DRYML pages won’t work with the new syntax, but the overall semantics is hardly changed so it should be a no-brainer to port everything over. It will be well worth it!

Episode 45: RJS Tips

Posted almost 7 years back at Railscasts

This episode is packed with little RJS goodies. Learn the different ways to access an element, how to add "if" conditions and how to apply an effect to multiple elements.

Testing Helpers in Rails

Posted almost 7 years back at

It can be useful at times to unit test helpers to make sure they generate correct html. It is not obvious how to do this at first. So far I have been testing my helper by defining a class “MyClass” at the top of my unit test and including all the appropriate modules. I also need to define a url_for method if I ever want to test helpers that generate links.

The code follows (Replace MyHelper with your appropriate helper class);

class MyClass
  include ERB::Util
  include ActionView::Helpers::TagHelper
  include ActionView::Helpers::UrlHelper
  include MyHelper

  def url_for(options)
    ActionController::Routing::Routes.reload if ActionController::Routing::Routes.empty?
    generated_path, extra_keys = ActionController::Routing::Routes.generate_extras(options, {})
    generated_path
  end
end

Then in my tests I do something like;

def test_revision_link
  assert_equal(
    "<a href=\"http://svn.sourceforge.net/viewvc/jikesrvm?view=rev&amp;revision=22\">22</a>", 
    MyClass.new.revision_link(22))
end

Seems easy enough to do in retrospect but things usually do.

Episode 44: Debugging RJS

Posted almost 7 years back at Railscasts

RJS and AJAX can be difficult to debug. Many times you don't get any error message in the browser. Learn different techniques for solving these tricky problems in this episode.

> YouTube Gem 0.8.6 Released

Posted almost 7 years back at Shane's Brain Extension

This is mainly a bugfix release but also makes it easier to search for videos by category. Searching by category greatly helps in finding videos that are more relevant. You can now do:

videos = videos_by_category_and_tag(YouTube::Category::MUSIC, 'bush')
or if you wanted to:
videos = videos_by_category_and_tag(YouTube::Category::NEWS_POLITICS, 'bush')

For more details check out the CHANGELOG. Thanks to all the contributers, Walter Korman, Lucas Carlson, Rob Tsuk, and Thomas Cox.

Related posts:

> YouTube Gem 0.8.6 Released

Posted almost 7 years back at Shane's Brain Extension

This is mainly a bugfix release but also makes it easier to search for videos by category. Searching by category greatly helps in finding videos that are more relevant. You can now do:

videos = videos_by_category_and_tag(YouTube::Category::MUSIC, 'bush')
or if you wanted to:
videos = videos_by_category_and_tag(YouTube::Category::NEWS_POLITICS, 'bush')

For more details check out the CHANGELOG. Thanks to all the contributers, Walter Korman, Lucas Carlson, Rob Tsuk, and Thomas Cox.

Related posts:

Text to speech for Ruby on Rails applications

Posted almost 7 years back at Spejman On Rails

I just published the first usable festivaltts4r version, it comes with its plugin for Ruby on Rails, festivalttsOnRails. With this library and this plugin you can make talk your Ruby and your Ruby on Rails applications.

The rails plugin is so easy to use in Ubuntu linux:

  1. Install tts and mp3 generation libraries:

    sudo apt-get install festival lame

  2. Install the festivalttsOnRails plugin for Ruby on Rails:

    script/plugin install \
    svn://rubyforge.org/var/svn/festivaltts4r/plugins/festivaltts_on_rails

  3. Use text_to_flash_player(text) method in your views:

    <%= text_to_flash_player "Talk me!" %>

At the moment the plugin works with a simple english voice but can be very useful as a proof of concept. If people found it interesting it could be improved.

It works so well in Ubuntu Linux, testing in other platforms will be appreciated.

You can also use the festivaltts4r gem in order to make local voice applications with Ruby:
  1. Install tts and mp3 generation libraries:

    sudo apt-get install festival lame

  2. Install festivaltts4r gem:

    sudo gem install festivaltts4r

  3. Include required gems and call to_speech method defined into the String class by festivaltts4r:

    require "rubygems"
    require "festivaltts4r"

    "I'm talking".to_speech

This project has been developed using Festival TTS and lame libraries.

The flash mp3 player used to play the voice has been developed by dew under Creative Commons Attribution-ShareAlike License France license.

More info about festivaltts4r and festivalttsOnRails: festivaltts4r.rubyforge.org