protector 0.5.2 → 0.5.3

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: 18a3586352df222ca67b0aa27be8711af98e9d44
4
- data.tar.gz: 5a55e8b998ba4ec4af975d05adb0d8687e2c38d7
3
+ metadata.gz: 823427ab7061d2b26ab6835d3c4bbfd17abe6430
4
+ data.tar.gz: dfa3edc7c59fca658f81ef6f1d377890a6ac9c1a
5
5
  SHA512:
6
- metadata.gz: 0dca6468a9b82c7e95615d46f2b93eab694b9ca9400a1c9b6dfbfea7d71320083c9b3731ec063c3078203e64d536c2a68704ce88845b071e13c1445bd89aa29b
7
- data.tar.gz: ef7c2c455894bf6d96adbd224c7cdd4922485e34d7112f988ade24338f18e7b6e982063b7fa3ebdb006f2adc4e62e1b4e60605d6ef99fc06fd2fea9f8c3691e1
6
+ metadata.gz: 4897c4ccea8d4b074158ba15b33395ec04522c067dc3a4c065366a6c8ceb81a1b9133c3862ed1625818c2f4d260107483f078277403b5375f09456d02f8e24d1
7
+ data.tar.gz: c9229df3995d7d6f71e0d71d638e6d1273ca165977badcdec63f643074a4598dbd370ae446c58cfa9f4df6bd418f7959c577cf19d1b1c7cd611764d062043557
@@ -10,7 +10,7 @@ GIT
10
10
  PATH
11
11
  remote: /Users/inossidabile/Repos/protector
12
12
  specs:
13
- protector (0.5.2)
13
+ protector (0.5.3)
14
14
  activesupport
15
15
  i18n
16
16
 
@@ -10,7 +10,7 @@ GIT
10
10
  PATH
11
11
  remote: /Users/inossidabile/Repos/protector
12
12
  specs:
13
- protector (0.5.2)
13
+ protector (0.5.3)
14
14
  activesupport
15
15
  i18n
16
16
 
@@ -19,7 +19,7 @@ GIT
19
19
  PATH
20
20
  remote: /Users/inossidabile/Repos/protector
21
21
  specs:
22
- protector (0.5.2)
22
+ protector (0.5.3)
23
23
  activesupport
24
24
  i18n
25
25
 
@@ -19,7 +19,7 @@ GIT
19
19
  PATH
20
20
  remote: /Users/inossidabile/Repos/protector
21
21
  specs:
22
- protector (0.5.2)
22
+ protector (0.5.3)
23
23
  activesupport
24
24
  i18n
25
25
 
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: /Users/inossidabile/Repos/protector
3
3
  specs:
4
- protector (0.5.2)
4
+ protector (0.5.3)
5
5
  activesupport
6
6
  i18n
7
7
 
@@ -56,6 +56,15 @@ module Protector
56
56
  end
57
57
 
58
58
  module ClassMethods
59
+ # Storage of {Protector::DSL::Meta}
60
+ def protector_meta
61
+ @protector_meta ||= Protector::DSL::Meta.new(
62
+ Protector::Adapters::ActiveRecord,
63
+ self,
64
+ self.column_names
65
+ )
66
+ end
67
+
59
68
  # Wraps every `.field` method with a check against {Protector::DSL::Meta::Box#readable?}
60
69
  def define_method_attribute(name)
61
70
  super
@@ -79,13 +88,7 @@ module Protector
79
88
 
80
89
  # Storage for {Protector::DSL::Meta::Box}
81
90
  def protector_meta(subject=protector_subject)
82
- @protector_meta ||= self.class.protector_meta.evaluate(
83
- Protector::Adapters::ActiveRecord,
84
- self.class,
85
- subject,
86
- self.class.column_names,
87
- self
88
- )
91
+ @protector_meta ||= self.class.protector_meta.evaluate(subject, self)
89
92
  end
90
93
 
91
94
  # Checks if current model can be selected in the context of current subject
@@ -31,11 +31,7 @@ module Protector
31
31
  def scope_with_protector(*args)
32
32
  return scope_without_protector unless protector_subject?
33
33
 
34
- @meta ||= klass.protector_meta.evaluate(
35
- Protector::Adapters::ActiveRecord,
36
- klass,
37
- protector_subject
38
- )
34
+ @meta ||= klass.protector_meta.evaluate(protector_subject)
39
35
 
40
36
  scope_without_protector.merge(@meta.relation)
41
37
  end
@@ -28,12 +28,7 @@ module Protector
28
28
 
29
29
  # Gets {Protector::DSL::Meta::Box} of this relation
30
30
  def protector_meta(subject=protector_subject)
31
- # We don't seem to require columns here as well
32
- @klass.protector_meta.evaluate(
33
- Protector::Adapters::ActiveRecord,
34
- @klass,
35
- subject
36
- )
31
+ @klass.protector_meta.evaluate(subject)
37
32
  end
38
33
 
39
34
  # @note Unscoped relation drops properties and therefore should be re-restricted
@@ -124,11 +119,7 @@ module Protector
124
119
  # AR drops default_scope for eagerly loadable associations
125
120
  # https://github.com/inossidabile/protector/issues/3
126
121
  # and so should we
127
- meta = klass.protector_meta.evaluate(
128
- Protector::Adapters::ActiveRecord,
129
- klass,
130
- subject
131
- )
122
+ meta = klass.protector_meta.evaluate(subject)
132
123
 
133
124
  if meta.scoped?
134
125
  unscoped = klass.unscoped
@@ -32,11 +32,7 @@ module Protector
32
32
 
33
33
  # Gets {Protector::DSL::Meta::Box} of this dataset
34
34
  def protector_meta(subject=protector_subject)
35
- model.protector_meta.evaluate(
36
- Protector::Adapters::Sequel,
37
- model,
38
- subject
39
- )
35
+ model.protector_meta.evaluate(subject)
40
36
  end
41
37
 
42
38
  # Substitutes `row_proc` with {Protector} and injects protection scope
@@ -57,11 +53,7 @@ module Protector
57
53
  @opts[:eager_graph][:reflections].each do |association, reflection|
58
54
  model = reflection[:cache][:class] if reflection[:cache].is_a?(Hash) && reflection[:cache][:class]
59
55
  model = reflection[:class_name].constantize unless model
60
- meta = model.protector_meta.evaluate(
61
- Protector::Adapters::Sequel,
62
- model,
63
- subject
64
- )
56
+ meta = model.protector_meta.evaluate(subject)
65
57
 
66
58
  relation = relation.instance_eval(&meta.scope_proc) if meta.scoped?
67
59
  end
@@ -16,6 +16,15 @@ module Protector
16
16
  end
17
17
 
18
18
  module ClassMethods
19
+ # Storage of {Protector::DSL::Meta}
20
+ def protector_meta
21
+ @protector_meta ||= Protector::DSL::Meta.new(
22
+ Protector::Adapters::Sequel,
23
+ self,
24
+ self.columns
25
+ )
26
+ end
27
+
19
28
  # Gets default restricted `Dataset`
20
29
  def restrict!(*args)
21
30
  dataset.clone.restrict! *args
@@ -24,13 +33,7 @@ module Protector
24
33
 
25
34
  # Storage for {Protector::DSL::Meta::Box}
26
35
  def protector_meta(subject=protector_subject)
27
- @protector_meta ||= self.class.protector_meta.evaluate(
28
- Protector::Adapters::Sequel,
29
- self.class,
30
- subject,
31
- self.class.columns,
32
- self
33
- )
36
+ @protector_meta ||= self.class.protector_meta.evaluate(subject, self)
34
37
  end
35
38
 
36
39
  # Checks if current model can be selected in the context of current subject
data/lib/protector/dsl.rb CHANGED
@@ -202,6 +202,12 @@ module Protector
202
202
  end
203
203
  end
204
204
 
205
+ def initialize(adapter, model, fields)
206
+ @adapter = adapter
207
+ @model = model
208
+ @fields = fields
209
+ end
210
+
205
211
  # Storage for `protect` blocks
206
212
  def blocks
207
213
  @blocks ||= []
@@ -218,8 +224,8 @@ module Protector
218
224
  # @param subject [Object] Restriction subject
219
225
  # @param fields [Array<String>] All the fields the model has
220
226
  # @param entry [Object] An instance of the model
221
- def evaluate(adapter, model, subject, fields=[], entry=nil)
222
- Box.new(adapter, model, fields, subject, entry, blocks)
227
+ def evaluate(subject, entry=nil)
228
+ Box.new(@adapter, @model, @fields, subject, entry, blocks)
223
229
  end
224
230
  end
225
231
 
@@ -269,11 +275,6 @@ module Protector
269
275
  def protect(&block)
270
276
  protector_meta << block
271
277
  end
272
-
273
- # Storage of {Protector::DSL::Meta}
274
- def protector_meta
275
- @protector_meta ||= Meta.new
276
- end
277
278
  end
278
279
  end
279
280
  end
@@ -1,4 +1,4 @@
1
1
  module Protector
2
2
  # Gem version
3
- VERSION = "0.5.2"
3
+ VERSION = "0.5.3"
4
4
  end
data/spec/lib/dsl_spec.rb CHANGED
@@ -38,7 +38,13 @@ describe Protector::DSL do
38
38
 
39
39
  describe Protector::DSL::Entry do
40
40
  before :each do
41
- @entry = Class.new{ include Protector::DSL::Entry }
41
+ @entry = Class.new do
42
+ include Protector::DSL::Entry
43
+
44
+ def self.protector_meta
45
+ @protector_meta ||= Protector::DSL::Meta.new nil, nil, []
46
+ end
47
+ end
42
48
  end
43
49
 
44
50
  it "instantiates meta entity" do
@@ -55,8 +61,7 @@ describe Protector::DSL do
55
61
  l = lambda {|x| x > 4 }
56
62
 
57
63
  before :each do
58
- @meta = Protector::DSL::Meta.new
59
-
64
+ @meta = Protector::DSL::Meta.new nil, nil, %w(field1 field2 field3 field4 field5)
60
65
  @meta << lambda {
61
66
  can :view
62
67
  }
@@ -84,30 +89,30 @@ describe Protector::DSL do
84
89
  end
85
90
 
86
91
  it "evaluates" do
87
- @meta.evaluate(nil, nil, 'user', [], 'entry')
92
+ @meta.evaluate('user', 'entry')
88
93
  end
89
94
 
90
95
  context "adequate", paranoid: false do
91
96
  it "sets scoped?" do
92
- data = @meta.evaluate(nil, nil, nil, [], 'entry')
97
+ data = @meta.evaluate(nil, 'entry')
93
98
  data.scoped?.should == false
94
99
  end
95
100
  end
96
101
 
97
102
  context "paranoid", paranoid: true do
98
103
  it "sets scoped?" do
99
- data = @meta.evaluate(nil, nil, nil, [], 'entry')
104
+ data = @meta.evaluate(nil, 'entry')
100
105
  data.scoped?.should == true
101
106
  end
102
107
  end
103
108
 
104
109
  it "sets relation" do
105
- data = @meta.evaluate(nil, nil, 'user', [], 'entry')
110
+ data = @meta.evaluate('user', 'entry')
106
111
  data.relation.should == 'relation'
107
112
  end
108
113
 
109
114
  it "sets access" do
110
- data = @meta.evaluate(nil, nil, 'user', %w(field1 field2 field3 field4 field5), 'entry')
115
+ data = @meta.evaluate('user', 'entry')
111
116
  data.access.should == {
112
117
  update: {
113
118
  "field1" => nil,
@@ -126,24 +131,24 @@ describe Protector::DSL do
126
131
  end
127
132
 
128
133
  it "marks destroyable" do
129
- data = @meta.evaluate(nil, nil, 'user', [], 'entry')
134
+ data = @meta.evaluate('user', 'entry')
130
135
  data.destroyable?.should == true
131
136
  end
132
137
 
133
138
  it "marks updatable" do
134
- data = @meta.evaluate(nil, nil, 'user', [], 'entry')
139
+ data = @meta.evaluate('user', 'entry')
135
140
  data.updatable?.should == true
136
141
  end
137
142
 
138
143
  it "marks creatable" do
139
- data = @meta.evaluate(nil, nil, 'user', [], 'entry')
144
+ data = @meta.evaluate('user', 'entry')
140
145
  data.creatable?.should == false
141
146
  end
142
147
  end
143
148
 
144
149
  context "custom methods" do
145
150
  before :each do
146
- @meta = Protector::DSL::Meta.new
151
+ @meta = Protector::DSL::Meta.new nil, nil, %w(field1 field2)
147
152
 
148
153
  @meta << lambda {
149
154
  can :drink, :field1
@@ -153,13 +158,13 @@ describe Protector::DSL do
153
158
  end
154
159
 
155
160
  it "sets field-level restriction" do
156
- box = @meta.evaluate(nil, nil, 'user', %w(field1 field2), 'entry')
161
+ box = @meta.evaluate('user', 'entry')
157
162
  box.can?(:drink, :field1).should == true
158
163
  box.can?(:drink).should == true
159
164
  end
160
165
 
161
166
  it "sets field-level protection" do
162
- box = @meta.evaluate(nil, nil, 'user', %w(field1 field2), 'entry')
167
+ box = @meta.evaluate('user', 'entry')
163
168
  box.can?(:eat, :field1).should == false
164
169
  box.can?(:eat).should == true
165
170
  end
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.5.2
4
+ version: 0.5.3
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-07-13 00:00:00.000000000 Z
11
+ date: 2013-07-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport