chusaku 1.4.0 → 1.4.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 6d38e9cae0023a682add1a559ef4c40e469247f55ef578b67fc190d81b35e824
4
- data.tar.gz: d70d7f1d4aa61451fff49dd99ee1358c375db963db45728f08784827b7f6bfa1
3
+ metadata.gz: f8ab5946d4be43df335d71e2cc2639c8c4f548230f3ba035a947b53d0008c20a
4
+ data.tar.gz: b396e52e72f519352e0bfee5d9bde697bd6047a1ba3a1ff79d25a56331ad523a
5
5
  SHA512:
6
- metadata.gz: b4d2aab917c320e5b821d4e53386b65d5017347e089fe2e62099c94838af14b1824fcde11f2c13c2b97a3af4417764849337a4e577d400d49410ce88e74e34ba
7
- data.tar.gz: 67fc92a8285bc86f0d46ff76da41ec425385d131a088be4f76d18892cfc80aeeea2cc8bbdda3cbc2bb458901788711427162a620d3f18f62c1664795b2bcf32b
6
+ metadata.gz: a586a909a9c01f86555af6e5416d5c20ee3b6baf492c30cde206ec85b26767f9cdc058e08b71fa3ccd660687baa36436a83b545d3533d3ccab2c3156b29a7a0f
7
+ data.tar.gz: 981fd3e8225cf30346e50c7bc0654fc35b77528e82fabf444a3e25f6254c151f49ba96047e02e685337aa27fd69951f551b75fcf147f03937ac8b0f34cf416db
@@ -9,16 +9,40 @@ module Chusaku
9
9
  # {
10
10
  # "users" => {
11
11
  # "edit" => [
12
- # {verb: "GET", path: "/users/:id", name: "edit_user"}
12
+ # {
13
+ # verb: "GET",
14
+ # path: "/users/:id",
15
+ # name: "edit_user",
16
+ # defaults: {},
17
+ # source_path: "/path/to/users_controller.rb"
18
+ # }
13
19
  # ],
14
20
  # "update" => [
15
- # {verb: "PATCH", path: "/users", name: "edit_user"},
16
- # {verb: "PUT", path: "/users", name: "edit_user"}
21
+ # {
22
+ # verb: "PATCH",
23
+ # path: "/users",
24
+ # name: "edit_user",
25
+ # defaults: {},
26
+ # source_path: "/path/to/users_controller.rb"
27
+ # },
28
+ # {
29
+ # verb: "PUT",
30
+ # path: "/users",
31
+ # name: "edit_user",
32
+ # defaults: {},
33
+ # source_path: "/path/to/users_controller.rb"
34
+ # }
17
35
  # ]
18
36
  # },
19
37
  # "empanadas" => {
20
38
  # "create" => [
21
- # {verb: "POST", path: "/empanadas", name: nil}
39
+ # {
40
+ # verb: "POST",
41
+ # path: "/empanadas",
42
+ # name: nil,
43
+ # defaults: {},
44
+ # source_path: "/path/to/empanadas_controller.rb"
45
+ # }
22
46
  # ]
23
47
  # }
24
48
  # }
@@ -33,6 +57,12 @@ module Chusaku
33
57
 
34
58
  private
35
59
 
60
+ # Recursively populate the routes hash with information from the given Rails
61
+ # application. Accounts for Rails engines.
62
+ #
63
+ # @param app [Rails::Application] Result of `Rails.application`
64
+ # @param routes [Hash] Collection of all route info
65
+ # @return [void]
36
66
  def populate_routes(app, routes)
37
67
  app.routes.routes.each do |route|
38
68
  if route.app.engine?
@@ -40,7 +70,7 @@ module Chusaku
40
70
  next
41
71
  end
42
72
 
43
- controller, action, defaults = extract_data_from(route)
73
+ controller, action, defaults, source_path = extract_data_from(route)
44
74
  routes[controller] ||= {}
45
75
  routes[controller][action] ||= []
46
76
 
@@ -49,7 +79,8 @@ module Chusaku
49
79
  routes: routes,
50
80
  controller: controller,
51
81
  action: action,
52
- defaults: defaults
82
+ defaults: defaults,
83
+ source_path: source_path
53
84
  end
54
85
  end
55
86
 
@@ -60,11 +91,17 @@ module Chusaku
60
91
  # @param controller [String] Controller key
61
92
  # @param action [String] Action key
62
93
  # @param defaults [Hash] Default parameters for route
94
+ # @param source_path [String] Path to controller file
63
95
  # @return [void]
64
- def add_info_for(route:, routes:, controller:, action:, defaults:)
96
+ def add_info_for(route:, routes:, controller:, action:, defaults:, source_path:)
65
97
  verbs_for(route).each do |verb|
66
- routes[controller][action]
67
- .push(format(route: route, verb: verb, defaults: defaults))
98
+ routes[controller][action].push \
99
+ format(
100
+ route: route,
101
+ verb: verb,
102
+ defaults: defaults,
103
+ source_path: source_path
104
+ )
68
105
  routes[controller][action].uniq!
69
106
  end
70
107
  end
@@ -88,13 +125,15 @@ module Chusaku
88
125
  # @param route [ActionDispatch::Journey::Route] Route given by Rails
89
126
  # @param verb [String] HTTP verb
90
127
  # @param defaults [Hash] Default parameters for route
128
+ # @param source_path [String] Path to controller file
91
129
  # @return [Hash] { verb => String, path => String, name => String }
92
- def format(route:, verb:, defaults:)
130
+ def format(route:, verb:, defaults:, source_path:)
93
131
  {
94
132
  verb: verb,
95
133
  path: route.path.spec.to_s.gsub("(.:format)", ""),
96
134
  name: route.name,
97
- defaults: defaults
135
+ defaults: defaults,
136
+ source_path: source_path
98
137
  }
99
138
  end
100
139
 
@@ -143,16 +182,26 @@ module Chusaku
143
182
  routes
144
183
  end
145
184
 
146
- # Given a route, extract the controller and action strings.
185
+ # Given a route, extract the controller & action strings as well as defaults
186
+ # hash and source path.
147
187
  #
148
188
  # @param route [ActionDispatch::Journey::Route] Route instance
149
- # @return [Array<Object>] (String, String, Hash)
189
+ # @return [Array<Object>] (String, String, Hash, String)
150
190
  def extract_data_from(route)
151
191
  defaults = route.defaults.dup
152
192
  controller = defaults.delete(:controller)
153
193
  action = defaults.delete(:action)
154
194
 
155
- [controller, action, defaults]
195
+ controller_class = controller ? "#{controller.underscore.camelize}Controller".constantize : nil
196
+ action_method_name = action&.to_sym
197
+ source_path =
198
+ if !action_method_name.nil? && controller_class&.method_defined?(action_method_name)
199
+ controller_class.instance_method(action_method_name).source_location&.[](0)
200
+ else
201
+ ""
202
+ end
203
+
204
+ [controller, action, defaults, source_path]
156
205
  end
157
206
  end
158
207
  end
@@ -1,3 +1,3 @@
1
1
  module Chusaku
2
- VERSION = "1.4.0"
2
+ VERSION = "1.4.1"
3
3
  end
data/lib/chusaku.rb CHANGED
@@ -29,14 +29,7 @@ module Chusaku
29
29
  .new(Rails.root.join(controllers_pattern))
30
30
  .exclude(Rails.root.join(exclusion_pattern))
31
31
 
32
- @routes.each do |controller, actions|
33
- next unless controller
34
-
35
- controller_class = "#{controller.underscore.camelize}Controller".constantize
36
- action_method_name = actions.keys.first&.to_sym
37
- next unless !action_method_name.nil? && controller_class.method_defined?(action_method_name)
38
-
39
- source_path = controller_class.instance_method(action_method_name).source_location&.[](0)
32
+ source_paths_map.each do |source_path, actions|
40
33
  next unless controllers_paths.include?(source_path)
41
34
 
42
35
  annotate_file(path: source_path, actions: actions)
@@ -56,6 +49,34 @@ module Chusaku
56
49
 
57
50
  private
58
51
 
52
+ # Maps source paths to their respective routes.
53
+ #
54
+ # Example output:
55
+ #
56
+ # {
57
+ # "/path/to/users_controller.rb" => {
58
+ # "edit" => [...],
59
+ # "update" => [...]
60
+ # }
61
+ # }
62
+ #
63
+ # @return [Hash] Source paths mapped to their respective routes
64
+ def source_paths_map
65
+ map = {}
66
+
67
+ @routes.each do |controller, actions|
68
+ actions.each do |action, data|
69
+ data.each do |datum|
70
+ map[datum[:source_path]] ||= {}
71
+ map[datum[:source_path]][action] ||= []
72
+ map[datum[:source_path]][action].push(datum)
73
+ end
74
+ end
75
+ end
76
+
77
+ map
78
+ end
79
+
59
80
  # Adds annotations to the given file.
60
81
  #
61
82
  # @param path [String] Path to file
@@ -110,8 +131,9 @@ module Chusaku
110
131
  # @param path [String] Rails path for route
111
132
  # @param name [String] Name used in route helpers
112
133
  # @param defaults [Hash] Default parameters for route
134
+ # @param source_path [String] Path to controller file
113
135
  # @return [String] "@route <verb> <path> {<defaults>} (<name>)"
114
- def annotate_route(verb:, path:, name:, defaults:)
136
+ def annotate_route(verb:, path:, name:, defaults:, source_path:)
115
137
  annotation = "@route #{verb} #{path}"
116
138
  if defaults&.any?
117
139
  defaults_str =
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: chusaku
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.4.0
4
+ version: 1.4.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nishiki Liu
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2024-10-08 00:00:00.000000000 Z
11
+ date: 2024-11-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -129,7 +129,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
129
129
  - !ruby/object:Gem::Version
130
130
  version: '0'
131
131
  requirements: []
132
- rubygems_version: 3.5.17
132
+ rubygems_version: 3.5.23
133
133
  signing_key:
134
134
  specification_version: 4
135
135
  summary: Annotate your Rails controllers with route info.