Module: Rocketeer::Helpers::Form

Defined in:
lib/rocketeer/helpers/form.rb

Instance Method Summary collapse

Instance Method Details

#checkbox(name, options = {}) ⇒ String

Checkbox

Parameters:

  • name (Mixed)
  • options (Hash) (defaults to: {})

Returns:

  • (String)

Since:

  • 0.6



153
154
155
156
157
158
# File 'lib/rocketeer/helpers/form.rb', line 153

def checkbox(name, options = {})
  if options.key?(:checked)
    options.delete(:checked) if !options[:checked]
  end
  input_field name, options.merge({ :type => 'checkbox' })
end

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

Email field builder

@example:

email_field :my_field, :class => 'text', :value => 'my value'

Parameters:

  • name (Mixed)

    The name of the field

  • options (Hash) (defaults to: {})

    The fields options

Author:

  • Jack Polgar

Since:

  • 0.1



110
111
112
# File 'lib/rocketeer/helpers/form.rb', line 110

def email_field(name, options = {})
  input_field name, options.merge({:type => 'email'})
end

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

Input field builder

@example:

input_field :my_field, :type => 'text'

Parameters:

  • name (Mixed)

    The name of the field

  • options (Hash) (defaults to: {})

    The fields options

Author:

  • Jack Polgar

Since:

  • 0.1



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/rocketeer/helpers/form.rb', line 53

def input_field(name, options = {})
  options = {
    :name => _name(name).to_s
  }.merge(options)

  # If the ID is not set, use the field name
  if !options.key? :id
    options[:id] = _id(name).to_s
  end

  # Remove the ID if it's false
  options.delete(:id) if options[:id] === false

  "<input#{HTML.attributes(options)}>"
end

#label(text, options = {}) ⇒ String

Label tag builder

@author: Jack Polgar @example:

label 'Username', :for => :username

Parameters:

  • Text (String)

    for thelabel

  • Options (Hash)

Returns:

  • (String)

Since:

  • 0.5



37
38
39
40
# File 'lib/rocketeer/helpers/form.rb', line 37

def label(text, options = {})
  options[:for] = _id(options[:for]) if options[:for]
  "<label#{HTML.attributes(options)}>#{text}</label>"
end

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

Password field builder

@example:

pasword_field :my_field, :class => 'text', :value => 'my value'

Parameters:

  • name (Mixed)

    The name of the field

  • options (Hash) (defaults to: {})

    The fields options

Author:

  • Jack Polgar

Since:

  • 0.1



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

def password_field(name, options = {})
  input_field name, options.merge({:type => 'password'})
end

#radio_button(name, options = {}) ⇒ String

Radio button

Parameters:

  • name (Mixed)
  • options (Hash) (defaults to: {})

Returns:

  • (String)

Since:

  • 0.6



170
171
172
# File 'lib/rocketeer/helpers/form.rb', line 170

def radio_button(name, options = {})
  input_field name, options.merge({ :type => 'radio', :id => false })
end

#select_box(name, rows, selected = nil) ⇒ Object

Select box builder

@example:

select_box :my_field, [['Hello', 1], ['World', 2]], 2

Parameters:

  • name (Mixed)

    The name of the field

  • rows (Array)

    Array of the select field options

  • selected (Mixed) (defaults to: nil)

    Value of the selected option

Author:

  • Jack Polgar

Since:

  • 0.1



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/rocketeer/helpers/form.rb', line 126

def select_box(name, rows, selected = nil)
  name = _name(name)
  html = ["<select name=\"#{name.to_s}\" id=\"#{_id(name).to_s}\">"]

  rows.each do |row|
    if row[1] == selected
      selected_html = ' selected'
    else
      selected_html = ''
    end
    html.push "  <option value=\"#{row[1]}\"#{selected_html}>#{row[0]}</option>"
  end

  html.push "</select>\n"
  return html.join("\n")
end

#submit_button(text, options = {}) ⇒ String

Creates a submit button (using <button>)

Parameters:

  • text (String)
  • options (Hash) (defaults to: {})

    HTML attributes

Returns:

  • (String)

Author:

  • Jack Polgar

Since:

  • 0.5



212
213
214
# File 'lib/rocketeer/helpers/form.rb', line 212

def submit_button(text, options = {})
  "<button type=\"submit\"#{HTML.attributes(options)}>#{text}</button>"
end

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

Text field builder

@example:

text_field :my_field, :class => 'text', :value => 'my value'

Parameters:

  • name (Mixed)

    The name of the field

  • options (Hash) (defaults to: {})

    The fields options

Author:

  • Jack Polgar

Since:

  • 0.1



80
81
82
# File 'lib/rocketeer/helpers/form.rb', line 80

def text_field(name, options = {})
  input_field name, options.merge({:type => 'text'})
end

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

Text area builder

@example:

textarea :my_field, :class => 'text', :value => 'my value'

Parameters:

  • name (Mixed)

    The name of the field

  • options (Hash) (defaults to: {})

    The fields options

Author:

  • Jack Polgar

Since:

  • 0.1



185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
# File 'lib/rocketeer/helpers/form.rb', line 185

def textarea(name, options = {})
  options = {
    :id => _id(name).to_s,
    :name => _name(name).to_s
  }.merge(options)

  if options[:value]
    value = options[:value]
    options[:value] = nil
  else
    value = ''
  end

  "<textarea#{HTML.attributes(options)}>#{value}</textarea>"
end