Module: Reform::Contract::Setup

Includes:
Readable, SetupOptions
Included in:
Reform::Contract
Defined in:
lib/reform/contract/setup.rb

Defined Under Namespace

Modules: Readable, SetupOptions

Instance Method Summary collapse

Methods included from Readable

#setup_options

Methods included from SetupOptions

#setup_options

Instance Method Details

#create_fields(field_names, fields) ⇒ Object



28
29
30
# File 'lib/reform/contract/setup.rb', line 28

def create_fields(field_names, fields)
  Fields.new(field_names, fields)
end

#initialize(model) ⇒ Object



4
5
6
7
# File 'lib/reform/contract/setup.rb', line 4

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

#setup_fieldsObject



20
21
22
23
24
25
26
# File 'lib/reform/contract/setup.rb', line 20

def setup_fields
  representer = setup_representer.new(aliased_model)
  options     = setup_options(Reform::Representer::Options[]) # handles :empty.

  # populate the internal @fields set with data from the model.
  create_fields(mapper.fields, representer.to_hash(options))
end

#setup_representerObject

Setup#to_hash will create a nested hash of property values from the model. Nested properties will be recursively wrapped in a form instance.



11
12
13
14
15
16
17
18
# File 'lib/reform/contract/setup.rb', line 11

def setup_representer
  self.class.representer(:setup) do |dfn| # only nested forms.
    dfn.merge!(
      :representable => false, # don't call #to_hash, only prepare.
      :prepare       => lambda { |model, args| args.binding[:form].new(model) } # wrap nested properties in form.
    )
  end
end