Skip to content

Commit

Permalink
Make it safe to call Thing.with_role(:admin, user) even if user is no…
Browse files Browse the repository at this point in the history
…t persisted (#480)

* Fix for: Don't know how to build task 'appraisal'

when running rake.

See: thoughtbot/appraisal#120

* Make it safe to call Thing.with_role(:admin, user) even if user is not persisted

This will return an empty result now (like it used to) instead of
raising an exception.

Fixes #382
  • Loading branch information
TylerRick authored and EppO committed May 11, 2018
1 parent 99ec022 commit c05551d
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 1 deletion.
1 change: 1 addition & 0 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
require 'bundler'
require 'rspec/core/rake_task'
require 'coveralls/rake/task'
require 'appraisal'

Bundler::GemHelper.install_tasks

Expand Down
2 changes: 1 addition & 1 deletion lib/rolify/adapters/active_record/resource_adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def resources_find(roles_table, relation, role_name)
end

def in(relation, user, role_names)
roles = user.roles.where(:name => role_names).select("#{quote_table(role_class.table_name)}.#{quote_column(role_class.primary_key)}")
roles = user.roles.where(:name => role_names).select("#{quote_table(role_class.table_name)}.#{quote_column(role_class.primary_key)}").to_a
relation.where("#{quote_table(role_class.table_name)}.#{quote_column(role_class.primary_key)} IN (?) AND ((#{quote_table(role_class.table_name)}.resource_id = #{quote_table(relation.table_name)}.#{quote_column(relation.primary_key)}) OR (#{quote_table(role_class.table_name)}.resource_id IS NULL))", roles)
end

Expand Down

0 comments on commit c05551d

Please sign in to comment.