Module: Sinatra::TagHelpers::HelperMethods

Defined in:
lib/sinatra/tag-helpers.rb

Instance Method Summary collapse

Instance Method Details

#checkbox_for(param, checked_if = false, attributes = {}) ⇒ Object

checkbox_for creates an input of type checkbox with a ‘checked_if` argument to determine if it should be checked

<%= checkbox_for 'is_cool', User.is_cool? %>

Yields:

<input type='checkbox' name='is_cool' id='is_cool' value='true'>

Which will be marked with ‘checked` if `User.is_cool?` evaluates to true



96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/sinatra/tag-helpers.rb', line 96

def checkbox_for(param, checked_if = false, attributes = {})
  attributes = {
    :type  => 'checkbox',
    :value => 'true'
  }.merge(attributes)

  if checked_if || param_value(param) == attributes[:value].to_s
    attributes.merge!({ checked: true })
  end

  input_for param, attributes
end

#days_for(param, attributes = {}) ⇒ Object



209
210
211
212
213
214
215
216
217
# File 'lib/sinatra/tag-helpers.rb', line 209

def days_for(param, attributes = {})
  options = { 'Day' => '' }

  (1..31).each do |day|
    options[day] = day
  end

  select_for(param, options, attributes)
end

#html_escape(text = '') ⇒ Object Also known as: h

don’t even try it



9
10
11
# File 'lib/sinatra/tag-helpers.rb', line 9

def html_escape(text = '')
  EscapeUtils.escape_html(text || '')
end

#input_for(param, attributes = {}) ⇒ Object

input_for creates an <input> tag with a number of configurable options

if `param` is set in the `params` hash, the values from the `params` hash will be populated in the tag.

<%= input_for 'something_hidden', type: 'hidden', value: 'Shhhhhh' %>

Yields:

<input type='hidden' name='something_hidden' id='something_hidden' value='Shhhhhh'>


62
63
64
65
66
67
68
69
70
71
# File 'lib/sinatra/tag-helpers.rb', line 62

def input_for(param, attributes = {})
  attributes = {
    :type  => 'text',
    :value => param_value(param),
    :name  => param,
    :id    => param
  }.merge(attributes)

  "<input #{ to_attributes(attributes) }>"
end

link helper examples:

link_to 'Overview', '/account/overview' # => <a href='/account/overview'>Overview</a>

when on /account/overview

<a href='/account/overview' class='current'>Overview</a>


43
44
45
46
47
48
49
50
51
# File 'lib/sinatra/tag-helpers.rb', line 43

def link_to(text, link, attributes = {})
  if URI.parse(link).path == request.path_info
    attributes[:class] = [attributes[:class], settings.link_to_current_class].join(' ')
  end

  attributes.merge!({ :href => to(link) })

  "<a #{ to_attributes(attributes) }>#{ text }</a>"
end

#months_for(param, attributes = {}) ⇒ Object

shortcut to generate a month list



187
188
189
# File 'lib/sinatra/tag-helpers.rb', line 187

def months_for(param, attributes = {})
  select_for(param, settings.months_hash, attributes)
end

#option_for(param, attributes = {}) ⇒ Object

option_for creates an <option> element with the specified attributes

if the param specified is set to the value of this option tag then it is marked as 'selected'
designed to be used within a <select> element

<%= option_for 'turtles', key: 'I love them', value: 'love' %>

Yields:

<option value='love'>I love them</option>

If params is set to ‘love’ this yields:

<option value='love' selected>I love them</option>


133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/sinatra/tag-helpers.rb', line 133

def option_for(param, attributes = {})
  default = attributes.delete(:default).to_s

  if !params[param.to_sym].nil? && !params[param.to_sym].empty?
    default = param_value(param)
  end

  attributes.merge!({ :selected => true }) if default == attributes[:value].to_s
  key = attributes.delete(:key)

  "<option #{ to_attributes(attributes) }>#{ key }</option>"
end

#param_value(param_name) ⇒ Object

mostly so we can override this if we need to look anywhere besides the params

  • cough * session * cough *



30
31
32
# File 'lib/sinatra/tag-helpers.rb', line 30

def param_value(param_name)
  html_escape(params[param_name.to_sym] || '')
end

#radio_for(param, attributes = {}) ⇒ Object

radio_for creates an input tag of type radio and marks it ‘checked` if the param argument is set to the same value in the `params` hash



74
75
76
77
78
79
80
81
82
83
84
# File 'lib/sinatra/tag-helpers.rb', line 74

def radio_for(param, attributes = {})
  attributes = {
    :type => 'radio'
  }.merge(attributes)

  if param_value(param) == attributes[:value].to_s
    attributes.merge!({ :checked => true })
  end

  input_for param, attributes
end

#select_for(param, options, attributes = {}) ⇒ Object

select_for creates a <select> element with the specified attributes

options are the available <option> tags within the <select> box

<%= select_for 'days', { monday: 'Monday', myday: 'MY DAY!' } %>

Yields:

<select name='days' id='days' size='1'>
  <option value='monday'>Monday</option>
  <option value='myday'>MY DAY!</option>
</select>

Raises:

  • (ArgumentError)


158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
# File 'lib/sinatra/tag-helpers.rb', line 158

def select_for(param, options, attributes = {})
  raise ArgumentError, "Options for `select_for` should be a Hash." unless options.is_a?(Hash)

  attributes = {
    :name => param,
    :id   => param
  }.merge(attributes)

  select = ["<select #{ to_attributes(attributes) }>"]

  options.collect do |key, val|
    # groups...
    if val.is_a?(Hash)
      select.push "<optgroup label='#{ key }'>"

      val.each do |group_key, group_val|
        select.push option_for(param, :key => group_key, :value => group_val, :default => attributes[:default] || param_value(param))
      end

      select.push "</optgroup>"
    else
      select.push option_for(param, :key => key, :value => val, :default => attributes[:default] || param_value(param))
    end
  end

  select.push('</select>').join(' ').chomp
end

#textarea_for(param, attributes = {}) ⇒ Object

creates a simple <textarea> tag



110
111
112
113
114
115
116
117
# File 'lib/sinatra/tag-helpers.rb', line 110

def textarea_for(param, attributes = {})
  attributes = {
    :name => param,
    :id   => param
  }.merge(attributes)

  "<textarea #{ to_attributes(attributes) }>#{ param_value(param) }</textarea>"
end

#to_attributes(hash) ⇒ Object

converts a hash into HTML style attributes



15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/sinatra/tag-helpers.rb', line 15

def to_attributes(hash)
  hash.collect do |key, value|
    # for things like data: { stuff: 'hey' }
    if value.is_a? Hash
      value.collect do |k, v|
        "#{key}-#{k}=\"#{v}\""
      end
    else
      value.is_a?(TrueClass) ? key.to_s : "#{key}=\"#{value}\""
    end
  end.join(' ').chomp
end

#years_for(param, range = settings.years_range, attributes = {}) ⇒ Object



191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
# File 'lib/sinatra/tag-helpers.rb', line 191

def years_for(param, range = settings.years_range, attributes = {})
  options = { 'Year' => '' }

  if range.last > range.first
    # top down
    range.last.downto(range.first) do |element|
      options[element] = element
    end
  else
    # bottom up
    range.last.upto(range.first) do |element|
      options[element] = element
    end
  end

  select_for(param, options, attributes)
end