Skip to content

Commit

Permalink
feat(recipes/pundit): configure for active admin
Browse files Browse the repository at this point in the history
  • Loading branch information
ldlsegovia committed Sep 20, 2021
1 parent 0e481bc commit 3a7a0dc
Show file tree
Hide file tree
Showing 10 changed files with 114 additions and 19 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ Features
- Include `run_test` as a valid example group [#379](/~https://github.com/platanus/potassium/pull/379)
- Updates ActiveAdmin installation to use webpacker [#350](/~https://github.com/platanus/potassium/pull/350)
- Replaces Active Skin with Arctic Admin [#350](/~https://github.com/platanus/potassium/pull/350)
- Separates Pundit's configuration for Active Admin from Application's configuration

Fixes
- Forces `vue-loader` version to 15, as 16 requires `@vue/compiler-sfc`, which is a part of Vue 3 [#375](/~https://github.com/platanus/potassium/pull/375)
Expand Down
2 changes: 0 additions & 2 deletions lib/potassium/assets/active_admin/admin_user_policy.rb

This file was deleted.

2 changes: 0 additions & 2 deletions lib/potassium/assets/active_admin/comment_policy.rb

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
class BackOffice::AdminUserPolicy < BackOffice::DefaultPolicy
end
2 changes: 2 additions & 0 deletions lib/potassium/assets/active_admin/policies/comment_policy.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
class BackOffice::ActiveAdmin::CommentPolicy < BackOffice::DefaultPolicy
end
49 changes: 49 additions & 0 deletions lib/potassium/assets/active_admin/policies/default_policy.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
class BackOffice::DefaultPolicy
attr_reader :admin_user, :record

def initialize(admin_user, record)
@admin_user = admin_user
@record = record
end

def index?
true
end

def show?
true
end

def create?
true
end

def new?
create?
end

def update?
true
end

def edit?
update?
end

def destroy?
true
end

class Scope
attr_reader :admin_user, :scope

def initialize(admin_user, scope)
@admin_user = admin_user
@scope = scope
end

def resolve
scope.all
end
end
end
2 changes: 2 additions & 0 deletions lib/potassium/assets/active_admin/policies/page_policy.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
class BackOffice::ActiveAdmin::PagePolicy < BackOffice::DefaultPolicy
end
5 changes: 0 additions & 5 deletions lib/potassium/assets/active_admin/pundit_page_policy.rb

This file was deleted.

39 changes: 29 additions & 10 deletions lib/potassium/recipes/pundit.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,20 +31,39 @@ def installed?
end

def install_admin_pundit
configure_active_admin_initializer
copy_policies
end

private

def configure_active_admin_initializer
config_active_admin_option("authorization_adapter", "ActiveAdmin::PunditAdapter")
config_active_admin_option("pundit_default_policy", "'BackOffice::DefaultPolicy'")
config_active_admin_option("pundit_policy_namespace", ":back_office")
end

def copy_policies
copy_policy("default_policy")
copy_policy("admin_user_policy")
copy_policy("page_policy", "active_admin")
copy_policy("comment_policy", "active_admin")
end

def config_active_admin_option(option, value)
initializer = "config/initializers/active_admin.rb"
gsub_file initializer, /# config\.authorization_adapter =[^\n]+\n/ do
"config.authorization_adapter = ActiveAdmin::PunditAdapter\n"
gsub_file initializer, /# config\.#{option} =[^\n]+\n/ do
"config.#{option} = #{value}\n"
end

template "../assets/active_admin/pundit_page_policy.rb",
"app/policies/active_admin/page_policy.rb"
template "../assets/active_admin/comment_policy.rb",
"app/policies/active_admin/comment_policy.rb"
template "../assets/active_admin/admin_user_policy.rb",
"app/policies/admin_user_policy.rb"
end

private
def copy_policy(policy_name, model_namespace = nil)
destination_path = [model_namespace, policy_name].compact.join("/")
template(
"../assets/active_admin/policies/#{policy_name}.rb",
"app/policies/back_office/#{destination_path}.rb"
)
end

def run_pundit_installer
gather_gem 'pundit'
Expand Down
29 changes: 29 additions & 0 deletions spec/features/pundit_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
require "spec_helper"

RSpec.describe "Pundit" do
before :all do
drop_dummy_database
remove_project_directory
create_dummy_project("pundit" => true, "devise" => true, "admin" => true)
end

it "adds the Pundit gem to Gemfile" do
content = IO.read("#{project_path}/Gemfile")

expect(content).to include("gem 'pundit'")
end

it "setup active admin" do
content = IO.read("#{project_path}/config/initializers/active_admin.rb")

expect(content).to include("config.authorization_adapter = ActiveAdmin::PunditAdapter")
expect(content).to include("config.pundit_default_policy = 'BackOffice::DefaultPolicy'")
expect(content).to include("config.pundit_policy_namespace = :back_office")
end

it "creates default policy" do
content = IO.read("#{project_path}/app/policies/back_office/default_policy.rb")

expect(content).to include("class BackOffice::DefaultPolicy")
end
end

0 comments on commit 3a7a0dc

Please sign in to comment.