Sha256: 1d639457e65443f353b5a7cc200b478dcaa664de183d4bdae44c0688210a6097

Contents?: true

Size: 1.47 KB

Versions: 5

Compression:

Stored size: 1.47 KB

Contents

class AbstractFormBuilder
  attr_accessor :template, :object
  
  def initialize(template, object)
    raise "FormBuilder template must be initialized!" unless template
    raise "FormBuilder object must be initialized!" unless object
    @template = template
    @object   = object
  end
  
  def error_messages(options={})
    @template.error_messages_for(@object, options)
  end
  
  def label(field, options={})
    options.reverse_merge!(:caption => field.to_s.titleize)
    @template.label_tag(field_id(field), options)
  end
  
  def text_field(field, options={})
    options.reverse_merge!(:value => field_value(field))
    @template.text_field_tag field_name(field), options
  end
  
  def text_area(field, options={})
    options.reverse_merge!(:value => field_value(field))
    @template.text_area_tag field_name(field), options
  end
  
  def password_field(field, options={})
    options.reverse_merge!(:value => field_value(field))
    @template.password_field_tag field_name(field), options
  end
  
  def file_field(field, options={})
    @template.file_field_tag field_name(field), options
  end
  
  def submit(caption, options={})
    @template.submit_tag caption, options
  end
  
  private
  
  def object_name
    object.class.to_s.underscore
  end
  
  def field_value(field)
    @object && @object.respond_to?(field) ? @object.send(field) : ""
  end
  
  def field_name(field)
    "#{object_name}[#{field}]"
  end
  
  def field_id(field)
    "#{object_name}_#{field}"
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
sinatra_more-0.0.6 lib/sinatra_more/markup_plugin/form_builder/abstract_form_builder.rb
sinatra_more-0.0.5 lib/sinatra_more/markup_plugin/form_builder/abstract_form_builder.rb
sinatra_more-0.0.4 lib/sinatra_more/form_builder/abstract_form_builder.rb
sinatra_more-0.0.3 lib/sinatra_more/form_builder/abstract_form_builder.rb
sinatra_more-0.0.2 lib/sinatra_more/form_builder/abstract_form_builder.rb