Skip to content

Commit

Permalink
Also look for controllers outside /app (#37)
Browse files Browse the repository at this point in the history
Hi! Thank you for this gem!

We're using [packs-rails](https://github.com/rubyatscale/packs-rails) in
our rails app, which means we've got controllers outside the `/app`
directory.
If you think it makes sense, could you consider supporting this
use-case? I found this way to be the most minimal change required to
support it.

Thank you!
  • Loading branch information
pineman authored Sep 3, 2023
1 parent dfefde9 commit b3882fb
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 14 deletions.
13 changes: 7 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,13 @@ Chusaku has some flags available for use as well:
```
$ bundle exec chusaku --help
Usage: chusaku [options]
--dry-run Run without file modifications
--exit-with-error-on-annotation
Fail if any file was annotated
--verbose Print all annotations
-v, --version Show Chusaku version number and quit
-h, --help Show this help message and quit
--dry-run Run without file modifications
--exit-with-error-on-annotation Fail if any file was annotated
-c, --controllers-pattern=GLOB Specify alternative controller files glob pattern
--verbose Print all annotations
-v, --version Show Chusaku version number and quit
-h, --help Show this help message and quit
```


Expand Down
2 changes: 1 addition & 1 deletion lib/chusaku.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def call(flags = {})
@routes = Chusaku::Routes.call
@changes = []
@changed_files = []
controllers_pattern = "app/controllers/**/*_controller.rb"
controllers_pattern = @flags[:controllers_pattern] || "app/controllers/**/*_controller.rb"

Dir.glob(Rails.root.join(controllers_pattern)).each do |path|
controller = %r{controllers/(.*)_controller\.rb}.match(path)[1]
Expand Down
12 changes: 12 additions & 0 deletions lib/chusaku/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,10 @@ def check_for_rails_project
def optparser
OptionParser.new do |opts|
opts.banner = "Usage: chusaku [options]"
opts.set_summary_width(35)
add_dry_run_flag(opts)
add_error_on_annotation_flag(opts)
add_controllers_pattern_flag(opts)
add_verbose_flag(opts)
add_version_flag(opts)
add_help_flag(opts)
Expand All @@ -80,6 +82,16 @@ def add_error_on_annotation_flag(opts)
end
end

# Adds `--controllers-pattern` flag.
#
# @param opts [OptionParser] OptionParser instance
# @return [void]
def add_controllers_pattern_flag(opts)
opts.on("-c", "--controllers-pattern", "=GLOB", "Specify alternative controller files glob pattern") do |value|
@options[:controllers_pattern] = value
end
end

# Adds `--verbose` flag.
#
# @param opts [OptionParser] OptionParser instance
Expand Down
11 changes: 11 additions & 0 deletions test/chusaku_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -144,4 +144,15 @@ def test_mock_app_with_no_pending_annotations
assert_empty(File.written_files)
assert_equal("Nothing to annotate.\n", out)
end

def test_mock_app_with_non_matching_controllers_pattern
exit_code = 0

args = {controllers_pattern: "**/nomatch/*_controller.rb"}
out, = capture_io { exit_code = Chusaku.call(args) }

assert_equal(0, exit_code)
assert_empty(File.written_files)
assert_equal("Nothing to annotate.\n", out)
end
end
20 changes: 20 additions & 0 deletions test/cli_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -76,4 +76,24 @@ def test_verbose_flag
assert_equal({verbose: true}, cli.options)
end
end

def test_controllers_pattern_flag
# --controllers-pattern
cli = Chusaku::CLI.new
cli.stub(:check_for_rails_project, nil) do
capture_io do
assert_equal(0, cli.call(["--controllers-pattern=**/controllers/**/*_controller.rb"]))
end
assert_equal({controllers_pattern: "**/controllers/**/*_controller.rb"}, cli.options)
end

# -c
cli = Chusaku::CLI.new
cli.stub(:check_for_rails_project, nil) do
capture_io do
assert_equal(0, cli.call(["-c", "**/controllers/**/*_controller.rb"]))
end
assert_equal({controllers_pattern: "**/controllers/**/*_controller.rb"}, cli.options)
end
end
end
12 changes: 5 additions & 7 deletions test/mock/rails.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
# for each version.
#
# The mocks used should reflect the files located in `test/mock/app/`.

require "pathname"

module Rails
class << self
# Lets us call `Rails.application.routes.routes` without a skeleton Rails
Expand Down Expand Up @@ -116,14 +119,9 @@ def set_route_allowlist(route_allowlist)

# Lets us call `Rails.root` without a skeleton Rails app.
#
# @return [Minitest::Mock] Mocked `Rails.root`
# @return [Pathname] Pathname object like Rails.root
def root
rails_root = Minitest::Mock.new
rails_root.expect \
:join,
"test/mock/app/controllers/**/*_controller.rb",
[String]
rails_root
Pathname.new("test/mock")
end

private
Expand Down

0 comments on commit b3882fb

Please sign in to comment.