Module: Sinatra::Assets::Helpers
- Defined in:
- lib/sinatra/assets.rb
Instance Method Summary collapse
-
#host ⇒ Object
(also: #server)
Convenience method for the full Host URL.
-
#image(source, attrs = {}) ⇒ Object
(also: #image_tag)
Return image tag to path.
-
#link_to(link_text, url, attrs = {}) ⇒ Object
A basic link helper .
-
#protocol ⇒ Object
Convenience helper method that returns the URL protocol used.
-
#redirect_back_or(path = nil, do_redirect = false) ⇒ Object
(also: #redirect_back)
Simple helper method that redirects the edit/new page Cancel button back to the previous page, or if no previous page, then back to URL given.
-
#url_for(path = '/', url_params = {}, mode = nil) ⇒ Object
Construct a link to
url_fragment
, which should be given relative to the base of this Sinatra app.
Instance Method Details
#host ⇒ Object Also known as: server
Convenience method for the full Host URL
NB! The path must be returned without a trailing slash ‘/’ or else the :redirect_back_or()
method outputs the wrong paths
Examples
<%= host %> => 'http://example.org'
246 247 248 249 250 |
# File 'lib/sinatra/assets.rb', line 246 def host port = (request.scheme =~ /https?/ && request.port.to_s =~ /(80|443)/) ? "" : ":#{request.port}" "#{protocol}://#{request.env['SERVER_NAME']}#{port}" # "#{protocol}://#{request.env['SERVER_NAME']}" end |
#image(source, attrs = {}) ⇒ Object Also known as: image_tag
Return image tag to path.
Examples
image 'foo.png'
# => <img src="/images/foo.png" />
image '/foo.png', :alt => 'Kung-foo'
# => <img src="/foo.png" alt="Kung-foo">
182 183 184 185 186 187 188 189 |
# File 'lib/sinatra/assets.rb', line 182 def image(source, attrs = {}) attrs[:src] = (source[0,1] == '/') ? url_for(source) : url_for("/images/#{source}") attrs[:alt] ||= File.basename(source,'.*').split('.').first.to_s.capitalize if size = attrs.delete(:size) attrs[:width], attrs[:height] = size.split("x") if size =~ %r{^\d+%?x\d+%?$} end %Q[<img #{attrs.to_html_attributes}>] end |
#link_to(link_text, url, attrs = {}) ⇒ Object
A basic link helper
Examples
link_to('FAQs', '#faqs')
=> <a href="#faqs">FAQs/a>
link_to('Apple', 'http://apple.com')
=> <a href="http://apple.com">Apple</a>
link_to('Apple', 'http://apple.com', :title => 'go buy a new computer')
=> <a href="http://apple.com" title="go buy a new computer">Apple</a>
207 208 209 210 211 212 213 214 |
# File 'lib/sinatra/assets.rb', line 207 def link_to(link_text, url, attrs={}) unless url[0,1] == '#' # tag url = url_for(url) unless remote_asset?(url) end attrs = {:href => url }.merge(attrs) %Q[<a #{attrs.to_html_attributes}>#{link_text}</a>] end |
#protocol ⇒ Object
Convenience helper method that returns the URL protocol used
Examples
<%= protocol %> => 'http'
261 262 263 |
# File 'lib/sinatra/assets.rb', line 261 def protocol request.env['rack.url_scheme'] end |
#redirect_back_or(path = nil, do_redirect = false) ⇒ Object Also known as: redirect_back
Simple helper method that redirects the edit/new page Cancel button back to the previous page, or if no previous page, then back to URL given.
Examples
redirect_back_or(url)
=>
227 228 229 230 231 232 |
# File 'lib/sinatra/assets.rb', line 227 def redirect_back_or(path = nil, do_redirect = false) past = request.nil? ? nil : (request.env['HTTP_REFERER'] ||= nil) path = past.nil? ? path : past.sub(host,'') do_redirect ? redirect(path) : path end |
#url_for(path = '/', url_params = {}, mode = nil) ⇒ Object
Construct a link to url_fragment
, which should be given relative to the base of this Sinatra app.
The mode should be either :path_only
, which will generate an absolute path within the current domain (the default), or :full
, which will include the site name and port number. (The latter is typically necessary for links in RSS feeds.)
Code inspiration from [ github.com/emk/sinatra-url-for/ ] by Eric Kidd
Examples
When request.script_name => ''
=> map ‘/’ do…
url_for() # Returns "/"
url_for('') # Returns "/"
url_for(nil) # Returns "/"
url_for "/" # Returns "/"
url_for("/", :root) # Returns "/" at the root of the app system
url_for "path" # Returns "/path"
url_for "/path" # Returns "/path"
url_for "path", :full # Returns "http://example.com/path"
url_for "/path", :full # Returns "http://example.com/path"
This also work with apps that are mounted Rack apps:
# config.ru
map '/somepath' do
run MyApp
end
url_for '/blog' => /somepath/blog
NB! ‘/urlmap’ represents the URL Map path given by request.script_name
When request.script_name => '/urlmap'
=> map ‘/urlmap’ do…
url_for() # Returns "/urlmap/"
url_for('') # Returns "/urlmap/"
url_for(nil) # Returns "/urlmap/"
url_for "/" # Returns "/urlmap/"
url_for("/", :root) # Returns "/" at the root of the app system
url_for "path" # Returns "/urlmap/path"
url_for "/path" # Returns "/path"
url_for "path", :full # Returns "http://example.com/urlmap/path"
url_for "/path", :full # Returns "http://example.com/path"
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 |
# File 'lib/sinatra/assets.rb', line 123 def url_for(path='/', url_params = {}, mode = nil) mode, url_params = url_params, {} if url_params.is_a?(Symbol) # Acceptable paths at this stage are: # -- '' # empty string # -- '/' # -- '/path/2/something' # -- 'path/2/something' # ensure our path is present & correct if path.nil? warn "url_for() method given a nil value" out_path = '/' # do we have an empty string? elsif (path.to_s == '' ) # this means we're looking to stay at root of our current app out_path = request.script_name.empty? ? '/' : "#{request.script_name}/" # do we have a starting slash ? elsif (path.to_s[0,1] == '/') # yes, we have. is it longer than 1 char (ie: not cleaned up nil value) if path.length > 1 # root path, so ignore script_name out_path = path else # no, short path, so are we # -- having a script_name value # -- having a mode identifier out_path = request.script_name.empty? ? path : "#{request.script_name}#{path}" end else # no, we are staying locally within our app out_path = request.script_name.empty? ? "/#{path}" : "#{request.script_name}/#{path}" end case mode when :full port = (request.scheme =~ /https?/ && request.port.to_s =~ /(80|443)/) ? "" : ":#{request.port}" base = "#{request.scheme}://#{request.host}#{port}#{request.script_name}" when :root base = '' # ignoring the script_name path else base = '' end "#{base}#{out_path}" end |