Module: Sinatra::Mapping::Helpers
- Defined in:
- lib/sinatra/mapping.rb
Overview
Copyright © 2009 Hallison Batista
This module contains several helper methods for paths written using map
method.
Instance Method Summary collapse
-
#link_to(name = nil, *args) ⇒ Object
Creates anchor links for name and extract path and HTML options from arguments.
-
#path_to(*args) ⇒ Object
Returns all paths with query parameters.
-
#title_path(path, *args) ⇒ Object
Creates a title using a path mapped.
Instance Method Details
#link_to(name = nil, *args) ⇒ Object
Creates anchor links for name and extract path and HTML options from arguments. Example:
In Sinatra application, add a map.
map :tasks, "tasks"
map :status, "tasks/status"
get tasks_path do
erb :tasks, :locals => { :name => params.values.join(', ') }
end
get status_path do
erb :status, :locals => { :status => "finished" }
end
In status view, add a link to status map.
<%= link_to "All finished", :status, status %>
#=> <a href="/tasks/status/finished">All finished</a>
<%= link_to "All finished", :status, :name => status %>
#=> <a href="/tasks/status?name=finished">All finished</a>
170 171 172 173 174 175 |
# File 'lib/sinatra/mapping.rb', line 170 def link_to(name = nil, *args) = args.last.kind_of?(Hash) ? args.pop : {} url = args.shift if args.first.to_s =~ /^\w.*?:/ args << extract_query_attributes() "<a href=\"#{url || path_to(*args)}\"#{extract_link_attributes()}>#{name || url}</a>" end |
#path_to(*args) ⇒ Object
Returns all paths with query parameters. Example:
In Sinatra application:
map :post, "articles"
map :tags, "labels"
Use the following instructions:
path_to :tags, "ruby", :posts
#=> "/labels/ruby/articles"
188 189 190 |
# File 'lib/sinatra/mapping.rb', line 188 def path_to(*args) self.class.send(:build_path_to, env['SCRIPT_NAME'], *args) end |
#title_path(path, *args) ⇒ Object
Creates a title using a path mapped. Otherwise, returns just arguments joined by spaces and capitalised.
In Sinatra application:
map :posts, "articles"
map :tags, "labels"
map :archive, "archive/articles"
In views:
<%=title_path :posts%>
#=> "Articles"
<%=title_path :tags%>
#=> "Labels"
<%=title_path :archive%>
#=> "Archive articles"
141 142 143 144 145 |
# File 'lib/sinatra/mapping.rb', line 141 def title_path(path, *args) title = (.locations[path] || path).to_s.gsub('/',' ').strip title.gsub!(/\W/,' ') # Cleanup (args.empty? ? title : "#{title} #{args.join(' ')}").strip.capitalize end |