ruby

Shmacros

I use shoulda constantly as it is my favorite testing framework. I figured the custom macros I write may prove useful to others, thus I released them as a plugin called shmacros. As of now it includes the following methods.

should_accept_nested_attributes_for should_act_as_taggable_on should_be should_callback should_delegate should_have_attached_file (removed in new revision) should_validate_associated

<p>It&#8217;s best to have <a href=”http://mocha.rubyforge.org”>Mocha</a> installed as some of the macros [...]

Give your :reject_if some sugar

Tired of writing stuff like this?

accepts_nested_attributes_for :photo,                               :allow_destroy => true,                               :reject_if => proc { |attrs| reject = %w(title picture).all?{|a| attrs[a].blank?} }

Throw this snippet into init.rb into [...]

Teach models to stalk each other with Stalker

I often come by a case where models need to talk to each other. ModelA sometimes needs to ask ModelB to do something after_save. Usually you’d just use a callback.

class ModelA < ActiveRecord::Base   after_save :update_model_b

  def update_model_b     ModelB.update!   end end

This is obvious coupling. ModelA needs to know what to call in ModelB. This isn’t ModelA’s [...]