Skip to content

Commit

Permalink
Merge pull request #114 from TruemarkDev/generator-for-cancancan
Browse files Browse the repository at this point in the history
add generator for the cancancan
  • Loading branch information
abhaynikam authored Jul 30, 2024
2 parents b23ae51 + e7f0e58 commit 44b5fc6
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
* Adds Honeybadger generator. ([@mausamp][])
* Adds Rails ERD generator. ([@mausamp][])
* Adds Annotate Generator. ([@TheZero0-ctrl][])
* Adds CanCanCan generator. ([@TheZero0-ctrl][])

## 0.13.0 (March 26th, 2024)
* Adds Letter Opener generator. ([@coolprobn][])
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ The boring generator introduces following generators:
- Install Honeybadger: `rails generate boring:honeybadger:install`
- Install Rails ERD: `rails generate boring:rails_erd:install`
- Install Annotate: `rails generate boring:annotate:install`
- Install CanCanCan: `rails generate boring:cancancan:install`

## Screencasts

Expand Down
34 changes: 34 additions & 0 deletions lib/generators/boring/cancancan/install/install_generator.rb
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 test/generators/cancancan/cancancan_install_generator_test.rb
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

0 comments on commit 44b5fc6

Please sign in to comment.