godmin 2.1.0 → 2.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.travis.yml +7 -0
- data/CHANGELOG.md +8 -0
- data/README.md +47 -3
- data/app/views/godmin/resource/_pagination.html.erb +4 -4
- data/lib/godmin/authorization.rb +34 -0
- data/lib/godmin/version.rb +1 -1
- data/test/dummy/admin/app/controllers/admin/authorized_articles_controller.rb +14 -0
- data/test/dummy/admin/app/models/admin/magazine.rb +4 -0
- data/test/dummy/admin/app/policies/admin/article_policy.rb +4 -0
- data/test/dummy/admin/app/policies/admin/magazine_policy.rb +4 -0
- data/test/dummy/admin/app/views/admin/shared/_navigation.html.erb +5 -0
- data/test/dummy/app/models/magazine.rb +2 -0
- data/test/dummy/db/migrate/20210519215502_create_magazines.rb +9 -0
- data/test/dummy/db/schema.rb +13 -6
- data/test/generators/resource_generator_test.rb +1 -0
- data/test/integration/authorization_test.rb +8 -0
- metadata +10 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 11671dfd7c0219abf4626b93f1f7273ee5769486e48a43b82889fe43601f3370
|
4
|
+
data.tar.gz: 0bb8078a5792d05b7fbf4543fd20d924432571e22dc87e00beafc6309a4cae08
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7f02e2821b7bae9cd0611a97292f6cb8159e9bcaac1f871e7077ddf0f1ad553be98804e4385c434de5326ca12138310032fa52e459db0b9c6572eeac381dbc51
|
7
|
+
data.tar.gz: 166aef398cffb04a6b38560b177f991cdaf5e9cec4be9bf8d2025188d3b83a7c8763502b183ca891bb32b5e2ce7a4a8bc3ba90b7e464164b3be90400bd6b22b6
|
data/.travis.yml
CHANGED
@@ -4,9 +4,16 @@ script: bundle exec rake test
|
|
4
4
|
rvm:
|
5
5
|
- 2.6.7
|
6
6
|
- 2.7.3
|
7
|
+
- 3.0.1
|
7
8
|
gemfile:
|
8
9
|
- gemfiles/rails_5.gemfile
|
9
10
|
- gemfiles/rails_6.gemfile
|
11
|
+
|
12
|
+
matrix:
|
13
|
+
exclude:
|
14
|
+
- rvm: 3.0.1
|
15
|
+
gemfile: gemfiles/rails_5.gemfile
|
16
|
+
|
10
17
|
addons:
|
11
18
|
code_climate:
|
12
19
|
repo_token:
|
data/CHANGELOG.md
CHANGED
@@ -1,5 +1,13 @@
|
|
1
1
|
# Changelog
|
2
2
|
|
3
|
+
### 2.2.0 - 2021-05-20
|
4
|
+
|
5
|
+
Other
|
6
|
+
- Build and test against Ruby 3.0
|
7
|
+
|
8
|
+
Bug fixes
|
9
|
+
- Regression: within an Engine, always look for Pundit policies in the engine (https://github.com/varvet/godmin/pull/259)
|
10
|
+
|
3
11
|
### 2.1.0 - 2021-05-10
|
4
12
|
|
5
13
|
Bug fixes
|
data/README.md
CHANGED
@@ -2,9 +2,8 @@
|
|
2
2
|
|
3
3
|
[![Gem Version](http://img.shields.io/gem/v/godmin.svg)](https://rubygems.org/gems/godmin)
|
4
4
|
[![Build Status](https://img.shields.io/travis/varvet/godmin/master.svg)](https://travis-ci.org/varvet/godmin)
|
5
|
-
[![Code Climate](https://
|
6
|
-
|
7
|
-
**If you are looking for the current stable version, which is Rails 4+ compatible, see the [v1.5](https://github.com/varvet/godmin/tree/v1.5) branch**
|
5
|
+
[![Code Climate](https://api.codeclimate.com/v1/badges/d8e5c7c54c1dba073689/maintainability)](https://codeclimate.com/github/varvet/godmin)
|
6
|
+
[![Test Coverage](https://api.codeclimate.com/v1/badges/d8e5c7c54c1dba073689/test_coverage)](https://codeclimate.com/github/varvet/godmin)
|
8
7
|
|
9
8
|
Godmin is an admin framework for Rails 5+. Use it to build dedicated admin sections for your apps, or stand alone admin apps such as internal tools. It has support for common features such as scoping, filtering and performing batch actions on your models. Check out the [demo app](http://godmin-sandbox.herokuapp.com) and its [source code](https://github.com/varvet/godmin-sandbox) to get a feel for how it works.
|
10
9
|
|
@@ -873,6 +872,51 @@ class ArticlesController < ApplicationController
|
|
873
872
|
end
|
874
873
|
```
|
875
874
|
|
875
|
+
### Authorization in Engines
|
876
|
+
|
877
|
+
When Godmin is installed as an engine, it expects policies to be defined
|
878
|
+
within the engine: eg. `Admin::ArticlePolicy` defined in
|
879
|
+
`admin/app/policies/article_policy.rb`.
|
880
|
+
|
881
|
+
If your admin application is itself broken up into several engines, then
|
882
|
+
either
|
883
|
+
|
884
|
+
1. the policies for those engines need to live in the main engine, or
|
885
|
+
2. those engines need to be namespaced under the namespace of the main engine.
|
886
|
+
|
887
|
+
Here is one example of a directory structure for approach 2:
|
888
|
+
|
889
|
+
```
|
890
|
+
admin
|
891
|
+
├── app
|
892
|
+
│ └── policies
|
893
|
+
│ └── admin
|
894
|
+
│ └── article_policy.rb
|
895
|
+
└── engines
|
896
|
+
└── content
|
897
|
+
└── policies
|
898
|
+
└── admin
|
899
|
+
└── content
|
900
|
+
└── text_block_policy.rb
|
901
|
+
app
|
902
|
+
└── models
|
903
|
+
└── article.rb
|
904
|
+
engines
|
905
|
+
└── content
|
906
|
+
└── models
|
907
|
+
└── content
|
908
|
+
└── text_block.rb
|
909
|
+
```
|
910
|
+
```ruby
|
911
|
+
# admin/engines/content/policies/admin/content/text_block_policy.rb
|
912
|
+
module Admin
|
913
|
+
module Content
|
914
|
+
class TextBlockPolicy < ::Admin::ApplicationPolicy
|
915
|
+
end
|
916
|
+
end
|
917
|
+
end
|
918
|
+
```
|
919
|
+
|
876
920
|
## Localization
|
877
921
|
|
878
922
|
Godmin supports localization out of the box. For a list of translatable strings, [look here](https://github.com/varvet/godmin/blob/master/config/locales/en.yml).
|
@@ -27,14 +27,14 @@
|
|
27
27
|
</div>
|
28
28
|
<div class="pagination-entries pull-right hidden-xs">
|
29
29
|
<% if @resources.length == 0 %>
|
30
|
-
<%= translate_scoped("pagination.entries.zero",
|
30
|
+
<%= translate_scoped("pagination.entries.zero",
|
31
31
|
resource: @resource_class.model_name.human(count: @resources.length).downcase
|
32
|
-
|
32
|
+
) %>
|
33
33
|
<% else %>
|
34
|
-
<%= translate_scoped("pagination.entries.other",
|
34
|
+
<%= translate_scoped("pagination.entries.other",
|
35
35
|
resource: @resource_class.model_name.human(count: @resources.length).downcase,
|
36
36
|
count: @resources.length,
|
37
37
|
total: @resource_service.paginator.total_resources
|
38
|
-
|
38
|
+
) %>
|
39
39
|
<% end %>
|
40
40
|
</div>
|
data/lib/godmin/authorization.rb
CHANGED
@@ -13,8 +13,42 @@ module Godmin
|
|
13
13
|
end
|
14
14
|
end
|
15
15
|
|
16
|
+
def policy(record)
|
17
|
+
policies[record] ||= Pundit.policy!(pundit_user, namespaced_record(record))
|
18
|
+
end
|
19
|
+
|
16
20
|
def pundit_user
|
17
21
|
admin_user
|
18
22
|
end
|
23
|
+
|
24
|
+
def namespaced_record(record)
|
25
|
+
return record unless engine_wrapper.namespaced?
|
26
|
+
|
27
|
+
class_name = find_class_name(record)
|
28
|
+
if already_namespaced?(class_name)
|
29
|
+
record
|
30
|
+
else
|
31
|
+
engine_wrapper.namespaced_path.map(&:to_sym) << record
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
# Borrowed from Pundit::PolicyFinder
|
36
|
+
def find_class_name(subject)
|
37
|
+
if subject.respond_to?(:model_name)
|
38
|
+
subject.model_name
|
39
|
+
elsif subject.class.respond_to?(:model_name)
|
40
|
+
subject.class.model_name
|
41
|
+
elsif subject.is_a?(Class)
|
42
|
+
subject
|
43
|
+
elsif subject.is_a?(Symbol)
|
44
|
+
subject.to_s.camelize
|
45
|
+
else
|
46
|
+
subject.class
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
def already_namespaced?(subject)
|
51
|
+
subject.to_s.start_with?("#{engine_wrapper.namespace.name}::")
|
52
|
+
end
|
19
53
|
end
|
20
54
|
end
|
data/lib/godmin/version.rb
CHANGED
@@ -8,6 +8,20 @@ module Admin
|
|
8
8
|
"admin"
|
9
9
|
end
|
10
10
|
|
11
|
+
def new
|
12
|
+
# The following calls to #policy are to check that the Authorization
|
13
|
+
# module can handle various different scenarios:
|
14
|
+
policy(Magazine).index?
|
15
|
+
policy(::Magazine).index?
|
16
|
+
policy(Magazine.new).index?
|
17
|
+
policy(Magazine.all).index?
|
18
|
+
policy(Admin::Magazine).index?
|
19
|
+
policy(Admin::Magazine.new).index?
|
20
|
+
policy(Admin::Magazine.where(name: "name")).index?
|
21
|
+
|
22
|
+
super
|
23
|
+
end
|
24
|
+
|
11
25
|
def resource_service_class
|
12
26
|
Admin::ArticleService
|
13
27
|
end
|
data/test/dummy/db/schema.rb
CHANGED
@@ -2,15 +2,15 @@
|
|
2
2
|
# of editing this file, please use the migrations feature of Active Record to
|
3
3
|
# incrementally modify your database, and then regenerate this schema definition.
|
4
4
|
#
|
5
|
-
#
|
6
|
-
#
|
7
|
-
#
|
8
|
-
# from scratch.
|
9
|
-
#
|
5
|
+
# This file is the source Rails uses to define your schema when running `bin/rails
|
6
|
+
# db:schema:load`. When creating a new database, `bin/rails db:schema:load` tends to
|
7
|
+
# be faster and is potentially less error prone than running all of your
|
8
|
+
# migrations from scratch. Old migrations may fail to apply correctly if those
|
9
|
+
# migrations use external dependencies or application code.
|
10
10
|
#
|
11
11
|
# It's strongly recommended that you check this file into your version control system.
|
12
12
|
|
13
|
-
ActiveRecord::Schema.define(version:
|
13
|
+
ActiveRecord::Schema.define(version: 2021_05_19_215502) do
|
14
14
|
|
15
15
|
create_table "admin_users", force: :cascade do |t|
|
16
16
|
t.string "email"
|
@@ -43,4 +43,11 @@ ActiveRecord::Schema.define(version: 20170207081043) do
|
|
43
43
|
t.index ["article_id"], name: "index_comments_on_article_id"
|
44
44
|
end
|
45
45
|
|
46
|
+
create_table "magazines", force: :cascade do |t|
|
47
|
+
t.string "name"
|
48
|
+
t.datetime "created_at", precision: 6, null: false
|
49
|
+
t.datetime "updated_at", precision: 6, null: false
|
50
|
+
end
|
51
|
+
|
52
|
+
add_foreign_key "comments", "articles"
|
46
53
|
end
|
@@ -6,6 +6,7 @@ module Godmin
|
|
6
6
|
tests ResourceGenerator
|
7
7
|
destination File.expand_path("../../tmp", __FILE__)
|
8
8
|
setup :prepare_destination
|
9
|
+
teardown :prepare_destination
|
9
10
|
|
10
11
|
def test_resource_generator_in_standalone_install
|
11
12
|
system "cd #{destination_root} && rails new . --skip-test --skip-spring --skip-bundle --skip-git --quiet"
|
@@ -34,4 +34,12 @@ class AuthorizationTest < ActionDispatch::IntegrationTest
|
|
34
34
|
visit admin.authorized_articles_path
|
35
35
|
assert_equal 403, page.status_code
|
36
36
|
end
|
37
|
+
|
38
|
+
def test_uses_engine_policy_in_engine?
|
39
|
+
visit admin.new_authorized_article_path
|
40
|
+
|
41
|
+
assert_equal 200, page.status_code
|
42
|
+
|
43
|
+
assert page.has_content?("Can't index"), "when used in an engine, the `policy` method is using the policy from the main app, not the engine"
|
44
|
+
end
|
37
45
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: godmin
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.
|
4
|
+
version: 2.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jens Ljungblad
|
@@ -10,7 +10,7 @@ authors:
|
|
10
10
|
autorequire:
|
11
11
|
bindir: bin
|
12
12
|
cert_chain: []
|
13
|
-
date: 2021-05-
|
13
|
+
date: 2021-05-20 00:00:00.000000000 Z
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
16
16
|
name: bcrypt
|
@@ -432,7 +432,9 @@ files:
|
|
432
432
|
- test/dummy/admin/app/controllers/admin/articles_controller.rb
|
433
433
|
- test/dummy/admin/app/controllers/admin/authorized_articles_controller.rb
|
434
434
|
- test/dummy/admin/app/models/admin/article.rb
|
435
|
+
- test/dummy/admin/app/models/admin/magazine.rb
|
435
436
|
- test/dummy/admin/app/policies/admin/article_policy.rb
|
437
|
+
- test/dummy/admin/app/policies/admin/magazine_policy.rb
|
436
438
|
- test/dummy/admin/app/services/admin/article_service.rb
|
437
439
|
- test/dummy/admin/app/views/admin/articles/.keep
|
438
440
|
- test/dummy/admin/app/views/admin/articles/columns/.keep
|
@@ -461,6 +463,7 @@ files:
|
|
461
463
|
- test/dummy/app/models/another_admin_user.rb
|
462
464
|
- test/dummy/app/models/article.rb
|
463
465
|
- test/dummy/app/models/comment.rb
|
466
|
+
- test/dummy/app/models/magazine.rb
|
464
467
|
- test/dummy/app/policies/article_policy.rb
|
465
468
|
- test/dummy/app/services/article_service.rb
|
466
469
|
- test/dummy/app/services/comment_service.rb
|
@@ -494,6 +497,7 @@ files:
|
|
494
497
|
- test/dummy/db/migrate/20150907133753_create_admin_users.rb
|
495
498
|
- test/dummy/db/migrate/20160713134238_create_comment.rb
|
496
499
|
- test/dummy/db/migrate/20170207081043_create_another_admin_user.rb
|
500
|
+
- test/dummy/db/migrate/20210519215502_create_magazines.rb
|
497
501
|
- test/dummy/db/schema.rb
|
498
502
|
- test/dummy/lib/assets/.keep
|
499
503
|
- test/dummy/log/.keep
|
@@ -562,7 +566,9 @@ test_files:
|
|
562
566
|
- test/dummy/admin/app/controllers/admin/articles_controller.rb
|
563
567
|
- test/dummy/admin/app/controllers/admin/authorized_articles_controller.rb
|
564
568
|
- test/dummy/admin/app/models/admin/article.rb
|
569
|
+
- test/dummy/admin/app/models/admin/magazine.rb
|
565
570
|
- test/dummy/admin/app/policies/admin/article_policy.rb
|
571
|
+
- test/dummy/admin/app/policies/admin/magazine_policy.rb
|
566
572
|
- test/dummy/admin/app/services/admin/article_service.rb
|
567
573
|
- test/dummy/admin/app/views/admin/articles/.keep
|
568
574
|
- test/dummy/admin/app/views/admin/articles/columns/.keep
|
@@ -591,6 +597,7 @@ test_files:
|
|
591
597
|
- test/dummy/app/models/another_admin_user.rb
|
592
598
|
- test/dummy/app/models/article.rb
|
593
599
|
- test/dummy/app/models/comment.rb
|
600
|
+
- test/dummy/app/models/magazine.rb
|
594
601
|
- test/dummy/app/policies/article_policy.rb
|
595
602
|
- test/dummy/app/services/article_service.rb
|
596
603
|
- test/dummy/app/services/comment_service.rb
|
@@ -624,6 +631,7 @@ test_files:
|
|
624
631
|
- test/dummy/db/migrate/20150907133753_create_admin_users.rb
|
625
632
|
- test/dummy/db/migrate/20160713134238_create_comment.rb
|
626
633
|
- test/dummy/db/migrate/20170207081043_create_another_admin_user.rb
|
634
|
+
- test/dummy/db/migrate/20210519215502_create_magazines.rb
|
627
635
|
- test/dummy/db/schema.rb
|
628
636
|
- test/dummy/lib/assets/.keep
|
629
637
|
- test/dummy/log/.keep
|