Sha256: d968fa4794d4c1c7cf2d079986417444493cdc4d7ff0515d5aacf20716690456

Contents?: true

Size: 1.58 KB

Versions: 2

Compression:

Stored size: 1.58 KB

Contents

Reform::Form.class_eval do
  # #sync!
  #   1. assign scalars to model (respecting virtual, excluded attributes)
  #   2. call sync! on nested
  module Sync
    # Mechanics for writing input to model.
    # Writes input to model.
    module Writer
      def from_hash(*)
        # process output from InputRepresenter {title: "Mint Car", hit: <Form>}
        # and just call sync! on nested forms.
        nested_forms do |attr|
          attr.merge!(
            :instance     => lambda { |fragment, *| fragment },
            :deserialize => lambda { |object, *| object.sync! },
            :setter => lambda { |*| } # don't write hit=<Form>.
          )
        end

        super
      end
    end

    # Transforms form input into what actually gets written to model.
    # output: {title: "Mint Car", hit: <Form>}
    module InputRepresenter
      include Reform::Representer::WithOptions
      # TODO: make dynamic.
      include Reform::Form::EmptyAttributesOptions
      include Reform::Form::ReadonlyAttributesOptions

      def to_hash(*)
        nested_forms do |attr|
          attr.merge!(
            :representable  => false,
            :prepare        => lambda { |obj, *| obj }
          )

        end

        super
      end
    end
  end

  ### TODO: add ToHash with :prepare => lambda { |form, args| form },

  def sync_models
    sync!
  end
  alias_method :sync, :sync_models

  def sync! # semi-public.
    input_representer = mapper.new(self).extend(Sync::InputRepresenter)

    input = input_representer.to_hash

    mapper.new(aliased_model).extend(Sync::Writer).from_hash(input)
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
reform-1.0.1 lib/reform/form/sync.rb
reform-1.0.0 lib/reform/form/sync.rb