Module: RESTFramework::Utils
- Defined in:
- lib/rest_framework/utils.rb
Constant Summary collapse
- HTTP_METHOD_ORDERING =
%w(GET POST PUT PATCH DELETE OPTIONS HEAD)
Class Method Summary collapse
-
.comparable_path(path) ⇒ Object
Normalize a path pattern by replacing URL params with generic placeholder, and removing the ‘(.:format)` at the end.
-
.get_request_route(application_routes, request) ⇒ Object
Get the first route pattern which matches the given request.
-
.get_routes(application_routes, request, current_route: nil) ⇒ Object
Show routes under a controller action; used for the browsable API.
-
.get_skipped_builtin_actions(controller_class) ⇒ Object
Get actions which should be skipped for a given controller.
-
.inflect(s, acronyms = nil) ⇒ Object
Custom inflector for RESTful controllers.
-
.parse_extra_actions(extra_actions, controller: nil) ⇒ Object
Convert ‘extra_actions` hash to a consistent format: `methods:, kwargs:`, and additional metadata fields.
-
.parse_fields_hash(fields_hash, model) ⇒ Object
Parse fields hashes.
Class Method Details
.comparable_path(path) ⇒ Object
Normalize a path pattern by replacing URL params with generic placeholder, and removing the ‘(.:format)` at the end.
73 74 75 |
# File 'lib/rest_framework/utils.rb', line 73 def self.comparable_path(path) return path.gsub("(.:format)", "").gsub(/:[0-9A-Za-z_-]+/, ":x") end |
.get_request_route(application_routes, request) ⇒ Object
Get the first route pattern which matches the given request.
67 68 69 |
# File 'lib/rest_framework/utils.rb', line 67 def self.get_request_route(application_routes, request) application_routes.router.recognize(request) { |route, _| return route } end |
.get_routes(application_routes, request, current_route: nil) ⇒ Object
Show routes under a controller action; used for the browsable API.
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 |
# File 'lib/rest_framework/utils.rb', line 78 def self.get_routes(application_routes, request, current_route: nil) current_route ||= self.get_request_route(application_routes, request) current_path = current_route.path.spec.to_s.gsub("(.:format)", "") current_levels = current_path.count("/") current_comparable_path = %r{^#{Regexp.quote(self.comparable_path(current_path))}(/|$)} # Add helpful properties of the current route. path_args = current_route.required_parts.map { |n| request.path_parameters[n] } route_props = { with_path_args: ->(r) { r.format(r.required_parts.each_with_index.map { |p, i| [p, path_args[i]] }.to_h) }, } # Return routes that match our current route subdomain/pattern, grouped by controller. We # precompute certain properties of the route for performance. return route_props, application_routes.routes.select { |r| # We `select` first to avoid unnecessarily calculating metadata for routes we don't even want # to show. ( (r.defaults[:subdomain].blank? || r.defaults[:subdomain] == request.subdomain) && current_comparable_path.match?(self.comparable_path(r.path.spec.to_s)) && r.defaults[:controller].present? && r.defaults[:action].present? ) }.map { |r| path = r.path.spec.to_s.gsub("(.:format)", "") levels = path.count("/") matches_path = current_path == path matches_params = r.required_parts.length == current_route.required_parts.length { route: r, verb: r.verb, path: path, # Starts at the number of levels in current path, and removes the `(.:format)` at the end. relative_path: path.split("/")[current_levels..]&.join("/"), controller: r.defaults[:controller].presence, action: r.defaults[:action].presence, matches_path: matches_path, matches_params: matches_params, # The following options are only used in subsequent processing in this method. _levels: levels, } }.sort_by { |r| # Sort by levels first, so the routes matching closely with current request show first, then # by the path, and finally by the HTTP verb. [r[:_levels], r[:_path], HTTP_METHOD_ORDERING.index(r[:verb]) || 99] }.group_by { |r| r[:controller] }.sort_by { |c, _r| # Sort the controller groups by current controller first, then depth, then alphanumerically. [request.params[:controller] == c ? 0 : 1, c.count("/"), c] }.to_h end |
.get_skipped_builtin_actions(controller_class) ⇒ Object
Get actions which should be skipped for a given controller.
58 59 60 61 62 63 64 |
# File 'lib/rest_framework/utils.rb', line 58 def self.get_skipped_builtin_actions(controller_class) return ( RESTFramework::BUILTIN_ACTIONS.keys + RESTFramework::BUILTIN_MEMBER_ACTIONS.keys ).reject do |action| controller_class.method_defined?(action) end end |
.inflect(s, acronyms = nil) ⇒ Object
Custom inflector for RESTful controllers.
133 134 135 136 137 138 139 |
# File 'lib/rest_framework/utils.rb', line 133 def self.inflect(s, acronyms=nil) acronyms&.each do |acronym| s = s.gsub(/\b#{acronym}\b/i, acronym) end return s end |
.parse_extra_actions(extra_actions, controller: nil) ⇒ Object
Convert ‘extra_actions` hash to a consistent format: `methods:, kwargs:`, and additional metadata fields.
If a controller is provided, labels will be added to any metadata fields.
8 9 10 11 12 13 14 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 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/rest_framework/utils.rb', line 8 def self.parse_extra_actions(extra_actions, controller: nil) return (extra_actions || {}).map { |k, v| path = k # Convert structure to path/methods/kwargs. if v.is_a?(Hash) # Allow kwargs to be used to define path differently from the key. # Symbolize keys (which also makes a copy so we don't mutate the original). v = v.symbolize_keys methods = v.delete(:methods) # First, remove any metadata keys. delegate = v.delete(:delegate) fields = v.delete(:fields) # Add label to fields. if controller && fields fields = fields.map { |f| [f, {}] }.to_h if f.is_a?(Array) fields&.each do |field, cfg| cfg[:label] = controller.get_label(field) end end # Override path if it's provided. if v.key?(:path) path = v.delete(:path) end # Pass any further kwargs to the underlying Rails interface. kwargs = v.presence elsif v.is_a?(Array) && v.length == 1 methods = v[0] else methods = v end next [ k, { path: path, methods: methods, kwargs: kwargs, delegate: delegate, fields: fields, type: :extra, }.compact, ] }.to_h end |
.parse_fields_hash(fields_hash, model) ⇒ Object
Parse fields hashes.
142 143 144 145 146 147 148 149 150 151 152 |
# File 'lib/rest_framework/utils.rb', line 142 def self.parse_fields_hash(fields_hash, model) parsed_fields = fields_hash[:only] || model&.column_names || [] parsed_fields -= fields_hash[:except] if fields_hash[:except] # Warn for any unknown keys. (fields_hash.keys - [:only, :except]).each do |k| Rails.logger.warn("RRF: Unknown key in fields hash: #{k}") end return parsed_fields end |