Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save johnbintz/3934311 to your computer and use it in GitHub Desktop.
Save johnbintz/3934311 to your computer and use it in GitHub Desktop.
Add accepts_embedded_resources_for to Mongoid::NestedAttributes
Gem::Specification.new do |s|
s.name = 'mongoid-accepts_embedded_resource_for'
s.version = '0.1.0'
s.platform = Gem::Platform::RUBY
s.author = 'John Bintz'
s.email = '[email protected]'
s.summary = 'Add accepts_embedded_resources_for to Mongoid::NestedAttributes, so it plays nicer with Carrierwave and embedded documents'
s.description = 'Add accepts_embedded_resources_for to Mongoid::NestedAttributes, so it plays nicer with Carrierwave and embedded documents'
s.files = ['mongoid-accepts_embedded_resource_for.rb']
s.require_path = '.'
s.add_dependency 'mongoid'
end
require 'mongoid'
require 'mongoid/nested_attributes'
module Mongoid
module NestedAttributes
module ClassMethods
def accepts_embedded_attributes_for(*args)
options = {}
if args.last.kind_of?(::Hash)
options = args.pop
end
accepts_nested_attributes_for(*args, options)
args.each do |column|
class_eval <<-RB
alias :original_#{column}_attributes= :#{column}_attributes=
def #{column}_attributes=(*args)
send("original_#{column}_attributes=", *args)
#{column}.each(&:save!)
end
RB
end
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment