Class: ActionDispatch::Journey::Formatter
- Defined in:
- actionpack/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.
Defined Under Namespace
Modules: RegexCaseComparator
Instance Attribute Summary collapse
-
#routes ⇒ Object
readonly
Returns the value of attribute routes.
Instance Method Summary collapse
- #clear ⇒ Object
- #generate(name, options, path_parameters, parameterize = nil) ⇒ Object
-
#initialize(routes) ⇒ Formatter
constructor
A new instance of Formatter.
Constructor Details
#initialize(routes) ⇒ Formatter
Returns a new instance of Formatter.
13 14 15 16 |
# File 'actionpack/lib/action_dispatch/journey/formatter.rb', line 13 def initialize(routes) @routes = routes @cache = nil end |
Instance Attribute Details
#routes ⇒ Object (readonly)
Returns the value of attribute routes
11 12 13 |
# File 'actionpack/lib/action_dispatch/journey/formatter.rb', line 11 def routes @routes end |
Instance Method Details
#clear ⇒ Object
60 61 62 |
# File 'actionpack/lib/action_dispatch/journey/formatter.rb', line 60 def clear @cache = nil end |
#generate(name, options, path_parameters, parameterize = nil) ⇒ Object
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'actionpack/lib/action_dispatch/journey/formatter.rb', line 18 def generate(name, , path_parameters, parameterize = nil) constraints = path_parameters.merge() missing_keys = nil match_route(name, constraints) do |route| parameterized_parts = extract_parameterized_parts(route, , path_parameters, 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 if missing_keys && !missing_keys.empty? params = .dup.delete_if do |key, _| parameterized_parts.key?(key) || route.defaults.key?(key) end defaults = route.defaults required_parts = route.required_parts route.parts.reverse_each do |key| break if defaults[key].nil? && parameterized_parts[key].present? next if parameterized_parts[key].to_s != defaults[key].to_s break if required_parts.include?(key) parameterized_parts.delete(key) end return [route.format(parameterized_parts), params] end unmatched_keys = (missing_keys || []) & constraints.keys missing_keys = (missing_keys || []) - unmatched_keys = +"No route matches #{Hash[constraints.sort_by { |k, v| k.to_s }].inspect}" << ", missing required keys: #{missing_keys.sort.inspect}" if missing_keys && !missing_keys.empty? << ", possible unmatched constraints: #{unmatched_keys.sort.inspect}" if unmatched_keys && !unmatched_keys.empty? raise ActionController::UrlGenerationError, end |