cancancan 1.15.0 → 1.16.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (41) hide show
  1. checksums.yaml +4 -4
  2. data/.rubocop.yml +38 -0
  3. data/.rubocop_todo.yml +48 -0
  4. data/.travis.yml +8 -2
  5. data/Appraisals +1 -0
  6. data/CHANGELOG.rdoc +5 -0
  7. data/Gemfile +1 -1
  8. data/README.md +58 -41
  9. data/Rakefile +7 -3
  10. data/cancancan.gemspec +13 -12
  11. data/gemfiles/activerecord_4.2.gemfile +1 -0
  12. data/lib/cancan.rb +2 -2
  13. data/lib/cancan/ability.rb +26 -24
  14. data/lib/cancan/controller_additions.rb +33 -23
  15. data/lib/cancan/controller_resource.rb +83 -56
  16. data/lib/cancan/exceptions.rb +1 -1
  17. data/lib/cancan/matchers.rb +2 -2
  18. data/lib/cancan/model_adapters/abstract_adapter.rb +8 -8
  19. data/lib/cancan/model_adapters/active_record_4_adapter.rb +48 -35
  20. data/lib/cancan/model_adapters/active_record_adapter.rb +18 -17
  21. data/lib/cancan/model_adapters/mongoid_adapter.rb +26 -21
  22. data/lib/cancan/model_adapters/sequel_adapter.rb +12 -12
  23. data/lib/cancan/model_additions.rb +0 -1
  24. data/lib/cancan/rule.rb +23 -17
  25. data/lib/cancan/version.rb +1 -1
  26. data/lib/generators/cancan/ability/ability_generator.rb +1 -1
  27. data/spec/cancan/ability_spec.rb +189 -180
  28. data/spec/cancan/controller_additions_spec.rb +77 -64
  29. data/spec/cancan/controller_resource_spec.rb +230 -228
  30. data/spec/cancan/exceptions_spec.rb +20 -20
  31. data/spec/cancan/inherited_resource_spec.rb +21 -21
  32. data/spec/cancan/matchers_spec.rb +12 -12
  33. data/spec/cancan/model_adapters/active_record_4_adapter_spec.rb +38 -32
  34. data/spec/cancan/model_adapters/active_record_adapter_spec.rb +155 -145
  35. data/spec/cancan/model_adapters/default_adapter_spec.rb +2 -2
  36. data/spec/cancan/model_adapters/mongoid_adapter_spec.rb +87 -88
  37. data/spec/cancan/model_adapters/sequel_adapter_spec.rb +44 -47
  38. data/spec/cancan/rule_spec.rb +18 -18
  39. data/spec/spec_helper.rb +2 -2
  40. data/spec/support/ability.rb +0 -1
  41. metadata +60 -19
@@ -1,7 +1,7 @@
1
- require "spec_helper"
1
+ require 'spec_helper'
2
2
 
3
3
  describe CanCan::ModelAdapters::DefaultAdapter do
4
- it "is default for generic classes" do
4
+ it 'is default for generic classes' do
5
5
  expect(CanCan::ModelAdapters::AbstractAdapter.adapter_class(Object)).to eq(CanCan::ModelAdapters::DefaultAdapter)
6
6
  end
7
7
  end
@@ -1,4 +1,4 @@
1
- require "spec_helper"
1
+ require 'spec_helper'
2
2
 
3
3
  if defined? CanCan::ModelAdapters::MongoidAdapter
4
4
 
@@ -22,11 +22,11 @@ if defined? CanCan::ModelAdapters::MongoidAdapter
22
22
  end
23
23
 
24
24
  Mongoid.configure do |config|
25
- config.master = Mongo::Connection.new('127.0.0.1', 27017).db("cancan_mongoid_spec")
25
+ config.master = Mongo::Connection.new('127.0.0.1', 27_017).db('cancan_mongoid_spec')
26
26
  end
27
27
 
28
28
  describe CanCan::ModelAdapters::MongoidAdapter do
29
- context "Mongoid defined" do
29
+ context 'Mongoid defined' do
30
30
  before(:each) do
31
31
  (@ability = double).extend(CanCan::Ability)
32
32
  end
@@ -37,136 +37,137 @@ if defined? CanCan::ModelAdapters::MongoidAdapter
37
37
  end.each(&:drop)
38
38
  end
39
39
 
40
- it "is for only Mongoid classes" do
40
+ it 'is for only Mongoid classes' do
41
41
  expect(CanCan::ModelAdapters::MongoidAdapter).not_to be_for_class(Object)
42
42
  expect(CanCan::ModelAdapters::MongoidAdapter).to be_for_class(MongoidProject)
43
- expect(CanCan::ModelAdapters::AbstractAdapter.adapter_class(MongoidProject)).to eq(CanCan::ModelAdapters::MongoidAdapter)
43
+ expect(CanCan::ModelAdapters::AbstractAdapter.adapter_class(MongoidProject))
44
+ .to eq(CanCan::ModelAdapters::MongoidAdapter)
44
45
  end
45
46
 
46
- it "finds record" do
47
+ it 'finds record' do
47
48
  project = MongoidProject.create
48
49
  expect(CanCan::ModelAdapters::MongoidAdapter.find(MongoidProject, project.id)).to eq(project)
49
50
  end
50
51
 
51
- it "compares properties on mongoid documents with the conditions hash" do
52
+ it 'compares properties on mongoid documents with the conditions hash' do
52
53
  model = MongoidProject.new
53
- @ability.can :read, MongoidProject, :id => model.id
54
+ @ability.can :read, MongoidProject, id: model.id
54
55
  expect(@ability).to be_able_to(:read, model)
55
56
  end
56
57
 
57
- it "is able to read hashes when field is array" do
58
- one_to_three = MongoidProject.create(:numbers => ['one', 'two', 'three'])
59
- two_to_five = MongoidProject.create(:numbers => ['two', 'three', 'four', 'five'])
58
+ it 'is able to read hashes when field is array' do
59
+ one_to_three = MongoidProject.create(numbers: %w(one two three))
60
+ two_to_five = MongoidProject.create(numbers: %w(two three four five))
60
61
 
61
- @ability.can :foo, MongoidProject, :numbers => 'one'
62
+ @ability.can :foo, MongoidProject, numbers: 'one'
62
63
  expect(@ability).to be_able_to(:foo, one_to_three)
63
64
  expect(@ability).not_to be_able_to(:foo, two_to_five)
64
65
  end
65
66
 
66
- it "returns [] when no ability is defined so no records are found" do
67
- MongoidProject.create(:title => 'Sir')
68
- MongoidProject.create(:title => 'Lord')
69
- MongoidProject.create(:title => 'Dude')
67
+ it 'returns [] when no ability is defined so no records are found' do
68
+ MongoidProject.create(title: 'Sir')
69
+ MongoidProject.create(title: 'Lord')
70
+ MongoidProject.create(title: 'Dude')
70
71
 
71
72
  expect(MongoidProject.accessible_by(@ability, :read).entries).to eq([])
72
73
  end
73
74
 
74
- it "returns the correct records based on the defined ability" do
75
- @ability.can :read, MongoidProject, :title => "Sir"
76
- sir = MongoidProject.create(:title => 'Sir')
77
- lord = MongoidProject.create(:title => 'Lord')
78
- dude = MongoidProject.create(:title => 'Dude')
75
+ it 'returns the correct records based on the defined ability' do
76
+ @ability.can :read, MongoidProject, title: 'Sir'
77
+ sir = MongoidProject.create(title: 'Sir')
78
+ MongoidProject.create(title: 'Lord')
79
+ MongoidProject.create(title: 'Dude')
79
80
 
80
81
  expect(MongoidProject.accessible_by(@ability, :read).entries).to eq([sir])
81
82
  end
82
83
 
83
- it "returns the correct records when a mix of can and cannot rules in defined ability" do
84
- @ability.can :manage, MongoidProject, :title => 'Sir'
84
+ it 'returns the correct records when a mix of can and cannot rules in defined ability' do
85
+ @ability.can :manage, MongoidProject, title: 'Sir'
85
86
  @ability.cannot :destroy, MongoidProject
86
87
 
87
- sir = MongoidProject.create(:title => 'Sir')
88
- lord = MongoidProject.create(:title => 'Lord')
89
- dude = MongoidProject.create(:title => 'Dude')
88
+ sir = MongoidProject.create(title: 'Sir')
89
+ MongoidProject.create(title: 'Lord')
90
+ MongoidProject.create(title: 'Dude')
90
91
 
91
92
  expect(MongoidProject.accessible_by(@ability, :destroy).entries).to eq([sir])
92
93
  end
93
94
 
94
- it "is able to mix empty conditions and hashes" do
95
+ it 'is able to mix empty conditions and hashes' do
95
96
  @ability.can :read, MongoidProject
96
- @ability.can :read, MongoidProject, :title => 'Sir'
97
- sir = MongoidProject.create(:title => 'Sir')
98
- lord = MongoidProject.create(:title => 'Lord')
97
+ @ability.can :read, MongoidProject, title: 'Sir'
98
+ MongoidProject.create(title: 'Sir')
99
+ MongoidProject.create(title: 'Lord')
99
100
 
100
101
  expect(MongoidProject.accessible_by(@ability, :read).count).to eq(2)
101
102
  end
102
103
 
103
- it "returns everything when the defined ability is access all" do
104
+ it 'returns everything when the defined ability is access all' do
104
105
  @ability.can :manage, :all
105
- sir = MongoidProject.create(:title => 'Sir')
106
- lord = MongoidProject.create(:title => 'Lord')
107
- dude = MongoidProject.create(:title => 'Dude')
106
+ sir = MongoidProject.create(title: 'Sir')
107
+ lord = MongoidProject.create(title: 'Lord')
108
+ dude = MongoidProject.create(title: 'Dude')
108
109
 
109
110
  expect(MongoidProject.accessible_by(@ability, :read).entries).to eq([sir, lord, dude])
110
111
  end
111
112
 
112
- it "allows a scope for conditions" do
113
- @ability.can :read, MongoidProject, MongoidProject.where(:title => 'Sir')
114
- sir = MongoidProject.create(:title => 'Sir')
115
- lord = MongoidProject.create(:title => 'Lord')
116
- dude = MongoidProject.create(:title => 'Dude')
113
+ it 'allows a scope for conditions' do
114
+ @ability.can :read, MongoidProject, MongoidProject.where(title: 'Sir')
115
+ sir = MongoidProject.create(title: 'Sir')
116
+ MongoidProject.create(title: 'Lord')
117
+ MongoidProject.create(title: 'Dude')
117
118
 
118
119
  expect(MongoidProject.accessible_by(@ability, :read).entries).to eq([sir])
119
120
  end
120
121
 
121
- describe "Mongoid::Criteria where clause Symbol extensions using MongoDB expressions" do
122
- it "handles :field.in" do
123
- obj = MongoidProject.create(:title => 'Sir')
124
- @ability.can :read, MongoidProject, :title.in => ["Sir", "Madam"]
122
+ describe 'Mongoid::Criteria where clause Symbol extensions using MongoDB expressions' do
123
+ it 'handles :field.in' do
124
+ obj = MongoidProject.create(title: 'Sir')
125
+ @ability.can :read, MongoidProject, :title.in => %w(Sir Madam)
125
126
  expect(@ability.can?(:read, obj)).to eq(true)
126
127
  expect(MongoidProject.accessible_by(@ability, :read)).to eq([obj])
127
128
 
128
- obj2 = MongoidProject.create(:title => 'Lord')
129
+ obj2 = MongoidProject.create(title: 'Lord')
129
130
  expect(@ability.can?(:read, obj2)).to be(false)
130
131
  end
131
132
 
132
- describe "activates only when there are Criteria in the hash" do
133
- it "Calls where on the model class when there are criteria" do
134
- obj = MongoidProject.create(:title => 'Bird')
135
- @conditions = {:title.nin => ["Fork", "Spoon"]}
133
+ describe 'activates only when there are Criteria in the hash' do
134
+ it 'Calls where on the model class when there are criteria' do
135
+ obj = MongoidProject.create(title: 'Bird')
136
+ @conditions = { :title.nin => %w(Fork Spoon) }
136
137
 
137
138
  @ability.can :read, MongoidProject, @conditions
138
139
  expect(@ability).to be_able_to(:read, obj)
139
140
  end
140
- it "Calls the base version if there are no mongoid criteria" do
141
- obj = MongoidProject.new(:title => 'Bird')
142
- @conditions = {:id => obj.id}
141
+ it 'Calls the base version if there are no mongoid criteria' do
142
+ obj = MongoidProject.new(title: 'Bird')
143
+ @conditions = { id: obj.id }
143
144
  @ability.can :read, MongoidProject, @conditions
144
145
  expect(@ability).to be_able_to(:read, obj)
145
146
  end
146
147
  end
147
148
 
148
- it "handles :field.nin" do
149
- obj = MongoidProject.create(:title => 'Sir')
150
- @ability.can :read, MongoidProject, :title.nin => ["Lord", "Madam"]
149
+ it 'handles :field.nin' do
150
+ obj = MongoidProject.create(title: 'Sir')
151
+ @ability.can :read, MongoidProject, :title.nin => %w(Lord Madam)
151
152
  expect(@ability.can?(:read, obj)).to eq(true)
152
153
  expect(MongoidProject.accessible_by(@ability, :read)).to eq([obj])
153
154
 
154
- obj2 = MongoidProject.create(:title => 'Lord')
155
+ obj2 = MongoidProject.create(title: 'Lord')
155
156
  expect(@ability.can?(:read, obj2)).to be(false)
156
157
  end
157
158
 
158
- it "handles :field.size" do
159
- obj = MongoidProject.create(:titles => ['Palatin', 'Margrave'])
159
+ it 'handles :field.size' do
160
+ obj = MongoidProject.create(titles: %w(Palatin Margrave))
160
161
  @ability.can :read, MongoidProject, :titles.size => 2
161
162
  expect(@ability.can?(:read, obj)).to eq(true)
162
163
  expect(MongoidProject.accessible_by(@ability, :read)).to eq([obj])
163
164
 
164
- obj2 = MongoidProject.create(:titles => ['Palatin', 'Margrave', 'Marquis'])
165
+ obj2 = MongoidProject.create(titles: %w(Palatin Margrave Marquis))
165
166
  expect(@ability.can?(:read, obj2)).to be(false)
166
167
  end
167
168
 
168
- it "handles :field.exists" do
169
- obj = MongoidProject.create(:titles => ['Palatin', 'Margrave'])
169
+ it 'handles :field.exists' do
170
+ obj = MongoidProject.create(titles: %w(Palatin Margrave))
170
171
  @ability.can :read, MongoidProject, :titles.exists => true
171
172
  expect(@ability.can?(:read, obj)).to eq(true)
172
173
  expect(MongoidProject.accessible_by(@ability, :read)).to eq([obj])
@@ -175,73 +176,71 @@ if defined? CanCan::ModelAdapters::MongoidAdapter
175
176
  expect(@ability.can?(:read, obj2)).to be(false)
176
177
  end
177
178
 
178
- it "handles :field.gt" do
179
- obj = MongoidProject.create(:age => 50)
179
+ it 'handles :field.gt' do
180
+ obj = MongoidProject.create(age: 50)
180
181
  @ability.can :read, MongoidProject, :age.gt => 45
181
182
  expect(@ability.can?(:read, obj)).to eq(true)
182
183
  expect(MongoidProject.accessible_by(@ability, :read)).to eq([obj])
183
184
 
184
- obj2 = MongoidProject.create(:age => 40)
185
+ obj2 = MongoidProject.create(age: 40)
185
186
  expect(@ability.can?(:read, obj2)).to be(false)
186
187
  end
187
188
 
188
- it "handles instance not saved to database" do
189
- obj = MongoidProject.new(:title => 'Sir')
190
- @ability.can :read, MongoidProject, :title.in => ["Sir", "Madam"]
189
+ it 'handles instance not saved to database' do
190
+ obj = MongoidProject.new(title: 'Sir')
191
+ @ability.can :read, MongoidProject, :title.in => %w(Sir Madam)
191
192
  expect(@ability.can?(:read, obj)).to eq(true)
192
193
 
193
194
  # accessible_by only returns saved records
194
195
  expect(MongoidProject.accessible_by(@ability, :read).entries).to eq([])
195
196
 
196
- obj2 = MongoidProject.new(:title => 'Lord')
197
+ obj2 = MongoidProject.new(title: 'Lord')
197
198
  expect(@ability.can?(:read, obj2)).to be(false)
198
199
  end
199
200
  end
200
201
 
201
- it "calls where with matching ability conditions" do
202
- obj = MongoidProject.create(:foo => {:bar => 1})
203
- @ability.can :read, MongoidProject, :foo => {:bar => 1}
202
+ it 'calls where with matching ability conditions' do
203
+ obj = MongoidProject.create(foo: { bar: 1 })
204
+ @ability.can :read, MongoidProject, foo: { bar: 1 }
204
205
  expect(MongoidProject.accessible_by(@ability, :read).entries.first).to eq(obj)
205
206
  end
206
207
 
207
- it "excludes from the result if set to cannot" do
208
- obj = MongoidProject.create(:bar => 1)
209
- obj2 = MongoidProject.create(:bar => 2)
208
+ it 'excludes from the result if set to cannot' do
209
+ obj = MongoidProject.create(bar: 1)
210
+ MongoidProject.create(bar: 2)
210
211
  @ability.can :read, MongoidProject
211
- @ability.cannot :read, MongoidProject, :bar => 2
212
+ @ability.cannot :read, MongoidProject, bar: 2
212
213
  expect(MongoidProject.accessible_by(@ability, :read).entries).to eq([obj])
213
214
  end
214
215
 
215
- it "combines the rules" do
216
- obj = MongoidProject.create(:bar => 1)
217
- obj2 = MongoidProject.create(:bar => 2)
218
- obj3 = MongoidProject.create(:bar => 3)
219
- @ability.can :read, MongoidProject, :bar => 1
220
- @ability.can :read, MongoidProject, :bar => 2
216
+ it 'combines the rules' do
217
+ obj = MongoidProject.create(bar: 1)
218
+ obj2 = MongoidProject.create(bar: 2)
219
+ MongoidProject.create(bar: 3)
220
+ @ability.can :read, MongoidProject, bar: 1
221
+ @ability.can :read, MongoidProject, bar: 2
221
222
  expect(MongoidProject.accessible_by(@ability, :read).entries).to match_array([obj, obj2])
222
223
  end
223
224
 
224
- it "does not allow to fetch records when ability with just block present" do
225
+ it 'does not allow to fetch records when ability with just block present' do
225
226
  @ability.can :read, MongoidProject do
226
227
  false
227
228
  end
228
- expect {
229
+ expect do
229
230
  MongoidProject.accessible_by(@ability)
230
- }.to raise_error(CanCan::Error)
231
+ end.to raise_error(CanCan::Error)
231
232
  end
232
233
 
233
- it "can handle nested queries for accessible_by" do
234
- @ability.can :read, MongoidSubProject, {:mongoid_project => {:mongoid_category => { :name => 'Authorization'}}}
234
+ it 'can handle nested queries for accessible_by' do
235
+ @ability.can :read, MongoidSubProject, mongoid_project: { mongoid_category: { name: 'Authorization' } }
235
236
  cat1 = MongoidCategory.create name: 'Authentication'
236
237
  cat2 = MongoidCategory.create name: 'Authorization'
237
238
  proj1 = cat1.mongoid_projects.create name: 'Proj1'
238
239
  proj2 = cat2.mongoid_projects.create name: 'Proj2'
239
240
  sub1 = proj1.mongoid_sub_projects.create name: 'Sub1'
240
- sub2 = proj2.mongoid_sub_projects.create name: 'Sub2'
241
+ proj2.mongoid_sub_projects.create name: 'Sub2'
241
242
  expect(MongoidSubProject.accessible_by(@ability)).to match_array([sub1])
242
243
  end
243
-
244
-
245
244
  end
246
245
  end
247
246
  end
@@ -1,13 +1,12 @@
1
- require "spec_helper"
1
+ require 'spec_helper'
2
2
 
3
3
  if defined? CanCan::ModelAdapters::SequelAdapter
4
-
5
4
  describe CanCan::ModelAdapters::SequelAdapter do
6
5
  DB = if RUBY_PLATFORM == 'java'
7
- Sequel.connect('jdbc:sqlite:db.sqlite3')
8
- else
9
- Sequel.sqlite
10
- end
6
+ Sequel.connect('jdbc:sqlite:db.sqlite3')
7
+ else
8
+ Sequel.sqlite
9
+ end
11
10
 
12
11
  DB.create_table :users do
13
12
  primary_key :id
@@ -49,83 +48,81 @@ if defined? CanCan::ModelAdapters::SequelAdapter
49
48
  (@ability = double).extend(CanCan::Ability)
50
49
  end
51
50
 
52
- it "should be for only sequel model classes" do
51
+ it 'should be for only sequel model classes' do
53
52
  expect(CanCan::ModelAdapters::SequelAdapter).to_not be_for_class(Object)
54
53
  expect(CanCan::ModelAdapters::SequelAdapter).to be_for_class(Article)
55
54
  expect(CanCan::ModelAdapters::AbstractAdapter.adapter_class(Article)).to eq CanCan::ModelAdapters::SequelAdapter
56
55
  end
57
56
 
58
- it "should find record" do
57
+ it 'should find record' do
59
58
  article = Article.create
60
59
  expect(CanCan::ModelAdapters::SequelAdapter.find(Article, article.id)).to eq article
61
60
  end
62
61
 
63
- it "should not fetch any records when no abilities are defined" do
62
+ it 'should not fetch any records when no abilities are defined' do
64
63
  Article.create
65
64
  expect(Article.accessible_by(@ability).all).to be_empty
66
65
  end
67
66
 
68
- it "should fetch all articles when one can read all" do
67
+ it 'should fetch all articles when one can read all' do
69
68
  @ability.can :read, Article
70
69
  article = Article.create
71
70
  expect(Article.accessible_by(@ability).all).to eq [article]
72
71
  end
73
72
 
74
- it "should fetch only the articles that are published" do
75
- @ability.can :read, Article, :published => true
76
- article1 = Article.create(:published => true)
77
- article2 = Article.create(:published => false)
73
+ it 'should fetch only the articles that are published' do
74
+ @ability.can :read, Article, published: true
75
+ article1 = Article.create(published: true)
76
+ Article.create(published: false)
78
77
  expect(Article.accessible_by(@ability).all).to eq [article1]
79
78
  end
80
79
 
81
- it "should fetch any articles which are published or secret" do
82
- @ability.can :read, Article, :published => true
83
- @ability.can :read, Article, :secret => true
84
- article1 = Article.create(:published => true, :secret => false)
85
- article2 = Article.create(:published => true, :secret => true)
86
- article3 = Article.create(:published => false, :secret => true)
87
- article4 = Article.create(:published => false, :secret => false)
80
+ it 'should fetch any articles which are published or secret' do
81
+ @ability.can :read, Article, published: true
82
+ @ability.can :read, Article, secret: true
83
+ article1 = Article.create(published: true, secret: false)
84
+ article2 = Article.create(published: true, secret: true)
85
+ article3 = Article.create(published: false, secret: true)
86
+ Article.create(published: false, secret: false)
88
87
  expect(Article.accessible_by(@ability).all).to eq([article1, article2, article3])
89
88
  end
90
89
 
91
- it "should fetch only the articles that are published and not secret" do
92
- @ability.can :read, Article, :published => true
93
- @ability.cannot :read, Article, :secret => true
94
- article1 = Article.create(:published => true, :secret => false)
95
- article2 = Article.create(:published => true, :secret => true)
96
- article3 = Article.create(:published => false, :secret => true)
97
- article4 = Article.create(:published => false, :secret => false)
90
+ it 'should fetch only the articles that are published and not secret' do
91
+ @ability.can :read, Article, published: true
92
+ @ability.cannot :read, Article, secret: true
93
+ article1 = Article.create(published: true, secret: false)
94
+ Article.create(published: true, secret: true)
95
+ Article.create(published: false, secret: true)
96
+ Article.create(published: false, secret: false)
98
97
  expect(Article.accessible_by(@ability).all).to eq [article1]
99
98
  end
100
99
 
101
- it "should only read comments for articles which are published" do
102
- @ability.can :read, Comment, :article => { :published => true }
103
- comment1 = Comment.create(:article => Article.create(:published => true))
104
- comment2 = Comment.create(:article => Article.create(:published => false))
100
+ it 'should only read comments for articles which are published' do
101
+ @ability.can :read, Comment, article: { published: true }
102
+ comment1 = Comment.create(article: Article.create(published: true))
103
+ Comment.create(article: Article.create(published: false))
105
104
  expect(Comment.accessible_by(@ability).all).to eq [comment1]
106
105
  end
107
106
 
108
107
  it "should only read comments for articles which are published and user is 'me'" do
109
- @ability.can :read, Comment, :article => { :user => { :name => 'me' }, :published => true }
110
- user1 = User.create(:name => 'me')
111
- comment1 = Comment.create(:article => Article.create(:published => true, :user => user1))
112
- comment2 = Comment.create(:article => Article.create(:published => true))
113
- comment3 = Comment.create(:article => Article.create(:published => false, :user => user1))
108
+ @ability.can :read, Comment, article: { user: { name: 'me' }, published: true }
109
+ user1 = User.create(name: 'me')
110
+ comment1 = Comment.create(article: Article.create(published: true, user: user1))
111
+ Comment.create(article: Article.create(published: true))
112
+ Comment.create(article: Article.create(published: false, user: user1))
114
113
  expect(Comment.accessible_by(@ability).all).to eq [comment1]
115
114
  end
116
115
 
117
- it "should allow conditions in SQL and merge with hash conditions" do
118
- @ability.can :read, Article, :published => true
119
- @ability.can :read, Article, ["secret=?", true] do |article|
120
- article.secret
121
- end
122
- @ability.cannot :read, Article, "priority > 1" do |article|
116
+ it 'should allow conditions in SQL and merge with hash conditions' do
117
+ @ability.can :read, Article, published: true
118
+ @ability.can :read, Article, ['secret=?', true], &:secret
119
+ @ability.cannot :read, Article, 'priority > 1' do |article|
123
120
  article.priority > 1
124
121
  end
125
- article1 = Article.create(:published => true, :secret => false, :priority => 1)
126
- article2 = Article.create(:published => true, :secret => true, :priority => 1)
127
- article3 = Article.create(:published => true, :secret => true, :priority => 2)
128
- article4 = Article.create(:published => false, :secret => false, :priority => 2)
122
+ article1 = Article.create(published: true, secret: false, priority: 1)
123
+ article2 = Article.create(published: true, secret: true, priority: 1)
124
+ Article.create(published: true, secret: true, priority: 2)
125
+ Article.create(published: false, secret: false, priority: 2)
129
126
  expect(Article.accessible_by(@ability).all).to eq [article1, article2]
130
127
  end
131
128
  end