Class: Formize::Definition::FormElement

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

Overview

Main class for form elements

Direct Known Subclasses

Field, Group

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(form, parent = nil, options = {}) ⇒ FormElement

Returns a new instance of FormElement.

Raises:

  • (ArgumentError)

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

def initialize(form, parent = nil, options={})
  raise ArgumentError.new("Bad form (#{form.class.name}). Must be an Formize::Definition::Form") unless form.is_a? Formize::Definition::Form
  @form = form
  @parent = parent
  @options = (options.is_a?(Hash) ? options : {})      
  @depend_on = nil
  @children = []
  @id = form.send(:new_id)
  if Rails.env == "development"
    @html_id = "fz_#{@form.options[:best_name]}_#{@id}"
  else
    @html_id = "fz#{@id}"
  end
  @unique_name = self.form.unique_name + "_" + @html_id
end

Instance Attribute Details

#childrenObject (readonly)

Returns the value of attribute children


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

def children
  @children
end

#depend_onObject (readonly)

Returns the value of attribute depend_on


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

def depend_on
  @depend_on
end

#formObject (readonly)

Returns the value of attribute form


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

def form
  @form
end

#html_idObject (readonly)

Returns the value of attribute html_id


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

def html_id
  @html_id
end

#idObject (readonly)

Returns the value of attribute id


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

def id
  @id
end

#optionsObject (readonly)

Returns the value of attribute options


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

def options
  @options
end

#parentObject (readonly)

Returns the value of attribute parent


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

def parent
  @parent
end

#unique_nameObject (readonly)

Returns the value of attribute unique_name


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

def unique_name
  @unique_name
end

Instance Method Details

#all_elementsObject


73
74
75
76
77
# File 'lib/formize/definition/form_element.rb', line 73

def all_elements
  elements = self.children.collect{|c| c.all_elements}.flatten
  elements << self
  return elements      
end

#all_fieldsObject


54
55
56
57
58
59
60
61
# File 'lib/formize/definition/form_element.rb', line 54

def all_fields
  elements = HashWithIndifferentAccess.new()
  for child in self.children
    elements.merge!(child.all_fields)
  end
  elements[self.name] = self if self.class == Formize::Definition::Field
  return elements
end

#argumentsObject


32
33
34
35
36
37
38
# File 'lib/formize/definition/form_element.rb', line 32

def arguments
  args = []
  args << {:name=>form.record_name}
  # args += self.dependeds
  # args << {:name=>@depend_on} if @depend_on
  return args
end

#dependedsObject


26
27
28
29
30
# File 'lib/formize/definition/form_element.rb', line 26

def dependeds
  l = (self.parent ? self.parent.dependeds : [])
  l << {:name=>self.depend_on} unless self.depend_on.blank?
  return l
end

#dependentsObject


63
64
65
66
67
68
69
70
# File 'lib/formize/definition/form_element.rb', line 63

def dependents
  elements = []
  for child in self.children
    elements += child.dependents
  end
  elements << self if self.options[:depend_on]
  return elements      
end

#dependents_on(element) ⇒ Object

Find form elements


81
82
83
84
85
86
87
88
# File 'lib/formize/definition/form_element.rb', line 81

def dependents_on(element)
  elements = []
  for child in self.children
    elements += child.dependents_on(element)
  end
  elements << self if self.depend_on and self.depend_on.to_s == element.name.to_s # form.fields[self.depend_on].name == element.name
  return elements
end

#hidden_if(element) ⇒ Object


99
100
101
102
103
104
105
106
# File 'lib/formize/definition/form_element.rb', line 99

def hidden_if(element)
  elements = []
  for child in self.children
    elements += child.hidden_if(element)
  end
  elements << self if self.options[:hidden_if] and self.options[:hidden_if].to_s == element.name.to_s
  return elements
end

#mono_choicesObject


45
46
47
48
49
50
51
52
# File 'lib/formize/definition/form_element.rb', line 45

def mono_choices
  elements = []
  for child in self.children
    elements += child.mono_choices
  end
  elements << self if self.class == Formize::Definition::Field and self.type == :mono_choice
  return elements      
end

#prototypeObject


40
41
42
# File 'lib/formize/definition/form_element.rb', line 40

def prototype
  return "#{@unique_name}(" + arguments.collect{|x| x[:name]}.join(', ') + ")"
end

#shown_if(element) ⇒ Object


90
91
92
93
94
95
96
97
# File 'lib/formize/definition/form_element.rb', line 90

def shown_if(element)
  elements = []
  for child in self.children
    elements += child.shown_if(element)
  end
  elements << self if self.options[:shown_if] and self.options[:shown_if].to_s == element.name.to_s
  return elements
end