Class: Frigate::Form::Synchronizer::Contract

Inherits:
Basic show all
Defined in:
lib/frigate/form/synchronizer/contract.rb

Overview

Synchronizes form properties with model properties

Instance Attribute Summary

Attributes inherited from Fundamental

#form

Instance Method Summary collapse

Methods inherited from Basic

#sync_errors, #sync_with_model

Methods inherited from Base

#sync_errors, #sync_with_model

Methods inherited from Fundamental

#initialize

Constructor Details

This class inherits a constructor from Frigate::Form::Synchronizer::Fundamental

Instance Method Details

#sync_properties_with_model_or_paramsObject

Synchronizes given model’s attributes with form properties and associations:properties



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/frigate/form/synchronizer/contract.rb', line 8

def sync_properties_with_model_or_params
  #
  # sync model properties with form properties
  #
  form.properties.each do |_prop|
    if form.model.respond_to?(_prop.name.to_sym)
      _prop.value = form.model.send(_prop.name.to_sym)
    else
      raise StandardError.new('form model does not have %s property' % [_prop.name])
    end
  end
  #
  # sync model associations' properties with form associations' properties
  #
  form.associations.each do |_a|
    if form.model.respond_to?(_a.name.to_sym) and !form.model.send(_a.name.to_sym).nil?
      _a.properties.each do |_p|
        if form.model.send(_a.name.to_sym).respond_to?(_p.name.to_sym)
          _p.value = form.model.send(_a.name.to_sym).send(_p.name.to_sym)
        else
          _err_msg = 'form model %s association does not have %s property' % [_a.name, _p.name]
          raise StandardError.new(_err_msg)
        end
      end
    else
      if form.model.respond_to?(_a.name.to_sym)
        _err_msg = 'form model %s association is nil' % [_a.name]
        raise StandardError.new(_err_msg)
      else
        _err_msg = 'form model does not have %s association' % [_a.name]
        raise StandardError.new(_err_msg)
      end
    end
  end
end