godmin 1.5.0 → 2.0.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/.travis.yml +8 -5
- data/Appraisals +4 -4
- data/CHANGELOG.md +51 -2
- data/Gemfile +1 -1
- data/README.md +38 -5
- data/Rakefile +2 -2
- data/gemfiles/rails_5.gemfile +2 -2
- data/gemfiles/rails_6.gemfile +8 -0
- data/godmin.gemspec +18 -15
- data/lib/generators/godmin/resource/resource_generator.rb +7 -1
- data/lib/generators/godmin/resource/templates/resource_model.rb +4 -0
- data/lib/godmin/application_controller.rb +10 -11
- data/lib/godmin/authentication.rb +11 -11
- data/lib/godmin/authentication/sessions_controller.rb +1 -0
- data/lib/godmin/authorization.rb +6 -20
- data/lib/godmin/engine_wrapper.rb +10 -1
- data/lib/godmin/generators/base.rb +4 -4
- data/lib/godmin/generators/named_base.rb +4 -4
- data/lib/godmin/helpers/application.rb +3 -3
- data/lib/godmin/resolver.rb +20 -5
- data/lib/godmin/resources/resource_controller/batch_actions.rb +1 -1
- data/lib/godmin/resources/resource_service.rb +1 -5
- data/lib/godmin/version.rb +1 -1
- data/template.rb +3 -3
- data/test/dummy/admin/app/controllers/admin/authorized_articles_controller.rb +15 -0
- data/test/dummy/admin/app/models/admin/article.rb +4 -0
- data/test/dummy/admin/app/policies/admin/article_policy.rb +7 -0
- data/test/dummy/admin/config/routes.rb +1 -0
- data/test/dummy/app/assets/config/manifest.js +4 -0
- data/test/dummy/app/controllers/another_admin_sessions_controller.rb +0 -2
- data/test/dummy/db/migrate/20150717121532_create_articles.rb +1 -1
- data/test/dummy/db/migrate/20150907133753_create_admin_users.rb +1 -1
- data/test/dummy/db/migrate/20160713134238_create_comment.rb +1 -1
- data/test/dummy/db/schema.rb +17 -16
- data/test/fakes/article.rb +1 -1
- data/test/fakes/article_service.rb +3 -7
- data/test/generators/resource_generator_test.rb +77 -0
- data/test/integration/authentication_test.rb +3 -8
- data/test/integration/authorization_test.rb +5 -0
- data/test/{lib/godmin → unit}/engine_wrapper_test.rb +0 -0
- data/test/{lib/godmin → unit}/helpers/filters_test.rb +0 -0
- data/test/{lib/godmin → unit}/paginator_test.rb +0 -0
- data/test/{lib/godmin → unit}/resolver_test.rb +0 -0
- data/test/{lib/godmin → unit}/resources/resource_service/batch_actions_test.rb +0 -0
- data/test/{lib/godmin → unit}/resources/resource_service/filters_test.rb +0 -0
- data/test/{lib/godmin → unit}/resources/resource_service/ordering_test.rb +0 -0
- data/test/{lib/godmin → unit}/resources/resource_service/pagination_test.rb +0 -0
- data/test/{lib/godmin → unit}/resources/resource_service/scopes_test.rb +0 -0
- data/test/{lib/godmin → unit}/resources/resource_service_test.rb +0 -0
- metadata +139 -85
- data/gemfiles/rails_4.gemfile +0 -8
- data/lib/godmin/authorization/policy_finder.rb +0 -31
- data/lib/tasks/godmin_tasks.rake +0 -4
- data/test/lib/godmin/policy_finder_test.rb +0 -55
@@ -4,7 +4,7 @@ class Godmin::ResourceGenerator < Godmin::Generators::NamedBase
|
|
4
4
|
argument :attributes, type: :array, default: [], banner: "attribute attribute"
|
5
5
|
|
6
6
|
def add_route
|
7
|
-
|
7
|
+
invoke "resource_route"
|
8
8
|
end
|
9
9
|
|
10
10
|
def add_navigation
|
@@ -15,6 +15,12 @@ class Godmin::ResourceGenerator < Godmin::Generators::NamedBase
|
|
15
15
|
end
|
16
16
|
end
|
17
17
|
|
18
|
+
def create_model
|
19
|
+
if namespaced?
|
20
|
+
template "resource_model.rb", File.join("app/models", class_path, "#{file_name}.rb")
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
18
24
|
def create_controller
|
19
25
|
template "resource_controller.rb", File.join("app/controllers", class_path, "#{file_name.pluralize}_controller.rb")
|
20
26
|
end
|
@@ -28,15 +28,6 @@ module Godmin
|
|
28
28
|
|
29
29
|
protected
|
30
30
|
|
31
|
-
def redirect_back
|
32
|
-
case Rails::VERSION::MAJOR
|
33
|
-
when 4
|
34
|
-
redirect_to :back
|
35
|
-
when 5
|
36
|
-
super(fallback_location: root_path)
|
37
|
-
end
|
38
|
-
end
|
39
|
-
|
40
31
|
private
|
41
32
|
|
42
33
|
def engine_wrapper
|
@@ -47,12 +38,20 @@ module Godmin
|
|
47
38
|
append_view_path Godmin::Resolver.resolvers(controller_path, engine_wrapper)
|
48
39
|
end
|
49
40
|
|
41
|
+
def disable_authentication
|
42
|
+
@_disable_authentication = true
|
43
|
+
end
|
44
|
+
|
45
|
+
def disable_authorization
|
46
|
+
@_disable_authorization = true
|
47
|
+
end
|
48
|
+
|
50
49
|
def authentication_enabled?
|
51
|
-
singleton_class.include?(Godmin::Authentication)
|
50
|
+
!@_disable_authentication && singleton_class.include?(Godmin::Authentication)
|
52
51
|
end
|
53
52
|
|
54
53
|
def authorization_enabled?
|
55
|
-
singleton_class.include?(Godmin::Authorization)
|
54
|
+
!@_disable_authorization && singleton_class.include?(Godmin::Authorization)
|
56
55
|
end
|
57
56
|
end
|
58
57
|
end
|
@@ -6,26 +6,26 @@ module Godmin
|
|
6
6
|
extend ActiveSupport::Concern
|
7
7
|
|
8
8
|
included do
|
9
|
-
before_action :
|
9
|
+
before_action :authenticate
|
10
10
|
|
11
11
|
helper_method :admin_user
|
12
12
|
helper_method :admin_user_signed_in?
|
13
13
|
end
|
14
14
|
|
15
|
-
def
|
16
|
-
unless
|
17
|
-
|
18
|
-
end
|
19
|
-
end
|
15
|
+
def authenticate
|
16
|
+
return unless authentication_enabled?
|
17
|
+
return if admin_user_signed_in?
|
20
18
|
|
21
|
-
|
22
|
-
raise NotImplementedError, "Must define the admin user class"
|
19
|
+
redirect_to new_session_path
|
23
20
|
end
|
24
21
|
|
22
|
+
def admin_user_class; end
|
23
|
+
|
25
24
|
def admin_user
|
26
|
-
|
27
|
-
|
28
|
-
|
25
|
+
return unless admin_user_class
|
26
|
+
return unless session[:admin_user_id]
|
27
|
+
|
28
|
+
@_admin_user ||= admin_user_class.find_by(id: session[:admin_user_id])
|
29
29
|
end
|
30
30
|
|
31
31
|
def admin_user_signed_in?
|
data/lib/godmin/authorization.rb
CHANGED
@@ -1,34 +1,20 @@
|
|
1
|
+
require "pundit"
|
1
2
|
require "godmin/authorization/policy"
|
2
|
-
require "godmin/authorization/policy_finder"
|
3
3
|
|
4
4
|
module Godmin
|
5
5
|
module Authorization
|
6
6
|
extend ActiveSupport::Concern
|
7
7
|
|
8
|
-
|
9
|
-
helper_method :policy
|
8
|
+
include Pundit
|
10
9
|
|
11
|
-
|
10
|
+
included do
|
11
|
+
rescue_from Pundit::NotAuthorizedError do
|
12
12
|
render plain: "You are not authorized to do this", status: 403, layout: "godmin/login"
|
13
13
|
end
|
14
14
|
end
|
15
15
|
|
16
|
-
def
|
17
|
-
|
18
|
-
|
19
|
-
unless policy.public_send(query || action_name + "?")
|
20
|
-
fail NotAuthorizedError
|
21
|
-
end
|
16
|
+
def pundit_user
|
17
|
+
admin_user
|
22
18
|
end
|
23
|
-
|
24
|
-
def policy(record)
|
25
|
-
policies[record] ||= PolicyFinder.find(record, engine_wrapper.namespace).new(admin_user, record)
|
26
|
-
end
|
27
|
-
|
28
|
-
def policies
|
29
|
-
@_policies ||= {}
|
30
|
-
end
|
31
|
-
|
32
|
-
class NotAuthorizedError < StandardError; end
|
33
19
|
end
|
34
20
|
end
|
@@ -40,8 +40,17 @@ module Godmin
|
|
40
40
|
end
|
41
41
|
end
|
42
42
|
|
43
|
+
# Some gymnastics because the `parents` function is slated for deprecation
|
44
|
+
# and being replaced by `module_parents` and we don't want to clutter our
|
45
|
+
# log with a million warnings
|
46
|
+
def parents_of(controller)
|
47
|
+
return controller.module_parents if controller.respond_to?(:module_parents)
|
48
|
+
|
49
|
+
controller.parents
|
50
|
+
end
|
51
|
+
|
43
52
|
def find_engine_module(controller)
|
44
|
-
controller.
|
53
|
+
parents_of(controller).find do |parent|
|
45
54
|
parent.respond_to?(:use_relative_model_naming?) && parent.use_relative_model_naming?
|
46
55
|
end
|
47
56
|
end
|
@@ -4,7 +4,7 @@ module Godmin
|
|
4
4
|
module Generators
|
5
5
|
class Base < Rails::Generators::Base
|
6
6
|
def self.source_paths
|
7
|
-
%w
|
7
|
+
%w[authentication install policy resource].map do |path|
|
8
8
|
File.expand_path("../../../generators/godmin/#{path}/templates", __FILE__)
|
9
9
|
end
|
10
10
|
end
|
@@ -12,15 +12,15 @@ module Godmin
|
|
12
12
|
private
|
13
13
|
|
14
14
|
def namespace
|
15
|
-
@
|
15
|
+
@_namespace ||= Rails::Generators.namespace
|
16
16
|
end
|
17
17
|
|
18
18
|
def namespaced?
|
19
|
-
@
|
19
|
+
@_namespaced ||= namespace.present?
|
20
20
|
end
|
21
21
|
|
22
22
|
def namespaced_path
|
23
|
-
@
|
23
|
+
@_namespaced_path ||= begin
|
24
24
|
if namespaced?
|
25
25
|
namespace.name.split("::").map(&:underscore)
|
26
26
|
else
|
@@ -8,7 +8,7 @@ module Godmin
|
|
8
8
|
private
|
9
9
|
|
10
10
|
def full_class_name
|
11
|
-
if
|
11
|
+
if namespaced?
|
12
12
|
"#{namespace}::#{class_name}"
|
13
13
|
else
|
14
14
|
class_name
|
@@ -16,15 +16,15 @@ module Godmin
|
|
16
16
|
end
|
17
17
|
|
18
18
|
def class_name
|
19
|
-
@
|
19
|
+
@_class_name ||= name.classify
|
20
20
|
end
|
21
21
|
|
22
22
|
def class_path
|
23
|
-
@
|
23
|
+
@_class_path ||= namespaced_path + name.classify.deconstantize.split("::").map(&:underscore)
|
24
24
|
end
|
25
25
|
|
26
26
|
def file_name
|
27
|
-
@
|
27
|
+
@_file_name ||= class_name.demodulize.underscore
|
28
28
|
end
|
29
29
|
end
|
30
30
|
end
|
@@ -4,17 +4,17 @@ module Godmin
|
|
4
4
|
# Renders the provided partial with locals if it exists, otherwise
|
5
5
|
# yields the given block. The lookup context call is cached for
|
6
6
|
# each partial.
|
7
|
-
def partial_override(partial, locals = {})
|
7
|
+
def partial_override(partial, locals = {}, &block)
|
8
8
|
@_partial_override ||= {}
|
9
9
|
|
10
|
-
|
10
|
+
unless @_partial_override.key?(partial)
|
11
11
|
@_partial_override[partial] = lookup_context.exists?(partial, nil, true)
|
12
12
|
end
|
13
13
|
|
14
14
|
if @_partial_override[partial]
|
15
15
|
render partial: partial, locals: locals
|
16
16
|
else
|
17
|
-
|
17
|
+
capture(&block)
|
18
18
|
end
|
19
19
|
end
|
20
20
|
|
data/lib/godmin/resolver.rb
CHANGED
@@ -16,15 +16,30 @@ module Godmin
|
|
16
16
|
@engine_wrapper = engine_wrapper
|
17
17
|
end
|
18
18
|
|
19
|
+
# This function is for Rails 6 and up since the `find_templates` function
|
20
|
+
# is deprecated. It does the same thing, just a little differently. It's
|
21
|
+
# not being run by versions previous to Rails 6.
|
22
|
+
def _find_all(name, prefix, partial, details, key, locals)
|
23
|
+
templates = []
|
24
|
+
|
25
|
+
template_paths(prefix).each do |p|
|
26
|
+
break if templates.present?
|
27
|
+
|
28
|
+
path = Path.build(name, "#{@path}/#{p}", partial)
|
29
|
+
templates = query(path, details, details[:formats], locals, cache: !!key)
|
30
|
+
end
|
31
|
+
|
32
|
+
templates
|
33
|
+
end
|
34
|
+
|
35
|
+
# This is how we find templates in Rails 5 and below.
|
19
36
|
def find_templates(name, prefix, *args)
|
20
37
|
templates = []
|
21
38
|
|
22
39
|
template_paths(prefix).each do |path|
|
23
|
-
if templates.present?
|
24
|
-
|
25
|
-
|
26
|
-
templates = super(name, path, *args)
|
27
|
-
end
|
40
|
+
break if templates.present?
|
41
|
+
|
42
|
+
templates = super(name, path, *args)
|
28
43
|
end
|
29
44
|
|
30
45
|
templates
|
@@ -24,11 +24,7 @@ module Godmin
|
|
24
24
|
end
|
25
25
|
|
26
26
|
def resource_class
|
27
|
-
|
28
|
-
end
|
29
|
-
|
30
|
-
def resource_class_name
|
31
|
-
self.class.name.demodulize.chomp("Service")
|
27
|
+
self.class.name.chomp("Service").constantize
|
32
28
|
end
|
33
29
|
|
34
30
|
def resources_relation
|
data/lib/godmin/version.rb
CHANGED
data/template.rb
CHANGED
@@ -3,7 +3,7 @@ require "active_support/all"
|
|
3
3
|
def install_standalone
|
4
4
|
set_ruby_version
|
5
5
|
|
6
|
-
gem "godmin", "1.
|
6
|
+
gem "godmin", "1.5.0"
|
7
7
|
|
8
8
|
after_bundle do
|
9
9
|
create_database
|
@@ -36,7 +36,7 @@ def install_engine
|
|
36
36
|
|
37
37
|
inject_into_file "admin/admin.gemspec", before: /^end/ do
|
38
38
|
<<-END.strip_heredoc.indent(2)
|
39
|
-
s.add_dependency "godmin", "~> 1.
|
39
|
+
s.add_dependency "godmin", "~> 1.5.0"
|
40
40
|
END
|
41
41
|
end
|
42
42
|
|
@@ -71,7 +71,7 @@ end
|
|
71
71
|
|
72
72
|
def set_ruby_version
|
73
73
|
prepend_to_file "Gemfile" do
|
74
|
-
"ruby '2.
|
74
|
+
"ruby '2.3.5'\n"
|
75
75
|
end
|
76
76
|
end
|
77
77
|
|
@@ -0,0 +1,15 @@
|
|
1
|
+
require_dependency "admin/articles_controller"
|
2
|
+
|
3
|
+
module Admin
|
4
|
+
class AuthorizedArticlesController < ArticlesController
|
5
|
+
include Godmin::Authorization
|
6
|
+
|
7
|
+
def admin_user
|
8
|
+
"admin"
|
9
|
+
end
|
10
|
+
|
11
|
+
def resource_service_class
|
12
|
+
Admin::ArticleService
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
data/test/dummy/db/schema.rb
CHANGED
@@ -13,32 +13,33 @@
|
|
13
13
|
ActiveRecord::Schema.define(version: 20170207081043) do
|
14
14
|
|
15
15
|
create_table "admin_users", force: :cascade do |t|
|
16
|
-
t.string
|
17
|
-
t.text
|
18
|
-
t.datetime "created_at",
|
19
|
-
t.datetime "updated_at",
|
16
|
+
t.string "email"
|
17
|
+
t.text "password_digest"
|
18
|
+
t.datetime "created_at", null: false
|
19
|
+
t.datetime "updated_at", null: false
|
20
20
|
end
|
21
21
|
|
22
22
|
create_table "another_admin_users", force: :cascade do |t|
|
23
|
-
t.string
|
24
|
-
t.text
|
25
|
-
t.datetime "created_at",
|
26
|
-
t.datetime "updated_at",
|
23
|
+
t.string "email"
|
24
|
+
t.text "password_digest"
|
25
|
+
t.datetime "created_at", null: false
|
26
|
+
t.datetime "updated_at", null: false
|
27
27
|
end
|
28
28
|
|
29
29
|
create_table "articles", force: :cascade do |t|
|
30
|
-
t.string
|
31
|
-
t.text
|
32
|
-
t.boolean
|
33
|
-
t.integer
|
34
|
-
t.datetime "created_at",
|
35
|
-
t.datetime "updated_at",
|
30
|
+
t.string "title"
|
31
|
+
t.text "body"
|
32
|
+
t.boolean "published", default: false
|
33
|
+
t.integer "admin_user_id"
|
34
|
+
t.datetime "created_at", null: false
|
35
|
+
t.datetime "updated_at", null: false
|
36
|
+
t.index ["admin_user_id"], name: "index_articles_on_admin_user_id"
|
36
37
|
end
|
37
38
|
|
38
39
|
create_table "comments", force: :cascade do |t|
|
39
40
|
t.integer "article_id"
|
40
|
-
t.string
|
41
|
-
t.text
|
41
|
+
t.string "title"
|
42
|
+
t.text "body"
|
42
43
|
t.index ["article_id"], name: "index_comments_on_article_id"
|
43
44
|
end
|
44
45
|
|