Sha256: c1d593881ebe3c195c61645c1aaee21462a2513d71c07f0f9058b8c8b43b1d7b

Contents?: true

Size: 1.25 KB

Versions: 1

Compression:

Stored size: 1.25 KB

Contents

module Para
  module FormBuilder
    module AttributesMappingsTracker
      def initialize(*) #:nodoc:
        @attributes_mappings = {}

        super
      end

      def input(attribute_name, options = {}, &block)
        store_attribute_mapping_for(attribute_name, options, &block)

        super(attribute_name, options, &block)
      end

      def input_field(attribute_name, options = {})
        store_attribute_mapping_for(attribute_name, options)

        super(attribute_name, options)
      end

      def fields_for(*args, options, &block)
        super(*args, options) do |fields|
          fields_html = @template.capture { block.call(fields) }

          fields_html + fields.attributes_mappings_field_for(fields)
        end
      end

      def attributes_mappings_field_for(fields)
        hidden_field :_attributes_mappings, value: @attributes_mappings.to_json,
                     data: { :'attributes-mappings' => fields.options[:child_index] }
      end

      private

      def store_attribute_mapping_for(attribute_name, options, &block)
        input = find_input(attribute_name, options, &block)
        type = input.class.name.demodulize.underscore.gsub(/_input\z/, '')

        @attributes_mappings[attribute_name] = type
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
para-0.7.0 lib/para/form_builder/attributes_mappings_tracker.rb