Sha256: 6b209933031018425b5ea38265d6a6ae6d2b263f2e62242a688796e496d5b822

Contents?: true

Size: 1.1 KB

Versions: 14

Compression:

Stored size: 1.1 KB

Contents

# frozen_string_literal: true

module StoreModel
  # Contains methods for working with nested StoreModel::Model attributes.
  module NestedAttributes
    def self.included(base) # :nodoc:
      base.extend ClassMethods
    end

    module ClassMethods # :nodoc:
      # Enables handling of nested StoreModel::Model attributes
      #
      # @param associations [Array] list of associations to define attributes
      def accepts_nested_attributes_for(*associations)
        associations.each do |association|
          case attribute_types[association.to_s]
          when Types::One
            alias_method "#{association}_attributes=", "#{association}="
          when Types::Many
            define_method "#{association}_attributes=" do |attributes|
              assign_nested_attributes_for_collection_association(association, attributes)
            end
          end
        end
      end
    end

    private

    def assign_nested_attributes_for_collection_association(association, attributes)
      attributes = attributes.values if attributes.is_a?(Hash)
      send "#{association}=", attributes
    end
  end
end

Version data entries

14 entries across 14 versions & 1 rubygems

Version Path
store_model-1.4.0 lib/store_model/nested_attributes.rb
store_model-1.3.0 lib/store_model/nested_attributes.rb
store_model-1.2.0 lib/store_model/nested_attributes.rb
store_model-1.1.0 lib/store_model/nested_attributes.rb
store_model-1.0.0 lib/store_model/nested_attributes.rb
store_model-0.13.0 lib/store_model/nested_attributes.rb
store_model-0.12.0 lib/store_model/nested_attributes.rb
store_model-0.11.1 lib/store_model/nested_attributes.rb
store_model-0.11.0 lib/store_model/nested_attributes.rb
store_model-0.10.0 lib/store_model/nested_attributes.rb
store_model-0.9.0 lib/store_model/nested_attributes.rb
store_model-0.8.2 lib/store_model/nested_attributes.rb
store_model-0.8.1 lib/store_model/nested_attributes.rb
store_model-0.8.0 lib/store_model/nested_attributes.rb