padrino-helpers 0.9.6 → 0.9.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README.rdoc +19 -19
- data/Rakefile +1 -1
- data/VERSION +1 -1
- data/lib/padrino-helpers.rb +6 -6
- data/lib/padrino-helpers/asset_tag_helpers.rb +132 -110
- data/lib/padrino-helpers/form_builder/abstract_form_builder.rb +1 -1
- data/lib/padrino-helpers/form_builder/standard_form_builder.rb +2 -2
- data/lib/padrino-helpers/form_helpers.rb +66 -66
- data/lib/padrino-helpers/format_helpers.rb +29 -29
- data/lib/padrino-helpers/locale/de.yml +3 -3
- data/lib/padrino-helpers/locale/en.yml +7 -7
- data/lib/padrino-helpers/locale/it.yml +3 -3
- data/lib/padrino-helpers/number_helpers.rb +23 -23
- data/lib/padrino-helpers/output_helpers.rb +31 -31
- data/lib/padrino-helpers/render_helpers.rb +4 -4
- data/lib/padrino-helpers/tag_helpers.rb +11 -11
- data/lib/padrino-helpers/translation_helpers.rb +2 -2
- data/padrino-helpers.gemspec +5 -5
- data/test/fixtures/markup_app/views/capture_concat.erb +1 -1
- data/test/fixtures/markup_app/views/capture_concat.haml +1 -2
- data/test/fixtures/markup_app/views/content_for.erb +3 -3
- data/test/fixtures/markup_app/views/content_for.haml +2 -2
- data/test/fixtures/markup_app/views/content_tag.erb +1 -1
- data/test/fixtures/markup_app/views/content_tag.haml +1 -1
- data/test/fixtures/markup_app/views/form_for.erb +1 -1
- data/test/fixtures/markup_app/views/form_for.haml +2 -2
- data/test/fixtures/markup_app/views/form_tag.erb +1 -1
- data/test/fixtures/render_app/app.rb +2 -2
- data/test/fixtures/render_app/views/template/_user.haml +1 -1
- data/test/helper.rb +0 -4
- data/test/test_asset_tag_helpers.rb +27 -10
- data/test/test_form_builder.rb +18 -10
- data/test/test_form_helpers.rb +8 -4
- data/test/test_tag_helpers.rb +0 -1
- metadata +5 -5
data/README.rdoc
CHANGED
@@ -10,7 +10,7 @@ methods should be very familiar to anyone who has used rails view helpers.
|
|
10
10
|
|
11
11
|
Output helpers are a collection of important methods for managing, capturing and displaying output
|
12
12
|
in various ways and is used frequently to support higher-level helper functions. There are
|
13
|
-
three output helpers worth mentioning: <tt>content_for</tt>, <tt>capture_html</tt>, and <tt>concat_content</tt>
|
13
|
+
three output helpers worth mentioning: <tt>content_for</tt>, <tt>capture_html</tt>, and <tt>concat_content</tt>
|
14
14
|
|
15
15
|
The content_for functionality supports capturing content and then rendering this into a different place
|
16
16
|
such as within a layout. One such popular example is including assets onto the layout from a template:
|
@@ -21,7 +21,7 @@ such as within a layout. One such popular example is including assets onto the l
|
|
21
21
|
<%= stylesheet_link_tag 'index', 'custom' %>
|
22
22
|
<% end %>
|
23
23
|
...
|
24
|
-
|
24
|
+
|
25
25
|
Added to a template, this will capture the includes from the block and allow them to be yielded into the layout:
|
26
26
|
|
27
27
|
# app/views/layout.erb
|
@@ -32,8 +32,8 @@ Added to a template, this will capture the includes from the block and allow the
|
|
32
32
|
<%= yield_content :assets %>
|
33
33
|
</head>
|
34
34
|
...
|
35
|
-
|
36
|
-
This will automatically insert the contents of the block (in this case a stylesheet include) into the
|
35
|
+
|
36
|
+
This will automatically insert the contents of the block (in this case a stylesheet include) into the
|
37
37
|
location the content is yielded within the layout.
|
38
38
|
|
39
39
|
The capture_html and the concat_content methods allow content to be manipulated and stored for use in building
|
@@ -46,9 +46,9 @@ these in constructing a simplified 'form_tag' helper which accepts a block.
|
|
46
46
|
inner_form_html = capture_html(&block)
|
47
47
|
concat_content '<form>' + inner_form_html + '</form>'
|
48
48
|
end
|
49
|
-
|
49
|
+
|
50
50
|
This will capture the template body passed into the form_tag block and then append the content
|
51
|
-
to the template through the use of <tt>concat_content</tt>. Note have been built to work for both haml and erb
|
51
|
+
to the template through the use of <tt>concat_content</tt>. Note have been built to work for both haml and erb
|
52
52
|
templates using the same syntax.
|
53
53
|
|
54
54
|
For more information on using output helpers, check out the guide for
|
@@ -64,12 +64,12 @@ the tag contains 'content' within then <tt>content_tag</tt> is used. For example
|
|
64
64
|
|
65
65
|
tag(:br, :style => ‘clear:both’) => <br style="clear:both" />
|
66
66
|
content_tag(:p, "demo", :class => ‘light’) => <p class="light">demo</p>
|
67
|
-
|
67
|
+
|
68
68
|
The input_tag is used to build tags that are related to accepting input from the user:
|
69
69
|
|
70
70
|
input_tag :text, :class => "demo" => <input type='text' class='demo' />
|
71
71
|
input_tag :password, :value => "secret", :class => "demo"
|
72
|
-
|
72
|
+
|
73
73
|
Note that all of these accept html options and result in returning a string containing html tags.
|
74
74
|
|
75
75
|
For more information on using tag helpers, check out the guide for
|
@@ -78,7 +78,7 @@ For more information on using tag helpers, check out the guide for
|
|
78
78
|
=== Asset Helpers
|
79
79
|
|
80
80
|
Asset helpers are intended to help insert useful html onto a view template such as 'flash' notices,
|
81
|
-
hyperlinks, mail_to links, images, stylesheets and javascript. An example of their uses would be on a
|
81
|
+
hyperlinks, mail_to links, images, stylesheets and javascript. An example of their uses would be on a
|
82
82
|
simple view template:
|
83
83
|
|
84
84
|
# app/views/example.haml
|
@@ -95,7 +95,7 @@ simple view template:
|
|
95
95
|
|
96
96
|
For more information on using asset helpers, check out the guide for
|
97
97
|
{Padrino Helpers}[http://wiki.github.com/padrino/padrino-framework/application-helpers].
|
98
|
-
|
98
|
+
|
99
99
|
=== Form Helpers
|
100
100
|
|
101
101
|
Form helpers are the 'standard' form tag helpers you would come to expect when building forms. A simple
|
@@ -120,7 +120,7 @@ example of constructing a non-object form would be:
|
|
120
120
|
|
121
121
|
For more information on using form helpers, check out the guide for
|
122
122
|
{Padrino Helpers}[http://wiki.github.com/padrino/padrino-framework/application-helpers].
|
123
|
-
|
123
|
+
|
124
124
|
=== FormBuilders
|
125
125
|
|
126
126
|
Form builders are full-featured objects allowing the construction of complex object-based forms
|
@@ -151,7 +151,7 @@ A form_for using these basic fields might look like:
|
|
151
151
|
= location.text_field :city
|
152
152
|
%p
|
153
153
|
= f.submit "Create", :class => 'button'
|
154
|
-
|
154
|
+
|
155
155
|
There is also an additional StandardFormBuilder which builds on the abstract fields that can be used within a form_for.
|
156
156
|
|
157
157
|
A form_for using these standard fields might be:
|
@@ -186,7 +186,7 @@ and <tt>js_escape_html</tt>.
|
|
186
186
|
|
187
187
|
The escape_html and js_escape_html function are for taking an html string and escaping certain characters.
|
188
188
|
<tt>escape_html</tt> will escape ampersands, brackets and quotes to their HTML/XML entities. This is useful
|
189
|
-
to sanitize user content before displaying this on a template. <tt>js_escape_html</tt> is used for
|
189
|
+
to sanitize user content before displaying this on a template. <tt>js_escape_html</tt> is used for
|
190
190
|
passing javascript information from a js template to a javascript function.
|
191
191
|
|
192
192
|
escape_html('<hello>&<goodbye>') # => <hello>&<goodbye>
|
@@ -194,7 +194,7 @@ passing javascript information from a js template to a javascript function.
|
|
194
194
|
There is also an alias for escape_html called <tt>h</tt> for even easier usage within templates.
|
195
195
|
|
196
196
|
Format helpers also includes a number of useful text manipulation functions such as <tt>simple_format</tt>,
|
197
|
-
<tt>pluralize</tt>, <tt>word_wrap</tt>, and <tt>truncate</tt>.
|
197
|
+
<tt>pluralize</tt>, <tt>word_wrap</tt>, and <tt>truncate</tt>.
|
198
198
|
|
199
199
|
simple_format("hello\nworld") # => "<p>hello<br/>world</p>"
|
200
200
|
pluralize(2, 'person') => '2 people'
|
@@ -208,26 +208,26 @@ For more information on using the format helpers, check out the guide for
|
|
208
208
|
|
209
209
|
=== Render Helpers
|
210
210
|
|
211
|
-
This component provides a number of rendering helpers making the process of displaying templates a bit easier.
|
211
|
+
This component provides a number of rendering helpers making the process of displaying templates a bit easier.
|
212
212
|
This plugin also has support for useful additions such as partials (with support for :collection) for the templating system.
|
213
213
|
|
214
214
|
Using render plugin helpers is extremely simple. If you want to render an erb template in your view path:
|
215
215
|
|
216
216
|
render :erb, 'path/to/erb/template'
|
217
|
-
|
217
|
+
|
218
218
|
or using haml templates works just as well:
|
219
219
|
|
220
220
|
render :haml, 'path/to/haml/template'
|
221
|
-
|
221
|
+
|
222
222
|
There is also a method which renders the first view matching the path and removes the need to define an engine:
|
223
223
|
|
224
224
|
render 'path/to/any/template'
|
225
|
-
|
225
|
+
|
226
226
|
Finally, we have the all-important partials support for rendering mini-templates onto a page:
|
227
227
|
|
228
228
|
partial 'photo/_item', :object => @photo, :locals => { :foo => 'bar' }
|
229
229
|
partial 'photo/_item', :collection => @photos
|
230
|
-
|
230
|
+
|
231
231
|
For more information on using the render and partial helpers, check out the guide for
|
232
232
|
{Padrino Helpers}[http://wiki.github.com/padrino/padrino-framework/application-helpers].
|
233
233
|
|
data/Rakefile
CHANGED
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.9.
|
1
|
+
0.9.7
|
data/lib/padrino-helpers.rb
CHANGED
@@ -10,14 +10,14 @@ I18n.load_path += Dir["#{File.dirname(__FILE__)}/padrino-helpers/locale/*.yml"]
|
|
10
10
|
|
11
11
|
module Padrino
|
12
12
|
##
|
13
|
-
# This component provides a great deal of view helpers related to html markup generation.
|
14
|
-
# There are helpers for generating tags, forms, links, images, and more.
|
13
|
+
# This component provides a great deal of view helpers related to html markup generation.
|
14
|
+
# There are helpers for generating tags, forms, links, images, and more.
|
15
15
|
# Most of the basic methods should be very familiar to anyone who has used rails view helpers.
|
16
|
-
#
|
16
|
+
#
|
17
17
|
module Helpers
|
18
18
|
##
|
19
19
|
# Register these helpers:
|
20
|
-
#
|
20
|
+
#
|
21
21
|
# Padrino::Helpers::OutputHelpers
|
22
22
|
# Padrino::Helpers::TagHelpers
|
23
23
|
# Padrino::Helpers::AssetTagHelpers
|
@@ -25,9 +25,9 @@ module Padrino
|
|
25
25
|
# Padrino::Helpers::FormatHelpers
|
26
26
|
# Padrino::Helpers::RenderHelpers
|
27
27
|
# Padrino::Helpers::NumberHelpers
|
28
|
-
#
|
28
|
+
#
|
29
29
|
# for Padrino::Application
|
30
|
-
#
|
30
|
+
#
|
31
31
|
def self.registered(app)
|
32
32
|
app.set :default_builder, 'StandardFormBuilder'
|
33
33
|
app.helpers Padrino::Helpers::OutputHelpers
|
@@ -3,11 +3,11 @@ module Padrino
|
|
3
3
|
module AssetTagHelpers
|
4
4
|
##
|
5
5
|
# Creates a div to display the flash of given type if it exists
|
6
|
-
#
|
6
|
+
#
|
7
7
|
# ==== Examples
|
8
|
-
# #
|
8
|
+
# # Generates: <div class="notice">flash-notice</div>
|
9
9
|
# flash_tag(:notice, :id => 'flash-notice')
|
10
|
-
#
|
10
|
+
#
|
11
11
|
def flash_tag(kind, options={})
|
12
12
|
flash_text = flash[kind]
|
13
13
|
return '' if flash_text.blank?
|
@@ -17,18 +17,18 @@ module Padrino
|
|
17
17
|
|
18
18
|
##
|
19
19
|
# Creates a link element with given name, url and options
|
20
|
-
#
|
20
|
+
#
|
21
21
|
# ==== Examples
|
22
|
-
#
|
22
|
+
#
|
23
23
|
# link_to 'click me', '/dashboard', :class => 'linky'
|
24
24
|
# link_to 'click me', '/dashboard', :if => @foo.present?
|
25
25
|
# link_to 'click me', '/dashboard', :unless => @foo.empty?
|
26
26
|
# link_to 'click me', '/dashboard', :unless => :current
|
27
27
|
# link_to('/dashboard', :class => 'blocky') do ... end
|
28
|
-
#
|
28
|
+
#
|
29
29
|
# Note that you can pass :+if+ or :+unless+ conditions, but if you provide :current as
|
30
30
|
# condition padrino return true/false if the request.path_info match the given url
|
31
|
-
#
|
31
|
+
#
|
32
32
|
def link_to(*args, &block)
|
33
33
|
options = args.extract_options!
|
34
34
|
anchor = "##{CGI.escape options.delete(:anchor).to_s}" if options[:anchor]
|
@@ -50,16 +50,16 @@ module Padrino
|
|
50
50
|
|
51
51
|
##
|
52
52
|
# Creates a form containing a single button that submits to the url.
|
53
|
-
#
|
53
|
+
#
|
54
54
|
# ==== Examples
|
55
|
-
#
|
55
|
+
#
|
56
56
|
# # Generates:
|
57
57
|
# # <form class="form" action="/admin/accounts/destroy/2" method="post">
|
58
58
|
# # <input type="hidden" value="delete" name="_method" />
|
59
59
|
# # <input type="submit" value="Delete" />
|
60
60
|
# # </form>
|
61
61
|
# button_to 'Delete', url(:accounts_destroy, :id => account), :method => :delete, :class => :form
|
62
|
-
#
|
62
|
+
#
|
63
63
|
def button_to(*args, &block)
|
64
64
|
name, url = args[0], args[1]
|
65
65
|
options = args.extract_options!
|
@@ -73,167 +73,189 @@ module Padrino
|
|
73
73
|
content_tag('form', inner_form_html, options)
|
74
74
|
end
|
75
75
|
|
76
|
+
##
|
77
|
+
# Creates a link tag that browsers and news readers can use to auto-detect an RSS or ATOM feed.
|
78
|
+
#
|
79
|
+
# === Options
|
80
|
+
#
|
81
|
+
# :rel:: Specify the relation of this link, defaults to "alternate"
|
82
|
+
# :type:: Override the auto-generated mime type
|
83
|
+
# :title:: Specify the title of the link, defaults to the type
|
84
|
+
#
|
85
|
+
# === Examples
|
86
|
+
#
|
87
|
+
# # Generates: <link type="application/atom+xml" rel="alternate" href="/blog/posts.atom" title="ATOM" />
|
88
|
+
# feed_tag :atom, url(:blog, :posts, :format => :atom), :title => "ATOM"
|
89
|
+
# # Generates: <link type="application/rss+xml" rel="alternate" href="/blog/posts.rss" title="rss" />
|
90
|
+
# feed_tag :rss, url(:blog, :posts, :format => :rss)
|
91
|
+
#
|
92
|
+
def feed_tag(mime, url, options={})
|
93
|
+
full_mime = (mime == :atom) ? 'application/atom+xml' : 'application/rss+xml'
|
94
|
+
content_tag(:link, options.reverse_merge(:rel => 'alternate', :type => full_mime, :title => mime, :href => url))
|
95
|
+
end
|
96
|
+
|
76
97
|
##
|
77
98
|
# Creates a mail link element with given name and caption
|
78
|
-
#
|
99
|
+
#
|
79
100
|
# ==== Examples
|
80
|
-
#
|
81
|
-
#
|
82
|
-
#
|
101
|
+
#
|
102
|
+
# # Generates: <a href="mailto:me@demo.com">me@demo.com</a>
|
103
|
+
# mail_to "me@demo.com"
|
104
|
+
# # Generates: <a href="mailto:me@demo.com">My Email</a>
|
105
|
+
# mail_to "me@demo.com", "My Email"
|
106
|
+
#
|
83
107
|
def mail_to(email, caption=nil, mail_options={})
|
84
108
|
html_options = mail_options.slice!(:cc, :bcc, :subject, :body)
|
85
109
|
mail_query = Rack::Utils.build_query(mail_options).gsub(/\+/, '%20').gsub('%40', '@')
|
86
110
|
mail_href = "mailto:#{email}"; mail_href << "?#{mail_query}" if mail_query.present?
|
87
|
-
link_to
|
111
|
+
link_to((caption || email), mail_href, html_options)
|
88
112
|
end
|
89
113
|
|
90
114
|
##
|
91
115
|
# Creates a meta element with the content and given options
|
92
|
-
#
|
116
|
+
#
|
93
117
|
# ==== Examples
|
94
|
-
#
|
95
|
-
#
|
96
|
-
# meta_tag "
|
97
|
-
#
|
118
|
+
#
|
119
|
+
# # Generates: <meta name="keywords" content="weblog,news">
|
120
|
+
# meta_tag "weblog,news", :name => "keywords"
|
121
|
+
#
|
122
|
+
# # Generates: <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
|
123
|
+
# meta_tag "text/html; charset=UTF-8", :http-equiv => "Content-Type"
|
124
|
+
#
|
98
125
|
def meta_tag(content, options={})
|
99
126
|
options.reverse_merge!("content" => content)
|
100
127
|
tag(:meta, options)
|
101
128
|
end
|
102
129
|
|
130
|
+
##
|
131
|
+
# Generates a favicon link. looks inside images folder
|
132
|
+
#
|
133
|
+
# ==== Examples
|
134
|
+
#
|
135
|
+
# favicon_tag 'favicon.png'
|
136
|
+
# favicon_tag 'icons/favicon.png'
|
137
|
+
# # or override some options
|
138
|
+
# favicon_tag 'favicon.png', :type => 'image/ico'
|
139
|
+
#
|
140
|
+
def favicon_tag(source,options={})
|
141
|
+
type = File.extname(source).gsub('.','')
|
142
|
+
options = options.dup.reverse_merge!(:href => image_path(source), :rel => 'icon', :type => "image/#{type}")
|
143
|
+
tag(:link, options)
|
144
|
+
end
|
145
|
+
|
103
146
|
##
|
104
147
|
# Creates an image element with given url and options
|
105
|
-
#
|
148
|
+
#
|
106
149
|
# ==== Examples
|
107
|
-
#
|
150
|
+
#
|
108
151
|
# image_tag('icons/avatar.png')
|
109
|
-
#
|
152
|
+
#
|
110
153
|
def image_tag(url, options={})
|
111
154
|
options.reverse_merge!(:src => image_path(url))
|
112
155
|
tag(:img, options)
|
113
156
|
end
|
114
157
|
|
115
158
|
##
|
116
|
-
# Returns an html script tag for each of the sources provided.
|
117
|
-
# You can pass in the filename without extension or a symbol and we search it in your +appname.public+
|
159
|
+
# Returns an html script tag for each of the sources provided.
|
160
|
+
# You can pass in the filename without extension or a symbol and we search it in your +appname.public+
|
118
161
|
# like app/public/stylesheets for inclusion. You can provide also a full path.
|
119
|
-
#
|
162
|
+
#
|
120
163
|
# ==== Examples
|
121
|
-
#
|
164
|
+
#
|
122
165
|
# stylesheet_link_tag 'style', 'application', 'layout'
|
123
|
-
#
|
166
|
+
#
|
124
167
|
def stylesheet_link_tag(*sources)
|
125
168
|
options = sources.extract_options!.symbolize_keys
|
126
|
-
|
169
|
+
options.reverse_merge!(:media => 'screen', :rel => 'stylesheet', :type => 'text/css')
|
170
|
+
sources.collect { |source|
|
171
|
+
tag(:link, options.reverse_merge(:href => asset_path(:css, source)))
|
172
|
+
}.join("\n")
|
127
173
|
end
|
128
174
|
|
129
175
|
##
|
130
|
-
# Returns an html script tag for each of the sources provided.
|
131
|
-
# You can pass in the filename without extension or a symbol and we search it in your +appname.public+
|
176
|
+
# Returns an html script tag for each of the sources provided.
|
177
|
+
# You can pass in the filename without extension or a symbol and we search it in your +appname.public+
|
132
178
|
# like app/public/javascript for inclusion. You can provide also a full path.
|
133
|
-
#
|
179
|
+
#
|
134
180
|
# ==== Examples
|
135
|
-
#
|
181
|
+
#
|
136
182
|
# javascript_include_tag 'application', :extjs
|
137
|
-
#
|
183
|
+
#
|
138
184
|
def javascript_include_tag(*sources)
|
139
185
|
options = sources.extract_options!.symbolize_keys
|
140
|
-
|
186
|
+
options.reverse_merge!(:type => 'text/javascript', :content => "")
|
187
|
+
sources.collect { |source|
|
188
|
+
tag(:script, options.reverse_merge(:src => asset_path(:js, source)))
|
189
|
+
}.join("\n")
|
141
190
|
end
|
142
191
|
|
143
192
|
##
|
144
|
-
# Returns the path to the image, either relative or absolute. We search it in your +appname.public+
|
193
|
+
# Returns the path to the image, either relative or absolute. We search it in your +appname.public+
|
145
194
|
# like app/public/images for inclusion. You can provide also a full path.
|
146
|
-
#
|
195
|
+
#
|
147
196
|
# ==== Examples
|
148
|
-
#
|
149
|
-
#
|
150
|
-
#
|
197
|
+
#
|
198
|
+
# # Generates: /images/foo.jpg?1269008689
|
199
|
+
# image_path("foo.jpg")
|
200
|
+
#
|
151
201
|
def image_path(src)
|
152
|
-
|
153
|
-
src =~ %r{^\s*(/|http)} ? src : uri_root_path('images', src)
|
202
|
+
asset_path(:images, src)
|
154
203
|
end
|
155
204
|
|
156
205
|
##
|
157
|
-
#
|
158
|
-
#
|
206
|
+
# Returns the path to the specified asset (css or javascript)
|
207
|
+
#
|
159
208
|
# ==== Examples
|
160
|
-
#
|
161
|
-
#
|
162
|
-
#
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
#
|
169
|
-
#
|
170
|
-
|
171
|
-
#
|
172
|
-
# javascript_tag 'application', :src => '/javascripts/base/application.js'
|
173
|
-
#
|
174
|
-
def javascript_tag(source, options={})
|
175
|
-
options = options.dup.reverse_merge!(:src => javascript_path(source), :type => 'text/javascript', :content => "")
|
176
|
-
tag(:script, options)
|
177
|
-
end
|
178
|
-
|
179
|
-
##
|
180
|
-
# Returns the javascript_path appending the default javascripts path if necessary
|
181
|
-
#
|
182
|
-
def javascript_path(source)
|
183
|
-
return source if source =~ /^http/
|
184
|
-
source = source.to_s.gsub(/\.js$/, '')
|
185
|
-
source_name = source; source_name << ".js" unless source =~ /\.js/
|
186
|
-
result_path = source_name if source =~ %r{^/} # absolute path
|
187
|
-
result_path ||= uri_root_path("javascripts", source_name)
|
188
|
-
return result_path if result_path =~ /\?/
|
189
|
-
public_path = Padrino.root("public", result_path)
|
190
|
-
stamp = File.exist?(public_path) ? File.mtime(public_path).to_i : Time.now.to_i
|
191
|
-
"#{result_path}?#{stamp}"
|
192
|
-
end
|
193
|
-
|
194
|
-
##
|
195
|
-
# Returns the stylesheet_path appending the default stylesheets path if necessary
|
196
|
-
#
|
197
|
-
def stylesheet_path(source)
|
209
|
+
#
|
210
|
+
# # Generates: /javascripts/application.js?1269008689
|
211
|
+
# asset_path :js, :application
|
212
|
+
#
|
213
|
+
# # Generates: /stylesheets/application.css?1269008689
|
214
|
+
# asset_path :css, :application
|
215
|
+
#
|
216
|
+
# # Generates: /images/example.jpg?1269008689
|
217
|
+
# asset_path :images, 'example.jpg'
|
218
|
+
#
|
219
|
+
def asset_path(kind, source)
|
198
220
|
return source if source =~ /^http/
|
199
|
-
|
200
|
-
|
201
|
-
|
202
|
-
|
203
|
-
|
204
|
-
|
205
|
-
|
206
|
-
"
|
207
|
-
|
208
|
-
|
209
|
-
|
210
|
-
|
211
|
-
#
|
212
|
-
# ==== Examples
|
213
|
-
#
|
214
|
-
# favicon_tag 'favicon.png'
|
215
|
-
# favicon_tag 'icons/favicon.png'
|
216
|
-
# # or override some options
|
217
|
-
# favicon_tag 'favicon.png', :type => 'image/ico'
|
218
|
-
#
|
219
|
-
def favicon_tag(source,options={})
|
220
|
-
type = File.extname(source).gsub('.','')
|
221
|
-
options = options.dup.reverse_merge!(:href => image_path(source), :rel => 'icon', :type => "image/#{type}")
|
222
|
-
tag(:link, options)
|
221
|
+
asset_folder = case kind
|
222
|
+
when :css then 'stylesheets'
|
223
|
+
when :js then 'javascripts'
|
224
|
+
else kind.to_s
|
225
|
+
end
|
226
|
+
source = source.to_s.gsub(/\s/, '')
|
227
|
+
ignore_extension = (asset_folder.to_s == kind.to_s) # don't append extension
|
228
|
+
source << ".#{kind}" unless ignore_extension or source =~ /\.#{kind}/
|
229
|
+
result_path = source if source =~ %r{^/} # absolute path
|
230
|
+
result_path ||= uri_root_path(asset_folder, source)
|
231
|
+
timestamp = asset_timestamp(result_path)
|
232
|
+
"#{result_path}#{timestamp}"
|
223
233
|
end
|
224
234
|
|
225
235
|
private
|
236
|
+
|
226
237
|
##
|
227
|
-
# Returns the uri root of the application.
|
228
|
-
#
|
238
|
+
# Returns the uri root of the application.
|
239
|
+
#
|
229
240
|
def uri_root_path(*paths)
|
230
241
|
root_uri = self.class.uri_root if self.class.respond_to?(:uri_root)
|
231
242
|
File.join(root_uri || '/', *paths)
|
232
243
|
end
|
233
244
|
|
245
|
+
##
|
246
|
+
# Returns the timestamp mtime for an asset
|
247
|
+
#
|
248
|
+
def asset_timestamp(file_path)
|
249
|
+
return nil if file_path =~ /\?/
|
250
|
+
public_path = Padrino.root("public", file_path) if Padrino.respond_to?(:root)
|
251
|
+
stamp = Time.now.to_i unless public_path && File.exist?(public_path)
|
252
|
+
stamp ||= File.mtime(public_path).to_i
|
253
|
+
"?#{stamp}"
|
254
|
+
end
|
255
|
+
|
234
256
|
##
|
235
257
|
# Parse link_to options for give correct conditions
|
236
|
-
#
|
258
|
+
#
|
237
259
|
def parse_conditions(url, options)
|
238
260
|
if options.has_key?(:if)
|
239
261
|
condition = options.delete(:if)
|
@@ -246,4 +268,4 @@ module Padrino
|
|
246
268
|
end
|
247
269
|
end # AssetTagHelpers
|
248
270
|
end # Helpers
|
249
|
-
end # Padrino
|
271
|
+
end # Padrino
|