From c05551d8dee3ad85b9c0e450155dbf81887f1a7c Mon Sep 17 00:00:00 2001 From: Tyler Rick Date: Fri, 11 May 2018 12:14:50 -0700 Subject: [PATCH] Make it safe to call Thing.with_role(:admin, user) even if user is not persisted (#480) * Fix for: Don't know how to build task 'appraisal' when running rake. See: https://github.com/thoughtbot/appraisal/issues/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 --- Rakefile | 1 + lib/rolify/adapters/active_record/resource_adapter.rb | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/Rakefile b/Rakefile index 521a30fa..37774c2f 100644 --- a/Rakefile +++ b/Rakefile @@ -1,6 +1,7 @@ require 'bundler' require 'rspec/core/rake_task' require 'coveralls/rake/task' +require 'appraisal' Bundler::GemHelper.install_tasks diff --git a/lib/rolify/adapters/active_record/resource_adapter.rb b/lib/rolify/adapters/active_record/resource_adapter.rb index 512974e9..26487c63 100644 --- a/lib/rolify/adapters/active_record/resource_adapter.rb +++ b/lib/rolify/adapters/active_record/resource_adapter.rb @@ -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