DRYing associations and behavior

module CommonBehaviour
  def self.included(base)
    base.class_eval <<-EOC
      belongs_to :foo
      has_many :bars
      delegate :baz, :to => :survey_execution
    EOC
    base.send :include, InstanceMethods
  end

  module InstanceMethods
    def bla
      p 'bla'
    end
  end
end

class OneOfCommon < ActiveRecord::Base
  include CommonBehaviour
end


Leave a Reply

Formatting: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>