Jan 12 Machinist and polymorphic associations

tags: machinist polymorphic | comments

You love machinist for fixtures creation, but then you need to blueprint a model that has a polymorphic association and that means two fields in your blueprint. Don’t worry, it’s dead easy using a helper class.

# spec/blueprints.rb

#container has content_type and content_id fields. But the machinist is a smart guy :-)
Container.blueprint do
  content { Content.make }
end

# this helper class creates classes so your blueprint is happy
class Content
  def self.make(attrs = { :what => :page })
    case attrs[:what]
    when :page
      Page.make attrs.delete(:what)
    end
  end
end

that’s it, happy coding

blog comments powered by Disqus