Skip to content

Commit

Permalink
applying PR: stffn#197
Browse files Browse the repository at this point in the history
Use references with includes to make sure the query doesn't fail
  • Loading branch information
dgm committed Feb 25, 2016
1 parent 45e91af commit bdddbca
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 27 deletions.
7 changes: 5 additions & 2 deletions lib/declarative_authorization/obligation_scope.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,13 @@ def scope
self
elsif Rails.version < "4"
# for Rails < 4: use scoped method
self.klass.scoped(@finder_options)
self.klass.scoped(finder_options)
else
# TODO Refactor this. There is certainly a better way.
self.klass.joins(@finder_options[:joins]).includes(@finder_options[:include]).where(@finder_options[:conditions])
self.klass.joins(finder_options[:joins]).
includes(finder_options[:include]).
references(finder_options[:include]).
where(finder_options[:conditions])
end
end

Expand Down
30 changes: 5 additions & 25 deletions test/model_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -186,11 +186,7 @@ def test_multiple_deep_ored_belongs_to
:test_another_model_id => test_model_2.id

user = MockUser.new(:test_role, :id => test_attr_1)
if Rails.version >= '4'
assert_equal 1, TestAttr.with_permissions_to(:read, :user => user).references(:test_attrs, :test_attrs_test_models, :test_attrs_test_models_2).length
else
assert_equal 1, TestAttr.with_permissions_to(:read, :user => user).length
end
assert_equal 1, TestAttr.with_permissions_to(:read, :user => user).length

TestAttr.delete_all
TestModel.delete_all
Expand Down Expand Up @@ -1210,11 +1206,7 @@ def test_with_anded_if_permitted_to
assert Authorization::Engine.instance.permit?(:read, :object => test_model_1.test_attrs.first, :user => user_with_both_roles)
assert Authorization::Engine.instance.permit?(:read, :object => test_model_for_second_role.test_attrs.first, :user => user_with_both_roles)
#p Authorization::Engine.instance.obligations(:read, :user => user_with_both_roles, :context => :test_attrs)
if Rails.version >= '4'
assert_equal 2, TestAttr.with_permissions_to(:read, :user => user_with_both_roles).references(:test_attrs, :test_models).length
else
assert_equal 1, TestAttr.with_permissions_to(:read, :user => user).length
end
assert_equal 1, TestAttr.with_permissions_to(:read, :user => user).length

TestModel.delete_all
TestAttr.delete_all
Expand Down Expand Up @@ -1475,11 +1467,7 @@ def test_with_ored_rules_and_reoccuring_tables
test_attr_2.test_model.test_attrs.create!

user = MockUser.new(:test_role, :test_attr => test_attr_2.test_model.test_attrs.last)
if Rails.version >= '4'
assert_equal 2, TestAttr.with_permissions_to(:read, :user => user).references(:test_attrs, :test_models, :test_models_test_attrs, :test_attrs_test_models).length
else
assert_equal 2, TestAttr.with_permissions_to(:read, :user => user).length
end
assert_equal 2, TestAttr.with_permissions_to(:read, :user => user).length
TestModel.delete_all
TestAttr.delete_all
end
Expand Down Expand Up @@ -1516,11 +1504,7 @@ def test_with_many_ored_rules_and_reoccuring_tables

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

if Rails.version >= '4'
assert_equal 2, TestAttr.with_permissions_to(:read, :user => user).references(:test_attrs, :test_models, :test_models_countries).length
else
assert_equal 2, TestAttr.with_permissions_to(:read, :user => user).length
end
assert_equal 2, TestAttr.with_permissions_to(:read, :user => user).length
TestModel.delete_all
TestAttr.delete_all
end
Expand Down Expand Up @@ -1933,11 +1917,7 @@ def test_multiple_roles_with_has_many_through
user = MockUser.new(:test_role_1, :test_role_2,
:test_attr_through_id => test_model_1.test_attr_throughs.first.id,
:test_attr_through_2_id => test_model_2.test_attr_throughs.first.id)
if Rails.version >= '4'
assert_equal 2, TestModel.with_permissions_to(:read, :user => user).references(:test_models, :test_attr_throughs).length
else
assert_equal 2, TestModel.with_permissions_to(:read, :user => user).length
end
assert_equal 2, TestModel.with_permissions_to(:read, :user => user).length
TestModel.delete_all
TestAttr.delete_all
TestAttrThrough.delete_all
Expand Down

0 comments on commit bdddbca

Please sign in to comment.