Skip to content

Commit eae9e7e

Browse files
committed
Fix behaviour of with_any_role with no args
User.with_any_role with no args should return all User instances having at least one role. fix #363 For the generator specs to pass, I needed to add this: ActiveRecord::Base.establish_connection(:adapter => "sqlite3", :database => ":memory:") Otherwise, generators specs are failing because of `ActiveRecord::ConnectionNotEstablished: No connection pool for ActiveRecord::Base` error. I had to require explicitly active_view and active_controller gems for mongoid adapter to make generators specs pass for mongoid. I changed also mongoid.yml to use clients instead of sessions and disabled mongod logging that were flooding travis-ci outputs and reached the log size limit (4MB)
1 parent 99d886e commit eae9e7e

File tree

7 files changed

+12
-3
lines changed

7 files changed

+12
-3
lines changed

lib/rolify/adapters/active_record/role_adapter.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ def scope(relation, conditions)
5151
query = relation.all
5252
end
5353
query = query.joins(:roles)
54-
query = where(query, conditions)
54+
query = where(query, conditions) if conditions
5555
query
5656
end
5757

lib/rolify/finders.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ def with_all_roles(*args)
1919
end
2020

2121
def with_any_role(*args)
22+
return self.adapter.scope(self, nil) if args.empty?
2223
users = []
2324
parse_args(args, users) do |users_to_add|
2425
users += users_to_add

spec/generators_helper.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,12 @@
1414

1515
if ENV['ADAPTER'] == 'active_record'
1616
require 'active_record/railtie'
17+
ActiveRecord::Base.establish_connection(:adapter => "sqlite3", :database => ":memory:")
18+
ActiveRecord::Base.extend Rolify
1719
else
1820
require 'mongoid'
21+
require 'action_view'
22+
require 'action_controller'
1923
end
2024

2125
module TestApp

spec/rolify/shared_contexts.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
subject { admin }
33

44
def admin
5-
user_class.first
5+
user_class.where(:login => "admin").first
66
end
77

88
before(:all) do

spec/rolify/shared_examples/shared_examples_for_finders.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@
116116
describe ".with_any_role" do
117117
it { should respond_to(:with_any_role) }
118118

119+
it { subject.with_any_role().should eq(subject.joins(:roles).all)}
119120
it { subject.with_any_role("admin".send(param_method), :staff).should eq([ root ]) }
120121
it { subject.with_any_role("admin".send(param_method), :staff, { :name => "moderator".send(param_method), :resource => Group }).should eq([ root ]) }
121122
it { subject.with_any_role("admin".send(param_method), "moderator".send(param_method)).should eq([ root ]) }

spec/support/adapters/mongoid.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
require 'mongoid'
22

33
Mongoid.load!("spec/support/adapters/mongoid.yml", :test)
4+
Mongoid.logger = ::Logger.new('/dev/null')
5+
Mongo::Logger.logger.level = Logger::INFO
46

57
::Mongoid::Document.module_eval do
68
def self.included(base)

spec/support/adapters/mongoid.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
test:
2-
sessions:
2+
clients:
33
default:
4+
logger: false
45
database: godfather
56
hosts:
67
- localhost:27017

0 commit comments

Comments
 (0)