Skip to content

Commit bdddbca

Browse files
committed
applying PR: stffn#197
Use references with includes to make sure the query doesn't fail
1 parent 45e91af commit bdddbca

2 files changed

Lines changed: 10 additions & 27 deletions

File tree

lib/declarative_authorization/obligation_scope.rb

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,13 @@ def scope
5757
self
5858
elsif Rails.version < "4"
5959
# for Rails < 4: use scoped method
60-
self.klass.scoped(@finder_options)
60+
self.klass.scoped(finder_options)
6161
else
6262
# TODO Refactor this. There is certainly a better way.
63-
self.klass.joins(@finder_options[:joins]).includes(@finder_options[:include]).where(@finder_options[:conditions])
63+
self.klass.joins(finder_options[:joins]).
64+
includes(finder_options[:include]).
65+
references(finder_options[:include]).
66+
where(finder_options[:conditions])
6467
end
6568
end
6669

test/model_test.rb

Lines changed: 5 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -186,11 +186,7 @@ def test_multiple_deep_ored_belongs_to
186186
:test_another_model_id => test_model_2.id
187187

188188
user = MockUser.new(:test_role, :id => test_attr_1)
189-
if Rails.version >= '4'
190-
assert_equal 1, TestAttr.with_permissions_to(:read, :user => user).references(:test_attrs, :test_attrs_test_models, :test_attrs_test_models_2).length
191-
else
192-
assert_equal 1, TestAttr.with_permissions_to(:read, :user => user).length
193-
end
189+
assert_equal 1, TestAttr.with_permissions_to(:read, :user => user).length
194190

195191
TestAttr.delete_all
196192
TestModel.delete_all
@@ -1210,11 +1206,7 @@ def test_with_anded_if_permitted_to
12101206
assert Authorization::Engine.instance.permit?(:read, :object => test_model_1.test_attrs.first, :user => user_with_both_roles)
12111207
assert Authorization::Engine.instance.permit?(:read, :object => test_model_for_second_role.test_attrs.first, :user => user_with_both_roles)
12121208
#p Authorization::Engine.instance.obligations(:read, :user => user_with_both_roles, :context => :test_attrs)
1213-
if Rails.version >= '4'
1214-
assert_equal 2, TestAttr.with_permissions_to(:read, :user => user_with_both_roles).references(:test_attrs, :test_models).length
1215-
else
1216-
assert_equal 1, TestAttr.with_permissions_to(:read, :user => user).length
1217-
end
1209+
assert_equal 1, TestAttr.with_permissions_to(:read, :user => user).length
12181210

12191211
TestModel.delete_all
12201212
TestAttr.delete_all
@@ -1475,11 +1467,7 @@ def test_with_ored_rules_and_reoccuring_tables
14751467
test_attr_2.test_model.test_attrs.create!
14761468

14771469
user = MockUser.new(:test_role, :test_attr => test_attr_2.test_model.test_attrs.last)
1478-
if Rails.version >= '4'
1479-
assert_equal 2, TestAttr.with_permissions_to(:read, :user => user).references(:test_attrs, :test_models, :test_models_test_attrs, :test_attrs_test_models).length
1480-
else
1481-
assert_equal 2, TestAttr.with_permissions_to(:read, :user => user).length
1482-
end
1470+
assert_equal 2, TestAttr.with_permissions_to(:read, :user => user).length
14831471
TestModel.delete_all
14841472
TestAttr.delete_all
14851473
end
@@ -1516,11 +1504,7 @@ def test_with_many_ored_rules_and_reoccuring_tables
15161504

15171505
user = MockUser.new(:test_role, :test_model => country.test_models.first)
15181506

1519-
if Rails.version >= '4'
1520-
assert_equal 2, TestAttr.with_permissions_to(:read, :user => user).references(:test_attrs, :test_models, :test_models_countries).length
1521-
else
1522-
assert_equal 2, TestAttr.with_permissions_to(:read, :user => user).length
1523-
end
1507+
assert_equal 2, TestAttr.with_permissions_to(:read, :user => user).length
15241508
TestModel.delete_all
15251509
TestAttr.delete_all
15261510
end
@@ -1933,11 +1917,7 @@ def test_multiple_roles_with_has_many_through
19331917
user = MockUser.new(:test_role_1, :test_role_2,
19341918
:test_attr_through_id => test_model_1.test_attr_throughs.first.id,
19351919
:test_attr_through_2_id => test_model_2.test_attr_throughs.first.id)
1936-
if Rails.version >= '4'
1937-
assert_equal 2, TestModel.with_permissions_to(:read, :user => user).references(:test_models, :test_attr_throughs).length
1938-
else
1939-
assert_equal 2, TestModel.with_permissions_to(:read, :user => user).length
1940-
end
1920+
assert_equal 2, TestModel.with_permissions_to(:read, :user => user).length
19411921
TestModel.delete_all
19421922
TestAttr.delete_all
19431923
TestAttrThrough.delete_all

0 commit comments

Comments
 (0)