Class: Presentation::Form

Inherits:
Base
  • Object
show all
Defined in:
lib/presentation/form.rb

Defined Under Namespace

Classes: Field, Group, GroupSet

Instance Attribute Summary collapse

Attributes inherited from Base

#controller, #presentable

Instance Method Summary collapse

Methods inherited from Base

#render

Methods included from Presenting::Configurable

#initialize

Instance Attribute Details

#buttonObject

the text on the submit button



95
96
97
# File 'lib/presentation/form.rb', line 95

def button
  @button ||= presentable.new_record? ? 'Create' : 'Update'
end

#htmlObject

a passthrough for form_for’s html. useful for classifying a form for ajax behavior (e.g. :html => => ‘ajax’)



101
102
103
# File 'lib/presentation/form.rb', line 101

def html
  @html
end

#methodObject

What method the form should use to post. Should default intelligently enough from the presentable. Not sure what use case would require it being set manually.



89
90
91
# File 'lib/presentation/form.rb', line 89

def method
  @method ||= presentable.new_record? ? :post : :put
end

#urlObject

The url where the form posts. May be anything that url_for accepts, including a set of records.



82
83
84
# File 'lib/presentation/form.rb', line 82

def url
  @url ||= presentable
end

Instance Method Details

#fieldsObject

Used to define fields in a group-less form.



68
69
70
71
72
73
# File 'lib/presentation/form.rb', line 68

def fields
  if groups.empty?
    groups << []
  end
  groups.first.fields
end

#fields=(args) ⇒ Object



74
75
76
77
78
# File 'lib/presentation/form.rb', line 74

def fields=(args)
  args.each do |field|
    fields << field
  end
end

#groupsObject

Fields may be grouped. Groups may or may not have names. Here’s how:

Presentation::Form.new(:groups => [
  [:a, :b],             # creates a nameless group with fields :a and :b
  {"foo" => [:c, :d]}   # creates a group named "foo" with fields :c and :d
])

Note that if you don’t need groups it’ll be simpler to just use fields= instead.



30
31
32
# File 'lib/presentation/form.rb', line 30

def groups
  @groups ||= GroupSet.new
end

#groups=(args) ⇒ Object



33
34
35
36
37
# File 'lib/presentation/form.rb', line 33

def groups=(args)
  args.each do |group|
    groups << group
  end
end

#inameObject



145
# File 'lib/presentation/form.rb', line 145

def iname; :form end

#protect_against_forgery?Boolean

:nodoc:

Returns:

  • (Boolean)


148
149
150
# File 'lib/presentation/form.rb', line 148

def protect_against_forgery? #:nodoc:
  allow_forgery_protection && request_forgery_protection_token
end