How Beautiful is Ruby?

Working with Ruby and in particular Rails, it’s easy to take the beauty inherent in the language for granted. I mean look at this code. If you read it aloud to yourself, it reads like an english sentence that any non programmer can understand.

Forum.categories.map do |category|
  link_to category.name, category
end.to_sentence

Forum categories map do category, link to category name, the category, end and convert to a sentence. The code returns a an english sentence as well.

Fish, French Bread, Coffee and Hamburgers with each linked to the correct resource as well!

Deploying to Dreamhost

Remember to include the host declaration in the database.yml file when you deploy to Dreamhost. Dreamhost does not use “localhost” which is typically the default setting when using the mysql adapter and developing locally or even on a small site.

At least for me, when I ported a Rails app to Dreamhost, this was the only “Gotcha”, because my log files were not reporting any errors and were instead serving the 500 something went wrong file.
A sample config/database.yml file

production:
  adapter: mysql
  username: youruser
  password: yourpasswd
  database: ror_production_db
  host: mysql.yourdomain.com

To port, I unpack my gems, if I haven’t already

rake gems:unpack

Then I freeze and package rails w/ my app just in case versions aren’t exact

rake rails:freeze:gems

Then I upload to Dreamhost!

Workshop Dog

Workshop Dog is a free events calendar for dog training workshops and group lessons.
more »

Natural Dog Training Buzz

NDT Buzz is a companion news site to Natural Dog Training.
more »

Active Record Find Methods

Active Record find methods for selecting range from http://charlesmaxwood.com/notes-from-reading-activerecordbase/

Student.find(:all, :conditions => { :grade => 9..12 })
return a range
Student.find(:all, :conditions => { :grade => [9,11,12] })
will return an "in()"

Hacking Rails Plugins

Using the Acts as Taggable On plugin to add categories to a model class, I wanted to override the to_param method and place the name attribute in the url. The plugin, installed as a gem, source shouldn’t need to be hacked in order to accomplish this. The solution is to add a plugin into the RAILS_ROOT/vendor/plugins directory and append the plugin name with _hack. This will, because of an alphabetical load order, allow you to reopen the any class in the plugin. In my example

# RAILS_ROOT/vendor/plugins/acts_as_taggable_on_hack/init.rb
Tag.class_eval do
  to_param
    "#{id}-#{name.gsub(/[^a-z0-9]+/i, '-'/)
  end
end

Got this tip from http://errtheblog.com/posts/67-evil-twin-plugin.

Setup Wildcard Subdomain on Localhost for Development Work without /Etc/hosts TomFoolery

Step 1.

Open up your browser and visit http://www.hexxie.com. You can also go to anything.hexxie.com and everything.hexxie.com, which will resolve to your local machine (assuming it’s localhost at 127.0.0.1).

How it works

Super simple. I just pointed hexxie.com and *.hexxie.com to 127.0.0.1, which is your localhost address. If you’re on Rails just append the port number as usual. http://hexxie.com:3000 or on Django http://hexxie.com:8000. Or you can always fire those guys up on port :80 with sudo ./script/server -p80 for Rails
or sudo django-admin.py runserver 80

To set up your own just configure DNS to point your domain to 127.0.0.1 for the IP address. No more futzing with /etc/hosts

Originally got this tip from http://tbaggery.com/2010/03/04/smack-a-ho-st.html who has created his own service at smackaho.st

The word “Hexe” is German for “Witch”. I have a dog named “Hexxie” after the German word and that is the origin of the domain name hexxie.com, in case you’re wondering.

Extending Rails Form Builders

Extending forms in Rails is simple and will greatly reduce the amount of code in your views. This example is taken right from the Agile Web Development book on Rails(2.1.*) with one minor tweak. I want to pass a label argument along with the field name so that I can display a more human friendly string to represent the form field.

# RAILS_ROOT/app/helpers/custom_tag_builder.rb
class CustomTagBuilder < ActionView::Helpers::FormBuilder

  def self.create_tagged_field(method_name)
    define_method(method_name) do |label, *args|
      label_name = args.first.blank? ? label : args.first[:label] # my change
      @template.content_tag("p",
        @template.content_tag("label",
          label_name.to_s.humanize, :for => "#{@object_name}_#{label}") +" 
"+ super) end end field_helpers.each do |name| create_tagged_field(name) end end

You can then use this in your views

form_for @your_model, :builder => CustomTagBuilder do |f|
  f.text_field :fullname
  f.text_field :email, :label => "Email (will not be published)"

My change tests for the presence of a label argument otherwise using the name of the form field/model attribute. In this case the fullname attribute will be outputted as “Fullname” while “Email (will not be published)” as the label for the email text field.

20 Feb 2010, 8:08pm
ruby:
by bseanvt

leave a comment

Ruby Strftime Day Without the Leading Zero

%e # rather than %d
Time.now.strftime("%e")

Placing an Authenticity Token in a Rails Form

 <%= hidden_field_tag :authenticity_token, form_authenticity_token %>