resource_policy 1.0.0 → 1.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: fed24d9cd11a66ed01e1c3502226d08552e28613059ebd7ec7df33b9516dd50b
4
- data.tar.gz: 46b4678e5d4c56bf3553869672e07fef6a31dca7a72b4c05633fa1845f79d0a1
3
+ metadata.gz: 63bbda72581f4db59a3a27f33616db5480c90a39baff857f4886a89338c5b3c9
4
+ data.tar.gz: 139231ccbac6d84cb8d697be8c34f46fded3ff66e46ce65b7fc4ee5397631f1a
5
5
  SHA512:
6
- metadata.gz: a92ff288f3b96e0733ffe975b44acace545db00526aca10da31aa947ae3c9f58e71458d88e76d24380f98cb438fab12edfac3d53e627ff53b4d9966bd39a95b3
7
- data.tar.gz: 4dd64068ede10a86e019872728f6d93522b266c6463e9853211067c5e2b990db6ecbd520d84230dec7a7ca784eed18bb0942e4be033f38aa600e09624564aebf
6
+ metadata.gz: e81553c1ec0c7dd0e00ec063ceef818a34f70a24084f515cc9d32302d8b8f628398def7ebe04c42bfc1c33174b970a4c6ab76d31f7f1e2ef1048fe4ae3dae151
7
+ data.tar.gz: 394f96baaba9120f15fc6b25465a230e4b89961faef05bcfc9f885d7ee6ba763501e9576cce16f0ac5557225a42fbaa678478ceff2c8b5ce159a7616d38741d7
@@ -1,20 +1,21 @@
1
1
  name: Ruby
2
-
3
- on: [push]
4
-
2
+ on: [push, pull_request]
5
3
  jobs:
6
- build:
4
+ specs:
5
+ strategy:
6
+ matrix:
7
+ ruby-version: ['3.2', '3.3', '3.4', '4.0']
7
8
 
8
9
  runs-on: ubuntu-latest
9
-
10
10
  steps:
11
- - uses: actions/checkout@v1
12
- - name: Set up Ruby 2.6
13
- uses: actions/setup-ruby@v1
14
- with:
15
- ruby-version: 2.6.x
16
- - name: Build and test with Rake
17
- run: |
18
- gem install bundler
19
- bundle install --jobs 4 --retry 3
20
- bundle exec rake
11
+ - uses: actions/checkout@v4
12
+ - uses: ruby/setup-ruby@v1
13
+ with:
14
+ ruby-version: ${{ matrix.ruby-version }}
15
+ bundler-cache: true # runs 'bundle install' and caches installed gems automatically
16
+ - run: bundle exec rake
17
+ - name: Upload coverage to Codecov
18
+ uses: codecov/codecov-action@v5
19
+ with:
20
+ token: ${{ secrets.CODECOV_TOKEN }}
21
+ files: ./coverage/lcov.info
data/.ruby-version CHANGED
@@ -1 +1 @@
1
- 3.1.2
1
+ 4.0.6
data/CHANGELOG.md CHANGED
@@ -7,8 +7,17 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
7
7
 
8
8
  ## [Unreleased]
9
9
 
10
+ * Removed: support for Ruby < 3.2 (CI now tests Ruby 3.2–4.0)
11
+ * Added: short-hand method for getting allowed actions
12
+ * Added: support for proc type validator options
13
+ * Added: `PolicyConfiguration#configured_attribute` for looking up one configured attribute
14
+ * Changed: protected resource and attributes policy reads look the attribute up directly instead of rebuilding the attributes hash on every read. These lookups now use `PolicyConfiguration#configured_attribute` and no longer go through `PolicyConfiguration#attributes`; if you override `#attributes` in a custom configuration, override `#configured_attribute` as well
10
15
  * Added/Changed/Deprecated/Removed/Fixed/Security: YOUR CHANGE HERE
11
16
 
17
+ ## [1.1.0]
18
+
19
+ * Added AttributesValidator
20
+
12
21
  ## [1.0.0]
13
22
 
14
23
  * Added Ruby on Rails validator
data/Gemfile CHANGED
@@ -3,8 +3,8 @@
3
3
  source 'https://rubygems.org'
4
4
 
5
5
  group :test do
6
- gem 'codecov', require: false
7
6
  gem 'simplecov', require: false
7
+ gem 'simplecov-lcov', require: false
8
8
  end
9
9
 
10
10
  # Specify your gem's dependencies in resource_policy.gemspec
data/Gemfile.lock CHANGED
@@ -1,224 +1,279 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- resource_policy (1.0.0)
4
+ resource_policy (1.2.0)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
8
8
  specs:
9
- actioncable (7.0.4.2)
10
- actionpack (= 7.0.4.2)
11
- activesupport (= 7.0.4.2)
9
+ actioncable (8.0.5.1)
10
+ actionpack (= 8.0.5.1)
11
+ activesupport (= 8.0.5.1)
12
12
  nio4r (~> 2.0)
13
13
  websocket-driver (>= 0.6.1)
14
- actionmailbox (7.0.4.2)
15
- actionpack (= 7.0.4.2)
16
- activejob (= 7.0.4.2)
17
- activerecord (= 7.0.4.2)
18
- activestorage (= 7.0.4.2)
19
- activesupport (= 7.0.4.2)
20
- mail (>= 2.7.1)
21
- net-imap
22
- net-pop
23
- net-smtp
24
- actionmailer (7.0.4.2)
25
- actionpack (= 7.0.4.2)
26
- actionview (= 7.0.4.2)
27
- activejob (= 7.0.4.2)
28
- activesupport (= 7.0.4.2)
29
- mail (~> 2.5, >= 2.5.4)
30
- net-imap
31
- net-pop
32
- net-smtp
33
- rails-dom-testing (~> 2.0)
34
- actionpack (7.0.4.2)
35
- actionview (= 7.0.4.2)
36
- activesupport (= 7.0.4.2)
37
- rack (~> 2.0, >= 2.2.0)
14
+ zeitwerk (~> 2.6)
15
+ actionmailbox (8.0.5.1)
16
+ actionpack (= 8.0.5.1)
17
+ activejob (= 8.0.5.1)
18
+ activerecord (= 8.0.5.1)
19
+ activestorage (= 8.0.5.1)
20
+ activesupport (= 8.0.5.1)
21
+ mail (>= 2.8.0)
22
+ actionmailer (8.0.5.1)
23
+ actionpack (= 8.0.5.1)
24
+ actionview (= 8.0.5.1)
25
+ activejob (= 8.0.5.1)
26
+ activesupport (= 8.0.5.1)
27
+ mail (>= 2.8.0)
28
+ rails-dom-testing (~> 2.2)
29
+ actionpack (8.0.5.1)
30
+ actionview (= 8.0.5.1)
31
+ activesupport (= 8.0.5.1)
32
+ nokogiri (>= 1.8.5)
33
+ rack (>= 2.2.4)
34
+ rack-session (>= 1.0.1)
38
35
  rack-test (>= 0.6.3)
39
- rails-dom-testing (~> 2.0)
40
- rails-html-sanitizer (~> 1.0, >= 1.2.0)
41
- actiontext (7.0.4.2)
42
- actionpack (= 7.0.4.2)
43
- activerecord (= 7.0.4.2)
44
- activestorage (= 7.0.4.2)
45
- activesupport (= 7.0.4.2)
36
+ rails-dom-testing (~> 2.2)
37
+ rails-html-sanitizer (~> 1.6)
38
+ useragent (~> 0.16)
39
+ actiontext (8.0.5.1)
40
+ actionpack (= 8.0.5.1)
41
+ activerecord (= 8.0.5.1)
42
+ activestorage (= 8.0.5.1)
43
+ activesupport (= 8.0.5.1)
46
44
  globalid (>= 0.6.0)
47
45
  nokogiri (>= 1.8.5)
48
- actionview (7.0.4.2)
49
- activesupport (= 7.0.4.2)
46
+ actionview (8.0.5.1)
47
+ activesupport (= 8.0.5.1)
50
48
  builder (~> 3.1)
51
- erubi (~> 1.4)
52
- rails-dom-testing (~> 2.0)
53
- rails-html-sanitizer (~> 1.1, >= 1.2.0)
54
- activejob (7.0.4.2)
55
- activesupport (= 7.0.4.2)
49
+ erubi (~> 1.11)
50
+ rails-dom-testing (~> 2.2)
51
+ rails-html-sanitizer (~> 1.6)
52
+ activejob (8.0.5.1)
53
+ activesupport (= 8.0.5.1)
56
54
  globalid (>= 0.3.6)
57
- activemodel (7.0.4.2)
58
- activesupport (= 7.0.4.2)
59
- activerecord (7.0.4.2)
60
- activemodel (= 7.0.4.2)
61
- activesupport (= 7.0.4.2)
62
- activestorage (7.0.4.2)
63
- actionpack (= 7.0.4.2)
64
- activejob (= 7.0.4.2)
65
- activerecord (= 7.0.4.2)
66
- activesupport (= 7.0.4.2)
55
+ activemodel (8.0.5.1)
56
+ activesupport (= 8.0.5.1)
57
+ activerecord (8.0.5.1)
58
+ activemodel (= 8.0.5.1)
59
+ activesupport (= 8.0.5.1)
60
+ timeout (>= 0.4.0)
61
+ activestorage (8.0.5.1)
62
+ actionpack (= 8.0.5.1)
63
+ activejob (= 8.0.5.1)
64
+ activerecord (= 8.0.5.1)
65
+ activesupport (= 8.0.5.1)
67
66
  marcel (~> 1.0)
68
- mini_mime (>= 1.1.0)
69
- activesupport (7.0.4.2)
70
- concurrent-ruby (~> 1.0, >= 1.0.2)
67
+ activesupport (8.0.5.1)
68
+ base64
69
+ benchmark (>= 0.3)
70
+ bigdecimal
71
+ concurrent-ruby (~> 1.0, >= 1.3.1)
72
+ connection_pool (>= 2.2.5)
73
+ drb
71
74
  i18n (>= 1.6, < 2)
75
+ logger (>= 1.4.2)
72
76
  minitest (>= 5.1)
73
- tzinfo (~> 2.0)
74
- ast (2.4.2)
75
- builder (3.2.4)
76
- byebug (11.1.3)
77
- codecov (0.6.0)
78
- simplecov (>= 0.15, < 0.22)
77
+ securerandom (>= 0.3)
78
+ tzinfo (~> 2.0, >= 2.0.5)
79
+ uri (>= 0.13.1)
80
+ ast (2.4.3)
81
+ base64 (0.3.0)
82
+ benchmark (0.5.0)
83
+ bigdecimal (4.1.3)
84
+ builder (3.3.0)
85
+ byebug (13.0.0)
86
+ reline (>= 0.6.0)
79
87
  coderay (1.1.3)
80
- concurrent-ruby (1.2.0)
81
- crass (1.0.6)
82
- date (3.3.3)
83
- diff-lcs (1.5.0)
84
- docile (1.4.0)
85
- erubi (1.12.0)
86
- globalid (1.1.0)
87
- activesupport (>= 5.0)
88
- i18n (1.12.0)
88
+ concurrent-ruby (1.3.8)
89
+ connection_pool (3.0.2)
90
+ crass (1.0.7)
91
+ date (3.5.1)
92
+ diff-lcs (1.6.2)
93
+ drb (2.2.3)
94
+ erb (6.0.7)
95
+ erubi (1.13.1)
96
+ globalid (1.4.0)
97
+ activesupport (>= 6.1)
98
+ i18n (1.15.2)
89
99
  concurrent-ruby (~> 1.0)
90
- json (2.6.3)
91
- loofah (2.19.1)
100
+ io-console (0.9.4)
101
+ irb (1.18.0)
102
+ pp (>= 0.6.0)
103
+ prism (>= 1.3.0)
104
+ rdoc (>= 4.0.0)
105
+ reline (>= 0.4.2)
106
+ json (3.0.2)
107
+ language_server-protocol (3.17.0.6)
108
+ lint_roller (1.1.0)
109
+ logger (1.7.0)
110
+ loofah (2.25.2)
92
111
  crass (~> 1.0.2)
93
- nokogiri (>= 1.5.9)
94
- mail (2.8.0.1)
112
+ nokogiri (>= 1.12.0)
113
+ mail (2.9.1)
114
+ logger
95
115
  mini_mime (>= 0.1.1)
96
116
  net-imap
97
117
  net-pop
98
118
  net-smtp
99
- marcel (1.0.2)
100
- method_source (1.0.0)
101
- mini_mime (1.1.2)
102
- mini_portile2 (2.8.1)
103
- minitest (5.17.0)
104
- net-imap (0.3.4)
119
+ marcel (1.2.1)
120
+ method_source (1.1.0)
121
+ mini_mime (1.1.5)
122
+ minitest (6.0.6)
123
+ drb (~> 2.0)
124
+ prism (~> 1.5)
125
+ net-imap (0.6.7)
105
126
  date
106
127
  net-protocol
107
128
  net-pop (0.1.2)
108
129
  net-protocol
109
- net-protocol (0.2.1)
130
+ net-protocol (0.4.0)
110
131
  timeout
111
- net-smtp (0.3.3)
132
+ net-smtp (0.5.1)
112
133
  net-protocol
113
- nio4r (2.5.8)
114
- nokogiri (1.14.1)
115
- mini_portile2 (~> 2.8.0)
134
+ nio4r (2.7.5)
135
+ nokogiri (1.19.4-arm64-darwin)
116
136
  racc (~> 1.4)
117
- parallel (1.22.1)
118
- parser (3.2.0.0)
137
+ nokogiri (1.19.4-x86_64-linux-gnu)
138
+ racc (~> 1.4)
139
+ parallel (1.28.0)
140
+ parser (3.3.12.0)
119
141
  ast (~> 2.4.1)
120
- pry (0.14.2)
142
+ racc
143
+ pp (0.6.4)
144
+ prettyprint
145
+ prettyprint (0.2.0)
146
+ prism (1.9.0)
147
+ pry (0.16.0)
121
148
  coderay (~> 1.1)
122
149
  method_source (~> 1.0)
123
- pry-byebug (3.10.1)
124
- byebug (~> 11.0)
125
- pry (>= 0.13, < 0.15)
126
- racc (1.6.2)
127
- rack (2.2.6.2)
128
- rack-test (2.0.2)
150
+ reline (>= 0.6.0)
151
+ pry-byebug (3.12.0)
152
+ byebug (~> 13.0)
153
+ pry (>= 0.13, < 0.17)
154
+ racc (1.8.1)
155
+ rack (3.2.7)
156
+ rack-session (2.1.2)
157
+ base64 (>= 0.1.0)
158
+ rack (>= 3.0.0)
159
+ rack-test (2.2.0)
129
160
  rack (>= 1.3)
130
- rails (7.0.4.2)
131
- actioncable (= 7.0.4.2)
132
- actionmailbox (= 7.0.4.2)
133
- actionmailer (= 7.0.4.2)
134
- actionpack (= 7.0.4.2)
135
- actiontext (= 7.0.4.2)
136
- actionview (= 7.0.4.2)
137
- activejob (= 7.0.4.2)
138
- activemodel (= 7.0.4.2)
139
- activerecord (= 7.0.4.2)
140
- activestorage (= 7.0.4.2)
141
- activesupport (= 7.0.4.2)
161
+ rackup (2.3.1)
162
+ rack (>= 3)
163
+ rails (8.0.5.1)
164
+ actioncable (= 8.0.5.1)
165
+ actionmailbox (= 8.0.5.1)
166
+ actionmailer (= 8.0.5.1)
167
+ actionpack (= 8.0.5.1)
168
+ actiontext (= 8.0.5.1)
169
+ actionview (= 8.0.5.1)
170
+ activejob (= 8.0.5.1)
171
+ activemodel (= 8.0.5.1)
172
+ activerecord (= 8.0.5.1)
173
+ activestorage (= 8.0.5.1)
174
+ activesupport (= 8.0.5.1)
142
175
  bundler (>= 1.15.0)
143
- railties (= 7.0.4.2)
144
- rails-dom-testing (2.0.3)
145
- activesupport (>= 4.2.0)
176
+ railties (= 8.0.5.1)
177
+ rails-dom-testing (2.3.0)
178
+ activesupport (>= 5.0.0)
179
+ minitest
146
180
  nokogiri (>= 1.6)
147
- rails-html-sanitizer (1.5.0)
148
- loofah (~> 2.19, >= 2.19.1)
149
- railties (7.0.4.2)
150
- actionpack (= 7.0.4.2)
151
- activesupport (= 7.0.4.2)
152
- method_source
181
+ rails-html-sanitizer (1.7.1)
182
+ loofah (~> 2.25, >= 2.25.2)
183
+ nokogiri (>= 1.15.7, != 1.16.7, != 1.16.6, != 1.16.5, != 1.16.4, != 1.16.3, != 1.16.2, != 1.16.1, != 1.16.0.rc1, != 1.16.0)
184
+ railties (8.0.5.1)
185
+ actionpack (= 8.0.5.1)
186
+ activesupport (= 8.0.5.1)
187
+ irb (~> 1.13)
188
+ rackup (>= 1.0.0)
153
189
  rake (>= 12.2)
154
- thor (~> 1.0)
155
- zeitwerk (~> 2.5)
190
+ thor (~> 1.0, >= 1.2.2)
191
+ tsort (>= 0.2)
192
+ zeitwerk (~> 2.6)
156
193
  rainbow (3.1.1)
157
- rake (13.0.6)
158
- regexp_parser (2.6.2)
159
- rexml (3.2.5)
160
- rspec (3.12.0)
161
- rspec-core (~> 3.12.0)
162
- rspec-expectations (~> 3.12.0)
163
- rspec-mocks (~> 3.12.0)
164
- rspec-core (3.12.0)
165
- rspec-support (~> 3.12.0)
166
- rspec-expectations (3.12.2)
194
+ rake (13.4.2)
195
+ rbs (4.1.3)
196
+ logger
197
+ prism (>= 1.6.0)
198
+ tsort
199
+ rdoc (8.0.0)
200
+ erb
201
+ prism (>= 1.6.0)
202
+ rbs (>= 4.0.0)
203
+ tsort
204
+ regexp_parser (2.13.0)
205
+ reline (0.7.0)
206
+ io-console (~> 0.5)
207
+ rspec (3.13.2)
208
+ rspec-core (~> 3.13.0)
209
+ rspec-expectations (~> 3.13.0)
210
+ rspec-mocks (~> 3.13.0)
211
+ rspec-core (3.13.6)
212
+ rspec-support (~> 3.13.0)
213
+ rspec-expectations (3.13.5)
167
214
  diff-lcs (>= 1.2.0, < 2.0)
168
- rspec-support (~> 3.12.0)
169
- rspec-mocks (3.12.3)
215
+ rspec-support (~> 3.13.0)
216
+ rspec-mocks (3.13.8)
170
217
  diff-lcs (>= 1.2.0, < 2.0)
171
- rspec-support (~> 3.12.0)
172
- rspec-support (3.12.0)
173
- rubocop (1.44.1)
174
- json (~> 2.3)
175
- parallel (~> 1.10)
176
- parser (>= 3.2.0.0)
218
+ rspec-support (~> 3.13.0)
219
+ rspec-support (3.13.7)
220
+ rubocop (1.91.0)
221
+ json (>= 2.3)
222
+ language_server-protocol (~> 3.17.0.2)
223
+ lint_roller (~> 1.1.0)
224
+ parallel (>= 1.10)
225
+ parser (>= 3.3.0.2)
177
226
  rainbow (>= 2.2.2, < 4.0)
178
- regexp_parser (>= 1.8, < 3.0)
179
- rexml (>= 3.2.5, < 4.0)
180
- rubocop-ast (>= 1.24.1, < 2.0)
227
+ regexp_parser (>= 2.9.3, < 3.0)
228
+ rubocop-ast (>= 1.49.0, < 2.0)
181
229
  ruby-progressbar (~> 1.7)
182
- unicode-display_width (>= 2.4.0, < 3.0)
183
- rubocop-ast (1.24.1)
184
- parser (>= 3.1.1.0)
185
- rubocop-capybara (2.17.0)
186
- rubocop (~> 1.41)
187
- rubocop-performance (1.15.2)
188
- rubocop (>= 1.7.0, < 2.0)
189
- rubocop-ast (>= 0.4.0)
190
- rubocop-rails (2.17.4)
230
+ unicode-display_width (>= 2.4.0, < 4.0)
231
+ rubocop-ast (1.50.0)
232
+ parser (>= 3.3.7.2)
233
+ prism (~> 1.7)
234
+ rubocop-performance (1.27.0)
235
+ lint_roller (~> 1.1)
236
+ rubocop (>= 1.89.0, < 2.0)
237
+ rubocop-ast (>= 1.47.1, < 2.0)
238
+ rubocop-rails (2.38.0)
191
239
  activesupport (>= 4.2.0)
240
+ lint_roller (~> 1.1)
192
241
  rack (>= 1.1)
193
- rubocop (>= 1.33.0, < 2.0)
194
- rubocop-rspec (2.18.1)
195
- rubocop (~> 1.33)
196
- rubocop-capybara (~> 2.17)
197
- ruby-progressbar (1.11.0)
198
- simplecov (0.21.2)
199
- docile (~> 1.1)
200
- simplecov-html (~> 0.11)
201
- simplecov_json_formatter (~> 0.1)
202
- simplecov-html (0.12.3)
203
- simplecov_json_formatter (0.1.4)
204
- thor (1.2.1)
205
- timeout (0.3.1)
242
+ rubocop (>= 1.89.0, < 2.0)
243
+ rubocop-ast (>= 1.44.0, < 2.0)
244
+ rubocop-rspec (3.10.2)
245
+ lint_roller (~> 1.1)
246
+ regexp_parser (>= 2.0)
247
+ rubocop (~> 1.86, >= 1.86.2)
248
+ ruby-progressbar (1.13.0)
249
+ securerandom (0.4.1)
250
+ simplecov (1.2.0)
251
+ simplecov-lcov (0.9.0)
252
+ thor (1.5.0)
253
+ timeout (0.6.1)
254
+ tsort (0.2.0)
206
255
  tzinfo (2.0.6)
207
256
  concurrent-ruby (~> 1.0)
208
- unicode-display_width (2.4.2)
209
- websocket-driver (0.7.5)
257
+ unicode-display_width (3.3.0)
258
+ unicode-emoji (~> 4.3)
259
+ unicode-emoji (4.3.0)
260
+ uri (1.1.1)
261
+ useragent (0.16.11)
262
+ websocket-driver (0.8.2)
263
+ base64
210
264
  websocket-extensions (>= 0.1.0)
211
265
  websocket-extensions (0.1.5)
212
- zeitwerk (2.6.6)
266
+ zeitwerk (2.8.3)
213
267
 
214
268
  PLATFORMS
215
- ruby
269
+ arm64-darwin-23
270
+ arm64-darwin-25
271
+ x86_64-linux
216
272
 
217
273
  DEPENDENCIES
218
- bundler (~> 2.0)
219
- codecov
274
+ bundler (~> 4.0)
220
275
  pry-byebug
221
- rails (~> 7.0.0)
276
+ rails (~> 8.0.0)
222
277
  rake (~> 13.0)
223
278
  resource_policy!
224
279
  rspec (~> 3.0)
@@ -227,6 +282,7 @@ DEPENDENCIES
227
282
  rubocop-rails
228
283
  rubocop-rspec
229
284
  simplecov
285
+ simplecov-lcov
230
286
 
231
287
  BUNDLED WITH
232
- 2.0.1
288
+ 4.0.21
data/docs/README.md CHANGED
@@ -51,11 +51,11 @@ class UserPolicy
51
51
  include ResourcePolicy::Policy
52
52
 
53
53
  policy do |c|
54
- c.action(:read).allowed # current_user can always see user
55
- c.action(:write).allowed(if: :admin?) # only admin current_user can update user
54
+ c.action(:show).allowed # current_user can always see user
55
+ c.action(:update).allowed(if: :admin?) # only admin current_user can update user
56
56
  end
57
57
 
58
- def initialize(user, current_user:)
58
+ def initialize(user, current_user)
59
59
  @user = user
60
60
  @current_user = current_user
61
61
  end
@@ -71,9 +71,9 @@ end
71
71
  #### Using action policy
72
72
 
73
73
  ```ruby
74
- policy = UserPolicy.new(user, current_user: current_user)
75
- policy.action(:read).allowed? # => true
76
- policy.action(:write).allowed? # ... depends on `admin?` result
74
+ policy = UserPolicy.new(user, current_user)
75
+ policy.action(:show).allowed? # => true
76
+ policy.action(:update).allowed? # ... depends on `admin?` result
77
77
  ```
78
78
 
79
79
  ### Attributes policy
@@ -92,7 +92,7 @@ class UserPolicy
92
92
  .allowed(:write, if: :admin?) # only admin current_user can change email
93
93
  end
94
94
 
95
- def initialize(user, current_user:)
95
+ def initialize(user, current_user)
96
96
  @user = user
97
97
  @current_user = current_user
98
98
  end
@@ -108,9 +108,9 @@ end
108
108
  #### Using attributes policy
109
109
 
110
110
  ```ruby
111
- policy = UserPolicy.new(user, current_user: current_user)
111
+ policy = UserPolicy.new(user, current_user)
112
112
  policy.attribute(:email).readable? # => true
113
- policy.action(:email).writable? # ... depends on `admin?` result
113
+ policy.attribute(:email).writable? # ... depends on `admin?` result
114
114
  ```
115
115
 
116
116
  #### Using protector
@@ -123,7 +123,7 @@ class UserPolicy
123
123
 
124
124
  policy do |c|
125
125
  c.attribute(:id).allowed(:read)
126
- c.attribute(:salary).allowed(:read, if: :admin?)
126
+ c.attribute(:email).allowed(:read, if: :admin?)
127
127
  end
128
128
 
129
129
  ...
@@ -139,7 +139,8 @@ user = User.find(1337)
139
139
  user.id #=> 1337
140
140
  user.email #=> "john.doe@example.com"
141
141
 
142
- protected_user = UserPolicy.attributes_policy.protect(user, current_user: current_user)
142
+ policy = UserPolicy.new(user, current_user)
143
+ protected_user = policy.protected_resource
143
144
  protected_user.id #=> 1337
144
145
  protected_user.email # nil
145
146
  ```
data/docs/_sidebar.md CHANGED
@@ -2,5 +2,6 @@
2
2
  * Components
3
3
  * [Policy](components/policy)
4
4
  * [ActionsPolicy](components/actions_policy)
5
- * [ActionsPolicy](components/actions_validator)
5
+ * [ActionValidator](components/action_validator)
6
6
  * [AttributesPolicy](components/attributes_policy)
7
+ * [AttributesValidator](components/attributes_validator)
@@ -6,7 +6,7 @@ The `ResourcePolicy::ActionValidator` is a validator used to check the policy of
6
6
 
7
7
  The `ResourcePolicy::ActionValidator` accepts two options:
8
8
 
9
- - `:allowed_to` (required) - Specifies the action type that needs to be checked. This can be a symbol or a string.
9
+ - `:allowed_to` (required) - Specifies the action type that needs to be checked. This can be a symbol, string or proc.
10
10
  - `:as` (optional) - Specifies the key that will be used to display errors. This is useful if you want to rename the attribute being validated.
11
11
 
12
12
  ## Usage Example
@@ -31,4 +31,30 @@ else
31
31
  end
32
32
  ```
33
33
 
34
- In this example, the `SomeClass` has an attribute named `some_policy` which is being validated using the `ResourcePolicy::ActionValidator`. The validator checks if the create action is allowed using the SomePolicy object. If the action is not allowed, an error message will be added to the `record.errors` object with the key `:some_item`. If the `:as` option is not provided, the key used to display the error will be the name of the attribute being validated.
34
+ ## Usage Example with proc
35
+
36
+ ```ruby
37
+ require 'resource_policy/action_validator'
38
+
39
+ class SomeClass < ActiveRecord::Base
40
+ validates :some_policy, 'resource_policy/action': { allowed_to: -> { custom_allowed_to }, as: :some_item }
41
+
42
+ def some_policy
43
+ SomePolicy.new
44
+ end
45
+
46
+ def custom_allowed_to
47
+ new_record? ? :create : :update
48
+ end
49
+ end
50
+
51
+ new_object = SomeClass.new
52
+ new_object.valid?
53
+ new_object.errors.messages # => { some_item: ['action "create" is not allowed'] }
54
+
55
+ existing_object = SomeClass.find(1)
56
+ existing_object.valid?
57
+ existing_object.errors.messages # => { some_item: ['action "update" is not allowed'] }
58
+ ```
59
+
60
+ In this example, the `SomeClass` has an attribute named `some_policy` which is validated using the `ResourcePolicy::ActionValidator`. The validator calls `custom_allowed_to` method on our model to determine which action to use. If record is new, it will check policy for the `create` action. If the record saved, it will check policy for `update` action. When the action is not allowed, an error message will be added to the `record.errors` object with the key `:some_item`. If the `:as` option is not provided, the key used to display the error will be the name of the attribute being validated.