Sha256: 567b0efcebc33db0432eb4dbb16bc9d89e99c1bc54ac995101b2bba7739f01ec

Contents?: true

Size: 1.35 KB

Versions: 38

Compression:

Stored size: 1.35 KB

Contents

module Her
  module Model
    module NestedAttributes
      extend ActiveSupport::Concern

      module ClassMethods
        # Allow nested attributes for an association
        #
        # @example
        #   class User
        #     include Her::Model
        #
        #     has_one :role
        #     accepts_nested_attributes_for :role
        #   end
        #
        #   class Role
        #     include Her::Model
        #   end
        #
        #   user = User.new(name: "Tobias", role_attributes: { title: "moderator" })
        #   user.role # => #<Role title="moderator">
        def accepts_nested_attributes_for(*associations)
          allowed_association_names = association_names

          associations.each do |association_name|
            unless allowed_association_names.include?(association_name)
              raise Her::Errors::AssociationUnknownError, "Unknown association name :#{association_name}"
            end

            class_eval <<-RUBY, __FILE__, __LINE__ + 1
              if method_defined?(:#{association_name}_attributes=)
                remove_method(:#{association_name}_attributes=)
              end

              def #{association_name}_attributes=(attributes)
                self.#{association_name}.assign_nested_attributes(attributes)
              end
            RUBY
          end
        end
      end
    end
  end
end

Version data entries

38 entries across 38 versions & 2 rubygems

Version Path
test_track_rails_client-7.1.2 vendor/gems/her/lib/her/model/nested_attributes.rb
test_track_rails_client-7.1.1 vendor/gems/her/lib/her/model/nested_attributes.rb
test_track_rails_client-7.0.0 vendor/gems/her/lib/her/model/nested_attributes.rb
test_track_rails_client-6.0.0 vendor/gems/her/lib/her/model/nested_attributes.rb
test_track_rails_client-5.0.1 vendor/gems/her/lib/her/model/nested_attributes.rb
test_track_rails_client-5.0.0 vendor/gems/her/lib/her/model/nested_attributes.rb
her-1.1.1 lib/her/model/nested_attributes.rb
test_track_rails_client-4.0.2 vendor/gems/her/lib/her/model/nested_attributes.rb
test_track_rails_client-4.0.1 vendor/gems/her/lib/her/model/nested_attributes.rb
test_track_rails_client-4.0.0 vendor/gems/her/lib/her/model/nested_attributes.rb
test_track_rails_client-4.0.0.rc3 vendor/gems/her/lib/her/model/nested_attributes.rb
test_track_rails_client-4.0.0.rc2 vendor/gems/her/lib/her/model/nested_attributes.rb
test_track_rails_client-4.0.0.alpha35 vendor/gems/her/lib/her/model/nested_attributes.rb
test_track_rails_client-4.0.0.alpha34 vendor/gems/her/lib/her/model/nested_attributes.rb
test_track_rails_client-4.0.0.alpha33 vendor/gems/her/lib/her/model/nested_attributes.rb
test_track_rails_client-4.0.0.alpha32 vendor/gems/her/lib/her/model/nested_attributes.rb
test_track_rails_client-4.0.0.alpha31 vendor/gems/her/lib/her/model/nested_attributes.rb
test_track_rails_client-4.0.0.alpha30 vendor/gems/her/lib/her/model/nested_attributes.rb
test_track_rails_client-4.0.0.alpha29 vendor/gems/her/lib/her/model/nested_attributes.rb
test_track_rails_client-4.0.0.alpha28 vendor/gems/her/lib/her/model/nested_attributes.rb