padrino-helpers 0.7.5 → 0.7.6
Sign up to get free protection for your applications and to get access to all the features.
- data/VERSION +1 -1
- data/lib/padrino-helpers.rb +21 -2
- data/lib/padrino-helpers/asset_tag_helpers.rb +11 -12
- data/lib/padrino-helpers/form_builder/abstract_form_builder.rb +6 -6
- data/lib/padrino-helpers/form_builder/standard_form_builder.rb +15 -12
- data/lib/padrino-helpers/form_helpers.rb +110 -27
- data/lib/padrino-helpers/format_helpers.rb +44 -11
- data/lib/padrino-helpers/number_helpers.rb +271 -0
- data/lib/padrino-helpers/output_helpers.rb +35 -10
- data/lib/padrino-helpers/render_helpers.rb +10 -5
- data/lib/padrino-helpers/tag_helpers.rb +24 -9
- data/padrino-helpers.gemspec +7 -5
- data/test/helper.rb +2 -0
- data/test/support_helpers.rb +0 -1
- data/test/test_number_helpers.rb +136 -0
- metadata +5 -3
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.7.
|
1
|
+
0.7.6
|
data/lib/padrino-helpers.rb
CHANGED
@@ -7,7 +7,25 @@ Dir[File.dirname(__FILE__) + '/padrino-helpers/**/*.rb'].each {|file| require fi
|
|
7
7
|
I18n.load_path += Dir["#{File.dirname(__FILE__)}/padrino-helpers/locale/*.yml"]
|
8
8
|
|
9
9
|
module Padrino
|
10
|
+
##
|
11
|
+
# This component provides a great deal of view helpers related to html markup generation.
|
12
|
+
# There are helpers for generating tags, forms, links, images, and more.
|
13
|
+
# Most of the basic methods should be very familiar to anyone who has used rails view helpers.
|
14
|
+
#
|
10
15
|
module Helpers
|
16
|
+
##
|
17
|
+
# Register these helpers:
|
18
|
+
#
|
19
|
+
# Padrino::Helpers::OutputHelpers
|
20
|
+
# Padrino::Helpers::TagHelpers
|
21
|
+
# Padrino::Helpers::AssetTagHelpers
|
22
|
+
# Padrino::Helpers::FormHelpers
|
23
|
+
# Padrino::Helpers::FormatHelpers
|
24
|
+
# Padrino::Helpers::RenderHelpers
|
25
|
+
# Padrino::Helpers::NumberHelpers
|
26
|
+
#
|
27
|
+
# for Padrino::Application
|
28
|
+
#
|
11
29
|
def self.registered(app)
|
12
30
|
app.set :default_builder, 'StandardFormBuilder'
|
13
31
|
app.helpers Padrino::Helpers::OutputHelpers
|
@@ -16,6 +34,7 @@ module Padrino
|
|
16
34
|
app.helpers Padrino::Helpers::FormHelpers
|
17
35
|
app.helpers Padrino::Helpers::FormatHelpers
|
18
36
|
app.helpers Padrino::Helpers::RenderHelpers
|
37
|
+
app.helpers Padrino::Helpers::NumberHelpers
|
19
38
|
end
|
20
|
-
end
|
21
|
-
end
|
39
|
+
end # Helpers
|
40
|
+
end # Padrino
|
@@ -1,11 +1,10 @@
|
|
1
1
|
module Padrino
|
2
2
|
module Helpers
|
3
3
|
module AssetTagHelpers
|
4
|
-
|
5
4
|
##
|
6
5
|
# Creates a div to display the flash of given type if it exists
|
7
6
|
#
|
8
|
-
# Examples
|
7
|
+
# ==== Examples
|
9
8
|
#
|
10
9
|
# flash_tag(:notice, :class => 'flash', :id => 'flash-notice')
|
11
10
|
def flash_tag(kind, options={})
|
@@ -18,7 +17,7 @@ module Padrino
|
|
18
17
|
##
|
19
18
|
# Creates a link element with given name, url and options
|
20
19
|
#
|
21
|
-
# Examples
|
20
|
+
# ==== Examples
|
22
21
|
#
|
23
22
|
# link_to 'click me', '/dashboard', :class => 'linky'
|
24
23
|
# link_to 'click me', '/dashboard', :if => @foo.present?
|
@@ -51,7 +50,7 @@ module Padrino
|
|
51
50
|
##
|
52
51
|
# Creates a mail link element with given name and caption
|
53
52
|
#
|
54
|
-
# Examples
|
53
|
+
# ==== Examples
|
55
54
|
# mail_to "me@demo.com" => <a href="mailto:me@demo.com">me@demo.com</a>
|
56
55
|
# mail_to "me@demo.com", "My Email" => <a href="mailto:me@demo.com">My Email</a>
|
57
56
|
#
|
@@ -65,7 +64,7 @@ module Padrino
|
|
65
64
|
##
|
66
65
|
# Creates a meta element with the content and given options
|
67
66
|
#
|
68
|
-
# Examples
|
67
|
+
# ==== Examples
|
69
68
|
#
|
70
69
|
# meta_tag "weblog,news", :name => "keywords" => <meta name="keywords" content="weblog,news">
|
71
70
|
# meta_tag "text/html; charset=UTF-8", :http-equiv => "Content-Type" => <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
|
@@ -78,7 +77,7 @@ module Padrino
|
|
78
77
|
##
|
79
78
|
# Creates an image element with given url and options
|
80
79
|
#
|
81
|
-
#
|
80
|
+
# ==== Examples
|
82
81
|
#
|
83
82
|
# image_tag('icons/avatar.png')
|
84
83
|
#
|
@@ -92,7 +91,7 @@ module Padrino
|
|
92
91
|
# You can pass in the filename without extension or a symbol and we search it in your +appname.public+
|
93
92
|
# like app/public/stylesheets for inclusion. You can provide also a full path.
|
94
93
|
#
|
95
|
-
#
|
94
|
+
# ==== Examples
|
96
95
|
#
|
97
96
|
# stylesheet_link_tag 'style', 'application', 'layout'
|
98
97
|
#
|
@@ -106,7 +105,7 @@ module Padrino
|
|
106
105
|
# You can pass in the filename without extension or a symbol and we search it in your +appname.public+
|
107
106
|
# like app/public/javascript for inclusion. You can provide also a full path.
|
108
107
|
#
|
109
|
-
#
|
108
|
+
# ==== Examples
|
110
109
|
#
|
111
110
|
# javascript_include_tag 'application', :extjs
|
112
111
|
#
|
@@ -119,7 +118,7 @@ module Padrino
|
|
119
118
|
# Returns the path to the image, either relative or absolute. We search it in your +appname.public+
|
120
119
|
# like app/public/images for inclusion. You can provide also a full path.
|
121
120
|
#
|
122
|
-
#
|
121
|
+
# ==== Examples
|
123
122
|
#
|
124
123
|
# image_path("foo.jpg") => "yourapp/public/images/foo.jpg"
|
125
124
|
#
|
@@ -184,6 +183,6 @@ module Padrino
|
|
184
183
|
true
|
185
184
|
end
|
186
185
|
end
|
187
|
-
end
|
188
|
-
end
|
189
|
-
end
|
186
|
+
end # AssetTagHelpers
|
187
|
+
end # Helpers
|
188
|
+
end # Padrino
|
@@ -1,7 +1,7 @@
|
|
1
1
|
module Padrino
|
2
2
|
module Helpers
|
3
|
-
module FormBuilder
|
4
|
-
class AbstractFormBuilder
|
3
|
+
module FormBuilder #:nodoc:
|
4
|
+
class AbstractFormBuilder #:nodoc:
|
5
5
|
attr_accessor :template, :object
|
6
6
|
|
7
7
|
def initialize(template, object)
|
@@ -144,7 +144,7 @@ module Padrino
|
|
144
144
|
def object_class(explicit_object)
|
145
145
|
explicit_object.is_a?(Symbol) ? explicit_object.to_s.classify.constantize : explicit_object.class
|
146
146
|
end
|
147
|
-
end
|
148
|
-
end
|
149
|
-
end
|
150
|
-
end
|
147
|
+
end # AbstractFormBuilder
|
148
|
+
end # FormBuilder
|
149
|
+
end # Helpers
|
150
|
+
end # Padrino
|
@@ -2,15 +2,18 @@ require File.dirname(__FILE__) + '/abstract_form_builder' unless defined?(Abstra
|
|
2
2
|
|
3
3
|
module Padrino
|
4
4
|
module Helpers
|
5
|
-
module FormBuilder
|
6
|
-
class StandardFormBuilder < AbstractFormBuilder
|
5
|
+
module FormBuilder #:nodoc:
|
6
|
+
class StandardFormBuilder < AbstractFormBuilder #:nodoc:
|
7
7
|
|
8
|
-
|
9
|
-
#
|
10
|
-
#
|
11
|
-
#
|
12
|
-
#
|
13
|
-
#
|
8
|
+
##
|
9
|
+
# StandardFormBuilder
|
10
|
+
#
|
11
|
+
# text_field_block(:username, { :class => 'long' }, { :class => 'wide-label' })
|
12
|
+
# text_area_block(:summary, { :class => 'long' }, { :class => 'wide-label' })
|
13
|
+
# password_field_block(:password, { :class => 'long' }, { :class => 'wide-label' })
|
14
|
+
# file_field_block(:photo, { :class => 'long' }, { :class => 'wide-label' })
|
15
|
+
# check_box_block(:remember_me, { :class => 'long' }, { :class => 'wide-label' })
|
16
|
+
# select_block(:color, :options => ['green', 'black'])
|
14
17
|
(self.field_types - [ :hidden_field, :radio_button ]).each do |field_type|
|
15
18
|
class_eval <<-EOF
|
16
19
|
def #{field_type}_block(field, options={}, label_options={})
|
@@ -33,7 +36,7 @@ module Padrino
|
|
33
36
|
submit_html = self.image_submit(source, options)
|
34
37
|
@template.content_tag(:p, submit_html)
|
35
38
|
end
|
36
|
-
end
|
37
|
-
end
|
38
|
-
end
|
39
|
-
end
|
39
|
+
end # StandardFormBuilder
|
40
|
+
end # FormBuilder
|
41
|
+
end # Helpers
|
42
|
+
end # Padrino
|
@@ -1,27 +1,42 @@
|
|
1
1
|
module Padrino
|
2
2
|
module Helpers
|
3
3
|
module FormHelpers
|
4
|
+
##
|
4
5
|
# Constructs a form for object using given or default form_builder
|
5
|
-
#
|
6
|
-
#
|
6
|
+
#
|
7
|
+
# ==== Examples
|
8
|
+
#
|
9
|
+
# form_for :user, '/register' do |f| ... end
|
10
|
+
# form_for @user, '/register', :id => 'register' do |f| ... end
|
11
|
+
#
|
7
12
|
def form_for(object, url, settings={}, &block)
|
8
13
|
builder_class = configured_form_builder_class(settings[:builder])
|
9
14
|
form_html = capture_html(builder_class.new(self, object), &block)
|
10
15
|
form_tag(url, settings) { form_html }
|
11
16
|
end
|
12
17
|
|
18
|
+
##
|
13
19
|
# Constructs form fields for an object using given or default form_builder
|
14
20
|
# Used within an existing form to allow alternate objects within one form
|
15
|
-
#
|
16
|
-
#
|
21
|
+
#
|
22
|
+
# ==== Examples
|
23
|
+
#
|
24
|
+
# fields_for @user.assignment do |assignment| ... end
|
25
|
+
# fields_for :assignment do |assigment| ... end
|
26
|
+
#
|
17
27
|
def fields_for(object, settings={}, &block)
|
18
28
|
builder_class = configured_form_builder_class(settings[:builder])
|
19
29
|
fields_html = capture_html(builder_class.new(self, object), &block)
|
20
30
|
concat_content fields_html
|
21
31
|
end
|
22
32
|
|
33
|
+
##
|
23
34
|
# Constructs a form without object based on options
|
24
|
-
#
|
35
|
+
#
|
36
|
+
# ==== Examples
|
37
|
+
#
|
38
|
+
# form_tag '/register' do ... end
|
39
|
+
#
|
25
40
|
def form_tag(url, options={}, &block)
|
26
41
|
options.reverse_merge!(:method => 'post', :action => url)
|
27
42
|
options[:enctype] = "multipart/form-data" if options.delete(:multipart)
|
@@ -30,9 +45,13 @@ module Padrino
|
|
30
45
|
concat_content content_tag('form', inner_form_html, options)
|
31
46
|
end
|
32
47
|
|
48
|
+
##
|
33
49
|
# Constructs a field_set to group fields with given options
|
34
|
-
#
|
35
|
-
#
|
50
|
+
#
|
51
|
+
# ==== Examples
|
52
|
+
#
|
53
|
+
# field_set_tag("Office", :class => 'office-set')
|
54
|
+
#
|
36
55
|
def field_set_tag(*args, &block)
|
37
56
|
options = args.extract_options!
|
38
57
|
legend_text = args[0].is_a?(String) ? args.first : nil
|
@@ -41,8 +60,13 @@ module Padrino
|
|
41
60
|
concat_content content_tag('fieldset', field_set_content, options)
|
42
61
|
end
|
43
62
|
|
63
|
+
##
|
44
64
|
# Constructs list html for the errors for a given symbol
|
45
|
-
#
|
65
|
+
#
|
66
|
+
# ==== Examples
|
67
|
+
#
|
68
|
+
# error_messages_for :user
|
69
|
+
#
|
46
70
|
def error_messages_for(*params)
|
47
71
|
options = params.extract_options!.symbolize_keys
|
48
72
|
objects = params.collect {|object_name| object_name.is_a?(Symbol) ? instance_variable_get("@#{object_name}") : object_name }.compact
|
@@ -84,9 +108,14 @@ module Padrino
|
|
84
108
|
end
|
85
109
|
end
|
86
110
|
|
111
|
+
##
|
87
112
|
# Constructs a label tag from the given options
|
88
|
-
#
|
89
|
-
#
|
113
|
+
#
|
114
|
+
# ==== Examples
|
115
|
+
#
|
116
|
+
# label_tag :username, :class => 'long-label'
|
117
|
+
# label_tag :username, :class => 'long-label' do ... end
|
118
|
+
#
|
90
119
|
def label_tag(name, options={}, &block)
|
91
120
|
options.reverse_merge!(:caption => "#{name.to_s.titleize}: ", :for => name)
|
92
121
|
caption_text = options.delete(:caption)
|
@@ -99,39 +128,64 @@ module Padrino
|
|
99
128
|
end
|
100
129
|
end
|
101
130
|
|
131
|
+
##
|
102
132
|
# Constructs a hidden field input from the given options
|
103
|
-
#
|
133
|
+
#
|
134
|
+
# ==== Examples
|
135
|
+
#
|
136
|
+
# hidden_field_tag :session_key, :value => "__secret__"
|
137
|
+
#
|
104
138
|
def hidden_field_tag(name, options={})
|
105
139
|
options.reverse_merge!(:name => name)
|
106
140
|
input_tag(:hidden, options)
|
107
141
|
end
|
108
142
|
|
143
|
+
##
|
109
144
|
# Constructs a text field input from the given options
|
110
|
-
#
|
145
|
+
#
|
146
|
+
# ==== Examples
|
147
|
+
#
|
148
|
+
# text_field_tag :username, :class => 'long'
|
149
|
+
#
|
111
150
|
def text_field_tag(name, options={})
|
112
151
|
options.reverse_merge!(:name => name)
|
113
152
|
input_tag(:text, options)
|
114
153
|
end
|
115
154
|
|
155
|
+
##
|
116
156
|
# Constructs a text area input from the given options
|
117
|
-
#
|
157
|
+
#
|
158
|
+
# ==== Examples
|
159
|
+
#
|
160
|
+
# text_area_tag :username, :class => 'long', :value => "Demo?"
|
161
|
+
#
|
118
162
|
def text_area_tag(name, options={})
|
119
163
|
options.reverse_merge!(:name => name)
|
120
164
|
content_tag(:textarea, options.delete(:value).to_s, options)
|
121
165
|
end
|
122
166
|
|
167
|
+
##
|
123
168
|
# Constructs a password field input from the given options
|
124
|
-
#
|
169
|
+
#
|
170
|
+
# ==== Examples
|
171
|
+
#
|
172
|
+
# password_field_tag :password, :class => 'long'
|
173
|
+
#
|
125
174
|
def password_field_tag(name, options={})
|
126
175
|
options.reverse_merge!(:name => name)
|
127
176
|
input_tag(:password, options)
|
128
177
|
end
|
129
178
|
|
179
|
+
##
|
130
180
|
# Constructs a check_box from the given options
|
131
|
-
#
|
132
|
-
#
|
133
|
-
#
|
134
|
-
#
|
181
|
+
#
|
182
|
+
# ==== Examples
|
183
|
+
#
|
184
|
+
# options = [['caption', 'value'], ['Green', 'green1'], ['Blue', 'blue1'], ['Black', "black1"]]
|
185
|
+
# options = ['option', 'red', 'yellow' ]
|
186
|
+
# select_tag(:favorite_color, :options => ['red', 'yellow'], :selected => 'green1')
|
187
|
+
# select_tag(:country, :collection => @countries, :fields => [:name, :code])
|
188
|
+
#
|
135
189
|
def select_tag(name, options={})
|
136
190
|
options.reverse_merge!(:name => name)
|
137
191
|
collection, fields = options.delete(:collection), options.delete(:fields)
|
@@ -142,43 +196,72 @@ module Padrino
|
|
142
196
|
content_tag(:select, select_options_html, options)
|
143
197
|
end
|
144
198
|
|
199
|
+
##
|
145
200
|
# Constructs a check_box from the given options
|
146
|
-
#
|
201
|
+
#
|
202
|
+
# ==== Examples
|
203
|
+
#
|
204
|
+
# check_box_tag :remember_me, :value => 'Yes'
|
205
|
+
#
|
147
206
|
def check_box_tag(name, options={})
|
148
207
|
options.reverse_merge!(:name => name, :value => '1')
|
149
208
|
input_tag(:checkbox, options)
|
150
209
|
end
|
151
210
|
|
211
|
+
##
|
152
212
|
# Constructs a radio_button from the given options
|
153
|
-
#
|
213
|
+
#
|
214
|
+
# ==== Examples
|
215
|
+
#
|
216
|
+
# radio_button_tag :remember_me, :value => 'true'
|
217
|
+
#
|
154
218
|
def radio_button_tag(name, options={})
|
155
219
|
options.reverse_merge!(:name => name)
|
156
220
|
input_tag(:radio, options)
|
157
221
|
end
|
158
222
|
|
223
|
+
##
|
159
224
|
# Constructs a file field input from the given options
|
160
|
-
#
|
225
|
+
#
|
226
|
+
# ==== Examples
|
227
|
+
#
|
228
|
+
# file_field_tag :photo, :class => 'long'
|
229
|
+
#
|
161
230
|
def file_field_tag(name, options={})
|
162
231
|
options.reverse_merge!(:name => name)
|
163
232
|
input_tag(:file, options)
|
164
233
|
end
|
165
234
|
|
235
|
+
##
|
166
236
|
# Constructs a submit button from the given options
|
167
|
-
#
|
237
|
+
#
|
238
|
+
# ==== Examples
|
239
|
+
#
|
240
|
+
# submit_tag "Create", :class => 'success'
|
241
|
+
#
|
168
242
|
def submit_tag(caption="Submit", options={})
|
169
243
|
options.reverse_merge!(:value => caption)
|
170
244
|
input_tag(:submit, options)
|
171
245
|
end
|
172
246
|
|
247
|
+
##
|
173
248
|
# Constructs a button input from the given options
|
174
|
-
#
|
249
|
+
#
|
250
|
+
# ==== Examples
|
251
|
+
#
|
252
|
+
# button_tag "Cancel", :class => 'clear'
|
253
|
+
#
|
175
254
|
def button_tag(caption, options = {})
|
176
255
|
options.reverse_merge!(:value => caption)
|
177
256
|
input_tag(:button, options)
|
178
257
|
end
|
179
258
|
|
180
259
|
# Constructs a submit button from the given options
|
181
|
-
#
|
260
|
+
#
|
261
|
+
# ==== Examples
|
262
|
+
#
|
263
|
+
# submit_tag "Create", :class => 'success'
|
264
|
+
#
|
182
265
|
def image_submit_tag(source, options={})
|
183
266
|
options.reverse_merge!(:src => image_path(source))
|
184
267
|
input_tag(:image, options)
|
@@ -221,6 +304,6 @@ module Padrino
|
|
221
304
|
configured_builder = "Padrino::Helpers::FormBuilder::#{configured_builder}".constantize if configured_builder.is_a?(String)
|
222
305
|
configured_builder
|
223
306
|
end
|
224
|
-
end
|
225
|
-
end
|
226
|
-
end
|
307
|
+
end # FormHelpers
|
308
|
+
end # Helpers
|
309
|
+
end # Padrino
|
@@ -1,25 +1,33 @@
|
|
1
1
|
module Padrino
|
2
2
|
module Helpers
|
3
3
|
module FormatHelpers
|
4
|
-
|
4
|
+
##
|
5
5
|
# Returns escaped text to protect against malicious content
|
6
|
+
#
|
6
7
|
def escape_html(text)
|
7
8
|
Rack::Utils.escape_html(text)
|
8
9
|
end
|
9
10
|
alias h escape_html
|
10
11
|
alias sanitize_html escape_html
|
11
12
|
|
13
|
+
##
|
12
14
|
# Returns escaped text to protect against malicious content
|
13
15
|
# Returns blank if the text is empty
|
16
|
+
#
|
14
17
|
def h!(text, blank_text = ' ')
|
15
18
|
return blank_text if text.nil? || text.empty?
|
16
19
|
h text
|
17
20
|
end
|
18
21
|
|
22
|
+
##
|
19
23
|
# Returns text transformed into HTML using simple formatting rules. Two or more consecutive newlines(\n\n) are considered
|
20
24
|
# as a paragraph and wrapped in <p> tags. One newline (\n) is considered as a linebreak and a <br /> tag is appended.
|
21
25
|
# This method does not remove the newlines from the text.
|
22
|
-
#
|
26
|
+
#
|
27
|
+
# ==== Examples
|
28
|
+
#
|
29
|
+
# simple_format("hello\nworld") # => "<p>hello<br/>world</p>"
|
30
|
+
#
|
23
31
|
def simple_format(text, html_options={})
|
24
32
|
start_tag = tag('p', html_options.merge(:open => true))
|
25
33
|
text = text.to_s.dup
|
@@ -30,16 +38,26 @@ module Padrino
|
|
30
38
|
text << "</p>"
|
31
39
|
end
|
32
40
|
|
41
|
+
##
|
33
42
|
# Attempts to pluralize the singular word unless count is 1. If plural is supplied, it will use that when count is > 1,
|
34
43
|
# otherwise it will use the Inflector to determine the plural form
|
35
|
-
#
|
44
|
+
#
|
45
|
+
# ==== Examples
|
46
|
+
#
|
47
|
+
# pluralize(2, 'person') => '2 people'
|
48
|
+
#
|
36
49
|
def pluralize(count, singular, plural = nil)
|
37
50
|
"#{count || 0} " + ((count == 1 || count == '1') ? singular : (plural || singular.pluralize))
|
38
51
|
end
|
39
52
|
|
53
|
+
##
|
40
54
|
# Truncates a given text after a given :length if text is longer than :length (defaults to 30).
|
41
55
|
# The last characters will be replaced with the :omission (defaults to "…") for a total length not exceeding :length.
|
42
|
-
#
|
56
|
+
#
|
57
|
+
# ==== Examples
|
58
|
+
#
|
59
|
+
# truncate("Once upon a time in a world far far away", :length => 8) => "Once upon..."
|
60
|
+
#
|
43
61
|
def truncate(text, *args)
|
44
62
|
options = args.extract_options!
|
45
63
|
options.reverse_merge!(:length => 30, :omission => "...")
|
@@ -50,9 +68,14 @@ module Padrino
|
|
50
68
|
end
|
51
69
|
end
|
52
70
|
|
71
|
+
##
|
53
72
|
# Wraps the text into lines no longer than line_width width.
|
54
73
|
# This method breaks on the first whitespace character that does not exceed line_width (which is 80 by default).
|
55
|
-
#
|
74
|
+
#
|
75
|
+
# ==== Examples
|
76
|
+
#
|
77
|
+
# word_wrap('Once upon a time', :line_width => 8) => "Once upon\na time"
|
78
|
+
#
|
56
79
|
def word_wrap(text, *args)
|
57
80
|
options = args.extract_options!
|
58
81
|
unless args.blank?
|
@@ -65,6 +88,7 @@ module Padrino
|
|
65
88
|
end * "\n"
|
66
89
|
end
|
67
90
|
|
91
|
+
##
|
68
92
|
# Reports the approximate distance in time between two Time or Date objects or integers as seconds.
|
69
93
|
# Set <tt>include_seconds</tt> to true if you want more detailed approximations when distance < 1 min, 29 secs
|
70
94
|
# Distances are reported based on the following table:
|
@@ -92,6 +116,7 @@ module Padrino
|
|
92
116
|
# 60-89 secs # => 1 minute
|
93
117
|
#
|
94
118
|
# ==== Examples
|
119
|
+
#
|
95
120
|
# from_time = Time.now
|
96
121
|
# distance_of_time_in_words(from_time, from_time + 50.minutes) # => about 1 hour
|
97
122
|
# distance_of_time_in_words(from_time, 50.minutes.from_now) # => about 1 hour
|
@@ -155,20 +180,24 @@ module Padrino
|
|
155
180
|
end
|
156
181
|
end
|
157
182
|
|
183
|
+
##
|
158
184
|
# Like distance_of_time_in_words, but where <tt>to_time</tt> is fixed to <tt>Time.now</tt>.
|
159
185
|
#
|
160
186
|
# ==== Examples
|
187
|
+
#
|
161
188
|
# time_ago_in_words(3.minutes.from_now) # => 3 minutes
|
162
189
|
# time_ago_in_words(Time.now - 15.hours) # => 15 hours
|
163
190
|
# time_ago_in_words(Time.now) # => less than a minute
|
164
191
|
#
|
165
192
|
# from_time = Time.now - 3.days - 14.minutes - 25.seconds # => 3 days
|
193
|
+
#
|
166
194
|
def time_ago_in_words(from_time, include_seconds = false)
|
167
195
|
distance_of_time_in_words(from_time, Time.now, include_seconds)
|
168
196
|
end
|
169
197
|
|
170
|
-
|
171
|
-
#
|
198
|
+
##
|
199
|
+
# Remove unsafe chars from our javascript
|
200
|
+
#
|
172
201
|
def escape_javascript(html_content)
|
173
202
|
return '' unless html_content
|
174
203
|
javascript_mapping = { '\\' => '\\\\', '</' => '<\/', "\r\n" => '\n', "\n" => '\n' }
|
@@ -177,10 +206,14 @@ module Padrino
|
|
177
206
|
escaped_string
|
178
207
|
end
|
179
208
|
|
209
|
+
##
|
210
|
+
# Used in xxxx.js.erb files to escape html so that it can be passed to javascript from Padrino
|
211
|
+
#
|
212
|
+
# js_escape_html("<h1>Hey</h1>")
|
213
|
+
#
|
180
214
|
def js_escape_html(html_content)
|
181
215
|
"\"#{escape_javascript(html_content)}\""
|
182
216
|
end
|
183
|
-
|
184
|
-
|
185
|
-
|
186
|
-
end
|
217
|
+
end # FormatHelpers
|
218
|
+
end # Helpers
|
219
|
+
end # Padrino
|
@@ -0,0 +1,271 @@
|
|
1
|
+
require 'bigdecimal'
|
2
|
+
|
3
|
+
module Padrino
|
4
|
+
module Helpers
|
5
|
+
##
|
6
|
+
# Provides methods for converting numbers into formatted strings.
|
7
|
+
# Methods are provided for phone numbers, currency, percentage,
|
8
|
+
# precision, positional notation, and file size.
|
9
|
+
#
|
10
|
+
# Verbatim copy of Rails Number helper
|
11
|
+
#
|
12
|
+
module NumberHelpers
|
13
|
+
##
|
14
|
+
# Formats a +number+ into a currency string (e.g., $13.65). You can customize the format
|
15
|
+
# in the +options+ hash.
|
16
|
+
#
|
17
|
+
# ==== Options
|
18
|
+
# * <tt>:precision</tt> - Sets the level of precision (defaults to 2).
|
19
|
+
# * <tt>:unit</tt> - Sets the denomination of the currency (defaults to "$").
|
20
|
+
# * <tt>:separator</tt> - Sets the separator between the units (defaults to ".").
|
21
|
+
# * <tt>:delimiter</tt> - Sets the thousands delimiter (defaults to ",").
|
22
|
+
# * <tt>:format</tt> - Sets the format of the output string (defaults to "%u%n"). The field types are:
|
23
|
+
#
|
24
|
+
# %u The currency unit
|
25
|
+
# %n The number
|
26
|
+
#
|
27
|
+
# ==== Examples
|
28
|
+
#
|
29
|
+
# number_to_currency(1234567890.50) # => $1,234,567,890.50
|
30
|
+
# number_to_currency(1234567890.506) # => $1,234,567,890.51
|
31
|
+
# number_to_currency(1234567890.506, :precision => 3) # => $1,234,567,890.506
|
32
|
+
#
|
33
|
+
# number_to_currency(1234567890.50, :unit => "£", :separator => ",", :delimiter => "")
|
34
|
+
# # => £1234567890,50
|
35
|
+
# number_to_currency(1234567890.50, :unit => "£", :separator => ",", :delimiter => "", :format => "%n %u")
|
36
|
+
# # => 1234567890,50 £
|
37
|
+
#
|
38
|
+
def number_to_currency(number, options = {})
|
39
|
+
options.symbolize_keys!
|
40
|
+
|
41
|
+
defaults = I18n.translate(:'number.format', :locale => options[:locale], :raise => true) rescue {}
|
42
|
+
currency = I18n.translate(:'number.currency.format', :locale => options[:locale], :raise => true) rescue {}
|
43
|
+
defaults = defaults.merge(currency)
|
44
|
+
|
45
|
+
precision = options[:precision] || defaults[:precision]
|
46
|
+
unit = options[:unit] || defaults[:unit]
|
47
|
+
separator = options[:separator] || defaults[:separator]
|
48
|
+
delimiter = options[:delimiter] || defaults[:delimiter]
|
49
|
+
format = options[:format] || defaults[:format]
|
50
|
+
separator = '' if precision == 0
|
51
|
+
|
52
|
+
begin
|
53
|
+
format.gsub(/%n/, number_with_precision(number,
|
54
|
+
:precision => precision,
|
55
|
+
:delimiter => delimiter,
|
56
|
+
:separator => separator)
|
57
|
+
).gsub(/%u/, unit)
|
58
|
+
rescue
|
59
|
+
number
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
##
|
64
|
+
# Formats a +number+ as a percentage string (e.g., 65%). You can customize the
|
65
|
+
# format in the +options+ hash.
|
66
|
+
#
|
67
|
+
# ==== Options
|
68
|
+
# * <tt>:precision</tt> - Sets the level of precision (defaults to 3).
|
69
|
+
# * <tt>:separator</tt> - Sets the separator between the units (defaults to ".").
|
70
|
+
# * <tt>:delimiter</tt> - Sets the thousands delimiter (defaults to "").
|
71
|
+
#
|
72
|
+
# ==== Examples
|
73
|
+
#
|
74
|
+
# number_to_percentage(100) # => 100.000%
|
75
|
+
# number_to_percentage(100, :precision => 0) # => 100%
|
76
|
+
# number_to_percentage(1000, :delimiter => '.', :separator => ',') # => 1.000,000%
|
77
|
+
# number_to_percentage(302.24398923423, :precision => 5) # => 302.24399%
|
78
|
+
#
|
79
|
+
def number_to_percentage(number, options = {})
|
80
|
+
options.symbolize_keys!
|
81
|
+
|
82
|
+
defaults = I18n.translate(:'number.format', :locale => options[:locale], :raise => true) rescue {}
|
83
|
+
percentage = I18n.translate(:'number.percentage.format', :locale => options[:locale], :raise => true) rescue {}
|
84
|
+
defaults = defaults.merge(percentage)
|
85
|
+
|
86
|
+
precision = options[:precision] || defaults[:precision]
|
87
|
+
separator = options[:separator] || defaults[:separator]
|
88
|
+
delimiter = options[:delimiter] || defaults[:delimiter]
|
89
|
+
|
90
|
+
begin
|
91
|
+
number_with_precision(number,
|
92
|
+
:precision => precision,
|
93
|
+
:separator => separator,
|
94
|
+
:delimiter => delimiter) + "%"
|
95
|
+
rescue
|
96
|
+
number
|
97
|
+
end
|
98
|
+
end
|
99
|
+
|
100
|
+
##
|
101
|
+
# Formats a +number+ with grouped thousands using +delimiter+ (e.g., 12,324). You can
|
102
|
+
# customize the format in the +options+ hash.
|
103
|
+
#
|
104
|
+
# ==== Options
|
105
|
+
# * <tt>:delimiter</tt> - Sets the thousands delimiter (defaults to ",").
|
106
|
+
# * <tt>:separator</tt> - Sets the separator between the units (defaults to ".").
|
107
|
+
#
|
108
|
+
# ==== Examples
|
109
|
+
#
|
110
|
+
# number_with_delimiter(12345678) # => 12,345,678
|
111
|
+
# number_with_delimiter(12345678.05) # => 12,345,678.05
|
112
|
+
# number_with_delimiter(12345678, :delimiter => ".") # => 12.345.678
|
113
|
+
# number_with_delimiter(12345678, :separator => ",") # => 12,345,678
|
114
|
+
# number_with_delimiter(98765432.98, :delimiter => " ", :separator => ",")
|
115
|
+
# # => 98 765 432,98
|
116
|
+
#
|
117
|
+
# You can still use <tt>number_with_delimiter</tt> with the old API that accepts the
|
118
|
+
# +delimiter+ as its optional second and the +separator+ as its
|
119
|
+
# optional third parameter:
|
120
|
+
#
|
121
|
+
# number_with_delimiter(12345678, " ") # => 12 345.678
|
122
|
+
# number_with_delimiter(12345678.05, ".", ",") # => 12.345.678,05
|
123
|
+
#
|
124
|
+
def number_with_delimiter(number, *args)
|
125
|
+
options = args.extract_options!
|
126
|
+
options.symbolize_keys!
|
127
|
+
|
128
|
+
defaults = I18n.translate(:'number.format', :locale => options[:locale], :raise => true) rescue {}
|
129
|
+
|
130
|
+
delimiter ||= (options[:delimiter] || defaults[:delimiter])
|
131
|
+
separator ||= (options[:separator] || defaults[:separator])
|
132
|
+
|
133
|
+
begin
|
134
|
+
parts = number.to_s.split('.')
|
135
|
+
parts[0].gsub!(/(\d)(?=(\d\d\d)+(?!\d))/, "\\1#{delimiter}")
|
136
|
+
parts.join(separator)
|
137
|
+
rescue
|
138
|
+
number
|
139
|
+
end
|
140
|
+
end
|
141
|
+
|
142
|
+
##
|
143
|
+
# Formats a +number+ with the specified level of <tt>:precision</tt> (e.g., 112.32 has a precision of 2).
|
144
|
+
# You can customize the format in the +options+ hash.
|
145
|
+
#
|
146
|
+
# ==== Options
|
147
|
+
#
|
148
|
+
# * <tt>:precision</tt> - Sets the level of precision (defaults to 3).
|
149
|
+
# * <tt>:separator</tt> - Sets the separator between the units (defaults to ".").
|
150
|
+
# * <tt>:delimiter</tt> - Sets the thousands delimiter (defaults to "").
|
151
|
+
#
|
152
|
+
# ==== Examples
|
153
|
+
#
|
154
|
+
# number_with_precision(111.2345) # => 111.235
|
155
|
+
# number_with_precision(111.2345, :precision => 2) # => 111.23
|
156
|
+
# number_with_precision(13, :precision => 5) # => 13.00000
|
157
|
+
# number_with_precision(389.32314, :precision => 0) # => 389
|
158
|
+
# number_with_precision(1111.2345, :precision => 2, :separator => ',', :delimiter => '.')
|
159
|
+
# # => 1.111,23
|
160
|
+
#
|
161
|
+
# You can still use <tt>number_with_precision</tt> with the old API that accepts the
|
162
|
+
# +precision+ as its optional second parameter:
|
163
|
+
#
|
164
|
+
# number_with_precision(number_with_precision(111.2345, 2) # => 111.23
|
165
|
+
#
|
166
|
+
def number_with_precision(number, *args)
|
167
|
+
options = args.extract_options!
|
168
|
+
options.symbolize_keys!
|
169
|
+
|
170
|
+
defaults = I18n.translate(:'number.format', :locale => options[:locale], :raise => true) rescue {}
|
171
|
+
precision_defaults = I18n.translate(:'number.precision.format', :locale => options[:locale],
|
172
|
+
:raise => true) rescue {}
|
173
|
+
defaults = defaults.merge(precision_defaults)
|
174
|
+
|
175
|
+
precision ||= (options[:precision] || defaults[:precision])
|
176
|
+
separator ||= (options[:separator] || defaults[:separator])
|
177
|
+
delimiter ||= (options[:delimiter] || defaults[:delimiter])
|
178
|
+
|
179
|
+
begin
|
180
|
+
rounded_number = BigDecimal.new((Float(number) * (10 ** precision)).to_s).round.to_f / 10 ** precision
|
181
|
+
number_with_delimiter("%01.#{precision}f" % rounded_number,
|
182
|
+
:separator => separator,
|
183
|
+
:delimiter => delimiter)
|
184
|
+
rescue
|
185
|
+
number
|
186
|
+
end
|
187
|
+
end
|
188
|
+
|
189
|
+
STORAGE_UNITS = [:byte, :kb, :mb, :gb, :tb].freeze
|
190
|
+
|
191
|
+
##
|
192
|
+
# Formats the bytes in +size+ into a more understandable representation
|
193
|
+
# (e.g., giving it 1500 yields 1.5 KB). This method is useful for
|
194
|
+
# reporting file sizes to users. This method returns nil if
|
195
|
+
# +size+ cannot be converted into a number. You can customize the
|
196
|
+
# format in the +options+ hash.
|
197
|
+
#
|
198
|
+
# ==== Options
|
199
|
+
# * <tt>:precision</tt> - Sets the level of precision (defaults to 1).
|
200
|
+
# * <tt>:separator</tt> - Sets the separator between the units (defaults to ".").
|
201
|
+
# * <tt>:delimiter</tt> - Sets the thousands delimiter (defaults to "").
|
202
|
+
#
|
203
|
+
# ==== Examples
|
204
|
+
#
|
205
|
+
# number_to_human_size(123) # => 123 Bytes
|
206
|
+
# number_to_human_size(1234) # => 1.2 KB
|
207
|
+
# number_to_human_size(12345) # => 12.1 KB
|
208
|
+
# number_to_human_size(1234567) # => 1.2 MB
|
209
|
+
# number_to_human_size(1234567890) # => 1.1 GB
|
210
|
+
# number_to_human_size(1234567890123) # => 1.1 TB
|
211
|
+
# number_to_human_size(1234567, :precision => 2) # => 1.18 MB
|
212
|
+
# number_to_human_size(483989, :precision => 0) # => 473 KB
|
213
|
+
# number_to_human_size(1234567, :precision => 2, :separator => ',') # => 1,18 MB
|
214
|
+
#
|
215
|
+
# Zeros after the decimal point are always stripped out, regardless of the
|
216
|
+
# specified precision:
|
217
|
+
#
|
218
|
+
# helper.number_to_human_size(1234567890123, :precision => 5) # => "1.12283 TB"
|
219
|
+
# helper.number_to_human_size(524288000, :precision=>5) # => "500 MB"
|
220
|
+
#
|
221
|
+
# You can still use <tt>number_to_human_size</tt> with the old API that accepts the
|
222
|
+
# +precision+ as its optional second parameter:
|
223
|
+
#
|
224
|
+
# number_to_human_size(1234567, 2) # => 1.18 MB
|
225
|
+
# number_to_human_size(483989, 0) # => 473 KB
|
226
|
+
#
|
227
|
+
def number_to_human_size(number, *args)
|
228
|
+
return nil if number.nil?
|
229
|
+
|
230
|
+
options = args.extract_options!
|
231
|
+
options.symbolize_keys!
|
232
|
+
|
233
|
+
defaults = I18n.translate(:'number.format', :locale => options[:locale], :raise => true) rescue {}
|
234
|
+
human = I18n.translate(:'number.human.format', :locale => options[:locale], :raise => true) rescue {}
|
235
|
+
defaults = defaults.merge(human)
|
236
|
+
|
237
|
+
precision ||= (options[:precision] || defaults[:precision])
|
238
|
+
separator ||= (options[:separator] || defaults[:separator])
|
239
|
+
delimiter ||= (options[:delimiter] || defaults[:delimiter])
|
240
|
+
|
241
|
+
storage_units_format = I18n.translate(:'number.human.storage_units.format', :locale => options[:locale], :raise => true)
|
242
|
+
|
243
|
+
if number.to_i < 1024
|
244
|
+
unit = I18n.translate(:'number.human.storage_units.units.byte', :locale => options[:locale], :count => number.to_i, :raise => true)
|
245
|
+
storage_units_format.gsub(/%n/, number.to_i.to_s).gsub(/%u/, unit)
|
246
|
+
else
|
247
|
+
max_exp = STORAGE_UNITS.size - 1
|
248
|
+
number = Float(number)
|
249
|
+
exponent = (Math.log(number) / Math.log(1024)).to_i # Convert to base 1024
|
250
|
+
exponent = max_exp if exponent > max_exp # we need this to avoid overflow for the highest unit
|
251
|
+
number /= 1024 ** exponent
|
252
|
+
|
253
|
+
unit_key = STORAGE_UNITS[exponent]
|
254
|
+
unit = I18n.translate(:"number.human.storage_units.units.#{unit_key}", :locale => options[:locale], :count => number, :raise => true)
|
255
|
+
|
256
|
+
begin
|
257
|
+
escaped_separator = Regexp.escape(separator)
|
258
|
+
formatted_number = number_with_precision(number,
|
259
|
+
:precision => precision,
|
260
|
+
:separator => separator,
|
261
|
+
:delimiter => delimiter
|
262
|
+
).sub(/(#{escaped_separator})(\d*[1-9])?0+\z/, '\1\2').sub(/#{escaped_separator}\z/, '')
|
263
|
+
storage_units_format.gsub(/%n/, formatted_number).gsub(/%u/, unit)
|
264
|
+
rescue
|
265
|
+
number
|
266
|
+
end
|
267
|
+
end
|
268
|
+
end
|
269
|
+
end # NumberHelpers
|
270
|
+
end # Helpers
|
271
|
+
end # Padrino
|
@@ -1,8 +1,13 @@
|
|
1
1
|
module Padrino
|
2
2
|
module Helpers
|
3
3
|
module OutputHelpers
|
4
|
+
##
|
4
5
|
# Captures the html from a block of template code for erb or haml
|
5
|
-
#
|
6
|
+
#
|
7
|
+
# ==== Examples
|
8
|
+
#
|
9
|
+
# capture_html(&block) => "...html..."
|
10
|
+
#
|
6
11
|
def capture_html(*args, &block)
|
7
12
|
if self.respond_to?(:is_haml?) && is_haml?
|
8
13
|
block_is_haml?(block) ? capture_haml(*args, &block) : block.call
|
@@ -14,8 +19,13 @@ module Padrino
|
|
14
19
|
end
|
15
20
|
end
|
16
21
|
|
22
|
+
##
|
17
23
|
# Outputs the given text to the templates buffer directly
|
18
|
-
#
|
24
|
+
#
|
25
|
+
# ==== Examples
|
26
|
+
#
|
27
|
+
# concat_content("This will be output to the template buffer in erb or haml")
|
28
|
+
#
|
19
29
|
def concat_content(text="")
|
20
30
|
if self.respond_to?(:is_haml?) && is_haml?
|
21
31
|
haml_concat(text)
|
@@ -26,26 +36,41 @@ module Padrino
|
|
26
36
|
end
|
27
37
|
end
|
28
38
|
|
39
|
+
##
|
29
40
|
# Returns true if the block is from an ERB or HAML template; false otherwise.
|
30
41
|
# Used to determine if html should be returned or concatted to view
|
31
|
-
#
|
42
|
+
#
|
43
|
+
# ==== Examples
|
44
|
+
#
|
45
|
+
# block_is_template?(block)
|
46
|
+
#
|
32
47
|
def block_is_template?(block)
|
33
48
|
block && (block_is_erb?(block) || (self.respond_to?(:block_is_haml?) && block_is_haml?(block)))
|
34
49
|
end
|
35
50
|
|
51
|
+
##
|
36
52
|
# Capture a block of content to be rendered at a later time.
|
37
53
|
# Your blocks can also receive values, which are passed to them by <tt>yield_content</tt>
|
38
|
-
#
|
39
|
-
#
|
54
|
+
#
|
55
|
+
# ==== Examples
|
56
|
+
#
|
57
|
+
# content_for(:name) { ...content... }
|
58
|
+
# content_for(:name) { |name| ...content... }
|
59
|
+
#
|
40
60
|
def content_for(key, &block)
|
41
61
|
content_blocks[key.to_sym] << block
|
42
62
|
end
|
43
63
|
|
64
|
+
##
|
44
65
|
# Render the captured content blocks for a given key.
|
45
66
|
# You can also pass values to the content blocks by passing them
|
46
67
|
# as arguments after the key.
|
47
|
-
#
|
48
|
-
#
|
68
|
+
#
|
69
|
+
# ==== Examples
|
70
|
+
#
|
71
|
+
# yield_content :include
|
72
|
+
# yield_content :head, "param1", "param2"
|
73
|
+
#
|
49
74
|
def yield_content(key, *args)
|
50
75
|
content_blocks[key.to_sym].map { |content|
|
51
76
|
capture_html(*args, &content)
|
@@ -92,6 +117,6 @@ module Padrino
|
|
92
117
|
ensure
|
93
118
|
@_out_buf = old_buffer
|
94
119
|
end
|
95
|
-
end
|
96
|
-
end
|
97
|
-
end
|
120
|
+
end # OutputHelpers
|
121
|
+
end # Helpers
|
122
|
+
end # Padrino
|
@@ -1,9 +1,14 @@
|
|
1
1
|
module Padrino
|
2
2
|
module Helpers
|
3
3
|
module RenderHelpers
|
4
|
+
##
|
4
5
|
# Partials implementation which includes collections support
|
5
|
-
#
|
6
|
-
#
|
6
|
+
#
|
7
|
+
# ==== Examples
|
8
|
+
#
|
9
|
+
# partial 'photo/_item', :object => @photo
|
10
|
+
# partial 'photo/_item', :collection => @photos
|
11
|
+
#
|
7
12
|
def partial(template, options={})
|
8
13
|
options.reverse_merge!(:locals => {}, :layout => false)
|
9
14
|
path = template.to_s.split(File::SEPARATOR)
|
@@ -27,6 +32,6 @@ module Padrino
|
|
27
32
|
end
|
28
33
|
end
|
29
34
|
alias :render_partial :partial
|
30
|
-
end
|
31
|
-
end
|
32
|
-
end
|
35
|
+
end # RenderHelpers
|
36
|
+
end # Helpers
|
37
|
+
end # Padrino
|
@@ -1,17 +1,27 @@
|
|
1
1
|
module Padrino
|
2
2
|
module Helpers
|
3
3
|
module TagHelpers
|
4
|
+
##
|
4
5
|
# Creates an html input field with given type and options
|
5
|
-
#
|
6
|
+
#
|
7
|
+
# ==== Examples
|
8
|
+
#
|
9
|
+
# input_tag :text, :class => "test"
|
10
|
+
#
|
6
11
|
def input_tag(type, options = {})
|
7
12
|
options.reverse_merge!(:type => type)
|
8
13
|
tag(:input, options)
|
9
14
|
end
|
10
15
|
|
16
|
+
##
|
11
17
|
# Creates an html tag with given name, content and options
|
12
|
-
#
|
13
|
-
#
|
14
|
-
#
|
18
|
+
#
|
19
|
+
# ==== Examples
|
20
|
+
#
|
21
|
+
# content_tag(:p, "hello", :class => 'light')
|
22
|
+
# content_tag(:p, :class => 'dark') do ... end
|
23
|
+
# content_tag(name, content=nil, options={}, &block)
|
24
|
+
#
|
15
25
|
def content_tag(*args, &block)
|
16
26
|
name = args.first
|
17
27
|
options = args.extract_options!
|
@@ -20,9 +30,14 @@ module Padrino
|
|
20
30
|
block_is_template?(block) ? concat_content(tag_result) : tag_result
|
21
31
|
end
|
22
32
|
|
33
|
+
##
|
23
34
|
# Creates an html tag with the given name and options
|
24
|
-
#
|
25
|
-
#
|
35
|
+
#
|
36
|
+
# ==== Examples
|
37
|
+
#
|
38
|
+
# tag(:br, :style => 'clear:both')
|
39
|
+
# tag(:p, :content => "hello", :class => 'large')
|
40
|
+
#
|
26
41
|
def tag(name, options={})
|
27
42
|
content, open_tag = options.delete(:content), options.delete(:open)
|
28
43
|
identity_tag_attributes.each { |attr| options[attr] = attr.to_s if options[attr] }
|
@@ -36,6 +51,6 @@ module Padrino
|
|
36
51
|
def identity_tag_attributes
|
37
52
|
[:checked, :disabled, :selected, :multiple]
|
38
53
|
end
|
39
|
-
end
|
40
|
-
end
|
41
|
-
end
|
54
|
+
end # TagHelpers
|
55
|
+
end # Helpers
|
56
|
+
end # Padrino
|
data/padrino-helpers.gemspec
CHANGED
@@ -5,11 +5,11 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{padrino-helpers}
|
8
|
-
s.version = "0.7.
|
8
|
+
s.version = "0.7.6"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Padrino Team", "Nathan Esquenazi", "Davide D'Agostino", "Arthur Chiu"]
|
12
|
-
s.date = %q{2010-02-
|
12
|
+
s.date = %q{2010-02-10}
|
13
13
|
s.description = %q{Tag helpers, asset helpers, form helpers, form builders and many more helpers for padrino}
|
14
14
|
s.email = %q{padrinorb@gmail.com}
|
15
15
|
s.extra_rdoc_files = [
|
@@ -29,6 +29,7 @@ Gem::Specification.new do |s|
|
|
29
29
|
"lib/padrino-helpers/form_helpers.rb",
|
30
30
|
"lib/padrino-helpers/format_helpers.rb",
|
31
31
|
"lib/padrino-helpers/locale/en.yml",
|
32
|
+
"lib/padrino-helpers/number_helpers.rb",
|
32
33
|
"lib/padrino-helpers/output_helpers.rb",
|
33
34
|
"lib/padrino-helpers/render_helpers.rb",
|
34
35
|
"lib/padrino-helpers/tag_helpers.rb",
|
@@ -64,6 +65,7 @@ Gem::Specification.new do |s|
|
|
64
65
|
"test/test_form_builder.rb",
|
65
66
|
"test/test_form_helpers.rb",
|
66
67
|
"test/test_format_helpers.rb",
|
68
|
+
"test/test_number_helpers.rb",
|
67
69
|
"test/test_output_helpers.rb",
|
68
70
|
"test/test_render_helpers.rb",
|
69
71
|
"test/test_tag_helpers.rb"
|
@@ -81,7 +83,7 @@ Gem::Specification.new do |s|
|
|
81
83
|
|
82
84
|
if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
|
83
85
|
s.add_runtime_dependency(%q<sinatra>, [">= 0.9.2"])
|
84
|
-
s.add_runtime_dependency(%q<padrino-core>, ["= 0.7.
|
86
|
+
s.add_runtime_dependency(%q<padrino-core>, ["= 0.7.6"])
|
85
87
|
s.add_development_dependency(%q<haml>, [">= 2.2.1"])
|
86
88
|
s.add_development_dependency(%q<shoulda>, [">= 0"])
|
87
89
|
s.add_development_dependency(%q<mocha>, [">= 0.9.7"])
|
@@ -89,7 +91,7 @@ Gem::Specification.new do |s|
|
|
89
91
|
s.add_development_dependency(%q<webrat>, [">= 0.5.1"])
|
90
92
|
else
|
91
93
|
s.add_dependency(%q<sinatra>, [">= 0.9.2"])
|
92
|
-
s.add_dependency(%q<padrino-core>, ["= 0.7.
|
94
|
+
s.add_dependency(%q<padrino-core>, ["= 0.7.6"])
|
93
95
|
s.add_dependency(%q<haml>, [">= 2.2.1"])
|
94
96
|
s.add_dependency(%q<shoulda>, [">= 0"])
|
95
97
|
s.add_dependency(%q<mocha>, [">= 0.9.7"])
|
@@ -98,7 +100,7 @@ Gem::Specification.new do |s|
|
|
98
100
|
end
|
99
101
|
else
|
100
102
|
s.add_dependency(%q<sinatra>, [">= 0.9.2"])
|
101
|
-
s.add_dependency(%q<padrino-core>, ["= 0.7.
|
103
|
+
s.add_dependency(%q<padrino-core>, ["= 0.7.6"])
|
102
104
|
s.add_dependency(%q<haml>, [">= 2.2.1"])
|
103
105
|
s.add_dependency(%q<shoulda>, [">= 0"])
|
104
106
|
s.add_dependency(%q<mocha>, [">= 0.9.7"])
|
data/test/helper.rb
CHANGED
data/test/support_helpers.rb
CHANGED
@@ -1,6 +1,5 @@
|
|
1
1
|
require 'padrino-core/support_lite'
|
2
2
|
unless Fixnum.method_defined?(:days)
|
3
|
-
# We don't add active_support/core_ext/object/misc because override some extlib defaults
|
4
3
|
if Padrino.support == :extlib
|
5
4
|
class Object
|
6
5
|
# A duck-type assistant method. For example, Active Support extends Date
|
@@ -0,0 +1,136 @@
|
|
1
|
+
class TestNumberHelpers < Test::Unit::TestCase
|
2
|
+
include Padrino::Helpers::NumberHelpers
|
3
|
+
|
4
|
+
def kilobytes(number)
|
5
|
+
number * 1024
|
6
|
+
end
|
7
|
+
|
8
|
+
def megabytes(number)
|
9
|
+
kilobytes(number) * 1024
|
10
|
+
end
|
11
|
+
|
12
|
+
def gigabytes(number)
|
13
|
+
megabytes(number) * 1024
|
14
|
+
end
|
15
|
+
|
16
|
+
def terabytes(number)
|
17
|
+
gigabytes(number) * 1024
|
18
|
+
end
|
19
|
+
|
20
|
+
context 'for number helpers functionality' do
|
21
|
+
|
22
|
+
should 'display number_to_currency' do
|
23
|
+
assert_equal "$1,234,567,890.50", number_to_currency(1234567890.50)
|
24
|
+
assert_equal "$1,234,567,890.51", number_to_currency(1234567890.506)
|
25
|
+
assert_equal "$1,234,567,892", number_to_currency(1234567891.50, {:precision => 0})
|
26
|
+
assert_equal "$1,234,567,890.5", number_to_currency(1234567890.50, {:precision => 1})
|
27
|
+
assert_equal "£1234567890,50", number_to_currency(1234567890.50, {:unit => "£", :separator => ",", :delimiter => ""})
|
28
|
+
assert_equal "$1,234,567,890.50", number_to_currency("1234567890.50")
|
29
|
+
assert_equal "1,234,567,890.50 Kč", number_to_currency("1234567890.50", {:unit => "Kč", :format => "%n %u"})
|
30
|
+
assert_equal "$x", number_to_currency("x")
|
31
|
+
|
32
|
+
assert_nil number_to_currency(nil)
|
33
|
+
end
|
34
|
+
|
35
|
+
should 'display number_to_percentage' do
|
36
|
+
assert_equal "100.000%", number_to_percentage(100)
|
37
|
+
assert_equal "100%", number_to_percentage(100, {:precision => 0})
|
38
|
+
assert_equal "302.06%", number_to_percentage(302.0574, {:precision => 2})
|
39
|
+
assert_equal "100.000%", number_to_percentage("100")
|
40
|
+
assert_equal "1000.000%", number_to_percentage("1000")
|
41
|
+
assert_equal "x%", number_to_percentage("x")
|
42
|
+
assert_equal "1.000,000%", number_to_percentage(1000, :delimiter => '.', :separator => ',')
|
43
|
+
|
44
|
+
assert_nil number_to_percentage(nil)
|
45
|
+
end
|
46
|
+
|
47
|
+
should 'display number_with_delimiter' do
|
48
|
+
assert_equal "12,345,678", number_with_delimiter(12345678)
|
49
|
+
assert_equal "0", number_with_delimiter(0)
|
50
|
+
assert_equal "123", number_with_delimiter(123)
|
51
|
+
assert_equal "123,456", number_with_delimiter(123456)
|
52
|
+
assert_equal "123,456.78", number_with_delimiter(123456.78)
|
53
|
+
assert_equal "123,456.789", number_with_delimiter(123456.789)
|
54
|
+
assert_equal "123,456.78901", number_with_delimiter(123456.78901)
|
55
|
+
assert_equal "123,456,789.78901", number_with_delimiter(123456789.78901)
|
56
|
+
assert_equal "0.78901", number_with_delimiter(0.78901)
|
57
|
+
assert_equal "123,456.78", number_with_delimiter("123456.78")
|
58
|
+
assert_equal "x", number_with_delimiter("x")
|
59
|
+
|
60
|
+
assert_nil number_with_delimiter(nil)
|
61
|
+
end
|
62
|
+
|
63
|
+
should 'display number_with_delimiter with options' do
|
64
|
+
assert_equal '12 345 678', number_with_delimiter(12345678, :delimiter => ' ')
|
65
|
+
assert_equal '12,345,678-05', number_with_delimiter(12345678.05, :separator => '-')
|
66
|
+
assert_equal '12.345.678,05', number_with_delimiter(12345678.05, :separator => ',', :delimiter => '.')
|
67
|
+
assert_equal '12.345.678,05', number_with_delimiter(12345678.05, :delimiter => '.', :separator => ',')
|
68
|
+
end
|
69
|
+
|
70
|
+
should 'display number_with_precision' do
|
71
|
+
assert_equal "111.235", number_with_precision(111.2346)
|
72
|
+
assert_equal "31.83", number_with_precision(31.825, :precision => 2)
|
73
|
+
assert_equal "111.23", number_with_precision(111.2346, :precision => 2)
|
74
|
+
assert_equal "111.00", number_with_precision(111, :precision => 2)
|
75
|
+
assert_equal "111.235", number_with_precision("111.2346")
|
76
|
+
assert_equal "31.83", number_with_precision("31.825", :precision => 2)
|
77
|
+
assert_equal "3268", number_with_precision((32.6751 * 100.00), :precision => 0)
|
78
|
+
assert_equal "112", number_with_precision(111.50, :precision => 0)
|
79
|
+
assert_equal "1234567892", number_with_precision(1234567891.50, :precision => 0)
|
80
|
+
|
81
|
+
# Return non-numeric params unchanged.
|
82
|
+
assert_equal "x", number_with_precision("x")
|
83
|
+
assert_nil number_with_precision(nil)
|
84
|
+
end
|
85
|
+
|
86
|
+
should 'display number_with_precision with custom delimiter and separator' do
|
87
|
+
assert_equal '31,83', number_with_precision(31.825, :precision => 2, :separator => ',')
|
88
|
+
assert_equal '1.231,83', number_with_precision(1231.825, :precision => 2, :separator => ',', :delimiter => '.')
|
89
|
+
end
|
90
|
+
|
91
|
+
should 'display number_to_human_size' do
|
92
|
+
assert_equal '0 Bytes', number_to_human_size(0)
|
93
|
+
assert_equal '1 Byte', number_to_human_size(1)
|
94
|
+
assert_equal '3 Bytes', number_to_human_size(3.14159265)
|
95
|
+
assert_equal '123 Bytes', number_to_human_size(123.0)
|
96
|
+
assert_equal '123 Bytes', number_to_human_size(123)
|
97
|
+
assert_equal '1.2 KB', number_to_human_size(1234)
|
98
|
+
assert_equal '12.1 KB', number_to_human_size(12345)
|
99
|
+
assert_equal '1.2 MB', number_to_human_size(1234567)
|
100
|
+
assert_equal '1.1 GB', number_to_human_size(1234567890)
|
101
|
+
assert_equal '1.1 TB', number_to_human_size(1234567890123)
|
102
|
+
assert_equal '1025 TB', number_to_human_size(terabytes(1025))
|
103
|
+
assert_equal '444 KB', number_to_human_size(kilobytes(444))
|
104
|
+
assert_equal '1023 MB', number_to_human_size(megabytes(1023))
|
105
|
+
assert_equal '3 TB', number_to_human_size(terabytes(3))
|
106
|
+
assert_equal '1.18 MB', number_to_human_size(1234567, :precision => 2)
|
107
|
+
assert_equal '3 Bytes', number_to_human_size(3.14159265, :precision => 4)
|
108
|
+
assert_equal '123 Bytes', number_to_human_size("123")
|
109
|
+
assert_equal '1.01 KB', number_to_human_size(kilobytes(1.0123), :precision => 2)
|
110
|
+
assert_equal '1.01 KB', number_to_human_size(kilobytes(1.0100), :precision => 4)
|
111
|
+
assert_equal '10 KB', number_to_human_size(kilobytes(10.000), :precision => 4)
|
112
|
+
assert_equal '1 Byte', number_to_human_size(1.1)
|
113
|
+
assert_equal '10 Bytes', number_to_human_size(10)
|
114
|
+
|
115
|
+
assert_nil number_to_human_size(nil)
|
116
|
+
end
|
117
|
+
|
118
|
+
should 'display number_to_human_size with options' do
|
119
|
+
assert_equal '1.18 MB', number_to_human_size(1234567, :precision => 2)
|
120
|
+
assert_equal '3 Bytes', number_to_human_size(3.14159265, :precision => 4)
|
121
|
+
assert_equal '1.01 KB', number_to_human_size(kilobytes(1.0123), :precision => 2)
|
122
|
+
assert_equal '1.01 KB', number_to_human_size(kilobytes(1.0100), :precision => 4)
|
123
|
+
assert_equal '10 KB', number_to_human_size(kilobytes(10.000), :precision => 4)
|
124
|
+
assert_equal '1 TB', number_to_human_size(1234567890123, :precision => 0)
|
125
|
+
assert_equal '500 MB', number_to_human_size(524288000, :precision => 0)
|
126
|
+
assert_equal '40 KB', number_to_human_size(41010, :precision => 0)
|
127
|
+
assert_equal '40 KB', number_to_human_size(41100, :precision => 0)
|
128
|
+
end
|
129
|
+
|
130
|
+
should 'display number_to_human_size with custom delimiter and separator' do
|
131
|
+
assert_equal '1,01 KB', number_to_human_size(kilobytes(1.0123), :precision => 2, :separator => ',')
|
132
|
+
assert_equal '1,01 KB', number_to_human_size(kilobytes(1.0100), :precision => 4, :separator => ',')
|
133
|
+
assert_equal '1.000,1 TB', number_to_human_size(terabytes(1000.1), :delimiter => '.', :separator => ',')
|
134
|
+
end
|
135
|
+
end
|
136
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: padrino-helpers
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.7.
|
4
|
+
version: 0.7.6
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Padrino Team
|
@@ -12,7 +12,7 @@ autorequire:
|
|
12
12
|
bindir: bin
|
13
13
|
cert_chain: []
|
14
14
|
|
15
|
-
date: 2010-02-
|
15
|
+
date: 2010-02-10 00:00:00 +01:00
|
16
16
|
default_executable:
|
17
17
|
dependencies:
|
18
18
|
- !ruby/object:Gem::Dependency
|
@@ -33,7 +33,7 @@ dependencies:
|
|
33
33
|
requirements:
|
34
34
|
- - "="
|
35
35
|
- !ruby/object:Gem::Version
|
36
|
-
version: 0.7.
|
36
|
+
version: 0.7.6
|
37
37
|
version:
|
38
38
|
- !ruby/object:Gem::Dependency
|
39
39
|
name: haml
|
@@ -107,6 +107,7 @@ files:
|
|
107
107
|
- lib/padrino-helpers/form_helpers.rb
|
108
108
|
- lib/padrino-helpers/format_helpers.rb
|
109
109
|
- lib/padrino-helpers/locale/en.yml
|
110
|
+
- lib/padrino-helpers/number_helpers.rb
|
110
111
|
- lib/padrino-helpers/output_helpers.rb
|
111
112
|
- lib/padrino-helpers/render_helpers.rb
|
112
113
|
- lib/padrino-helpers/tag_helpers.rb
|
@@ -142,6 +143,7 @@ files:
|
|
142
143
|
- test/test_form_builder.rb
|
143
144
|
- test/test_form_helpers.rb
|
144
145
|
- test/test_format_helpers.rb
|
146
|
+
- test/test_number_helpers.rb
|
145
147
|
- test/test_output_helpers.rb
|
146
148
|
- test/test_render_helpers.rb
|
147
149
|
- test/test_tag_helpers.rb
|