Module: SinatraMore::FormHelpers

Defined in:
lib/sinatra_more/markup_plugin/form_helpers.rb

Instance Method Summary collapse

Instance Method Details

#button_tag(caption, options = {}) ⇒ Object

Constructs a button input from the given options button_tag “Cancel”, :class => 'clear'


141
142
143
144
# File 'lib/sinatra_more/markup_plugin/form_helpers.rb', line 141

def button_tag(caption, options = {})
  options.reverse_merge!(:value => caption)
  input_tag(:button, options)
end

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

Constructs a check_box from the given options check_box_tag :remember_me, :value => 'Yes'


113
114
115
116
# File 'lib/sinatra_more/markup_plugin/form_helpers.rb', line 113

def check_box_tag(name, options={})
  options.reverse_merge!(:name => name, :value => '1')
  input_tag(:checkbox, options)
end

#error_messages_for(record, options = {}) ⇒ Object

Constructs list html for the errors for a given object error_messages_for @user


44
45
46
47
48
49
50
51
52
# File 'lib/sinatra_more/markup_plugin/form_helpers.rb', line 44

def error_messages_for(record, options={})
  return "" if record.blank? or record.errors.none?
  options.reverse_merge!(:header_message => "The #{record.class.to_s.downcase} could not be saved!")
  error_messages = record.errors.full_messages
  error_items = error_messages.collect { |er| (:li, er) }.join("\n")
  error_html = (:p, options.delete(:header_message))
  error_html << (:ul, error_items, :class => 'errors-list')
  (:div, error_html, :class => 'field-errors')
end

#field_set_tag(*args, &block) ⇒ Object

Constructs a field_set to group fields with given options field_set_tag(“Office”, :class => 'office-set') parameters: legend_text=nil, options={}


34
35
36
37
38
39
40
# File 'lib/sinatra_more/markup_plugin/form_helpers.rb', line 34

def field_set_tag(*args, &block)
  options = args.extract_options!
  legend_text = args[0].is_a?(String) ? args.first : nil
  legend_html = legend_text.blank? ? '' : (:legend, legend_text)
  field_set_content = legend_html + capture_html(&block)
  concat_content ('fieldset', field_set_content, options)
end

#fields_for(object, settings = {}, &block) ⇒ Object

Constructs form fields for an object using given or default form_builder Used within an existing form to allow alternate objects within one form fields_for @user.assignment do |assignment| … end fields_for :assignment do |assigment| … end


16
17
18
19
20
# File 'lib/sinatra_more/markup_plugin/form_helpers.rb', line 16

def fields_for(object, settings={}, &block)
  builder_class = configured_form_builder_class(settings[:builder])
  fields_html = capture_html(builder_class.new(self, object), &block)
  concat_content fields_html
end

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

Constructs a file field input from the given options file_field_tag :photo, :class => 'long'


127
128
129
130
# File 'lib/sinatra_more/markup_plugin/form_helpers.rb', line 127

def file_field_tag(name, options={})
  options.reverse_merge!(:name => name)
  input_tag(:file, options)
end

#form_for(object, url, settings = {}, &block) ⇒ Object

Constructs a form for object using given or default form_builder form_for :user, '/register' do |f| … end form_for @user, '/register', :id => 'register' do |f| … end


6
7
8
9
10
# File 'lib/sinatra_more/markup_plugin/form_helpers.rb', line 6

def form_for(object, url, settings={}, &block)
  builder_class = configured_form_builder_class(settings[:builder])
  form_html = capture_html(builder_class.new(self, object), &block)
  form_tag(url, settings) { form_html }
end

#form_tag(url, options = {}, &block) ⇒ Object

Constructs a form without object based on options form_tag '/register' do … end


24
25
26
27
28
29
# File 'lib/sinatra_more/markup_plugin/form_helpers.rb', line 24

def form_tag(url, options={}, &block)
  options.reverse_merge!(:method => 'post', :action => url)
  options[:enctype] = "multipart/form-data" if options.delete(:multipart)
  inner_form_html = hidden_form_method_field(options[:method]) + capture_html(&block)
  concat_content ('form', inner_form_html, options)
end

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

Constructs a hidden field input from the given options hidden_field_tag :session_key, :value => “__secret__”


70
71
72
73
# File 'lib/sinatra_more/markup_plugin/form_helpers.rb', line 70

def hidden_field_tag(name, options={})
  options.reverse_merge!(:name => name)
  input_tag(:hidden, options)
end

#image_submit_tag(source, options = {}) ⇒ Object

Constructs a submit button from the given options submit_tag “Create”, :class => 'success'


148
149
150
151
# File 'lib/sinatra_more/markup_plugin/form_helpers.rb', line 148

def image_submit_tag(source, options={})
  options.reverse_merge!(:src => image_path(source))
  input_tag(:image, options)
end

#label_tag(name, options = {}, &block) ⇒ Object

Constructs a label tag from the given options label_tag :username, :class => 'long-label' label_tag :username, :class => 'long-label' do … end


57
58
59
60
61
62
63
64
65
66
# File 'lib/sinatra_more/markup_plugin/form_helpers.rb', line 57

def label_tag(name, options={}, &block)
  options.reverse_merge!(:caption => "#{name.to_s.titleize}: ", :for => name)
  caption_text = options.delete(:caption)
  if block_given? # label with inner content
    label_content = caption_text + capture_html(&block)
    concat_content((:label, label_content, options))
  else # regular label
    (:label, caption_text, options)
  end
end

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

Constructs a password field input from the given options password_field_tag :password, :class => 'long'


91
92
93
94
# File 'lib/sinatra_more/markup_plugin/form_helpers.rb', line 91

def password_field_tag(name, options={})
  options.reverse_merge!(:name => name)
  input_tag(:password, options)
end

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

Constructs a radio_button from the given options radio_button_tag :remember_me, :value => 'true'


120
121
122
123
# File 'lib/sinatra_more/markup_plugin/form_helpers.rb', line 120

def radio_button_tag(name, options={})
  options.reverse_merge!(:name => name)
  input_tag(:radio, options)
end

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

Constructs a check_box from the given options options = [['caption', 'value'], ['Green', 'green1'], ['Blue', 'blue1'], ['Black', “black1”]] options = ['option', 'red', 'yellow' ] select_tag(:favorite_color, :options => ['red', 'yellow'], :selected => 'green1') select_tag(:country, :collection => @countries, :fields => [:name, :code])


101
102
103
104
105
106
107
108
109
# File 'lib/sinatra_more/markup_plugin/form_helpers.rb', line 101

def select_tag(name, options={})
  options.reverse_merge!(:name => name)
  collection, fields = options.delete(:collection), options.delete(:fields)
  options[:options] = options_from_collection(collection, fields) if collection
  options[:options].unshift('') if options.delete(:include_blank)
  select_options_html = options_for_select(options.delete(:options), options.delete(:selected))
  options.merge!(:name => "#{options[:name]}[]") if options[:multiple]
  (:select, select_options_html, options)
end

#submit_tag(caption = "Submit", options = {}) ⇒ Object

Constructs a submit button from the given options submit_tag “Create”, :class => 'success'


134
135
136
137
# File 'lib/sinatra_more/markup_plugin/form_helpers.rb', line 134

def submit_tag(caption="Submit", options={})
  options.reverse_merge!(:value => caption)
  input_tag(:submit, options)
end

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

Constructs a text area input from the given options text_area_tag :username, :class => 'long', :value => “Demo?”


84
85
86
87
# File 'lib/sinatra_more/markup_plugin/form_helpers.rb', line 84

def text_area_tag(name, options={})
  options.reverse_merge!(:name => name)
  (:textarea, options.delete(:value).to_s, options)
end

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

Constructs a text field input from the given options text_field_tag :username, :class => 'long'


77
78
79
80
# File 'lib/sinatra_more/markup_plugin/form_helpers.rb', line 77

def text_field_tag(name, options={})
  options.reverse_merge!(:name => name)
  input_tag(:text, options)
end