moat 0.1 → 0.2

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
  SHA1:
3
- metadata.gz: 159b847055cd79c58ce1e5b99788d74d78cb7dae
4
- data.tar.gz: b5393955e97f8b5ccb8f9d8ff1dc29942307ca33
3
+ metadata.gz: e99313f748a138e0c9e6f5899ad3e7df1f255d8c
4
+ data.tar.gz: 4c9638daa7e628b3ce83d5e154bdb002762e5e78
5
5
  SHA512:
6
- metadata.gz: 1f00dbf92a42d220e5ee6ff92a6f3a39994d4d37904a73ac4c328356a01796273cc1f74afa6621707c00936965d21ed2e490bfc640843f1b77dbb56b577bed84
7
- data.tar.gz: cc8fbf90c9e3200b14c2f09646fe1ba9229b2451c6e15ca1ef016e508038e7638a4605bfd5c2532851aafe8815c2462af7d3beb9d263c355657cdd330bc89169
6
+ metadata.gz: 580d3411e85435820dcdcfb4bfcd7ae0f3411744744b1291a6986d1d121bb4396b9c6231674a418d0cd53209d770c4a085a1f9990ecb45b6c31e9e8670e32739
7
+ data.tar.gz: 7a91973ffcf54b70b5fb05367f2439ad1a4faba44dd7232ed5f459c4923979762cc6100c8435286811050d4cfa2aa464d386d1adb072e3adb331df7d0df531c6
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- moat (0.1)
4
+ moat (0.2)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
data/README.md CHANGED
@@ -319,6 +319,14 @@ describe ThingPolicy do
319
319
  end
320
320
  ```
321
321
 
322
+ If a non-standard scope is required for filters, it can be overridden. It
323
+ defaults to the `all` relation for ActiveRecord models or a simple Array
324
+ otherwise.
325
+
326
+ ```ruby
327
+ scope { resource.container }
328
+ ```
329
+
322
330
  ## Ensure all policies have full test coverage
323
331
 
324
332
  ```ruby
@@ -78,6 +78,8 @@ module Moat
78
78
  object.policy_class
79
79
  elsif object.class.respond_to?(:policy_class)
80
80
  object.class.policy_class
81
+ elsif object.respond_to?(:to_ary)
82
+ find_policy(object.to_ary.first)
81
83
  else
82
84
  infer_policy(object)
83
85
  end
@@ -138,7 +138,7 @@ module Moat
138
138
  end
139
139
 
140
140
  def permitted_through_filters(policy_class)
141
- policy_instance = policy_class::Filter.new(public_send(role), policy_example_resource.class.all)
141
+ policy_instance = policy_class::Filter.new(public_send(role), policy_example_scope)
142
142
  policy_filters.select do |filter|
143
143
  policy_instance.public_send(filter).include?(policy_example_resource)
144
144
  end
@@ -160,10 +160,15 @@ module Moat
160
160
  alias_method :role, :roles
161
161
 
162
162
  def resource(&block)
163
- fail ArgumentError, "#resource called without a block" if block.nil?
163
+ fail ArgumentError, "#{__method__} called without a block" unless block
164
164
  let(:policy_example_resource) { instance_eval(&block) }
165
165
  end
166
166
 
167
+ def scope(&block)
168
+ fail ArgumentError, "#{__method__} called without a block" unless block
169
+ let(:policy_example_scope) { instance_eval(&block) }
170
+ end
171
+
167
172
  def policy_filters(*filters)
168
173
  let(:policy_filters) { filters }
169
174
  end
@@ -187,6 +192,15 @@ module Moat
187
192
  let(:policy_example_resource) do
188
193
  fail NotImplementedError, "A resource has not been defined"
189
194
  end
195
+
196
+ # a scope that contains at least the resource
197
+ let(:policy_example_scope) do
198
+ if policy_example_resource.class.respond_to?(:all)
199
+ policy_example_resource.class.all
200
+ else
201
+ [policy_example_resource]
202
+ end
203
+ end
190
204
  end
191
205
  end
192
206
  end
@@ -1,3 +1,3 @@
1
1
  module Moat
2
- VERSION = "0.1".freeze
2
+ VERSION = "0.2".freeze
3
3
  end
@@ -113,25 +113,24 @@ describe Moat do
113
113
  end
114
114
 
115
115
  it "fails if a corresponding action can't be found" do
116
- expect { moat_consumer.policy_filter([1, 2, 3], :invalid_action, policy: IntegerPolicy) }.
116
+ expect { moat_consumer.policy_filter([1, 2, 3], :invalid_action) }.
117
117
  to raise_error(Moat::ActionNotFoundError, "IntegerPolicy::Filter#invalid_action")
118
118
  end
119
119
 
120
120
  it "returns the value of applying a policy scope filter to the original scope" do
121
- expect(moat_consumer.policy_filter([1, 2, 3, 4, 5], policy: IntegerPolicy)).to eql([2, 4])
121
+ expect(moat_consumer.policy_filter([1, 2, 3, 4, 5])).to eql([2, 4])
122
122
  end
123
123
 
124
124
  it "uses specified action" do
125
- expect(moat_consumer.policy_filter([2, 3], :show, policy: IntegerPolicy)).to eql([3])
125
+ expect(moat_consumer.policy_filter([2, 3], :show)).to eql([3])
126
126
  end
127
127
 
128
128
  it "uses specified policy" do
129
- expect(moat_consumer.policy_filter([2, 3], policy: OtherIntegerPolicy)).
130
- to eql([3])
129
+ expect(moat_consumer.policy_filter([2, 3], policy: OtherIntegerPolicy)).to eql([3])
131
130
  end
132
131
 
133
132
  it "uses specified user" do
134
- expect(moat_consumer.policy_filter([2, 3], user: "specified user", policy: IntegerPolicy)).to eql([2, 3])
133
+ expect(moat_consumer.policy_filter([2, 3], user: "specified user")).to eql([2, 3])
135
134
  end
136
135
  end
137
136
 
@@ -149,7 +148,7 @@ describe Moat do
149
148
  end
150
149
 
151
150
  it "fails if a corresponding action can't be found" do
152
- expect { moat_consumer.authorize([1, 2, 3], :invalid_action?, policy: IntegerPolicy) }.
151
+ expect { moat_consumer.authorize([1, 2, 3], :invalid_action?) }.
153
152
  to raise_error(Moat::ActionNotFoundError, "IntegerPolicy::Authorization#invalid_action?")
154
153
  end
155
154
 
@@ -184,7 +183,7 @@ describe Moat do
184
183
  end
185
184
  context "policy_filter called" do
186
185
  it "does not raise an exception" do
187
- moat_consumer.policy_filter([1, 2], policy: IntegerPolicy)
186
+ moat_consumer.policy_filter([1, 2])
188
187
  expect { moat_consumer.verify_policy_applied }.not_to raise_error
189
188
  end
190
189
  end
@@ -281,7 +280,7 @@ describe Moat do
281
280
  end
282
281
 
283
282
  it "infers a policy from an object's `model` method" do
284
- class DefinesModelName
283
+ class DefinesModel
285
284
  def self.model
286
285
  Fake
287
286
  end
@@ -290,7 +289,25 @@ describe Moat do
290
289
  [11, 12]
291
290
  end
292
291
  end
293
- expect(moat_consumer.policy_filter(DefinesModelName)).to eql([11, 12])
292
+ expect(moat_consumer.policy_filter(DefinesModel)).to eql([11, 12])
293
+ end
294
+
295
+ it "infers a policy from when scope is a non-Array that implements to_ary" do
296
+ class IntContainer
297
+ def initialize(*numbers)
298
+ @numbers = numbers
299
+ end
300
+
301
+ def to_ary
302
+ @numbers
303
+ end
304
+
305
+ def select
306
+ @numbers.select { |n| yield(n) }
307
+ end
308
+ end
309
+
310
+ expect(moat_consumer.policy_filter(IntContainer.new(1, 2, 3, 4, 5))).to eql([2, 4])
294
311
  end
295
312
  end
296
313
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: moat
3
3
  version: !ruby/object:Gem::Version
4
- version: '0.1'
4
+ version: '0.2'
5
5
  platform: ruby
6
6
  authors:
7
7
  - Poll Everywhere
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-06-14 00:00:00.000000000 Z
11
+ date: 2018-08-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rspec