Module: Reform::Contract::PropertyMethods

Included in:
Reform::Contract
Defined in:
lib/reform/contract.rb

Instance Method Summary collapse

Instance Method Details

#collection(name, options = {}, &block) ⇒ Object



73
74
75
76
77
# File 'lib/reform/contract.rb', line 73

def collection(name, options={}, &block)
  options[:collection] = true

  property(name, options, &block)
end

#properties(*args) ⇒ Object



63
64
65
66
67
68
69
70
71
# File 'lib/reform/contract.rb', line 63

def properties(*args)
  options = args.extract_options!

  if args.first.is_a? Array # TODO: REMOVE in 2.0.
    warn "[Reform] Deprecation: Please pass a list of names instead of array to ::properties, like `properties :title, :id`."
    args = args.first
  end
  args.each { |name| property(name, options.dup) }
end

#property(name, options = {}, &block) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/reform/contract.rb', line 31

def property(name, options={}, &block)
  deprecate_as!(options)
  options[:private_name] = options.delete(:from)
  options[:coercion_type] = options.delete(:type)
  options[:features] ||= []
  options[:features] += features.keys if block_given?
  options[:pass_options] = true

  # readable and writeable is true as it's not == false

  if reform_2_0
    if options.delete(:virtual)
      options[:_readable]  = false
      options[:_writeable] = false
    else
      options[:_readable]  = options.delete(:readable)
      options[:_writeable] = options.delete(:writeable)
    end

  else # TODO: remove me in 2.0.
    deprecate_virtual_and_empty!(options)
  end

  validates(name, options.delete(:validates).dup) if options[:validates]

  definition = representer_class.property(name, options, &block)
  setup_form_definition(definition) if block_given? or options[:form]

  create_accessor(name)
  definition
end

#setup_form_definition(definition) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
# File 'lib/reform/contract.rb', line 79

def setup_form_definition(definition)
  options = {
    # TODO: make this a bit nicer. why do we need :form at all?
    :form         => (definition.representer_module) || definition[:form], # :form is always just a Form class name.
    :pass_options => true, # new style of passing args
    :prepare      => lambda { |form, args| form }, # always just return the form without decorating.
    :representable => true, # form: Class must be treated as a typed property.
  }

  definition.merge!(options)
end