Sha256: daef3e2492c71194c9906603d773320b5943fdcd5ee8ec9c15b4f861f85ec445

Contents?: true

Size: 1.3 KB

Versions: 7

Compression:

Stored size: 1.3 KB

Contents

module Reform
  class Contract
    module Setup
      def initialize(model)
        @model  = model # we need this for #save.
        @fields = setup_fields  # delegate all methods to Fields instance.
      end

      def setup_fields
        representer = mapper.new(aliased_model).extend(Setup::Representer)

        create_fields(representer.fields, representer.to_hash)
      end

      # DISCUSS: setting up the Validation (populating with values) will soon be handled with Disposable::Twin logic.
      def create_fields(field_names, fields)
        Fields.new(field_names, fields)
      end


      # Mechanics for setting up initial Field values.
      module Representer
        require 'reform/form/virtual_attributes' # FIXME: that shouldn't be here.

        include Reform::Representer::WithOptions
        include Reform::Form::EmptyAttributesOptions # FIXME: that shouldn't be here.

        def to_hash(*)
          nested_forms do |attr|
            attr.merge!(
              :representable => false, # don't call #to_hash.
              :prepare       => lambda do |model, args|
                args.binding[:form].new(model)
              end
            )
          end

          super # TODO: allow something like super(:exclude => empty_fields)
        end
      end # Representer
    end
  end # Validation
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
reform-1.1.1 lib/reform/contract/setup.rb
reform-1.1.0 lib/reform/contract/setup.rb
reform-1.0.4 lib/reform/contract/setup.rb
reform-1.0.3 lib/reform/contract/setup.rb
reform-1.0.2 lib/reform/contract/setup.rb
reform-1.0.1 lib/reform/contract/setup.rb
reform-1.0.0 lib/reform/contract/setup.rb