Class: Formize::Definition::Form

Inherits:
Object
  • Object
show all
Defined in:
lib/formize/definition/form.rb

Overview

Represents an environment for a form or list of fields of one Record

Constant Summary collapse

@@count =
0

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, model, options = {}) ⇒ Form

Returns a new instance of Form.


11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/formize/definition/form.rb', line 11

def initialize(name, model, options={})
  @name = name
  @model = model
  @options = options
  @elements = []
  @@count += 1
  @id = @@count.to_s(36)
  @unique_name = @options.delete(:unique_name) unless @options[:unique_name].blank?
  @unique_name ||= "_formize#{@id}"
  @record_name = @model.name.underscore
  @elements_count = 0
end

Instance Attribute Details

#elementsObject (readonly)

Returns the value of attribute elements


7
8
9
# File 'lib/formize/definition/form.rb', line 7

def elements
  @elements
end

#idObject (readonly)

Returns the value of attribute id


7
8
9
# File 'lib/formize/definition/form.rb', line 7

def id
  @id
end

#modelObject (readonly)

Returns the value of attribute model


7
8
9
# File 'lib/formize/definition/form.rb', line 7

def model
  @model
end

#nameObject (readonly)

Returns the value of attribute name


7
8
9
# File 'lib/formize/definition/form.rb', line 7

def name
  @name
end

#optionsObject (readonly)

Returns the value of attribute options


7
8
9
# File 'lib/formize/definition/form.rb', line 7

def options
  @options
end

#record_nameObject (readonly)

Returns the value of attribute record_name


7
8
9
# File 'lib/formize/definition/form.rb', line 7

def record_name
  @record_name
end

#unique_nameObject (readonly)

Returns the value of attribute unique_name


7
8
9
# File 'lib/formize/definition/form.rb', line 7

def unique_name
  @unique_name
end

Instance Method Details

#action_nameObject


60
61
62
# File 'lib/formize/definition/form.rb', line 60

def action_name
  @options[:action_name] || :formize
end

#all_elementsObject


76
77
78
# File 'lib/formize/definition/form.rb', line 76

def all_elements
  return elements.collect{|e| e.all_elements}.flatten
end

#all_fieldsObject


68
69
70
# File 'lib/formize/definition/form.rb', line 68

def all_fields
  return elements.inject(HashWithIndifferentAccess.new){|h, e| h.merge!(e.all_fields)}
end

#controller_method_nameObject

protected


52
53
54
# File 'lib/formize/definition/form.rb', line 52

def controller_method_name
  @options[:controller_method_name] || "formize_#{model.underscore}"
end

#dependentsObject


72
73
74
# File 'lib/formize/definition/form.rb', line 72

def dependents
  return elements.collect{|e| e.dependents}.flatten
end

#dependents_on(element) ⇒ Object


80
81
82
# File 'lib/formize/definition/form.rb', line 80

def dependents_on(element)
  return elements.collect{|e| e.dependents_on(element)}.flatten
end

#field(name, options = {}) ⇒ Object


37
38
39
# File 'lib/formize/definition/form.rb', line 37

def field(name, options={})
  return new_element(Field, name, options)
end

#field_set(name = nil, options = {}) {|field_set| ... } ⇒ Object

Yields:

Raises:

  • (ArgumentError)

24
25
26
27
28
# File 'lib/formize/definition/form.rb', line 24

def field_set(name=nil, options={}, &block)
  raise ArgumentError.new("Missing block") unless block_given?
  field_set = new_element(FieldSet, name, options)
  yield field_set
end

#fields(*args) ⇒ Object


41
42
43
44
45
46
47
# File 'lib/formize/definition/form.rb', line 41

def fields(*args)
  options = {}
  options = args.delete_at(-1) if args[-1].is_a?(Hash)
  for name in args
    new_element(Field, name, options)
  end
end

#group(name, options = {}) {|group| ... } ⇒ Object

Yields:

Raises:

  • (ArgumentError)

30
31
32
33
34
35
# File 'lib/formize/definition/form.rb', line 30

def group(name, options={}, &block)
  raise ArgumentError.new("Missing block") unless block_given?
  name, options = nil, name if name.is_a? Hash
  group = new_element(Group, name, options)
  yield group
end

#hidden_if(element) ⇒ Object


88
89
90
# File 'lib/formize/definition/form.rb', line 88

def hidden_if(element)
  return elements.collect{|e| e.hidden_if(element)}.flatten
end

#mono_choicesObject


64
65
66
# File 'lib/formize/definition/form.rb', line 64

def mono_choices
  return elements.collect{|e| e.mono_choices}.flatten
end

#shown_if(element) ⇒ Object


84
85
86
# File 'lib/formize/definition/form.rb', line 84

def shown_if(element)
  return elements.collect{|e| e.shown_if(element)}.flatten
end

#view_method_nameObject


56
57
58
# File 'lib/formize/definition/form.rb', line 56

def view_method_name
  @options[:method_name] || "_form_#{model.underscore}"
end