chusaku 1.2.0 → 1.3.0

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: f73db691fb40f399f999f495736dca02bd8c372934aa4122824d086a723d0479
4
- data.tar.gz: dea436d47f3101f5d3112c3722b04c00b317fc32c7ad3ee2c2594ca12fbe3c01
3
+ metadata.gz: 48034ba3796c8cd1ab1a16b0db1065dcdf487ea5e6bc1b08a7fcd18db4ad1d33
4
+ data.tar.gz: 65cf7dbbf5571f547aeb9cefc8cd804ccab9467b0765edcc00691c777c158554
5
5
  SHA512:
6
- metadata.gz: 03bff26c3f0a2de5ed0b4c64feb3c2a198c4704f53f3683d5bbcf0699f2c36081672324ea871ed664836d0a6d09f9314b1b4c23d886885597c7e9f03bcb13821
7
- data.tar.gz: 8d4def4efa795ac278608c7d9c8f19dd361e361b4e1ff4da7a9434fa85d865acb3de28e183d67f74bf244023220ad84464735da275516a236576c29dde9d9d64
6
+ metadata.gz: 83c00966e23c6d8a63f9b68e35cc48a975689f21473e83344927764ac200d9eb378274c6ad82fa90a0ac6a06d286763f45661f4b2433983f903cf93371158167
7
+ data.tar.gz: 70afe36985465e34f3fa18e5eea3d51d130daaada6414a51b680a0e4f32f31e14a3e3225f8f70b594d19d8c23e9ad3b00ac9f43c4dd83c493bdd5befba483e8a
data/.gitignore CHANGED
@@ -7,5 +7,7 @@
7
7
  /spec/reports/
8
8
  /tmp/
9
9
 
10
+ .DS_Store
11
+
10
12
  .ruby-version
11
13
  Gemfile.lock
data/lib/chusaku/cli.rb CHANGED
@@ -41,7 +41,8 @@ module Chusaku
41
41
  # @raise [Chusaku::CLI::NotARailsProject] Exception if not Rails project
42
42
  # @return [void]
43
43
  def check_for_rails_project
44
- has_controllers = File.directory?("./app/controllers")
44
+ controllers_pattern = options[:controllers_pattern] || DEFAULT_CONTROLLERS_PATTERN
45
+ has_controllers = !Dir.glob(Rails.root.join(controllers_pattern)).empty?
45
46
  has_rakefile = File.exist?("./Rakefile")
46
47
  raise NotARailsProject unless has_controllers && has_rakefile
47
48
  end
@@ -27,7 +27,19 @@ module Chusaku
27
27
  def call
28
28
  routes = {}
29
29
 
30
- Rails.application.routes.routes.each do |route|
30
+ populate_routes(Rails.application, routes)
31
+ backfill_routes(routes)
32
+ end
33
+
34
+ private
35
+
36
+ def populate_routes(app, routes)
37
+ app.routes.routes.each do |route|
38
+ if route.app.engine?
39
+ populate_routes(route.app.app, routes)
40
+ next
41
+ end
42
+
31
43
  controller, action, defaults = extract_data_from(route)
32
44
  routes[controller] ||= {}
33
45
  routes[controller][action] ||= []
@@ -39,12 +51,8 @@ module Chusaku
39
51
  action: action,
40
52
  defaults: defaults
41
53
  end
42
-
43
- backfill_routes(routes)
44
54
  end
45
55
 
46
- private
47
-
48
56
  # Adds formatted route info for the given param combination.
49
57
  #
50
58
  # @param route [Hash] Route info
@@ -1,3 +1,3 @@
1
1
  module Chusaku
2
- VERSION = "1.2.0"
2
+ VERSION = "1.3.0"
3
3
  end
data/lib/chusaku.rb CHANGED
@@ -4,6 +4,8 @@ require "chusaku/routes"
4
4
 
5
5
  # Handles core functionality of annotating projects.
6
6
  module Chusaku
7
+ DEFAULT_CONTROLLERS_PATTERN = "**/*_controller.rb".freeze
8
+
7
9
  class << self
8
10
  # The main method to run Chusaku. Annotate all actions in a Rails project as
9
11
  # follows:
@@ -19,14 +21,20 @@ module Chusaku
19
21
  @flags = flags
20
22
  @routes = Chusaku::Routes.call
21
23
  @changed_files = []
22
- controllers_pattern = @flags[:controllers_pattern] || "app/controllers/**/*_controller.rb"
24
+ controllers_pattern = @flags[:controllers_pattern] || DEFAULT_CONTROLLERS_PATTERN
25
+ controllers_paths = Dir.glob(Rails.root.join(controllers_pattern))
26
+
27
+ @routes.each do |controller, actions|
28
+ next unless controller
29
+
30
+ controller_class = "#{controller.underscore.camelize}Controller".constantize
31
+ action_method_name = actions.keys.first&.to_sym
32
+ next unless !action_method_name.nil? && controller_class.method_defined?(action_method_name)
23
33
 
24
- Dir.glob(Rails.root.join(controllers_pattern)).each do |path|
25
- controller = %r{controllers/(.*)_controller\.rb}.match(path)[1]
26
- actions = @routes[controller]
27
- next if actions.nil?
34
+ source_path = controller_class.instance_method(action_method_name).source_location&.[](0)
35
+ next unless controllers_paths.include?(source_path)
28
36
 
29
- annotate_file(path: path, controller: controller, actions: actions.keys)
37
+ annotate_file(path: source_path, actions: actions)
30
38
  end
31
39
 
32
40
  output_results
@@ -46,16 +54,15 @@ module Chusaku
46
54
  # Adds annotations to the given file.
47
55
  #
48
56
  # @param path [String] Path to file
49
- # @param controller [String] Controller name
50
- # @param actions [Array<String>] List of valid actions for the controller
57
+ # @param actions [Hash<String, Hash>] List of valid action data for the controller
51
58
  # @return [void]
52
- def annotate_file(path:, controller:, actions:)
53
- parsed_file = Chusaku::Parser.call(path: path, actions: actions)
59
+ def annotate_file(path:, actions:)
60
+ parsed_file = Chusaku::Parser.call(path: path, actions: actions.keys)
54
61
  parsed_file[:groups].each_cons(2) do |prev, curr|
55
62
  clean_group(prev)
56
63
  next unless curr[:type] == :action
57
64
 
58
- route_data = @routes[controller][curr[:action]]
65
+ route_data = actions[curr[:action]]
59
66
  next unless route_data.any?
60
67
 
61
68
  annotate_group(group: curr, route_data: route_data)
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.2.0
4
+ version: 1.3.0
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-01-06 00:00:00.000000000 Z
11
+ date: 2024-08-05 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.1
132
+ rubygems_version: 3.5.17
133
133
  signing_key:
134
134
  specification_version: 4
135
135
  summary: Annotate your Rails controllers with route info.