Module: DuckMap::Route
Overview
Conveience methods that wrap standard ActionDispatch::Routing::Route methods and data. The purpose is to harness the existing code to allow extra config arguments to be included on routes within config/routes.rb. ActionDispatch::Routing::Route has a couple of instance variables (defaults and requirements).
- defaults is a Hash that contains the arguments passed to methods within config/routes.rb such as
resources, match, etc. The arguments passed to most routes config methods will actually make a call
to match to add a route and will include any extra arguments you pass in the defaults Hash.
The methods of this module extract those values.
- requirements is a Hash that contains the controller and action name of the current route.
Instance Attribute Summary collapse
-
#is_sitemap ⇒ Object
Identifies the current routes as being a sitemap route.
-
#sitemap_raw_route_name ⇒ Object
The route name of the sitemap without the namespace.
-
#sitemap_route_name ⇒ Object
The route name of the sitemap which the current route is assigned.
-
#sitemap_with_block ⇒ Object
Identifies that the current sitemap route was defined with a block.
-
#url_limit ⇒ Object
Total amount of URL nodes allowed in the sitemap.
Instance Method Summary collapse
-
#action_name ⇒ String
Returns the action assigned to the route.
-
#available ⇒ FalseClass, TrueClass
Returns a boolean indicating if the route is available for use.
-
#available=(value) ⇒ FalseClass, TrueClass
Sets a boolean indicating if the route is available for use.
-
#changefreq ⇒ String
Setting changefreq directly on the route will override values set within a controller and model.
-
#controller_name ⇒ String
Returns the controller_name assigned to the route.
-
#duckmap_defaults(key) ⇒ Object
Looks for a key within ActionDispatch::Routing::Route.defaults.
- #exclude_actions ⇒ Object
- #exclude_controllers ⇒ Object
- #exclude_names ⇒ Object
- #exclude_verbs ⇒ Object
- #include_actions ⇒ Object
- #include_controllers ⇒ Object
- #include_names ⇒ Object
- #include_verbs ⇒ Object
-
#is_available? ⇒ FalseClass, TrueClass
Returns a boolean indicating if the route is available for use.
-
#is_sitemap? ⇒ Boolean
Identifies the current routes as being a sitemap route.
-
#keys_required? ⇒ Boolean
Indicates if the current route requirements segments keys to generate a url.
-
#model ⇒ String
The class name (as a String) of a model to be used as the source of rows for a route when generating sitemap content.
-
#namespace_prefix ⇒ String
Namespace prefix used when creating the route.
- #namespace_prefix_underscores ⇒ Object
-
#priority ⇒ String
Setting priority directly on the route will override values set within a controller and model.
-
#route_name ⇒ String
Conveience method to return the name assigned to the route.
-
#sitemap_with_block? ⇒ Boolean
Identifies that the current sitemap route was defined with a block.
-
#url_format ⇒ String
Specifies the extension that should be used when generating a url for the route within a sitemap.
- #verb_symbol ⇒ Object
Methods included from ArrayHelper
Instance Attribute Details
#is_sitemap ⇒ Object
Identifies the current routes as being a sitemap route.
20 21 22 |
# File 'lib/duck_map/route.rb', line 20 def is_sitemap @is_sitemap end |
#sitemap_raw_route_name ⇒ Object
The route name of the sitemap without the namespace.
26 27 28 |
# File 'lib/duck_map/route.rb', line 26 def sitemap_raw_route_name @sitemap_raw_route_name end |
#sitemap_route_name ⇒ Object
The route name of the sitemap which the current route is assigned.
23 24 25 |
# File 'lib/duck_map/route.rb', line 23 def sitemap_route_name @sitemap_route_name end |
#sitemap_with_block ⇒ Object
Identifies that the current sitemap route was defined with a block.
29 30 31 |
# File 'lib/duck_map/route.rb', line 29 def sitemap_with_block @sitemap_with_block end |
#url_limit ⇒ Object
Total amount of URL nodes allowed in the sitemap.
32 33 34 |
# File 'lib/duck_map/route.rb', line 32 def url_limit @url_limit end |
Instance Method Details
#action_name ⇒ String
Returns the action assigned to the route.
115 116 117 |
# File 'lib/duck_map/route.rb', line 115 def action_name return self.requirements[:action].blank? ? "" : self.requirements[:action].to_s end |
#available ⇒ FalseClass, TrueClass
Returns a boolean indicating if the route is available for use.
37 38 39 40 41 42 |
# File 'lib/duck_map/route.rb', line 37 def available unless defined?(@available) @available = true end return @available end |
#available=(value) ⇒ FalseClass, TrueClass
Sets a boolean indicating if the route is available for use.
48 49 50 |
# File 'lib/duck_map/route.rb', line 48 def available=(value) @available = value end |
#changefreq ⇒ String
Setting changefreq directly on the route will override values set within a controller and model. This value will be used when generating a sitemap for the specific route.
MyApp::Application.routes.draw do
resources :trucks, :changefreq => "daily"
end
produces url’s like the following:
<url>
<loc>http://localhost:3000/trucks/1.html</loc>
<lastmod>2011-10-13T06:16:24+00:00</lastmod>
<changefreq>daily</changefreq>
<priority>0.5</priority>
</url>
147 148 149 |
# File 'lib/duck_map/route.rb', line 147 def changefreq return self.defaults[:changefreq] end |
#controller_name ⇒ String
Returns the controller_name assigned to the route.
108 109 110 |
# File 'lib/duck_map/route.rb', line 108 def controller_name return self.requirements[:controller].blank? ? "" : self.requirements[:controller].to_s end |
#duckmap_defaults(key) ⇒ Object
Looks for a key within ActionDispatch::Routing::Route.defaults. If found:
- determine the type of value:
- If Array, return the array.
- If String, create an array, add the String to it, and return the array.
- If Symbol, create an array, add the Symbol to it, and return the array.
If nothing found, return an empty array. returns [Array]
276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 |
# File 'lib/duck_map/route.rb', line 276 def duckmap_defaults(key) values = [] if self.defaults && self.defaults[key] if self.defaults[key].kind_of?(Array) values = self.defaults[key] elsif self.defaults[key].kind_of?(String) values.push(self.defaults[key]) elsif self.defaults[key].kind_of?(Symbol) values.push(self.defaults[key]) end end return values end |
#exclude_actions ⇒ Object
196 197 198 |
# File 'lib/duck_map/route.rb', line 196 def exclude_actions return self.duckmap_defaults(:exclude_actions) end |
#exclude_controllers ⇒ Object
201 202 203 |
# File 'lib/duck_map/route.rb', line 201 def exclude_controllers return self.duckmap_defaults(:exclude_controllers) end |
#exclude_names ⇒ Object
206 207 208 |
# File 'lib/duck_map/route.rb', line 206 def exclude_names return self.duckmap_defaults(:exclude_names) end |
#exclude_verbs ⇒ Object
211 212 213 |
# File 'lib/duck_map/route.rb', line 211 def exclude_verbs return self.duckmap_defaults(:exclude_verbs) end |
#include_actions ⇒ Object
216 217 218 |
# File 'lib/duck_map/route.rb', line 216 def include_actions return self.duckmap_defaults(:include_actions) end |
#include_controllers ⇒ Object
221 222 223 |
# File 'lib/duck_map/route.rb', line 221 def include_controllers return self.duckmap_defaults(:include_controllers) end |
#include_names ⇒ Object
226 227 228 |
# File 'lib/duck_map/route.rb', line 226 def include_names return self.convert_to(self.duckmap_defaults(:include_names), :string) end |
#include_verbs ⇒ Object
231 232 233 |
# File 'lib/duck_map/route.rb', line 231 def include_verbs return self.duckmap_defaults(:include_verbs) end |
#is_available? ⇒ FalseClass, TrueClass
Returns a boolean indicating if the route is available for use.
55 56 57 |
# File 'lib/duck_map/route.rb', line 55 def is_available? return self.available end |
#is_sitemap? ⇒ Boolean
Identifies the current routes as being a sitemap route.
62 63 64 |
# File 'lib/duck_map/route.rb', line 62 def is_sitemap? @is_sitemap = @is_sitemap.nil? ? false : @is_sitemap end |
#keys_required? ⇒ Boolean
Indicates if the current route requirements segments keys to generate a url.
261 262 263 264 265 |
# File 'lib/duck_map/route.rb', line 261 def keys_required? keys = self.segment_keys.dup keys.delete(:format) return keys.length > 0 ? true : false end |
#model ⇒ String
The class name (as a String) of a model to be used as the source of rows for a route when generating sitemap content.
123 124 125 |
# File 'lib/duck_map/route.rb', line 123 def model return self.defaults[:model] end |
#namespace_prefix ⇒ String
Returns Namespace prefix used when creating the route.
68 69 70 71 72 73 74 75 76 |
# File 'lib/duck_map/route.rb', line 68 def namespace_prefix value = nil unless self.sitemap_raw_route_name.blank? value = self.sitemap_route_name.gsub(self.sitemap_raw_route_name, "") end return value end |
#namespace_prefix_underscores ⇒ Object
79 80 81 82 83 84 85 86 87 88 |
# File 'lib/duck_map/route.rb', line 79 def namespace_prefix_underscores value = 0 buffer = self.namespace_prefix unless buffer.blank? value = buffer.split("_").length end return value end |
#priority ⇒ String
Setting priority directly on the route will override values set within a controller and model. This value will be used when generating a sitemap for the specific route.
MyApp::Application.routes.draw do
resources :trucks, :priority => "0.4"
end
produces url’s like the following:
<url>
<loc>http://localhost:3000/trucks/1.html</loc>
<lastmod>2011-10-13T06:16:24+00:00</lastmod>
<changefreq>monthly</changefreq>
<priority>0.4</priority>
</url>
171 172 173 |
# File 'lib/duck_map/route.rb', line 171 def priority return self.defaults[:priority] end |
#route_name ⇒ String
Conveience method to return the name assigned to the route. There is no need for nil checking the return value of this method. It will simply return an empty String.
101 102 103 |
# File 'lib/duck_map/route.rb', line 101 def route_name return "#{self.name}" end |
#sitemap_with_block? ⇒ Boolean
Identifies that the current sitemap route was defined with a block.
93 94 95 |
# File 'lib/duck_map/route.rb', line 93 def sitemap_with_block? @sitemap_with_block = @sitemap_with_block.nil? ? false : @sitemap_with_block end |
#url_format ⇒ String
Specifies the extension that should be used when generating a url for the route within a sitemap.
MyApp::Application.routes.draw do
resources :trucks, :url_format => "xml"
end
produces url’s like the following:
<loc>http://localhost:3000/trucks/1.xml</loc>
<loc>http://localhost:3000/trucks/2.xml</loc>
190 191 192 193 |
# File 'lib/duck_map/route.rb', line 190 def url_format # a quick hack to default the extension for the root url to :none return self.defaults[:url_format].blank? && self.route_name.eql?("root") ? :none : self.defaults[:url_format] end |
#verb_symbol ⇒ Object
236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 |
# File 'lib/duck_map/route.rb', line 236 def verb_symbol value = nil unless self.verb.blank? buffer = self.verb.to_s.downcase if buffer.include?("delete") value = :delete elsif buffer.include?("get") value = :get elsif buffer.include?("post") value = :post elsif buffer.include?("put") value = :put end end return value end |