class Formize::Definition::FormElement

Main class for form elements

Attributes

children[R]
depend_on[R]
form[R]
html_id[R]
id[R]
options[R]
parent[R]
unique_name[R]

Public Class Methods

new(form, parent = nil, options={}) click to toggle source
# 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

Public Instance Methods

all_elements() click to toggle source
# 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_fields() click to toggle source
# 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
arguments() click to toggle source
# 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
dependeds() click to toggle source
# 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
dependents() click to toggle source
# 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) click to toggle source

Find form elements

# 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) click to toggle source
# 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_choices() click to toggle source
# 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
prototype() click to toggle source
# File lib/formize/definition/form_element.rb, line 40
def prototype
  return "#{@unique_name}(" + arguments.collect{|x| x[:name]}.join(', ') + ")"
end
shown_if(element) click to toggle source
# 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

Protected Instance Methods

new_child(klass, *args) click to toggle source
# File lib/formize/definition/form_element.rb, line 111
def new_child(klass, *args)
  raise ArgumentError.new("Bad child type (#{klass.name}). Must be an Formize::Definition::FormElement") unless klass < Formize::Definition::FormElement
  element = klass.new(self.form, self, *args)
  @children << element
  return element
end