Sha256: 1e3543ba32f007372e620551c6cd4342b36e76b480b2763eb8cc2aa75eef8b4f

Contents?: true

Size: 1.82 KB

Versions: 105

Compression:

Stored size: 1.82 KB

Contents

# encoding: utf-8
module Mongoid #:nodoc:

  # Mongoid's implementation of Rails' nested attributes.
  module NestedAttributes
    extend ActiveSupport::Concern

    included do
      class_attribute :nested_attributes
      self.nested_attributes = []
    end

    module ClassMethods #:nodoc:

      REJECT_ALL_BLANK_PROC = proc { |attributes| attributes.all? { |key, value| key == '_destroy' || value.blank? } }

      # Used when needing to update related models from a parent relation. Can
      # be used on embedded or referenced relations.
      #
      # @example Defining nested attributes.
      #
      #   class Person
      #     include Mongoid::Document
      #
      #     embeds_many :addresses
      #     embeds_one :game
      #     references_many :posts
      #
      #     accepts_nested_attributes_for :addresses, :game, :posts
      #   end
      #
      # @param [ Array<Symbol>, Hash ] *args A list of relation names, followed
      #   by a hash of options.
      #
      # @option *args [ true, false ] :allow_destroy Can deletion occur?
      # @option *args [ Proc, Symbol ] :reject_if Block or symbol pointing to a class method to reject documents with.
      # @option *args [ Integer ] :limit The max number to create.
      # @option *args [ true, false ] :update_only Only update existing docs.
      def accepts_nested_attributes_for(*args)
        options = args.extract_options!
        options[:reject_if] = REJECT_ALL_BLANK_PROC if options[:reject_if] == :all_blank
        args.each do |name|
          self.nested_attributes += [ "#{name}_attributes=" ]
          define_method("#{name}_attributes=") do |attrs|
            _assigning do
              relation = relations[name.to_s]
              relation.nested_builder(attrs, options).build(self)
            end
          end
        end
      end
    end
  end
end

Version data entries

105 entries across 59 versions & 2 rubygems

Version Path
classiccms-0.7.5 vendor/bundle/gems/mongoid-2.4.9/lib/mongoid/nested_attributes.rb
classiccms-0.7.5 vendor/bundle/gems/mongoid-2.4.10/lib/mongoid/nested_attributes.rb
classiccms-0.7.4 vendor/bundle/gems/mongoid-2.4.9/lib/mongoid/nested_attributes.rb
classiccms-0.7.4 vendor/bundle/gems/mongoid-2.4.10/lib/mongoid/nested_attributes.rb
classiccms-0.7.3 vendor/bundle/gems/mongoid-2.4.9/lib/mongoid/nested_attributes.rb
classiccms-0.7.3 vendor/bundle/gems/mongoid-2.4.10/lib/mongoid/nested_attributes.rb
mongoid-2.8.1 lib/mongoid/nested_attributes.rb
mongoid-2.8.0 lib/mongoid/nested_attributes.rb
classiccms-0.7.2 vendor/bundle/gems/mongoid-2.4.9/lib/mongoid/nested_attributes.rb
classiccms-0.7.2 vendor/bundle/gems/mongoid-2.4.10/lib/mongoid/nested_attributes.rb
classiccms-0.7.1 vendor/bundle/gems/mongoid-2.4.9/lib/mongoid/nested_attributes.rb
classiccms-0.7.1 vendor/bundle/gems/mongoid-2.4.10/lib/mongoid/nested_attributes.rb
classiccms-0.7.0 vendor/bundle/gems/mongoid-2.4.10/lib/mongoid/nested_attributes.rb
classiccms-0.7.0 vendor/bundle/gems/mongoid-2.4.9/lib/mongoid/nested_attributes.rb
mongoid-2.7.1 lib/mongoid/nested_attributes.rb
mongoid-2.7.0 lib/mongoid/nested_attributes.rb
mongoid-2.6.0 lib/mongoid/nested_attributes.rb
mongoid-2.5.2 lib/mongoid/nested_attributes.rb
classiccms-0.6.9 vendor/bundle/gems/mongoid-2.4.9/lib/mongoid/nested_attributes.rb
classiccms-0.6.9 vendor/bundle/gems/mongoid-2.4.10/lib/mongoid/nested_attributes.rb