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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 9a084849cf4dd5f729b2f5ca0dabdcec727ef1172eb1cb19baf9de27ca822950
4
- data.tar.gz: 8b655c6dad3b1a1fc95d88b08cf2f25e848b646266fd63808dffad77b47f767e
3
+ metadata.gz: 11671dfd7c0219abf4626b93f1f7273ee5769486e48a43b82889fe43601f3370
4
+ data.tar.gz: 0bb8078a5792d05b7fbf4543fd20d924432571e22dc87e00beafc6309a4cae08
5
5
  SHA512:
6
- metadata.gz: f0fec37368ae0548a3e3adb1b662f214aff97756f916d11da1907acbc7caa43304b920844f9a1f8ebb500a35d7f261c060fd8e605e5cb841e7fdafb9da8a744b
7
- data.tar.gz: d6a218a707bafe5ac68611b13c74e6975c13702562d5a804514faad4af0100544b5bf722e252077af89653f2702deb932e36f1c5131855b2604af86247b069fe
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://img.shields.io/codeclimate/github/varvet/godmin.svg)](https://codeclimate.com/github/varvet/godmin)
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>
@@ -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
@@ -1,3 +1,3 @@
1
1
  module Godmin
2
- VERSION = "2.1.0"
2
+ VERSION = "2.2.0"
3
3
  end
@@ -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
@@ -0,0 +1,4 @@
1
+ module Admin
2
+ class Magazine < ::Magazine
3
+ end
4
+ end
@@ -3,5 +3,9 @@ module Admin
3
3
  def index?
4
4
  false
5
5
  end
6
+
7
+ def new?
8
+ true
9
+ end
6
10
  end
7
11
  end
@@ -0,0 +1,4 @@
1
+ module Admin
2
+ class MagazinePolicy < Godmin::Authorization::Policy
3
+ end
4
+ end
@@ -1 +1,6 @@
1
1
  <%= navbar_item Article %>
2
+ <% if policy(Article).index? %>
3
+ Can index
4
+ <% else %>
5
+ Can't index
6
+ <% end %>
@@ -0,0 +1,2 @@
1
+ class Magazine < ActiveRecord::Base
2
+ end
@@ -0,0 +1,9 @@
1
+ class CreateMagazines < ActiveRecord::Migration[6.1]
2
+ def change
3
+ create_table :magazines do |t|
4
+ t.string :name
5
+
6
+ t.timestamps
7
+ end
8
+ end
9
+ end
@@ -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
- # Note that this schema.rb definition is the authoritative source for your
6
- # database schema. If you need to create the application database on another
7
- # system, you should be using db:schema:load, not running all the migrations
8
- # from scratch. The latter is a flawed and unsustainable approach (the more migrations
9
- # you'll amass, the slower it'll run and the greater likelihood for issues).
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: 20170207081043) do
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.1.0
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-09 00:00:00.000000000 Z
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