-
-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #114 from TruemarkDev/generator-for-cancancan
add generator for the cancancan
- Loading branch information
Showing
4 changed files
with
70 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
lib/generators/boring/cancancan/install/install_generator.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'boring_generators/generator_helper' | ||
|
||
module Boring | ||
module Cancancan | ||
class InstallGenerator < Rails::Generators::Base | ||
include BoringGenerators::GeneratorHelper | ||
|
||
desc "Adds cancancan gem to the application" | ||
|
||
class_option :skip_config, | ||
type: :boolean, | ||
default: false, | ||
desc: "Skip adding cancancan configuration. Default to false" | ||
|
||
def add_cancancan_gem | ||
say "Adding cancancan gem", :green | ||
check_and_install_gem("cancancan") | ||
bundle_install | ||
end | ||
|
||
def configure_cancancan | ||
return if options[:skip_config] | ||
|
||
say "Configuring cancancan", :green | ||
|
||
Bundler.with_unbundled_env do | ||
run "bundle exec rails g cancan:ability" | ||
end | ||
end | ||
end | ||
end | ||
end |
34 changes: 34 additions & 0 deletions
34
test/generators/cancancan/cancancan_install_generator_test.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
# frozen_string_literal: true | ||
|
||
require "test_helper" | ||
require "generators/boring/cancancan/install/install_generator" | ||
|
||
class CanCanCanInstallGeneratorTest < Rails::Generators::TestCase | ||
tests Boring::Cancancan::InstallGenerator | ||
setup :build_app | ||
teardown :teardown_app | ||
|
||
include GeneratorHelper | ||
include ActiveSupport::Testing::Isolation | ||
|
||
def destination_root | ||
app_path | ||
end | ||
|
||
def test_should_configure_cancancan | ||
Dir.chdir(app_path) do | ||
quietly { run_generator } | ||
|
||
assert_gem "cancancan" | ||
assert_file "app/models/ability.rb" | ||
end | ||
end | ||
|
||
def test_should_skip_cancancan_configuration | ||
Dir.chdir(app_path) do | ||
quietly { run_generator %w[--skip-config] } | ||
|
||
assert_no_file "app/models/ability.rb" | ||
end | ||
end | ||
end |