Module: DuckMap::RouteFilter

Extended by:
ActiveSupport::Concern
Defined in:
lib/duck_map/route_filter.rb

Overview

Mixin module for ActionDispatch::Routing::RouteSet. This module is responsible for evaluating each route for consideration to be included in a sitemap.

You can find a few examples and apps at: (www.jeffduckett.com/blog/15/route-filters.html)

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#blank_route_nameObject

Indicates to the route filter to exclude / include routes that are missing names.



29
30
31
# File 'lib/duck_map/route_filter.rb', line 29

def blank_route_name
  @blank_route_name
end

Instance Method Details

#blank_route_name?Boolean

Indicates to the route filter to exclude / include routes that are missing names.

Returns:

  • (Boolean)


34
35
36
# File 'lib/duck_map/route_filter.rb', line 34

def blank_route_name?
  return self.blank_route_name.nil? ? false : self.blank_route_name
end

#include_route?(route) ⇒ Boolean

Determines if the current routes passes the current filter criteria.

Returns:

  • (Boolean)

    True if it passes, otherwise, false.



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
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
# File 'lib/duck_map/route_filter.rb', line 48

def include_route?(route)
  value = false

  unless route.blank? || route.path.spec =~ %r{/rails/info/properties|^/assets}
    # this block looks very busy, but, actually all that is really going on here is we are matching
    # parts of the current route against sitemap_filter data configured via config/routes.rb, then,
    # raising and catching exceptions based on the outcome of each evaluation.
    # the rule is all routes excluded, unless included via some type of filter criteria.

    begin

      DuckMap.logger.debug "\r\n  Route: #{route.verb_symbol} #{route.route_name.ljust(30)} #{route.controller_name} => #{route.action_name}"
      DuckMap.logger.debug route
      DuckMap.logger.debug %(#{"Path:".rjust(30)} #{route.path.spec})

      # we don't want to include routes without a name.
      if route.name.blank?
        unless self.blank_route_name?
          raise RouteNameMissing, "route name blank"
        end
      end

      if match_any?(route.action_name, route.exclude_actions) ||
          match_any?(route.controller_name, route.exclude_controllers) ||
          match_any?(route.name, route.exclude_names) ||
          match_any?(route.verb_symbol, route.exclude_verbs)

        raise DuckmapExclude, "exclude"

      end

      if match_any?(route.action_name, route.include_actions) ||
          match_any?(route.controller_name, route.include_controllers) ||
          match_any?(route.name, route.include_names) ||
          match_any?(route.verb_symbol, route.include_verbs)

        raise DuckmapInclude, "include"

      end

      if match_any?(route.action_name, self.sitemap_filters.current_filter[:exclude][:actions])
        raise ExplicitExclude, "exclude"
      end

      if match_any?(route.verb_symbol, self.sitemap_filters.current_filter[:exclude][:verbs])
        raise ExplicitExclude, "exclude"
      end

      if match_any?(route.controller_name, self.sitemap_filters.current_filter[:exclude][:controllers])
        raise ExplicitExclude, "exclude"
      end

      if match_any?(route.name, self.sitemap_filters.current_filter[:exclude][:names])
        raise ExplicitExclude, "exclude"
      end

      if match_any?(route.action_name, self.sitemap_filters.current_filter[:include][:actions])
        raise ExplicitInclude, "include"
      end

      if match_any?(route.verb_symbol, self.sitemap_filters.current_filter[:include][:verbs])
        raise ExplicitInclude, "include"
      end

      if match_any?(route.controller_name, self.sitemap_filters.current_filter[:include][:controllers])
        raise ExplicitInclude, "include"
      end

      if match_any?(route.name, self.sitemap_filters.current_filter[:include][:names])
        raise ExplicitInclude, "include"
      end

    rescue DuckmapExclude => e
      DuckMap.logger.debug %(#{"Duckmap Exclude".rjust(30)} -> #{e})

      if match_any?(route.action_name, route.include_actions) ||
          match_any?(route.controller_name, route.include_controllers) ||
          match_any?(route.name, route.include_names) ||
          match_any?(route.verb_symbol, route.include_verbs)

        DuckMap.logger.debug %(#{"Duckmap Exclude".rjust(30)} -> included again...)
        value = true
      end

    rescue DuckmapInclude => e
      DuckMap.logger.debug %(#{"Duckmap Include".rjust(30)} -> #{e})
      value = true

    rescue ExplicitExclude => e
      DuckMap.logger.debug %(#{"Explicit Exclude".rjust(30)} -> #{e})

    rescue ExplicitInclude => e
      DuckMap.logger.debug %(#{"Explicit Include".rjust(30)} -> #{e})
      value = true

    rescue RouteNameMissing => e
      DuckMap.logger.debug %(#{"Route Name Missing".rjust(30)} -> #{e})

    rescue Exception => e
      DuckMap.logger.info %(#{"Unknown Exception".rjust(30)} -> #{e})
      DuckMap.logger.info e.backtrace.join("\r\n")
    end

  end

  return value
end

#match_any?(data = nil, values = []) ⇒ Boolean

Matches a single value against an array of Strings, Symbols, and Regexp’s.

Parameters:

  • data (String) (defaults to: nil)

    Any value as a String to compare against any of the Strings, Symbols, or Regexp’s in the values argument.

  • values (Array) (defaults to: [])

    An array of Strings, Symbols, or Regexp’s to compare against the data argument. The array can be a mix of all three possible types.

Returns:

  • (Boolean)

    True if data matches any of the values or expressions in the values argument, otherwise, false.



161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
# File 'lib/duck_map/route_filter.rb', line 161

def match_any?(data = nil, values = [])

  unless data.blank?

    unless values.kind_of?(Array)
      # wow, this worked!!??
      # values was not an array, so, add values to a new array and assign back to values
      values = [values]
    end

    values.each do |value|

      if value.kind_of?(String) && (data.to_s.downcase.eql?(value.downcase) || value.eql?("all"))
        return true

      elsif value.kind_of?(Symbol) && (data.downcase.to_sym.eql?(value) || value.eql?(:all))
        return true

      elsif value.kind_of?(Regexp) && data.match(value)
        return true

      end
    end

  end

  return false
end

#sitemap_filtersHash

A Hash containing all of the values for exlude sitemap_filters.

Returns:

  • (Hash)


41
42
43
# File 'lib/duck_map/route_filter.rb', line 41

def sitemap_filters
  return @sitemap_filters ||= FilterStack.new
end