sinatra-tag-helpers 0.0.10 → 0.0.11
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +8 -8
- data/README.md +1 -1
- data/lib/sinatra/tag-helpers.rb +202 -176
- data/lib/sinatra/tag-helpers/version.rb +1 -1
- data/sinatra-tag-helpers.gemspec +2 -2
- metadata +8 -8
checksums.yaml
CHANGED
@@ -1,15 +1,15 @@
|
|
1
1
|
---
|
2
2
|
!binary "U0hBMQ==":
|
3
3
|
metadata.gz: !binary |-
|
4
|
-
|
4
|
+
NzI1YTcwY2ExN2RmMTRlN2E5NTlkOWZkNDkwYzA4M2FkMmEyMTRhZA==
|
5
5
|
data.tar.gz: !binary |-
|
6
|
-
|
6
|
+
NmNlMzQwYjY1MzMxNWI1NDcxMGM4MDg3Yjc3NGE0YThiNmNhNTVlZg==
|
7
7
|
SHA512:
|
8
8
|
metadata.gz: !binary |-
|
9
|
-
|
10
|
-
|
11
|
-
|
9
|
+
NmU4NzUxNzhkY2M1ZTIxNzM3NWU1MjgyOGQwMjRjNWYzYmUwYjRhYzExMWFi
|
10
|
+
OTgyNjIyZTYxYmQ2NDkyNWY1NTYxY2I3ODc2ZTczNTZjNTA0YjIyY2UzMWY5
|
11
|
+
ZmE3MDQ3MGIwYTBmOWFjNzhhOWM0YzZlMTU5NzhlNGFhOGQ2YTQ=
|
12
12
|
data.tar.gz: !binary |-
|
13
|
-
|
14
|
-
|
15
|
-
|
13
|
+
ZjFmZjZmOWMwYTAyMWMwYmFjNGY1ZWQ3NDYwZDlkYmI4ZjVjNTJlOTRkMzYz
|
14
|
+
ZGZkMzFjYTVjZTIzNzYxZjU2MGVhNGRjZjFmY2Y4ZDZmMWFhOGY1YjdjY2Yw
|
15
|
+
OWM5ZmM1MjNiZjg2ODM0NjJmNWMxMzZiMWZlZTgzNjM2ODI2Mjg=
|
data/README.md
CHANGED
data/lib/sinatra/tag-helpers.rb
CHANGED
@@ -1,217 +1,243 @@
|
|
1
1
|
require 'sinatra/base'
|
2
2
|
require 'sinatra/tag-helpers/version'
|
3
|
+
require 'escape_utils'
|
3
4
|
|
4
5
|
module Sinatra
|
5
6
|
module TagHelpers
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
hash
|
15
|
-
|
16
|
-
|
17
|
-
value.
|
18
|
-
|
7
|
+
module HelperMethods
|
8
|
+
# don't even try it
|
9
|
+
def html_escape(text = '')
|
10
|
+
EscapeUtils.escape_html(text || '')
|
11
|
+
end
|
12
|
+
alias_method :h, :html_escape
|
13
|
+
|
14
|
+
# converts a hash into HTML style attributes
|
15
|
+
def to_attributes(hash)
|
16
|
+
hash.collect do |key, value|
|
17
|
+
# for things like data: { stuff: 'hey' }
|
18
|
+
if value.is_a? Hash
|
19
|
+
value.collect do |k, v|
|
20
|
+
"#{key}-#{k}=\"#{v}\""
|
21
|
+
end
|
22
|
+
else
|
23
|
+
value.is_a?(TrueClass) ? key.to_s : "#{key}=\"#{value}\""
|
19
24
|
end
|
20
|
-
|
21
|
-
value.is_a?(TrueClass) ? key.to_s : "#{key}=\"#{value}\""
|
22
|
-
end
|
23
|
-
end.join(' ').chomp
|
24
|
-
end
|
25
|
-
|
26
|
-
# mostly so we can override this if we need to look anywhere besides the params
|
27
|
-
# * cough * session * cough *
|
28
|
-
def param_value(param_name)
|
29
|
-
html_escape(params[param_name.to_sym] || '')
|
30
|
-
end
|
31
|
-
|
32
|
-
# link helper
|
33
|
-
# examples:
|
34
|
-
#
|
35
|
-
# link_to 'Overview', '/account/overview' # => <a href='/account/overview'>Overview</a>
|
36
|
-
#
|
37
|
-
# when on /account/overview
|
38
|
-
#
|
39
|
-
# <a href='/account/overview' class='current'>Overview</a>
|
40
|
-
#
|
41
|
-
def link_to(text, link, attributes = {})
|
42
|
-
if URI.parse(link).path == request.path_info
|
43
|
-
attributes[:class] = "#{ attributes[:class] } current"
|
25
|
+
end.join(' ').chomp
|
44
26
|
end
|
45
27
|
|
46
|
-
|
28
|
+
# mostly so we can override this if we need to look anywhere besides the params
|
29
|
+
# * cough * session * cough *
|
30
|
+
def param_value(param_name)
|
31
|
+
html_escape(params[param_name.to_sym] || '')
|
32
|
+
end
|
47
33
|
|
48
|
-
|
49
|
-
|
34
|
+
# link helper
|
35
|
+
# examples:
|
36
|
+
#
|
37
|
+
# link_to 'Overview', '/account/overview' # => <a href='/account/overview'>Overview</a>
|
38
|
+
#
|
39
|
+
# when on /account/overview
|
40
|
+
#
|
41
|
+
# <a href='/account/overview' class='current'>Overview</a>
|
42
|
+
#
|
43
|
+
def link_to(text, link, attributes = {})
|
44
|
+
if URI.parse(link).path == request.path_info
|
45
|
+
attributes[:class] = [attributes[:class], settings.link_to_current_class].join(' ')
|
46
|
+
end
|
50
47
|
|
51
|
-
|
52
|
-
# if `param` is set in the `params` hash, the values from the `params` hash will be populated in the tag.
|
53
|
-
#
|
54
|
-
# <%= input_for 'something_hidden', type: 'hidden', value: 'Shhhhhh' %>
|
55
|
-
#
|
56
|
-
# Yields:
|
57
|
-
#
|
58
|
-
# <input type='hidden' name='something_hidden' id='something_hidden' value='Shhhhhh'>
|
59
|
-
#
|
60
|
-
def input_for(param, attributes = {})
|
61
|
-
attributes = {
|
62
|
-
:type => 'text',
|
63
|
-
:value => param_value(param),
|
64
|
-
:name => param,
|
65
|
-
:id => param
|
66
|
-
}.merge(attributes)
|
67
|
-
|
68
|
-
"<input #{ to_attributes(attributes) }>"
|
69
|
-
end
|
48
|
+
attributes.merge!({ :href => to(link) })
|
70
49
|
|
71
|
-
|
72
|
-
|
73
|
-
attributes = {
|
74
|
-
:type => 'radio'
|
75
|
-
}.merge(attributes)
|
50
|
+
"<a #{ to_attributes(attributes) }>#{ text }</a>"
|
51
|
+
end
|
76
52
|
|
77
|
-
|
78
|
-
|
53
|
+
# input_for creates an <input> tag with a number of configurable options
|
54
|
+
# if `param` is set in the `params` hash, the values from the `params` hash will be populated in the tag.
|
55
|
+
#
|
56
|
+
# <%= input_for 'something_hidden', type: 'hidden', value: 'Shhhhhh' %>
|
57
|
+
#
|
58
|
+
# Yields:
|
59
|
+
#
|
60
|
+
# <input type='hidden' name='something_hidden' id='something_hidden' value='Shhhhhh'>
|
61
|
+
#
|
62
|
+
def input_for(param, attributes = {})
|
63
|
+
attributes = {
|
64
|
+
:type => 'text',
|
65
|
+
:value => param_value(param),
|
66
|
+
:name => param,
|
67
|
+
:id => param
|
68
|
+
}.merge(attributes)
|
69
|
+
|
70
|
+
"<input #{ to_attributes(attributes) }>"
|
79
71
|
end
|
80
72
|
|
81
|
-
|
82
|
-
|
73
|
+
# 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
|
+
def radio_for(param, attributes = {})
|
75
|
+
attributes = {
|
76
|
+
:type => 'radio'
|
77
|
+
}.merge(attributes)
|
78
|
+
|
79
|
+
if param_value(param) == attributes[:value].to_s
|
80
|
+
attributes.merge!({ :checked => true })
|
81
|
+
end
|
83
82
|
|
84
|
-
|
85
|
-
#
|
86
|
-
# <%= checkbox_for 'is_cool', User.is_cool? %>
|
87
|
-
#
|
88
|
-
# Yields:
|
89
|
-
#
|
90
|
-
# <input type='checkbox' name='is_cool' id='is_cool' value='true'>
|
91
|
-
#
|
92
|
-
# Which will be marked with `checked` if `User.is_cool?` evaluates to true
|
93
|
-
#
|
94
|
-
def checkbox_for(param, checked_if = false, attributes = {})
|
95
|
-
attributes = {
|
96
|
-
:type => 'checkbox',
|
97
|
-
:value => 'true'
|
98
|
-
}.merge(attributes)
|
99
|
-
|
100
|
-
if checked_if || param_value(param) == attributes[:value].to_s
|
101
|
-
attributes.merge!({ checked: true })
|
83
|
+
input_for param, attributes
|
102
84
|
end
|
103
85
|
|
104
|
-
|
105
|
-
|
86
|
+
# checkbox_for creates an input of type checkbox with a `checked_if` argument to determine if it should be checked
|
87
|
+
#
|
88
|
+
# <%= checkbox_for 'is_cool', User.is_cool? %>
|
89
|
+
#
|
90
|
+
# Yields:
|
91
|
+
#
|
92
|
+
# <input type='checkbox' name='is_cool' id='is_cool' value='true'>
|
93
|
+
#
|
94
|
+
# Which will be marked with `checked` if `User.is_cool?` evaluates to true
|
95
|
+
#
|
96
|
+
def checkbox_for(param, checked_if = false, attributes = {})
|
97
|
+
attributes = {
|
98
|
+
:type => 'checkbox',
|
99
|
+
:value => 'true'
|
100
|
+
}.merge(attributes)
|
101
|
+
|
102
|
+
if checked_if || param_value(param) == attributes[:value].to_s
|
103
|
+
attributes.merge!({ checked: true })
|
104
|
+
end
|
106
105
|
|
107
|
-
|
108
|
-
|
109
|
-
attributes = {
|
110
|
-
:name => param,
|
111
|
-
:id => param
|
112
|
-
}.merge(attributes)
|
106
|
+
input_for param, attributes
|
107
|
+
end
|
113
108
|
|
114
|
-
|
115
|
-
|
109
|
+
# creates a simple <textarea> tag
|
110
|
+
def textarea_for(param, attributes = {})
|
111
|
+
attributes = {
|
112
|
+
:name => param,
|
113
|
+
:id => param
|
114
|
+
}.merge(attributes)
|
116
115
|
|
117
|
-
|
118
|
-
# if the param specified is set to the value of this option tag then it is marked as 'selected'
|
119
|
-
# designed to be used within a <select> element
|
120
|
-
#
|
121
|
-
# <%= option_for 'turtles', key: 'I love them', value: 'love' %>
|
122
|
-
#
|
123
|
-
# Yields:
|
124
|
-
#
|
125
|
-
# <option value='love'>I love them</option>
|
126
|
-
#
|
127
|
-
# If params[:turtle] is set to 'love' this yields:
|
128
|
-
#
|
129
|
-
# <option value='love' selected>I love them</option>
|
130
|
-
#
|
131
|
-
def option_for(param, attributes = {})
|
132
|
-
default = attributes.delete(:default).to_s
|
133
|
-
|
134
|
-
if !params[param.to_sym].nil? && !params[param.to_sym].empty?
|
135
|
-
default = param_value(param)
|
116
|
+
"<textarea #{ to_attributes(attributes) }>#{ param_value(param) }</textarea>"
|
136
117
|
end
|
137
118
|
|
138
|
-
|
139
|
-
|
119
|
+
# option_for creates an <option> element with the specified attributes
|
120
|
+
# if the param specified is set to the value of this option tag then it is marked as 'selected'
|
121
|
+
# designed to be used within a <select> element
|
122
|
+
#
|
123
|
+
# <%= option_for 'turtles', key: 'I love them', value: 'love' %>
|
124
|
+
#
|
125
|
+
# Yields:
|
126
|
+
#
|
127
|
+
# <option value='love'>I love them</option>
|
128
|
+
#
|
129
|
+
# If params[:turtle] is set to 'love' this yields:
|
130
|
+
#
|
131
|
+
# <option value='love' selected>I love them</option>
|
132
|
+
#
|
133
|
+
def option_for(param, attributes = {})
|
134
|
+
default = attributes.delete(:default).to_s
|
135
|
+
|
136
|
+
if !params[param.to_sym].nil? && !params[param.to_sym].empty?
|
137
|
+
default = param_value(param)
|
138
|
+
end
|
140
139
|
|
141
|
-
|
142
|
-
|
140
|
+
attributes.merge!({ :selected => true }) if default == attributes[:value].to_s
|
141
|
+
key = attributes.delete(:key)
|
143
142
|
|
144
|
-
|
145
|
-
|
146
|
-
#
|
147
|
-
# <%= select_for 'days', { monday: 'Monday', myday: 'MY DAY!' } %>
|
148
|
-
#
|
149
|
-
# Yields:
|
150
|
-
#
|
151
|
-
# <select name='days' id='days' size='1'>
|
152
|
-
# <option value='monday'>Monday</option>
|
153
|
-
# <option value='myday'>MY DAY!</option>
|
154
|
-
# </select>
|
155
|
-
#
|
156
|
-
def select_for(param, options, attributes = {})
|
157
|
-
attributes = {
|
158
|
-
:name => param,
|
159
|
-
:id => param
|
160
|
-
}.merge(attributes)
|
161
|
-
|
162
|
-
select = ["<select #{ to_attributes(attributes) }>"]
|
163
|
-
|
164
|
-
options.collect do |key, val|
|
165
|
-
# groups...
|
166
|
-
if val.is_a?(Hash)
|
167
|
-
select.push "<optgroup label='#{ key }'>"
|
168
|
-
|
169
|
-
val.each do |group_key, group_val|
|
170
|
-
select.push option_for(param, :key => group_key, :value => group_val, :default => attributes[:default] || param_value(param))
|
171
|
-
end
|
143
|
+
"<option #{ to_attributes(attributes) }>#{ key }</option>"
|
144
|
+
end
|
172
145
|
|
173
|
-
|
174
|
-
|
175
|
-
|
146
|
+
# select_for creates a <select> element with the specified attributes
|
147
|
+
# options are the available <option> tags within the <select> box
|
148
|
+
#
|
149
|
+
# <%= select_for 'days', { monday: 'Monday', myday: 'MY DAY!' } %>
|
150
|
+
#
|
151
|
+
# Yields:
|
152
|
+
#
|
153
|
+
# <select name='days' id='days' size='1'>
|
154
|
+
# <option value='monday'>Monday</option>
|
155
|
+
# <option value='myday'>MY DAY!</option>
|
156
|
+
# </select>
|
157
|
+
#
|
158
|
+
def select_for(param, options, attributes = {})
|
159
|
+
attributes = {
|
160
|
+
:name => param,
|
161
|
+
:id => param
|
162
|
+
}.merge(attributes)
|
163
|
+
|
164
|
+
select = ["<select #{ to_attributes(attributes) }>"]
|
165
|
+
|
166
|
+
options.collect do |key, val|
|
167
|
+
# groups...
|
168
|
+
if val.is_a?(Hash)
|
169
|
+
select.push "<optgroup label='#{ key }'>"
|
170
|
+
|
171
|
+
val.each do |group_key, group_val|
|
172
|
+
select.push option_for(param, :key => group_key, :value => group_val, :default => attributes[:default] || param_value(param))
|
173
|
+
end
|
174
|
+
|
175
|
+
select.push "</optgroup>"
|
176
|
+
else
|
177
|
+
select.push option_for(param, :key => key, :value => val, :default => attributes[:default] || param_value(param))
|
178
|
+
end
|
176
179
|
end
|
177
|
-
end
|
178
180
|
|
179
|
-
|
180
|
-
|
181
|
+
select.push('</select>').join(' ').chomp
|
182
|
+
end
|
181
183
|
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
|
184
|
+
# shortcut to generate a month list
|
185
|
+
def months_for(param, attributes = {})
|
186
|
+
select_for(param, settings.months_hash, attributes)
|
187
|
+
end
|
186
188
|
|
187
|
-
|
188
|
-
|
189
|
+
def years_for(param, range = settings.years_range, attributes = {})
|
190
|
+
options = { 'Year' => '' }
|
189
191
|
|
190
|
-
|
191
|
-
|
192
|
-
|
193
|
-
|
194
|
-
|
195
|
-
|
196
|
-
|
197
|
-
|
198
|
-
|
192
|
+
if range.last > range.first
|
193
|
+
# top down
|
194
|
+
range.last.downto(range.first) do |element|
|
195
|
+
options[element] = element
|
196
|
+
end
|
197
|
+
else
|
198
|
+
# bottom up
|
199
|
+
range.last.upto(range.first) do |element|
|
200
|
+
options[element] = element
|
201
|
+
end
|
199
202
|
end
|
203
|
+
|
204
|
+
select_for(param, options, attributes)
|
200
205
|
end
|
201
206
|
|
202
|
-
|
203
|
-
|
207
|
+
def days_for(param, attributes = {})
|
208
|
+
options = { 'Day' => '' }
|
204
209
|
|
205
|
-
|
206
|
-
|
210
|
+
(1..31).each do |day|
|
211
|
+
options[day] = day
|
212
|
+
end
|
207
213
|
|
208
|
-
|
209
|
-
options[day] = day
|
214
|
+
select_for(param, options, attributes)
|
210
215
|
end
|
216
|
+
end
|
211
217
|
|
212
|
-
|
218
|
+
# settings and such
|
219
|
+
def self.registered(app)
|
220
|
+
app.helpers TagHelpers::HelperMethods
|
221
|
+
|
222
|
+
app.set :link_to_current_class, 'current'
|
223
|
+
app.set :years_range, 1940..Date.today.year
|
224
|
+
app.set :months_hash, {
|
225
|
+
'Month' => '',
|
226
|
+
'1 - January' => '01',
|
227
|
+
'2 - February' => '02',
|
228
|
+
'3 - March' => '03',
|
229
|
+
'4 - April' => '04',
|
230
|
+
'5 - May' => '05',
|
231
|
+
'6 - June' => '06',
|
232
|
+
'7 - July' => '07',
|
233
|
+
'8 - August' => '08',
|
234
|
+
'9 - September' => '09',
|
235
|
+
'10 - October' => '10',
|
236
|
+
'11 - November' => '11',
|
237
|
+
'12 - December' => '12'
|
238
|
+
}
|
213
239
|
end
|
214
240
|
end
|
215
241
|
|
216
|
-
|
242
|
+
register TagHelpers
|
217
243
|
end
|
data/sinatra-tag-helpers.gemspec
CHANGED
@@ -16,9 +16,9 @@ Gem::Specification.new do |spec|
|
|
16
16
|
spec.require_paths = ['lib']
|
17
17
|
spec.license = 'MIT'
|
18
18
|
|
19
|
-
spec.add_dependency 'sinatra', '
|
19
|
+
spec.add_dependency 'sinatra', '>= 1.4.0'
|
20
20
|
spec.add_dependency 'escape_utils', '>= 0.3.0'
|
21
21
|
|
22
|
-
spec.add_development_dependency 'bundler'
|
22
|
+
spec.add_development_dependency 'bundler'
|
23
23
|
spec.add_development_dependency 'rake'
|
24
24
|
end
|
metadata
CHANGED
@@ -1,27 +1,27 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: sinatra-tag-helpers
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.11
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Evan Lecklider
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-02-
|
11
|
+
date: 2014-02-17 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: sinatra
|
15
15
|
requirement: !ruby/object:Gem::Requirement
|
16
16
|
requirements:
|
17
|
-
- -
|
17
|
+
- - ! '>='
|
18
18
|
- !ruby/object:Gem::Version
|
19
19
|
version: 1.4.0
|
20
20
|
type: :runtime
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
|
-
- -
|
24
|
+
- - ! '>='
|
25
25
|
- !ruby/object:Gem::Version
|
26
26
|
version: 1.4.0
|
27
27
|
- !ruby/object:Gem::Dependency
|
@@ -42,16 +42,16 @@ dependencies:
|
|
42
42
|
name: bundler
|
43
43
|
requirement: !ruby/object:Gem::Requirement
|
44
44
|
requirements:
|
45
|
-
- -
|
45
|
+
- - ! '>='
|
46
46
|
- !ruby/object:Gem::Version
|
47
|
-
version: '
|
47
|
+
version: '0'
|
48
48
|
type: :development
|
49
49
|
prerelease: false
|
50
50
|
version_requirements: !ruby/object:Gem::Requirement
|
51
51
|
requirements:
|
52
|
-
- -
|
52
|
+
- - ! '>='
|
53
53
|
- !ruby/object:Gem::Version
|
54
|
-
version: '
|
54
|
+
version: '0'
|
55
55
|
- !ruby/object:Gem::Dependency
|
56
56
|
name: rake
|
57
57
|
requirement: !ruby/object:Gem::Requirement
|