active_admin_role 0.1.1 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +5 -5
- data/.gitignore +1 -0
- data/.rubocop.yml +16 -13
- data/.rubocop_todo.yml +23 -0
- data/.travis.yml +12 -3
- data/Appraisals +8 -9
- data/Gemfile +13 -10
- data/README.md +8 -4
- data/Rakefile +32 -3
- data/active_admin_role.gemspec +4 -3
- data/app/models/active_admin/manageable_resource.rb +51 -0
- data/app/models/active_admin/managed_resource.rb +5 -42
- data/app/models/active_admin/permission.rb +12 -17
- data/app/models/active_admin/permission_reloader.rb +47 -0
- data/app/models/active_admin_role/can_can/ability.rb +17 -0
- data/app/models/active_admin_role/dsl.rb +7 -0
- data/app/models/active_admin_role/role_based_authorizable.rb +43 -0
- data/gemfiles/{rails42.gemfile → 5.0.gemfile} +11 -11
- data/gemfiles/5.1.gemfile +26 -0
- data/gemfiles/5.2.gemfile +26 -0
- data/lib/active_admin_role.rb +3 -10
- data/lib/active_admin_role/active_admin/dsl.rb +1 -2
- data/lib/active_admin_role/active_admin/resource_controller.rb +11 -11
- data/lib/active_admin_role/config.rb +3 -2
- data/lib/active_admin_role/engine.rb +5 -2
- data/lib/active_admin_role/version.rb +1 -1
- data/lib/generators/active_admin_role/helper.rb +41 -41
- data/lib/generators/active_admin_role/install_generator.rb +16 -13
- data/lib/generators/active_admin_role/templates/admin/{permission.rb → permissions.tt} +1 -1
- data/lib/generators/active_admin_role/templates/{initializer.rb → initializer.tt} +0 -0
- data/lib/generators/active_admin_role/templates/migration/{add_role_to_admin_users.rb → add_role_to_admin_users.tt} +0 -0
- data/lib/generators/active_admin_role/templates/migration/{create_active_admin_managed_resources.rb → create_active_admin_managed_resources.tt} +0 -0
- data/lib/generators/active_admin_role/templates/migration/{create_active_admin_permissions.rb → create_active_admin_permissions.tt} +0 -0
- data/lib/generators/active_admin_role/templates/model/{ability.rb → ability.tt} +0 -0
- metadata +35 -21
- data/.ruby-style.yml +0 -242
- data/gemfiles/.bundle/config +0 -2
- data/gemfiles/rails50.gemfile +0 -27
- data/lib/active_admin_role/can_can/ability.rb +0 -15
- data/lib/active_admin_role/manageable_resource.rb +0 -45
- data/lib/active_admin_role/model.rb +0 -7
- data/lib/active_admin_role/role_based_authorizable.rb +0 -45
- data/tasks/test.rake +0 -10
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 3ea9f11c0722efc41a64baff3ca74086dd34d878a482cc00a95ebe3d40e3523a
|
4
|
+
data.tar.gz: dbb330580027c71ba91d1b0f6a50d2adf69f79280f9b2ee77a61f64b4426f0aa
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 12f29a42a12e324e635e99c804cdc51f3f877e36592a34e64d82251ba2a69c82250805a4ae2194910d87fcd725e9a2d07093651adfec3bfff0896262bec795c9
|
7
|
+
data.tar.gz: efa55e48d456fecd90b82139b1d13ef7de91413f7087c543a3f40a42a4139d8e2c1829a31382e9a7b590b7b1f2752cec783156a34eadc3d0946335df2a6eb83e
|
data/.gitignore
CHANGED
data/.rubocop.yml
CHANGED
@@ -1,17 +1,20 @@
|
|
1
|
-
inherit_from:
|
2
|
-
|
1
|
+
inherit_from: .rubocop_todo.yml
|
2
|
+
|
3
|
+
inherit_gem:
|
4
|
+
onkcop:
|
5
|
+
- "config/rubocop.yml"
|
6
|
+
# uncomment if use rails cops
|
7
|
+
# - "config/rails.yml"
|
8
|
+
# uncomment if use rspec cops
|
9
|
+
# - "config/rspec.yml"
|
3
10
|
|
4
11
|
AllCops:
|
12
|
+
TargetRubyVersion: 2.2
|
13
|
+
# uncomment if use rails cops
|
14
|
+
# TargetRailsVersion: 5.1
|
5
15
|
Exclude:
|
6
|
-
- "bin/*"
|
7
|
-
- "vendor/**/*"
|
8
|
-
- "gemfiles/**/*"
|
9
16
|
- "spec/rails/**/*"
|
10
|
-
- "
|
11
|
-
-
|
12
|
-
-
|
13
|
-
|
14
|
-
TargetRubyVersion: 2.1
|
15
|
-
|
16
|
-
Metrics/LineLength:
|
17
|
-
Enabled: false
|
17
|
+
- "vendor/bundle/**/*"
|
18
|
+
- "gemfiles/vendor/**/*"
|
19
|
+
- "gemfiles/.bundle/**/*"
|
20
|
+
- "tmp/**/*"
|
data/.rubocop_todo.yml
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
# This configuration was generated by
|
2
|
+
# `rubocop --auto-gen-config`
|
3
|
+
# on 2018-04-08 11:32:33 +0900 using RuboCop version 0.53.0.
|
4
|
+
# The point is for the user to remove these configuration records
|
5
|
+
# one by one as the offenses are removed from the code base.
|
6
|
+
# Note that changes in the inspected code, or installation of new
|
7
|
+
# versions of RuboCop, may require this file to be generated again.
|
8
|
+
|
9
|
+
# Offense count: 2
|
10
|
+
# Configuration parameters: CheckForMethodsWithNoSideEffects.
|
11
|
+
Lint/Void:
|
12
|
+
Exclude:
|
13
|
+
- 'spec/features/integration_spec.rb'
|
14
|
+
|
15
|
+
# Offense count: 1
|
16
|
+
Metrics/AbcSize:
|
17
|
+
Max: 28
|
18
|
+
|
19
|
+
# Offense count: 2
|
20
|
+
Naming/MemoizedInstanceVariableName:
|
21
|
+
Exclude:
|
22
|
+
- 'app/models/active_admin/manageable_resource.rb'
|
23
|
+
- 'app/models/concerns/active_admin_role/role_based_authorizable.rb'
|
data/.travis.yml
CHANGED
@@ -1,5 +1,14 @@
|
|
1
|
-
sudo: false
|
2
1
|
language: ruby
|
2
|
+
before_install:
|
3
|
+
- gem update --system
|
4
|
+
- gem install bundler
|
5
|
+
gemfiles:
|
6
|
+
- gemfiles/5.0.gemfile
|
7
|
+
- gemfiles/5.1.gemfile
|
8
|
+
- gemfiles/5.2.gemfile
|
3
9
|
rvm:
|
4
|
-
- 2.
|
5
|
-
|
10
|
+
- 2.3.7
|
11
|
+
- 2.4.4
|
12
|
+
- 2.5.1
|
13
|
+
script:
|
14
|
+
- bundle exec rspec
|
data/Appraisals
CHANGED
@@ -1,12 +1,11 @@
|
|
1
|
-
appraise
|
2
|
-
gem
|
3
|
-
gem 'devise', '~> 4.2.0'
|
4
|
-
gem 'activeadmin', '1.0.0.pre4'
|
1
|
+
appraise "5.0" do
|
2
|
+
gem "rails", "~> 5.0.0"
|
5
3
|
end
|
6
4
|
|
7
|
-
appraise
|
8
|
-
gem
|
9
|
-
|
10
|
-
|
11
|
-
|
5
|
+
appraise "5.1" do
|
6
|
+
gem "rails", "~> 5.1.0"
|
7
|
+
end
|
8
|
+
|
9
|
+
appraise "5.2" do
|
10
|
+
gem "rails", "~> 5.2.0.rc2"
|
12
11
|
end
|
data/Gemfile
CHANGED
@@ -1,23 +1,26 @@
|
|
1
|
-
source
|
1
|
+
source "https://rubygems.org"
|
2
|
+
|
3
|
+
git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
|
2
4
|
|
3
5
|
gemspec
|
4
6
|
|
5
|
-
gem "rails", "~> 4.2.0"
|
6
|
-
gem "activeadmin", "1.0.0.pre4"
|
7
|
-
gem "devise", "~> 4.2.0"
|
8
|
-
gem "pry"
|
9
7
|
gem "appraisal"
|
8
|
+
gem "devise"
|
9
|
+
gem "jquery-ui-rails", "~> 4.2.1"
|
10
|
+
gem "rails", ">= 5.0.0"
|
11
|
+
|
12
|
+
group :development, :test do
|
13
|
+
gem "sqlite3", platforms: :mri
|
14
|
+
end
|
10
15
|
|
11
16
|
group :development do
|
12
|
-
gem "
|
13
|
-
gem "rubocop", "~> 0.40.0"
|
17
|
+
gem "onkcop", require: false
|
14
18
|
end
|
15
19
|
|
16
20
|
group :test do
|
17
21
|
gem "capybara"
|
18
|
-
gem "rspec-rails"
|
19
22
|
gem "database_cleaner"
|
20
|
-
gem "shoulda-matchers"
|
21
|
-
gem "sqlite3", platforms: :mri
|
22
23
|
gem "poltergeist"
|
24
|
+
gem "rspec-rails"
|
25
|
+
gem "shoulda-matchers"
|
23
26
|
end
|
data/README.md
CHANGED
@@ -14,10 +14,14 @@ And run `bundle`
|
|
14
14
|
|
15
15
|
## Dependencies
|
16
16
|
|
17
|
-
- rails (>=
|
18
|
-
- activeadmin (>= 1.
|
17
|
+
- rails (>= 5.0.0)
|
18
|
+
- activeadmin (>= 1.2.0)
|
19
19
|
- cancancan (>= 1.15.0)
|
20
20
|
|
21
|
+
## Sample application
|
22
|
+
|
23
|
+
https://github.com/yhirano55-playground/active_admin_role_sample
|
24
|
+
|
21
25
|
## Usage
|
22
26
|
|
23
27
|
1. Run this command after `rails generate active_admin:install`:
|
@@ -31,8 +35,8 @@ $ bin/rails generate active_admin_role:install
|
|
31
35
|
create db/migrate/20161128090643_create_active_admin_permissions.rb
|
32
36
|
create app/models/ability.rb
|
33
37
|
gsub config/initializers/active_admin.rb
|
34
|
-
create app/admin/
|
35
|
-
insert app/admin/
|
38
|
+
create app/admin/permissions.rb
|
39
|
+
insert app/admin/admin_users.rb
|
36
40
|
|
37
41
|
$ bin/rails db:migrate
|
38
42
|
```
|
data/Rakefile
CHANGED
@@ -1,7 +1,36 @@
|
|
1
|
-
require
|
2
|
-
require
|
1
|
+
require "bundler"
|
2
|
+
require "rake"
|
3
3
|
|
4
4
|
Bundler.setup
|
5
5
|
Bundler::GemHelper.install_tasks
|
6
6
|
|
7
|
-
|
7
|
+
require "rspec/core/rake_task"
|
8
|
+
RSpec::Core::RakeTask.new(:spec)
|
9
|
+
task default: :spec
|
10
|
+
|
11
|
+
desc "Creates a test rails app for the specs to run against"
|
12
|
+
task :setup do
|
13
|
+
require "rails/version"
|
14
|
+
system "bundle exec rails --version"
|
15
|
+
system <<-COMMAND
|
16
|
+
bundle exec rails new tmp/rails-#{Rails::VERSION::STRING} \
|
17
|
+
-m spec/support/rails_template.rb \
|
18
|
+
--skip-spring \
|
19
|
+
--skip-listen \
|
20
|
+
--skip-turbolinks \
|
21
|
+
--skip-bootsnap \
|
22
|
+
--skip-test \
|
23
|
+
--skip-git \
|
24
|
+
--skip-yarn \
|
25
|
+
--skip-puma \
|
26
|
+
--skip-action-mailer \
|
27
|
+
--skip-action-cable
|
28
|
+
COMMAND
|
29
|
+
end
|
30
|
+
|
31
|
+
namespace :tmp do
|
32
|
+
task :clear do
|
33
|
+
require "fileutils"
|
34
|
+
FileUtils.rm_r("tmp")
|
35
|
+
end
|
36
|
+
end
|
data/active_admin_role.gemspec
CHANGED
@@ -11,11 +11,12 @@ Gem::Specification.new do |gem|
|
|
11
11
|
gem.homepage = "https://github.com/yhirano55/active_admin_role"
|
12
12
|
gem.license = "MIT"
|
13
13
|
|
14
|
-
gem.files = `git ls-files -z`.split("\x0").reject {
|
14
|
+
gem.files = `git ls-files -z`.split("\x0").reject {|f| f.match(%r{^(test|spec|features)/}) }
|
15
15
|
gem.require_paths = ["lib"]
|
16
16
|
|
17
|
-
gem.required_ruby_version = ">= 2.
|
17
|
+
gem.required_ruby_version = ">= 2.2.2"
|
18
18
|
|
19
|
-
gem.add_dependency "activeadmin", ">= 1.
|
19
|
+
gem.add_dependency "activeadmin", ">= 1.2.0"
|
20
20
|
gem.add_dependency "cancancan", ">= 1.15.0"
|
21
|
+
gem.add_dependency "railties", ">= 5.0.0"
|
21
22
|
end
|
@@ -0,0 +1,51 @@
|
|
1
|
+
require "set"
|
2
|
+
|
3
|
+
module ActiveAdmin
|
4
|
+
class ManageableResource
|
5
|
+
class << self
|
6
|
+
def call
|
7
|
+
new.call
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
def call
|
12
|
+
namespace = ::ActiveAdmin.application.default_namespace
|
13
|
+
::ActiveAdmin.application.namespaces[namespace].resources.inject([]) do |result, resource|
|
14
|
+
class_name = resource.controller.resource_class.to_s
|
15
|
+
name = resource.resource_name.name
|
16
|
+
actions = collect_defined_actions(resource)
|
17
|
+
|
18
|
+
result += eval_actions(actions).map do |action|
|
19
|
+
{ class_name: class_name, name: name, action: action }
|
20
|
+
end
|
21
|
+
|
22
|
+
result
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
private
|
27
|
+
|
28
|
+
def collect_defined_actions(resource)
|
29
|
+
if resource.respond_to?(:defined_actions)
|
30
|
+
defined_actions = resource.defined_actions
|
31
|
+
member_actions = resource.member_actions.map(&:name)
|
32
|
+
collection_actions = resource.collection_actions.map(&:name)
|
33
|
+
batch_actions = resource.batch_actions_enabled? ? [:batch_action] : []
|
34
|
+
|
35
|
+
defined_actions | member_actions | member_actions | collection_actions | batch_actions
|
36
|
+
else
|
37
|
+
resource.page_actions.map(&:name) | [:index]
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
def eval_actions(actions)
|
42
|
+
actions.inject(Set.new) do |result, action|
|
43
|
+
result << (actions_dictionary[action] || action).to_s
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
def actions_dictionary
|
48
|
+
@_actions_dictionary ||= ::ActiveAdmin::BaseController::Authorization::ACTIONS_DICTIONARY.dup
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
@@ -1,16 +1,13 @@
|
|
1
1
|
module ActiveAdmin
|
2
2
|
class ManagedResource < ActiveRecord::Base
|
3
|
-
self.table_name =
|
3
|
+
self.table_name = "active_admin_managed_resources"
|
4
4
|
|
5
5
|
has_many :permissions, dependent: :destroy
|
6
|
-
|
7
|
-
|
8
|
-
validates :class_name
|
9
|
-
validates :action
|
10
|
-
end
|
6
|
+
validates :class_name, presence: true
|
7
|
+
validates :action, presence: true
|
11
8
|
|
12
9
|
def const
|
13
|
-
@
|
10
|
+
@const ||= class_name.try(:safe_constantize)
|
14
11
|
end
|
15
12
|
|
16
13
|
def active?
|
@@ -23,41 +20,7 @@ module ActiveAdmin
|
|
23
20
|
|
24
21
|
class << self
|
25
22
|
def reload
|
26
|
-
|
27
|
-
clear_cache
|
28
|
-
update_managed_resources
|
29
|
-
cleanup_managed_resources
|
30
|
-
update_permissions
|
31
|
-
end
|
32
|
-
end
|
33
|
-
|
34
|
-
private
|
35
|
-
|
36
|
-
def update_managed_resources
|
37
|
-
manageable_resources.each(&method(:find_or_create_by!))
|
38
|
-
end
|
39
|
-
|
40
|
-
def cleanup_managed_resources
|
41
|
-
(persisted_resources - manageable_resources).each do |condition|
|
42
|
-
where(condition).destroy_all
|
43
|
-
end
|
44
|
-
end
|
45
|
-
|
46
|
-
def update_permissions
|
47
|
-
::ActiveAdmin::Permission.clear_cache
|
48
|
-
::ActiveAdmin::Permission.update_all_from_managed_resources(all)
|
49
|
-
end
|
50
|
-
|
51
|
-
def persisted_resources
|
52
|
-
all.map(&:attributes).map { |attribute| attribute.slice(*%w(class_name action name)).symbolize_keys }
|
53
|
-
end
|
54
|
-
|
55
|
-
def manageable_resources
|
56
|
-
@_manageable_resources ||= ::ActiveAdminRole::ManageableResource.new.call
|
57
|
-
end
|
58
|
-
|
59
|
-
def clear_cache
|
60
|
-
@_manageable_resources = nil
|
23
|
+
ActiveAdmin::PermissionReloader.reload
|
61
24
|
end
|
62
25
|
end
|
63
26
|
end
|
@@ -1,26 +1,21 @@
|
|
1
1
|
module ActiveAdmin
|
2
2
|
class Permission < ActiveRecord::Base
|
3
3
|
self.table_name = :active_admin_permissions
|
4
|
-
|
5
4
|
role_based_authorizable
|
6
5
|
|
7
6
|
enum state: { cannot: 0, can: 1 }
|
8
7
|
|
9
8
|
belongs_to :managed_resource
|
10
|
-
|
11
9
|
delegate :class_name, :action, :name, :const, :active?, :for_active_admin_page?, to: :managed_resource
|
12
|
-
delegate :clear_cache, to: :class
|
13
10
|
|
14
|
-
|
11
|
+
validates :role, presence: true
|
12
|
+
validates :state, presence: true
|
13
|
+
validates :managed_resource_id, uniqueness: { scope: :role }
|
15
14
|
|
16
|
-
|
17
|
-
|
18
|
-
validates :role
|
19
|
-
validates :state
|
15
|
+
after_update do
|
16
|
+
self.class.clear_cache
|
20
17
|
end
|
21
18
|
|
22
|
-
validates :managed_resource_id, uniqueness: { scope: [:role] }
|
23
|
-
|
24
19
|
def to_condition
|
25
20
|
[].tap do |cond|
|
26
21
|
cond << state
|
@@ -31,8 +26,8 @@ module ActiveAdmin
|
|
31
26
|
end
|
32
27
|
|
33
28
|
class << self
|
34
|
-
def update_all_from_managed_resources
|
35
|
-
|
29
|
+
def update_all_from_managed_resources
|
30
|
+
::ActiveAdmin::ManagedResource.all.find_each do |managed_resource|
|
36
31
|
manageable_roles.values.each do |value_of_role|
|
37
32
|
find_or_create_by!(managed_resource_id: managed_resource.id, role: value_of_role) do |permission|
|
38
33
|
permission.state = default_state
|
@@ -42,18 +37,18 @@ module ActiveAdmin
|
|
42
37
|
end
|
43
38
|
|
44
39
|
def indexed_cache
|
45
|
-
@
|
40
|
+
@indexed_cache ||= eager_load(:managed_resource).all.group_by(&:role)
|
46
41
|
end
|
47
42
|
|
48
43
|
def clear_cache
|
49
|
-
@
|
44
|
+
@indexed_cache = nil
|
50
45
|
end
|
51
46
|
|
52
47
|
private
|
53
48
|
|
54
|
-
|
55
|
-
|
56
|
-
|
49
|
+
def default_state
|
50
|
+
@default_state ||= ::ActiveAdminRole.config.default_state
|
51
|
+
end
|
57
52
|
end
|
58
53
|
end
|
59
54
|
end
|
@@ -0,0 +1,47 @@
|
|
1
|
+
module ActiveAdmin
|
2
|
+
module PermissionReloader
|
3
|
+
class << self
|
4
|
+
def reload
|
5
|
+
ActiveRecord::Base.transaction do
|
6
|
+
clear_cache
|
7
|
+
update_managed_resources
|
8
|
+
cleanup_managed_resources
|
9
|
+
update_permissions
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
private
|
14
|
+
|
15
|
+
def clear_cache
|
16
|
+
@manageable_resources = nil
|
17
|
+
end
|
18
|
+
|
19
|
+
def update_managed_resources
|
20
|
+
manageable_resources.each do |manageable_resource|
|
21
|
+
::ActiveAdmin::ManagedResource.find_or_create_by!(manageable_resource)
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
def cleanup_managed_resources
|
26
|
+
(persisted_resources - manageable_resources).each do |condition|
|
27
|
+
::ActiveAdmin::ManagedResource.where(condition).destroy_all
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
def update_permissions
|
32
|
+
::ActiveAdmin::Permission.clear_cache
|
33
|
+
::ActiveAdmin::Permission.update_all_from_managed_resources
|
34
|
+
end
|
35
|
+
|
36
|
+
def persisted_resources
|
37
|
+
::ActiveAdmin::ManagedResource.all.map(&:attributes).map do |attribute|
|
38
|
+
attribute.slice("class_name", "action", "name").symbolize_keys
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
def manageable_resources
|
43
|
+
@manageable_resources ||= ::ActiveAdmin::ManageableResource.call
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|