Module: Formation::Form::ClassMethods

Defined in:
lib/formation/form.rb

Instance Method Summary collapse

Instance Method Details

#elementsObject



5
6
7
# File 'lib/formation/form.rb', line 5

def elements
  @elements ||= []
end

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



9
10
11
12
13
14
15
16
17
18
19
# File 'lib/formation/form.rb', line 9

def field(name, options = {})
  @_current_resource ||= nil
  @_current_fieldset ||= nil
  if @_current_resource
    name = "#{@_current_resource}[#{name}]"
  else
    name = name.to_s
  end
  fields[name] = Formation::Field.new(name, { :fieldset => @_current_fieldset }.merge(options))
  elements << fields[name] unless @_current_fieldset
end

#fieldsObject



21
22
23
# File 'lib/formation/form.rb', line 21

def fields
  @fields ||= {}
end

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



25
26
27
28
29
30
31
32
33
34
# File 'lib/formation/form.rb', line 25

def fieldset(name, options = {})
  if name.is_a? Hash
    options = name
    name = Formation::Util.underscore(options[:legend])
  end
  @_current_fieldset = (fieldsets[name] ||= Formation::Fieldset.new(name, options))
  elements << @_current_fieldset
  yield
  @_current_fieldset = nil
end

#fieldsetsObject



36
37
38
# File 'lib/formation/form.rb', line 36

def fieldsets
  @fieldsets ||= {}
end

#resource(resource) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
# File 'lib/formation/form.rb', line 40

def resource(resource)
  resources << resource
  class_eval <<-RUBY
    attr_accessor :#{resource}
  RUBY
  if block_given?
    @_current_resource = resource
    yield
    @_current_resource = nil
  end
end

#resourcesObject



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

def resources
  @resources ||= []
end

#validates_with_method(method_name) ⇒ Object



56
57
58
59
# File 'lib/formation/form.rb', line 56

def validates_with_method(method_name)
  method_name = method_name.to_sym
  validators << method_name
end

#validatorsObject



61
62
63
# File 'lib/formation/form.rb', line 61

def validators
  @validators ||= []
end