Sha256: 0b8a182a00d3fe0cb016901370d4071b0c5191caa282a3f6f1ca46f00aa39699

Contents?: true

Size: 1.67 KB

Versions: 2

Compression:

Stored size: 1.67 KB

Contents

module Para
  module AttributeField
    module NestedField
      def nested_model_mappings(nested_attributes)
        model = if (type = nested_attributes[:type]).present?
          nested_attributes[:type].try(:constantize)
        else
          reflection.klass
        end

        mappings = attributes_mappings_for(nested_attributes)

        AttributeFieldMappings.new(model, mappings: mappings)
      end

      def nested_attributes_key
        @nested_attributes_key ||= :"#{ name }_attributes"
      end

      # This method extends the current resource so its #id method returns a
      # fake id based on the given attributes, but immediately returns to its
      # standard behavior of returning nil as soon as the `#assign_attributes`
      # method is called.
      #
      # During nested attributes assignation, the id of the resource is used
      # to assign it its nested params. When it is nil, a new resource is
      # created, but since we already need our resource to be created at the
      # time we parse the input params, we fake the presence of an id while the
      # nested attributes assignation is running, and remove that behavior as
      # soon as we don't need it anymore.
      #
      def temporarily_extend_new_resource(resource, attributes)
        resource.instance_variable_set(:@_waiting_for_attributes_assignation, true)

        resource.define_singleton_method(:assign_attributes) do |*args|
          @_waiting_for_attributes_assignation = false
          super(*args)
        end

        resource.define_singleton_method(:id) do
          @_waiting_for_attributes_assignation ? attributes['id'] : read_attribute(:id)
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
para-0.7.1 lib/para/attribute_field/nested_field.rb
para-0.7.0 lib/para/attribute_field/nested_field.rb