Why your pretty, tested, Observer might not work

I normally have my observers tested, so the announcement “there is no mail comming” was pretty irritating at first, until i noticed that it was true. So i began my search … and after quiet some time digging and trying i found that i forgot to add the new observer to the config….

Normal Observer Setup

#environment.rb
config.active_record.observers = :user_observer, ....

Automatic Observer Setup
And since i am forgetful, how about auto-loading all observers:

#environment.rb
observers = Dir.glob("#{RAILS_ROOT}/app/models/*_observer.rb").map do |file|
  File.basename(file).sub('.rb','')
end
config.active_record.observers = observers

Readable Specs: it renders, it assigns, it flashes

Today i got struck by simplicity, as a reviewer said:
what exactly is response.should render_template(“new”)
and my answer was: it says that it renders new

Usage

  #delete this
  flash[:error].should_not be_nil
  response.should render_template("new")
  response.should redirect_to('/')
  response.should redirect_to(user_path(User.first))

  #enjoy this
  it_flashes :error
  it_renders :new
  it_redirects '/'
  it_redirects User.first

Install

#spec/spec_helper.rb
def it_redirects_to(what)
  what = send(what.class.to_s.underscore+'_path',what) if what.kind_of? ActiveRecord::Base
  response.should redirect_to(what.to_s)
end
alias :it_redirects :it_redirects_to

def it_renders(what)
  response.should render_template(what.to_s)
end

def it_has_flash(what)
  flash[what].should_not be_blank
end
alias :it_flashes :it_has_flash
  
def it_assigns(what,to=nil)
  if to
    assigns[what].should == to
  else
    assigns[what].should_not be_blank
  end
end

Shouldless convention as outlined in my groundbreaking paper. 😉

Small State_Machine Helper

Some small helpers to

  • access all states
  • access all events
  • define all status? methods

for rails state_machine plugin (for former acts_as_state_machine).

(this will not work if you got multiple status fields).

Usage

movie.online?
movie.banned?
...
f.select(:status,Movie.states)

Install

#lib/status_helper.rb
module StatusHelper
  def self.included(base)
    base.class_eval do
      def self.states; state_machines['status'].states; end
      def self.events; state_machines['status'].events.keys; end
      states.each {|state| define_method("#{state}?"){status == state} }
    end
  end
end

#your model
class Movie
  include StatusHelpers
end

select_month Fix for gettextlocalize on Rails 2.1

When using a simple f.date_select, i got a nice “wrong number of arguments (3 for 2)”, turns out the problem lies with gettext localize, which overwrites select_month, but seems to break in rails 2.1.

To fix monkeypatch it:

#vendor/plugins/gettext_localize/lib/gettext_localize_extend.rb @line 172
  def select_month(date, options = {},html_options={})

Now it should do your bidding again 🙂

Fixing upadtepo, Duplicated Messages

So far i found 2 causes of duplicated messages (duplicate message definition) when updating po files(rake updatepo)

  1. Subclassing — hard to fix, official manual says: ” write a model per a file(modelname.rb)”, which helped me really…not.. adding common columns id/created_at/updated_at to untranslated colums can help
      GetText::ActiveRecordParser.init(
        :untranslate_columns => %w[id created_at updated_at user_id]
      ) 
    

    You can also limit the classes that will be parsed with :activerecord_classes = [‘name’], which will be used like current_class =~ /class.*name /, so one could add wildcards and stuff.

  2. Observers: deactivate them in environment.rb while updating

While trying through these options, remember to always clenup or delete your old pot file, or updatepo can never succeed.

BTW: never try to use :use_classes => false,