class LazyForm::Builder

Constants

BUTTONS
INPUT_TYPES

Attributes

object[R]

Public Class Methods

new(object) click to toggle source
# File lib/lazy_form.rb, line 86
def initialize(object)
  @object = object
end

Public Instance Methods

checkbox(object_attribute, attributes = {}) click to toggle source
# 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 = {}) click to toggle source
# 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 = {}) click to toggle source
# 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 = {}) click to toggle source
# 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 = {}) click to toggle source
# 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

Private Instance Methods

as_id(attribute) click to toggle source
# File lib/lazy_form.rb, line 164
def as_id(attribute)
  Inflecto.underscore "#{object.class.name}_#{attribute}"
end
as_name(attribute) click to toggle source
# File lib/lazy_form.rb, line 168
def as_name(attribute)
  "#{Inflecto.underscore object.class.name}[#{attribute}]"
end
build_options(object_attribute, options = {}) click to toggle source
# File lib/lazy_form.rb, line 172
def build_options(object_attribute, options = {})
  options.collect do |k, v|
    if v.is_a? Hash
      Tag.new('optgroup', label: k) { build_options object_attribute, v }
    else
      opts = { value: k }
      begin
        opts[:selected] = :selected if k == object.send(object_attribute)
      rescue NoMethodError
      end
      Tag.new('option', opts) { escape v }
    end
  end.join
end
escape(text) click to toggle source
# File lib/lazy_form.rb, line 160
def escape(text)
  CGI.escapeHTML text unless text.nil?
end