Sha256: 4149d2a5a7498c8a9a53a67b1e52387732f71a24e964491cd07074443ddac1e5

Contents?: true

Size: 1.48 KB

Versions: 6

Compression:

Stored size: 1.48 KB

Contents

# encoding: utf-8
module Mongoid #:nodoc:
  module NestedAttributes
    extend ActiveSupport::Concern

    module ClassMethods
      REJECT_ALL_BLANK_PROC = proc { |attributes| attributes.all? { |_, value| 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 ] :reject_if Block 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|
          define_method("#{name}_attributes=") do |attrs|
            relation = relations[name.to_s]
            relation.nested_builder(attrs, options).build(self)
          end
        end
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 2 rubygems

Version Path
stonegao-mongoid-2.0.0.rc.6 lib/mongoid/nested_attributes.rb
mongoid-2.0.0.rc.6 lib/mongoid/nested_attributes.rb
mongoid-2.0.0.rc.5 lib/mongoid/nested_attributes.rb
mongoid-2.0.0.rc.4 lib/mongoid/nested_attributes.rb
mongoid-2.0.0.rc.3 lib/mongoid/nested_attributes.rb
mongoid-2.0.0.rc.2 lib/mongoid/nested_attributes.rb