Created
October 22, 2012 21:07
-
-
Save johnbintz/3934311 to your computer and use it in GitHub Desktop.
Add accepts_embedded_resources_for to Mongoid::NestedAttributes
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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