Class: LazyForm::Builder

Inherits:
Object
  • Object
show all
Defined in:
lib/lazy_form.rb

Constant Summary collapse

BUTTONS =
%w(button image reset submit)
INPUT_TYPES =
[
  'color',
  'date',
  'datetime',
  'email',
  'file',
  'hidden',
  'month',
  'number',
  'password',
  'radio',
  'range',
  'search',
  'tel',
  'text',
  'time',
  'url',
  'week'
]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(object) ⇒ Builder

Returns a new instance of Builder.



86
87
88
# File 'lib/lazy_form.rb', line 86

def initialize(object)
  @object = object
end

Instance Attribute Details

#objectObject (readonly)

Returns the value of attribute object.



84
85
86
# File 'lib/lazy_form.rb', line 84

def object
  @object
end

Instance Method Details

#checkbox(object_attribute, attributes = {}) ⇒ Object



98
99
100
101
102
103
104
105
106
107
108
# File 'lib/lazy_form.rb', line 98

def checkbox(object_attribute, attributes = {})
  attributes[:id] ||= as_id object_attribute
  attributes[:name] ||= as_name object_attribute
  attributes[:type] = 'checkbox'
  begin
    attributes[:checked] = :checked if object.send object_attribute
  rescue NoMethodError
  end

  Tag.new 'input', attributes
end

#datetime_local(object_attribute, attributes = {}) ⇒ Object



110
111
112
113
114
115
116
117
118
119
120
# File 'lib/lazy_form.rb', line 110

def datetime_local(object_attribute, attributes = {})
  attributes[:id] ||= as_id object_attribute
  attributes[:name] ||= as_name object_attribute
  attributes[:type] = 'datetime-local'
  begin
    attributes[:value] ||= object.send object_attribute
  rescue NoMethodError
  end

  Tag.new 'input', attributes
end

#label(object_attribute, content = nil, attributes = {}) ⇒ Object



137
138
139
140
141
# File 'lib/lazy_form.rb', line 137

def label(object_attribute, content = nil, attributes = {})
  attributes[:for] ||= as_id object_attribute

  Tag.new('label', attributes) { escape content }
end

#select(object_attribute, options = {}, attributes = {}) ⇒ Object



143
144
145
146
147
148
# File 'lib/lazy_form.rb', line 143

def select(object_attribute, options = {}, attributes = {})
  attributes[:id] ||= as_id object_attribute
  attributes[:name] ||= as_name object_attribute

  Tag.new('select', attributes) { build_options object_attribute, options }
end

#textarea(object_attribute, content = nil, attributes = {}) ⇒ Object



150
151
152
153
154
155
156
# File 'lib/lazy_form.rb', line 150

def textarea(object_attribute, content = nil, attributes = {})
  attributes[:id] ||= as_id object_attribute
  attributes[:name] ||= as_name object_attribute
  content ||= object.send object_attribute

  Tag.new('textarea', attributes) { escape content }
end