Skip to content

Instantly share code, notes, and snippets.

@eschulte
Created March 23, 2009 18:11
Show Gist options
  • Save eschulte/83699 to your computer and use it in GitHub Desktop.
Save eschulte/83699 to your computer and use it in GitHub Desktop.
module ActiveRecord::Associations::ClassMethods
%w{has_and_belongs_to_many has_many has_one}.each do |meth|
eval(<<METHOD_OVERRIDE
# intercepting the #{meth} method to add the :as_component option
def #{meth}_with_component(name, options = {})
if options.delete(:as_component)
options[:after_add] = :tell_my_parents
self.components ||= []
self.components << name
end
#{meth}_without_component(name, options)
end
alias_method_chain(:#{meth}, :component)
METHOD_OVERRIDE
)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment