pundit_can 0.1.4 → 0.1.5
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 +4 -4
- data/README.md +17 -1
- data/lib/pundit_can/load_and_authorize.rb +19 -7
- data/lib/pundit_can/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 95df5ff56989946351acaa622d23e1aa0d0575e8cce7c47dd4e47b266a02c247
|
|
4
|
+
data.tar.gz: e9f75be20b4e055f0583657ef05d3dcea4cdb314f6998642bef734c369fd8024
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 1ff5fb33b343efd582132d77edeb12651b73dd40f0c035c301997beb6f775cdd3a9dbfb660f16897f0a0f34655beeddab96d32f48732497229dd03900cdeafc6
|
|
7
|
+
data.tar.gz: 365fbe976dea92ef37d57701f363f6f416a078983e46bd9a6002cdb9283a1341df0ca5c163c7b5de95f2861b483a2e17cc086ba1518d5a6d365ac523cdbdccbd
|
data/README.md
CHANGED
|
@@ -49,7 +49,7 @@ When the association name doesn't match the model name, use the `:association` o
|
|
|
49
49
|
class ArticlesController < ApplicationController
|
|
50
50
|
load_resource model_class: User, parent: true
|
|
51
51
|
load_resource model_class: Article, through: :user, association: :articles
|
|
52
|
-
|
|
52
|
+
|
|
53
53
|
# This passes @user.articles instead of @user.posts to the ArticlePolicy::Scope
|
|
54
54
|
end
|
|
55
55
|
```
|
|
@@ -57,6 +57,22 @@ end
|
|
|
57
57
|
This is useful when you have associations like `has_many :published_posts, class_name: "Post"`
|
|
58
58
|
or other cases where the association method name differs from the model class name.
|
|
59
59
|
|
|
60
|
+
When the parent relationship attribute in the model uses a different name than the `:through`
|
|
61
|
+
option, use the `:parent_key` option:
|
|
62
|
+
|
|
63
|
+
```ruby
|
|
64
|
+
class ArticlesController < ApplicationController
|
|
65
|
+
load_resource model_class: User, parent: true
|
|
66
|
+
load_resource model_class: Article, through: :user, parent_key: :admin
|
|
67
|
+
|
|
68
|
+
# This builds Article.new(admin: @user) instead of Article.new(user: @user)
|
|
69
|
+
end
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
This is useful when the model's foreign key or association attribute name differs from the
|
|
73
|
+
parent's instance name. For example, when `belongs_to :admin, class_name: "User"` or
|
|
74
|
+
`belongs_to :author, class_name: "User"` is used instead of a standard `belongs_to :user`.
|
|
75
|
+
|
|
60
76
|
#### Customized loading
|
|
61
77
|
|
|
62
78
|
You can customize the loading for cases when the model, controller, and policies don't match up name-wise.
|
|
@@ -78,6 +78,12 @@ module PunditCan
|
|
|
78
78
|
# differs from the model name. For example, +through: :user, association: :articles+ will
|
|
79
79
|
# pass +@user.articles+ as the scope.
|
|
80
80
|
#
|
|
81
|
+
# @param [Symbol] parent_key Optional. When used with +:through+, specifies the associated
|
|
82
|
+
# method on the model and sets the parent. By default the parent_key value = +:through+. Use
|
|
83
|
+
# this when the parent relationship in the model uses a different name.
|
|
84
|
+
# For example, +through: :user, parent_key: :admin+ will
|
|
85
|
+
# pass +Article.new(admin: @user)+ when creating new model instances.
|
|
86
|
+
#
|
|
81
87
|
# @param [Constant] policy_class Optional. The policy class to use. Defaults from controller name.
|
|
82
88
|
#
|
|
83
89
|
# @param [Constant] policy_scope_class Optional. The policy scope class to use. Defaults from controller name.
|
|
@@ -103,17 +109,19 @@ module PunditCan
|
|
|
103
109
|
policy_kwopts = options.extract!(:policy_class)
|
|
104
110
|
policy_scope_kwopts = options.extract!(:policy_scope_class)
|
|
105
111
|
|
|
112
|
+
parent = instance_variable_get("@#{options[:through]}") if options[:through]
|
|
113
|
+
|
|
106
114
|
loaded = if options[:parent]
|
|
107
115
|
load_parent_instance_var(model_class, param_key, policy_kwopts, policy_scope_kwopts)
|
|
108
116
|
else
|
|
109
117
|
# When :through is specified, pass the parent's association as the scope
|
|
110
|
-
scope_class = if
|
|
118
|
+
scope_class = if parent
|
|
111
119
|
assoc = options[:association] || model_class.name.underscore.pluralize
|
|
112
120
|
parent.public_send(assoc)
|
|
113
121
|
else
|
|
114
122
|
model_class
|
|
115
123
|
end
|
|
116
|
-
load_instance_var(model_class, param_key, policy_kwopts, policy_scope_kwopts, scope_class)
|
|
124
|
+
load_instance_var(model_class, param_key, policy_kwopts, policy_scope_kwopts, scope_class, parent, options[:through] || options[:parent_key])
|
|
117
125
|
end
|
|
118
126
|
|
|
119
127
|
instance_name = instance_name.pluralize unless loaded.is_a?(model_class)
|
|
@@ -134,14 +142,18 @@ module PunditCan
|
|
|
134
142
|
options[:model_class].name.underscore if options[:model_class].present? && options[:parent]
|
|
135
143
|
end
|
|
136
144
|
|
|
137
|
-
def load_instance_var(model_class, param_key, policy_kwopts, policy_scope_kwopts, scope_class = model_class)
|
|
145
|
+
def load_instance_var(model_class, param_key, policy_kwopts, policy_scope_kwopts, scope_class = model_class, parent = nil, parent_key = nil)
|
|
138
146
|
case params[:action]
|
|
139
147
|
when "index"
|
|
140
148
|
load_scope(scope_class, policy_kwopts, policy_scope_kwopts)
|
|
141
|
-
when "new"
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
149
|
+
when "create", "new"
|
|
150
|
+
parent_attributes = {}
|
|
151
|
+
parent_attributes[parent_key] = parent if parent
|
|
152
|
+
if params[:action] == "new"
|
|
153
|
+
authorize(model_class.new(parent_attributes), **policy_kwopts)
|
|
154
|
+
else
|
|
155
|
+
authorize(model_class.new(parent_attributes.merge(permitted_attributes(model_class))), **policy_kwopts)
|
|
156
|
+
end
|
|
145
157
|
when "edit", "update", "show", "destroy"
|
|
146
158
|
load_model(scope_class, param_key, policy_kwopts, policy_scope_kwopts)
|
|
147
159
|
else
|
data/lib/pundit_can/version.rb
CHANGED
metadata
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: pundit_can
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.1.
|
|
4
|
+
version: 0.1.5
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- candland
|
|
8
8
|
autorequire:
|
|
9
9
|
bindir: bin
|
|
10
10
|
cert_chain: []
|
|
11
|
-
date: 2026-
|
|
11
|
+
date: 2026-05-18 00:00:00.000000000 Z
|
|
12
12
|
dependencies:
|
|
13
13
|
- !ruby/object:Gem::Dependency
|
|
14
14
|
name: pundit
|