• 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

92.71
/lib/rest_framework/routers.rb
1
require "action_dispatch/routing/mapper"
1✔
2
require_relative "utils"
1✔
3

4
module ActionDispatch::Routing
1✔
5
  class Mapper
1✔
6
    # Internal interface to get the controller class from the name and current scope.
7
    def _get_controller_class(name, pluralize: true, fallback_reverse_pluralization: true)
1✔
8
      # Get class name.
9
      name = name.to_s.camelize  # Camelize to leave plural names plural.
25✔
10
      name = name.pluralize if pluralize
25✔
11
      if name == name.pluralize
25✔
12
        name_reverse = name.singularize
18✔
13
      else
14
        name_reverse = name.pluralize
7✔
15
      end
16
      name += "Controller"
25✔
17
      name_reverse += "Controller"
25✔
18

19
      # Get scope for the class.
20
      if @scope[:module]
25✔
21
        mod = @scope[:module].to_s.camelize.constantize
22✔
22
      else
23
        mod = Object
3✔
24
      end
25

26
      # Convert class name to class.
27
      begin
28
        controller = mod.const_get(name)
25✔
29
      rescue NameError
30
        if fallback_reverse_pluralization
6✔
31
          controller = mod.const_get(name_reverse)
6✔
32
        else
33
          raise
×
34
        end
35
      end
36

37
      return controller
25✔
38
    end
39

40
    # Interal interface for routing extra actions.
41
    def _route_extra_actions(actions, &block)
1✔
42
      parsed_actions = RESTFramework::Utils.parse_extra_actions(actions)
45✔
43

44
      parsed_actions.each do |action, config|
45✔
45
        [config[:methods]].flatten.each do |m|
18✔
46
          public_send(m, config[:path], action: action, **(config[:kwargs] || {}))
18✔
47
        end
48
        yield if block_given?
18✔
49
      end
50
    end
51

52
    # Internal core implementation of the `rest_resource(s)` router, both singular and plural.
53
    # @param default_singular [Boolean] the default plurality of the resource if the plurality is
54
    #   not otherwise defined by the controller
55
    # @param name [Symbol] the resource name, from which path and controller are deduced by default
56
    def _rest_resources(default_singular, name, **kwargs, &block)
1✔
57
      controller = kwargs.delete(:controller) || name
20✔
58
      if controller.is_a?(Class)
20✔
59
        controller_class = controller
×
60
      else
61
        controller_class = self._get_controller_class(controller, pluralize: !default_singular)
20✔
62
      end
63

64
      # Set controller if it's not explicitly set.
65
      kwargs[:controller] = name unless kwargs[:controller]
20✔
66

67
      # Passing `unscoped: true` will prevent a nested resource from being scoped.
68
      unscoped = kwargs.delete(:unscoped)
20✔
69

70
      # Determine plural/singular resource.
71
      force_singular = kwargs.delete(:force_singular)
20✔
72
      force_plural = kwargs.delete(:force_plural)
20✔
73
      if force_singular
20✔
74
        singular = true
1✔
75
      elsif force_plural
19✔
76
        singular = false
1✔
77
      elsif !controller_class.singleton_controller.nil?
18✔
78
        singular = controller_class.singleton_controller
1✔
79
      else
80
        singular = default_singular
17✔
81
      end
82
      resource_method = singular ? :resource : :resources
20✔
83

84
      # Call either `resource` or `resources`, passing appropriate modifiers.
85
      skip = RESTFramework::Utils.get_skipped_builtin_actions(controller_class)
20✔
86
      public_send(resource_method, name, except: skip, **kwargs) do
20✔
87
        if controller_class.respond_to?(:extra_member_actions)
20✔
88
          member do
20✔
89
            self._route_extra_actions(controller_class.extra_member_actions)
20✔
90
          end
91
        end
92

93
        collection do
20✔
94
          # Route extra controller-defined actions.
95
          self._route_extra_actions(controller_class.extra_actions)
20✔
96

97
          # Route extra RRF-defined actions.
98
          RESTFramework::RRF_BUILTIN_ACTIONS.each do |action, methods|
20✔
99
            next unless controller_class.method_defined?(action)
20✔
100

101
            [methods].flatten.each do |m|
20✔
102
              public_send(m, "", action: action) if self.respond_to?(m)
20✔
103
            end
104
          end
105

106
          # Route bulk actions, if configured.
107
          RESTFramework::RRF_BUILTIN_BULK_ACTIONS.each do |action, methods|
20✔
108
            next unless controller_class.method_defined?(action)
40✔
109

110
            [methods].flatten.each do |m|
×
111
              public_send(m, "", action: action) if self.respond_to?(m)
×
112
            end
113
          end
114
        end
115

116
        if unscoped
20✔
117
          yield if block_given?
×
118
        else
119
          scope(module: name, as: name) do
20✔
120
            yield if block_given?
20✔
121
          end
122
        end
123
      end
124
    end
125

126
    # Public interface for creating singular RESTful resource routes.
127
    def rest_resource(*names, **kwargs, &block)
1✔
128
      names.each do |n|
2✔
129
        self._rest_resources(true, n, **kwargs, &block)
2✔
130
      end
131
    end
132

133
    # Public interface for creating plural RESTful resource routes.
134
    def rest_resources(*names, **kwargs, &block)
1✔
135
      names.each do |n|
18✔
136
        self._rest_resources(false, n, **kwargs, &block)
18✔
137
      end
138
    end
139

140
    # Route a controller without the default resourceful paths.
141
    def rest_route(name=nil, **kwargs, &block)
1✔
142
      controller = kwargs.delete(:controller) || name
5✔
143
      route_root_to = kwargs.delete(:route_root_to)
5✔
144
      if controller.is_a?(Class)
5✔
145
        controller_class = controller
×
146
      else
147
        controller_class = self._get_controller_class(controller, pluralize: false)
5✔
148
      end
149

150
      # Set controller if it's not explicitly set.
151
      kwargs[:controller] = name unless kwargs[:controller]
5✔
152

153
      # Passing `unscoped: true` will prevent a nested resource from being scoped.
154
      unscoped = kwargs.delete(:unscoped)
5✔
155

156
      # Route actions using the resourceful router, but skip all builtin actions.
157
      public_send(:resource, name, only: [], **kwargs) do
5✔
158
        # Route a root for this resource.
159
        if route_root_to
5✔
160
          get("", action: route_root_to, as: "")
4✔
161
        end
162

163
        collection do
5✔
164
          # Route extra controller-defined actions.
165
          self._route_extra_actions(controller_class.extra_actions)
5✔
166

167
          # Route extra RRF-defined actions.
168
          RESTFramework::RRF_BUILTIN_ACTIONS.each do |action, methods|
5✔
169
            next unless controller_class.method_defined?(action)
5✔
170

171
            [methods].flatten.each do |m|
5✔
172
              public_send(m, "", action: action) if self.respond_to?(m)
5✔
173
            end
174
          end
175
        end
176

177
        if unscoped
5✔
178
          yield if block_given?
×
179
        else
180
          scope(module: name, as: name) do
5✔
181
            yield if block_given?
5✔
182
          end
183
        end
184
      end
185
    end
186

187
    # Route a controller's `#root` to '/' in the current scope/namespace, along with other actions.
188
    def rest_root(name=nil, **kwargs, &block)
1✔
189
      # By default, use RootController#root.
190
      root_action = kwargs.delete(:action) || :root
4✔
191
      controller = kwargs.delete(:controller) || name || :root
4✔
192

193
      # Remove path if name is nil (routing to the root of current namespace).
194
      unless name
4✔
195
        kwargs[:path] = ""
3✔
196
      end
197

198
      return rest_route(controller, route_root_to: root_action, **kwargs) do
4✔
199
        yield if block_given?
4✔
200
      end
201
    end
202
  end
203
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