Skip to content

Instantly share code, notes, and snippets.

@codatory
Created May 3, 2012 21:37
Show Gist options
  • Select an option

  • Save codatory/2589714 to your computer and use it in GitHub Desktop.

Select an option

Save codatory/2589714 to your computer and use it in GitHub Desktop.
module Sanity
module ActsAsSaneNestedAttributes
def self.included(base)
base.extend(ClassMethods)
end
module ClassMethods
def acts_as_sane_nested_attributes(options = [])
puts 'Setting up child callbacks'
ActiveRecord::Callbacks::CALLBACKS.each do |callback|
options.to_a.each do |child|
puts "Checking if #{child} supports #{callback}"
if child.to_s.classify.constantize.send(callback.to_sym).any?
puts "Setting #{callback} for #{child}"
self.send(callback.to_sym, "#{child.to_s.pluralize}.each{|c| c.run_callbacks(#{callback.to_sym})}")
end
end
end
end
end
end
end
ActiveRecord::Base.send :include, Sanity::ActsAsSaneNestedAttributes
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment