Module: DuckMap::Route

Extended by:
ActiveSupport::Concern
Includes:
ArrayHelper
Defined in:
lib/duck_map/route.rb

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

Instance Method Summary collapse

Methods included from ArrayHelper

#convert_to

Instance Attribute Details

#is_sitemapObject

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_nameObject

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_nameObject

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_blockObject

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_limitObject

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_nameString

Returns the action assigned to the route.

Returns:

  • (String)

    Action name 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

#availableFalseClass, TrueClass

Returns a boolean indicating if the route is available for use.

Returns:

  • (FalseClass, TrueClass)


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.

Parameters:

  • value (FalseClass, TrueClass)

    Boolean indicating if the route is available for use.

Returns:

  • (FalseClass, TrueClass)


48
49
50
# File 'lib/duck_map/route.rb', line 48

def available=(value)
  @available = value
end

#changefreqString

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>

Returns:

  • (String)

    Current value of changefreq.



147
148
149
# File 'lib/duck_map/route.rb', line 147

def changefreq
  return self.defaults[:changefreq]
end

#controller_nameString

Returns the controller_name assigned to the route.

Returns:

  • (String)

    Controller_name 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_actionsObject



196
197
198
# File 'lib/duck_map/route.rb', line 196

def exclude_actions
  return self.duckmap_defaults(:exclude_actions)
end

#exclude_controllersObject



201
202
203
# File 'lib/duck_map/route.rb', line 201

def exclude_controllers
  return self.duckmap_defaults(:exclude_controllers)
end

#exclude_namesObject



206
207
208
# File 'lib/duck_map/route.rb', line 206

def exclude_names
  return self.duckmap_defaults(:exclude_names)
end

#exclude_verbsObject



211
212
213
# File 'lib/duck_map/route.rb', line 211

def exclude_verbs
  return self.duckmap_defaults(:exclude_verbs)
end

#include_actionsObject



216
217
218
# File 'lib/duck_map/route.rb', line 216

def include_actions
  return self.duckmap_defaults(:include_actions)
end

#include_controllersObject



221
222
223
# File 'lib/duck_map/route.rb', line 221

def include_controllers
  return self.duckmap_defaults(:include_controllers)
end

#include_namesObject



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_verbsObject



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.

Returns:

  • (FalseClass, TrueClass)


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.

Returns:

  • (Boolean)

    True if the route is a sitemap route, otherwise, false.



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.

Returns:

  • (Boolean)

    True if keys are required to generate a url, otherwise, false.



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

#modelString

The class name (as a String) of a model to be used as the source of rows for a route when generating sitemap content.

Returns:

  • (String)


123
124
125
# File 'lib/duck_map/route.rb', line 123

def model
  return self.defaults[:model]
end

#namespace_prefixString

Returns Namespace prefix used when creating the route.

Returns:

  • (String)

    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_underscoresObject



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

#priorityString

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>

Returns:

  • (String)

    Current value of priority.



171
172
173
# File 'lib/duck_map/route.rb', line 171

def priority
  return self.defaults[:priority]
end

#route_nameString

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.

Returns:

  • (String)

    Name assigned to the route.



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.

Returns:

  • (Boolean)

    True if the route was defined with a block, otherwise, false.



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_formatString

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>

Returns:

  • (String)


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_symbolObject



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