lib/accepts_nested_ids.rb
def accepts_nested_ids_for(*args)
        @_nested_id_associations = map_nested_id_associations(*args)

        nested_id_associations.each do |nested_id_association|

          # Define ids_attr getter
          #
          # @example Method definition
          #   def document_ids
          #     @document_ids || (documents.loaded? ? documents.map(&:id) : documents.pluck(:id))
          #   end
          #
          define_method("#{nested_id_association.ids_attr}") do
            association = send(nested_id_association.attr)
            instance_variable_get("@#{nested_id_association.ids_attr}") ||
              (association.loaded? ? association.map(&:id) : association.pluck(:id))
          end

          # Define ids_attr setter
          #
          # @example Method definition
          #   def document_ids=(value)
          #     return if document_ids == value
          #     attribute_will_change!('document_ids')
          #     @document_ids = value
          #   end
          #
          define_method("#{nested_id_association.ids_attr}=") do |value|
            return if send(nested_id_association.ids_attr) == value
            attribute_will_change!(nested_id_association.ids_attr)
            instance_variable_set(:"@#{nested_id_association.ids_attr}", value)
          end

        end
      end
# Sets up defered save and dirty tracking for the specified associations
#
# @example When class_name can be inferred from association name
#   include AcceptsNestedIds
#   accepts_nested_ids_for :documents, :users
#
# @example When class_name is different from association name
#   include AcceptsNestedIds
#   accepts_nested_ids_for :documents, included_users: "User"
#
# @param args [Array]
Sets up defered save and dirty tracking for the specified associations
Sets up defered save and dirty tracking for the specified associations
def accepts_nested_ids_for(*args)
        @_nested_id_associations = map_nested_id_associations(*args)
        nested_id_associations.each do |nested_id_association|
           @example Method definition
             def document_ids
               @document_ids || (documents.loaded? ? documents.map(&:id) : documents.pluck(:id))
             end
          define_method("{nested_id_association.ids_attr}") do
            association = send(nested_id_association.attr)
            instance_variable_get("@{nested_id_association.ids_attr}") ||
              (association.loaded? ? association.map(&:id) : association.pluck(:id))
          end
           Define ids_attr setter
           @example Method definition
             def document_ids=(value)
               return if document_ids == value
               attribute_will_change!('document_ids')
               @document_ids = value
             end
          define_method("{nested_id_association.ids_attr}=") do |value|
            return if send(nested_id_association.ids_attr) == value
            attribute_will_change!(nested_id_association.ids_attr)
            instance_variable_set(:"@{nested_id_association.ids_attr}", value)
          end
        end
      end
def accepts_nested_ids_for ( * args ) @_nested_id_associations = map_nested_id_associations ( * args ) nested_id_associations . each do | nested_id_association | define_method ( "#{nested_id_association.ids_attr}" ) do association = send ( nested_id_association . attr ) instance_variable_get ( "@#{nested_id_association.ids_attr}" ) || ( association . loaded? ? association . map ( & :id ) : association . pluck ( :id ) ) end define_method ( "#{nested_id_association.ids_attr}=" ) do | value | return if send ( nested_id_association . ids_attr ) == value attribute_will_change! ( nested_id_association . ids_attr ) instance_variable_set ( :" @ #{ nested_id_association . ids_attr } " , value ) end end end
Sets up defered save and dirty tracking for the specified associations
Sets up defered save and dirty tracking for the specified associations
# Define ids_attr getter # # @example Method definition #   def document_ids #     @document_ids || (documents.loaded? ? documents.map(&:id) : documents.pluck(:id)) #   end # # Define ids_attr setter # # @example Method definition #   def document_ids=(value) #     return if document_ids == value #     attribute_will_change!('document_ids') #     @document_ids = value #   end #
args
None
example
When class_name can be inferred from association name include AcceptsNestedIds accepts_nested_ids_for :documents, :users
When class_name can be inferred from association name include AcceptsNestedIds accepts_nested_ids_for : documents : users
example
When class_name is different from association name include AcceptsNestedIds accepts_nested_ids_for :documents, included_users: "User"
When class_name is different from association name include AcceptsNestedIds accepts_nested_ids_for : documents included_users : " User "