protector 0.7.1 → 0.7.2

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
  SHA1:
3
- metadata.gz: 8046a17b4ab071f2e5af6511c48a213eb46a8c8a
4
- data.tar.gz: 789efaa941d0b0fa7590b2d91e6a7cbfac879975
3
+ metadata.gz: 4db3bb657e21375e7642202dac85b1c694a9b572
4
+ data.tar.gz: 3e3e2498415ba55e7fd8b2c6cdc4ff432aaaf8b0
5
5
  SHA512:
6
- metadata.gz: bc9c1a541de4183af641b8b1d5f498d27eb3804e284d0682f83bba43653ba86475165468f9099dd48a1ccbff266d33724dae82a1737a9c57df5cdc839b3a923b
7
- data.tar.gz: c863b7e9044aa4e0b46776280be869df766a2e631746efb048c5ed62ecbd71ccdad82987f0abadeafeb3a5cdd700f45c8a479cde2c73e5d31b1990cfb2e95b01
6
+ metadata.gz: 27cbad32b5ac59f45af321ae849854146c69ec716b4c7cd51ae0941621b5971352c569746bf352ed3879bb3fa42af76b419d71be604d7e0faa7740c908e9934d
7
+ data.tar.gz: 77170151fc817bad0d5913ff82025f98e3ef31346062c68821cae69f9165eb80f16aedcbf21aab0a03b4b74f1b8937780ca1d8bff3993059bdeddd5a0c2d7b9e
data/Rakefile CHANGED
@@ -9,7 +9,7 @@ task :default => :all
9
9
 
10
10
  desc 'Test the plugin under all supported Rails versions.'
11
11
  task :all do |t|
12
- exec('bundle exec appraisal install; bundle exec appraisal rspec')
12
+ exec('bundle exec appraisal rspec')
13
13
  end
14
14
 
15
15
  task :perf do
@@ -1,6 +1,8 @@
1
1
  require 'protector/adapters/active_record/base'
2
2
  require 'protector/adapters/active_record/association'
3
+ require 'protector/adapters/active_record/singular_association'
3
4
  require 'protector/adapters/active_record/relation'
5
+ require 'protector/adapters/active_record/collection_proxy'
4
6
  require 'protector/adapters/active_record/preloader'
5
7
  require 'protector/adapters/active_record/strong_parameters'
6
8
 
@@ -15,9 +17,11 @@ module Protector
15
17
  ::ActiveRecord::Base.send :include, Protector::Adapters::ActiveRecord::Base
16
18
  ::ActiveRecord::Relation.send :include, Protector::Adapters::ActiveRecord::Relation
17
19
  ::ActiveRecord::Associations::SingularAssociation.send :include, Protector::Adapters::ActiveRecord::Association
20
+ ::ActiveRecord::Associations::SingularAssociation.send :include, Protector::Adapters::ActiveRecord::SingularAssociation
18
21
  ::ActiveRecord::Associations::CollectionAssociation.send :include, Protector::Adapters::ActiveRecord::Association
19
22
  ::ActiveRecord::Associations::Preloader.send :include, Protector::Adapters::ActiveRecord::Preloader
20
23
  ::ActiveRecord::Associations::Preloader::Association.send :include, Protector::Adapters::ActiveRecord::Preloader::Association
24
+ ::ActiveRecord::Associations::CollectionProxy.send :include, Protector::Adapters::ActiveRecord::CollectionProxy
21
25
  end
22
26
 
23
27
  def self.modern?
@@ -6,6 +6,8 @@ module Protector
6
6
  extend ActiveSupport::Concern
7
7
 
8
8
  included do
9
+ include Protector::DSL::Base
10
+
9
11
  # AR 4 has renamed `scoped` to `scope`
10
12
  if method_defined?(:scope)
11
13
  alias_method_chain :scope, :protector
@@ -14,20 +16,20 @@ module Protector
14
16
  alias_method 'scoped', 'scope_with_protector'
15
17
  end
16
18
 
17
- alias_method_chain :build, :protector
19
+ alias_method_chain :build_record, :protector
18
20
  end
19
21
 
20
22
  # Wraps every association with current subject
21
23
  def scope_with_protector(*args)
22
24
  scope = scope_without_protector(*args)
23
- scope = scope.restrict!(owner.protector_subject) if owner.protector_subject?
25
+ scope = scope.restrict!(protector_subject) if protector_subject?
24
26
  scope
25
27
  end
26
28
 
27
29
  # Forwards protection subject to the new instance
28
- def build_with_protector(*args, &block)
29
- return build_without_protector(*args, &block) unless owner.protector_subject?
30
- build_without_protector(*args, &block).restrict!(owner.protector_subject)
30
+ def build_record_with_protector(*args)
31
+ return build_record_without_protector(*args) unless protector_subject?
32
+ build_record_without_protector(*args).restrict!(protector_subject)
31
33
  end
32
34
  end
33
35
  end
@@ -60,6 +60,11 @@ module Protector
60
60
  end
61
61
  # rubocop:enable ParenthesesAroundCondition
62
62
  end
63
+
64
+ def association(*params)
65
+ return super unless protector_subject?
66
+ super.restrict!(protector_subject)
67
+ end
63
68
  end
64
69
 
65
70
  module ClassMethods
@@ -0,0 +1,16 @@
1
+ module Protector
2
+ module Adapters
3
+ module ActiveRecord
4
+ # Patches `ActiveRecord::Associations::CollectionProxy`
5
+ module CollectionProxy
6
+ extend ActiveSupport::Concern
7
+ delegate :protector_subject, :protector_subject?, :to => :@association
8
+
9
+ def restrict!(*args)
10
+ @association.restrict!(*args)
11
+ self
12
+ end
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,27 @@
1
+ module Protector
2
+ module Adapters
3
+ module ActiveRecord
4
+ # Patches `ActiveRecord::Associations::SingularAssociation`
5
+ module SingularAssociation
6
+ extend ActiveSupport::Concern
7
+
8
+ included do
9
+ alias_method_chain :reader, :protector
10
+ end
11
+
12
+ # Reader has to be explicitly overrided for cases when the
13
+ # loaded association is cached
14
+ def reader_with_protector(*args)
15
+ return reader_without_protector(*args) unless protector_subject?
16
+ reader_without_protector(*args).try :restrict!, protector_subject
17
+ end
18
+
19
+ # Forwards protection subject to the new instance
20
+ def build_record_with_protector(*args)
21
+ return build_record_without_protector(*args) unless protector_subject?
22
+ build_record_without_protector(*args).restrict!(protector_subject)
23
+ end
24
+ end
25
+ end
26
+ end
27
+ end
@@ -1,4 +1,4 @@
1
1
  module Protector
2
2
  # Gem version
3
- VERSION = '0.7.1'
3
+ VERSION = '0.7.2'
4
4
  end
@@ -137,6 +137,7 @@ if defined?(ActiveRecord)
137
137
  Dummy.restrict!('!').where(number: 999).to_a.first.protector_subject.should == '!'
138
138
  Dummy.restrict!('!').new.protector_subject.should == '!'
139
139
  Dummy.restrict!('!').first.fluffies.new.protector_subject.should == '!'
140
+ Dummy.first.fluffies.restrict!('!').new.protector_subject.should == '!'
140
141
  end
141
142
 
142
143
  it "checks creatability" do
@@ -242,6 +243,149 @@ if defined?(ActiveRecord)
242
243
  end
243
244
  end
244
245
 
246
+ #
247
+ # Model scope
248
+ #
249
+ describe Protector::Adapters::ActiveRecord::Association do
250
+ describe "validates on create! within association" do
251
+ it "when restricted from entity" do
252
+ expect { Dummy.first.restrict!('-').fluffies.create!(string: 'test').delete }.to raise_error
253
+ end
254
+
255
+ it "when restricted from association" do
256
+ expect { Dummy.first.fluffies.restrict!('-').create!(string: 'test').delete }.to raise_error
257
+ end
258
+ end
259
+
260
+ context "singular association" do
261
+ it "forwards subject" do
262
+ Fluffy.restrict!('!').first.dummy.protector_subject.should == '!'
263
+ Fluffy.first.restrict!('!').dummy.protector_subject.should == '!'
264
+ end
265
+
266
+ it "forwards cached subject" do
267
+ Dummy.first.fluffies.restrict!('!').first.dummy.protector_subject.should == '!'
268
+ end
269
+ end
270
+
271
+ context "collection association" do
272
+ it "forwards subject" do
273
+ Dummy.restrict!('!').first.fluffies.protector_subject.should == '!'
274
+ Dummy.first.restrict!('!').fluffies.protector_subject.should == '!'
275
+ Dummy.restrict!('!').first.fluffies.new.protector_subject.should == '!'
276
+ Dummy.first.restrict!('!').fluffies.new.protector_subject.should == '!'
277
+ Dummy.first.fluffies.restrict!('!').new.protector_subject.should == '!'
278
+ end
279
+
280
+ context "with open relation" do
281
+ context "adequate", paranoid: false do
282
+
283
+ it "checks existence" do
284
+ Dummy.first.fluffies.any?.should == true
285
+ Dummy.first.restrict!('!').fluffies.any?.should == true
286
+ Dummy.first.fluffies.restrict!('!').any?.should == true
287
+ end
288
+
289
+ it "counts" do
290
+ Dummy.first.fluffies.count.should == 2
291
+
292
+ fluffies = Dummy.first.restrict!('!').fluffies
293
+ fluffies.count.should == 2
294
+ fluffies.protector_subject?.should == true
295
+
296
+ fluffies = Dummy.first.fluffies.restrict!('!')
297
+ fluffies.count.should == 2
298
+ fluffies.protector_subject?.should == true
299
+ end
300
+
301
+ it "fetches" do
302
+ Dummy.first.fluffies.count.should == 2
303
+ Dummy.first.restrict!('!').fluffies.length.should == 2
304
+ Dummy.first.fluffies.restrict!('!').length.should == 2
305
+ end
306
+ end
307
+
308
+ context "paranoid", paranoid: true do
309
+ it "checks existence" do
310
+ Dummy.first.fluffies.any?.should == true
311
+ Dummy.first.restrict!('!').fluffies.any?.should == false
312
+ Dummy.first.fluffies.restrict!('!').any?.should == false
313
+ end
314
+
315
+ it "counts" do
316
+ Dummy.first.fluffies.count.should == 2
317
+
318
+ fluffies = Dummy.first.restrict!('!').fluffies
319
+ fluffies.count.should == 0
320
+ fluffies.protector_subject?.should == true
321
+
322
+ fluffies = Dummy.first.fluffies.restrict!('!')
323
+ fluffies.count.should == 0
324
+ fluffies.protector_subject?.should == true
325
+ end
326
+
327
+ it "fetches" do
328
+ Dummy.first.fluffies.count.should == 2
329
+ Dummy.first.restrict!('!').fluffies.length.should == 0
330
+ Dummy.first.fluffies.restrict!('!').length.should == 0
331
+ end
332
+ end
333
+ end
334
+ end
335
+
336
+ context "with null relation" do
337
+ it "checks existence" do
338
+ Dummy.first.fluffies.any?.should == true
339
+ Dummy.first.restrict!('-').fluffies.any?.should == false
340
+ Dummy.first.fluffies.restrict!('-').any?.should == false
341
+ end
342
+
343
+ it "counts" do
344
+ Dummy.first.fluffies.count.should == 2
345
+
346
+ fluffies = Dummy.first.restrict!('-').fluffies
347
+ fluffies.count.should == 0
348
+ fluffies.protector_subject?.should == true
349
+
350
+ fluffies = Dummy.first.fluffies.restrict!('-')
351
+ fluffies.count.should == 0
352
+ fluffies.protector_subject?.should == true
353
+ end
354
+
355
+ it "fetches" do
356
+ Dummy.first.fluffies.count.should == 2
357
+ Dummy.first.restrict!('-').fluffies.length.should == 0
358
+ Dummy.first.fluffies.restrict!('-').length.should == 0
359
+ end
360
+ end
361
+
362
+ context "with active relation" do
363
+ it "checks existence" do
364
+ Dummy.first.fluffies.any?.should == true
365
+ Dummy.first.restrict!('+').fluffies.any?.should == true
366
+ Dummy.first.fluffies.restrict!('+').any?.should == true
367
+ end
368
+
369
+ it "counts" do
370
+ Dummy.first.fluffies.count.should == 2
371
+
372
+ fluffies = Dummy.first.restrict!('+').fluffies
373
+ fluffies.count.should == 1
374
+ fluffies.protector_subject?.should == true
375
+
376
+ fluffies = Dummy.first.fluffies.restrict!('+')
377
+ fluffies.count.should == 1
378
+ fluffies.protector_subject?.should == true
379
+ end
380
+
381
+ it "fetches" do
382
+ Dummy.first.fluffies.count.should == 2
383
+ Dummy.first.restrict!('+').fluffies.length.should == 1
384
+ Dummy.first.fluffies.restrict!('+').length.should == 1
385
+ end
386
+ end
387
+ end
388
+
245
389
  #
246
390
  # Eager loading
247
391
  #
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: protector
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.7.1
4
+ version: 0.7.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Boris Staal
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-12-02 00:00:00.000000000 Z
11
+ date: 2013-12-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -66,8 +66,10 @@ files:
66
66
  - lib/protector/adapters/active_record.rb
67
67
  - lib/protector/adapters/active_record/association.rb
68
68
  - lib/protector/adapters/active_record/base.rb
69
+ - lib/protector/adapters/active_record/collection_proxy.rb
69
70
  - lib/protector/adapters/active_record/preloader.rb
70
71
  - lib/protector/adapters/active_record/relation.rb
72
+ - lib/protector/adapters/active_record/singular_association.rb
71
73
  - lib/protector/adapters/active_record/strong_parameters.rb
72
74
  - lib/protector/adapters/sequel.rb
73
75
  - lib/protector/adapters/sequel/dataset.rb