• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

gregschmit / rails-rest-framework / 4002900794

pending completion
4002900794

push

github

GitHub
Bump commonmarker from 0.23.6 to 0.23.7 in /docs

807 of 890 relevant lines covered (90.67%)

74.1 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

87.34
/lib/rest_framework/utils.rb
1
module RESTFramework::Utils
1✔
2
  HTTP_METHOD_ORDERING = %w(GET POST PUT PATCH DELETE OPTIONS HEAD)
1✔
3
  LABEL_FIELDS = %w(name label login title email username url)
1✔
4

5
  # Convert `extra_actions` hash to a consistent format: `{path:, methods:, kwargs:}`, and
6
  # additional metadata fields.
7
  #
8
  # If a controller is provided, labels will be added to any metadata fields.
9
  def self.parse_extra_actions(extra_actions, controller: nil)
1✔
10
    return (extra_actions || {}).map { |k, v|
48✔
11
      path = k
22✔
12

13
      # Convert structure to path/methods/kwargs.
14
      if v.is_a?(Hash)  # Allow kwargs to be used to define path differently from the key.
22✔
15
        # Symbolize keys (which also makes a copy so we don't mutate the original).
16
        v = v.symbolize_keys
3✔
17
        methods = v.delete(:methods)
3✔
18

19
        # First, remove the route metadata.
20
        metadata = v.delete(:metadata) || {}
3✔
21

22
        # Add label to fields.
23
        if controller && metadata[:fields]
3✔
24
          metadata[:fields] = metadata[:fields].map { |f| [f, {}] }.to_h if f.is_a?(Array)
×
25
          metadata[:fields]&.each do |field, cfg|
×
26
            cfg[:label] = controller.get_label(field) unless cfg[:label]
×
27
          end
28
        end
29

30
        # Override path if it's provided.
31
        if v.key?(:path)
3✔
32
          path = v.delete(:path)
1✔
33
        end
34

35
        # Pass any further kwargs to the underlying Rails interface.
36
        kwargs = v.presence
3✔
37
      elsif v.is_a?(Array) && v.length == 1
19✔
38
        methods = v[0]
×
39
      else
40
        methods = v
19✔
41
      end
42

43
      next [
44
        k,
22✔
45
        {
46
          path: path,
47
          methods: methods,
48
          kwargs: kwargs,
49
          metadata: metadata.presence,
50
          type: :extra,
51
        }.compact,
52
      ]
53
    }.to_h
54
  end
55

56
  # Get actions which should be skipped for a given controller.
57
  def self.get_skipped_builtin_actions(controller_class)
1✔
58
    return (
59
      RESTFramework::BUILTIN_ACTIONS.keys + RESTFramework::BUILTIN_MEMBER_ACTIONS.keys
20✔
60
    ).reject do |action|
61
      controller_class.method_defined?(action)
140✔
62
    end
63
  end
64

65
  # Get the first route pattern which matches the given request.
66
  def self.get_request_route(application_routes, request)
1✔
67
    application_routes.router.recognize(request) { |route, _| return route }
84✔
68
  end
69

70
  # Normalize a path pattern by replacing URL params with generic placeholder, and removing the
71
  # `(.:format)` at the end.
72
  def self.comparable_path(path)
1✔
73
    return path.gsub("(.:format)", "").gsub(/:[0-9A-Za-z_-]+/, ":x")
6,552✔
74
  end
75

76
  # Show routes under a controller action; used for the browsable API.
77
  def self.get_routes(application_routes, request, current_route: nil)
1✔
78
    current_route ||= self.get_request_route(application_routes, request)
42✔
79
    current_path = current_route.path.spec.to_s.gsub("(.:format)", "")
42✔
80
    current_path = "" if current_path == "/"
42✔
81
    current_levels = current_path.count("/")
42✔
82
    current_comparable_path = %r{^#{Regexp.quote(self.comparable_path(current_path))}(/|$)}
42✔
83

84
    # Add helpful properties of the current route.
85
    path_args = current_route.required_parts.map { |n| request.path_parameters[n] }
62✔
86
    route_props = {
87
      with_path_args: ->(r) {
42✔
88
        r.format(r.required_parts.each_with_index.map { |p, i| [p, path_args[i]] }.to_h)
220✔
89
      },
90
    }
91

92
    # Return routes that match our current route subdomain/pattern, grouped by controller. We
93
    # precompute certain properties of the route for performance.
94
    return route_props, application_routes.routes.select { |r|
42✔
95
      # We `select` first to avoid unnecessarily calculating metadata for routes we don't even want
96
      # to show.
97
      (
98
        (r.defaults[:subdomain].blank? || r.defaults[:subdomain] == request.subdomain) &&
6,510✔
99
        current_comparable_path.match?(self.comparable_path(r.path.spec.to_s)) &&
100
        r.defaults[:controller].present? &&
101
        r.defaults[:action].present?
102
      )
103
    }.map { |r|
104
      path = r.path.spec.to_s.gsub("(.:format)", "")
475✔
105
      levels = path.count("/")
475✔
106
      matches_path = current_path == path
475✔
107
      matches_params = r.required_parts.length == current_route.required_parts.length
475✔
108

109
      {
110
        route: r,
475✔
111
        verb: r.verb,
112
        path: path,
113
        # Starts at the number of levels in current path, and removes the `(.:format)` at the end.
114
        relative_path: path.split("/")[current_levels..]&.join("/").presence || "/",
115
        controller: r.defaults[:controller].presence,
116
        action: r.defaults[:action].presence,
117
        matches_path: matches_path,
118
        matches_params: matches_params,
119
        # The following options are only used in subsequent processing in this method.
120
        _levels: levels,
121
      }
122
    }.sort_by { |r|
123
      # Sort by levels first, so the routes matching closely with current request show first, then
124
      # by the path, and finally by the HTTP verb.
125
      [r[:_levels], r[:_path], HTTP_METHOD_ORDERING.index(r[:verb]) || 99]
475✔
126
    }.group_by { |r| r[:controller] }.sort_by { |c, _r|
475✔
127
      # Sort the controller groups by current controller first, then alphanumerically.
128
      [request.params[:controller] == c ? 0 : 1, c]
84✔
129
    }.to_h
130
  end
131

132
  # Custom inflector for RESTful controllers.
133
  def self.inflect(s, acronyms=nil)
1✔
134
    acronyms&.each do |acronym|
113✔
135
      s = s.gsub(/\b#{acronym}\b/i, acronym)
565✔
136
    end
137

138
    return s
113✔
139
  end
140

141
  # Parse fields hashes.
142
  def self.parse_fields_hash(fields_hash, model, exclude_associations: nil)
1✔
143
    parsed_fields = fields_hash[:only] || (
13✔
144
      model ? self.fields_for(model, exclude_associations: exclude_associations) : []
×
145
    )
146
    parsed_fields += fields_hash[:include] if fields_hash[:include]
13✔
147
    parsed_fields -= fields_hash[:exclude] if fields_hash[:exclude]
13✔
148

149
    # Warn for any unknown keys.
150
    (fields_hash.keys - [:only, :include, :exclude]).each do |k|
13✔
151
      Rails.logger.warn("RRF: Unknown key in fields hash: #{k}")
×
152
    end
153

154
    return parsed_fields
13✔
155
  end
156

157
  # Get the fields for a given model, including not just columns (which includes
158
  # foreign keys), but also associations.
159
  def self.fields_for(model, exclude_associations: nil)
1✔
160
    foreign_keys = model.reflect_on_all_associations(:belongs_to).map(&:foreign_key)
272✔
161

162
    if exclude_associations
272✔
163
      return model.column_names.reject { |c| c.in?(foreign_keys) }
×
164
    end
165

166
    # Add associations in addition to normal columns.
167
    return model.column_names.reject { |c|
272✔
168
      c.in?(foreign_keys)
2,176✔
169
    } + model.reflections.map { |association, ref|
170
      # Exclude certain associations (by default, active storage and action text associations).
171
      if ref.class_name.in?(RESTFramework.config.exclude_association_classes)
1,304✔
172
        next nil
528✔
173
      end
174

175
      if ref.collection? && RESTFramework.config.large_reverse_association_tables&.include?(
776✔
176
        ref.table_name,
177
      )
178
        next nil
×
179
      end
180

181
      next association
776✔
182
    }.compact
183
  end
184

185
  # Get the sub-fields that may be serialized and filtered/ordered for a reflection.
186
  def self.sub_fields_for(ref)
1✔
187
    if !ref.polymorphic? && model = ref.klass
242✔
188
      sub_fields = [model.primary_key].flatten.compact
242✔
189

190
      # Preferrably find a database column to use as label.
191
      if match = LABEL_FIELDS.find { |f| f.in?(model.column_names) }
872✔
192
        return sub_fields + [match]
230✔
193
      end
194

195
      # Otherwise, find a method.
196
      if match = LABEL_FIELDS.find { |f| model.method_defined?(f) }
96✔
197
        return sub_fields + [match]
×
198
      end
199

200
      return sub_fields
12✔
201
    end
202

203
    return ["id", "name"]
×
204
  end
205
end
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc