Class: ActionDispatch::Journey::Formatter
- Inherits:
-
Object
- Object
- ActionDispatch::Journey::Formatter
- Defined in:
- lib/action_dispatch/journey/formatter.rb
Overview
The Formatter class is used for formatting URLs. For example, parameters passed to url_for
in Rails will eventually call Formatter#generate.
Instance Attribute Summary collapse
-
#routes ⇒ Object
readonly
:nodoc:.
Instance Method Summary collapse
- #clear ⇒ Object
- #generate(type, name, options, recall = {}, parameterize = nil) ⇒ Object
-
#initialize(routes) ⇒ Formatter
constructor
A new instance of Formatter.
Constructor Details
#initialize(routes) ⇒ Formatter
Returns a new instance of Formatter.
10 11 12 13 |
# File 'lib/action_dispatch/journey/formatter.rb', line 10 def initialize(routes) @routes = routes @cache = nil end |
Instance Attribute Details
#routes ⇒ Object (readonly)
:nodoc:
8 9 10 |
# File 'lib/action_dispatch/journey/formatter.rb', line 8 def routes @routes end |
Instance Method Details
#clear ⇒ Object
42 43 44 |
# File 'lib/action_dispatch/journey/formatter.rb', line 42 def clear @cache = nil end |
#generate(type, name, options, recall = {}, parameterize = nil) ⇒ Object
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/action_dispatch/journey/formatter.rb', line 15 def generate(type, name, , recall = {}, parameterize = nil) constraints = recall.merge() missing_keys = [] match_route(name, constraints) do |route| parameterized_parts = extract_parameterized_parts(route, , recall, parameterize) # Skip this route unless a name has been provided or it is a # standard Rails route since we can't determine whether an options # hash passed to url_for matches a Rack application or a redirect. next unless name || route.dispatcher? missing_keys = missing_keys(route, parameterized_parts) next unless missing_keys.empty? params = .dup.delete_if do |key, _| parameterized_parts.key?(key) || route.defaults.key?(key) end return [route.format(parameterized_parts), params] end = "No route matches #{Hash[constraints.sort].inspect}" << " missing required keys: #{missing_keys.sort.inspect}" unless missing_keys.empty? raise ActionController::UrlGenerationError, end |