rolify 4.1.1 → 5.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 1e51b0a50895b89514d5f41266e0eb5373d47158
4
- data.tar.gz: d99233b97fd7e18a79b0958ef9d359d8266b58b3
3
+ metadata.gz: ecd709c2e7e50d751a0161d8f0ab5b02ee5fd1aa
4
+ data.tar.gz: f0454a8410eba30cf7aff44ee27ffc70ee999182
5
5
  SHA512:
6
- metadata.gz: 0846e497eac805c2bcbd8227ae96efe949e337c4872f9b6295c1df4c32df2699702ebcd957f516204d7c28948aca8557391b2b17da9d9db4da1079d130d8f144
7
- data.tar.gz: 5ca314c4b2c63b9897f60e948c28310dcd9ebb5623614831fb1b5368a290090987829e31bc388d297a7665ee753cd2dd6e045c2c28d6eda4910f902ab79e7740
6
+ metadata.gz: 350c0f03b4d1a7764fee723122aad166d38728dd0ebeed8b39b6cd1523f03bfeb3cc5ef40b813b8d37e84ab28a37088f59fabac7cf3271c34badc3910b77762b
7
+ data.tar.gz: dbfcaebac05d8b0dc0d5cdca10f847d84080e28ef08b2473c759cf7cc1eba4bcae80eea2bf77cc0cec3f180a3e43f04b029247b2d894fb68f34e2fed2f8dc3d2
@@ -2,10 +2,10 @@ language: ruby
2
2
  sudo: false
3
3
 
4
4
  rvm:
5
+ - 2.2.3
5
6
  - 2.2.2
6
7
  - 2.1.2
7
8
  - 2.0.0
8
-
9
9
 
10
10
  before_install:
11
11
  - gem update --system
@@ -16,7 +16,9 @@ script: bundle exec rake
16
16
 
17
17
  env:
18
18
  - ADAPTER=active_record
19
- - ADAPTER=mongoid
19
+ - ADAPTER=mongoid MONGOID_VERSION=3
20
+ - ADAPTER=mongoid MONGOID_VERSION=4
21
+ - ADAPTER=mongoid MONGOID_VERSION=5
20
22
 
21
23
  services: mongodb
22
24
 
@@ -1,3 +1,10 @@
1
+ = 5.0.0 (Nov 17, 2015)
2
+ * Fix migration warning showing when it shouldn't
3
+ * Add role lazy-loading
4
+ * Add has_cached_role and has_cached_strict_role to project.
5
+ See: https://github.com/RolifyCommunity/rolify#cached-roles-to-avoid-n1-issue
6
+ * Update specs to pass with all Mongoid version.
7
+
1
8
  = 4.1.1 (Aug 06, 2015)
2
9
  * Fix a regression in the dynamic method loader.
3
10
 
data/Gemfile CHANGED
@@ -7,7 +7,14 @@ group :test do
7
7
  gem "sqlite3", :platform => "ruby"
8
8
  gem "activerecord", ">= 3.2.0", :require => "active_record"
9
9
  when "mongoid"
10
- gem "mongoid", ">= 3.1"
10
+ case ENV["MONGOID_VERSION"]
11
+ when nil, '5'
12
+ gem "mongoid", "~> 5"
13
+ when '4'
14
+ gem "mongoid", "~> 4"
15
+ when '3'
16
+ gem "mongoid", "~> 3"
17
+ end
11
18
  gem "bson_ext", :platform => "ruby"
12
19
  else
13
20
  raise "Unknown model adapter: #{ENV["ADAPTER"]}"
@@ -17,6 +24,8 @@ group :test do
17
24
  gem 'its'
18
25
  gem 'byebug'
19
26
  gem 'pry-byebug'
27
+ gem 'test-unit' # Implicitly loaded by ammeter
28
+ gem 'database_cleaner'
20
29
  gem 'codeclimate-test-reporter', :require => nil
21
30
  end
22
31
 
data/README.md CHANGED
@@ -1,4 +1,4 @@
1
- # rolify [![Gem Version](https://badge.fury.io/rb/rolify.svg)](http://badge.fury.io/rb/rolify) [![build status](https://secure.travis-ci.org/RolifyCommunity/rolify.png)](http://travis-ci.org/RolifyCommunity/rolify) [![Dependency Status](https://gemnasium.com/RolifyCommunity/rolify.svg)](https://gemnasium.com/RolifyCommunity/rolify) [![Code Climate](https://codeclimate.com/github/RolifyCommunity/rolify.png)](https://codeclimate.com/github/RolifyCommunity/rolify) [![Coverage Status](https://img.shields.io/coveralls/RolifyCommunity/rolify.svg)](https://coveralls.io/r/RolifyCommunity/rolify?branch=master)
1
+ # rolify [![Gem Version](https://badge.fury.io/rb/rolify.svg)](http://badge.fury.io/rb/rolify) [![build status](https://secure.travis-ci.org/RolifyCommunity/rolify.png)](http://travis-ci.org/RolifyCommunity/rolify) [![Dependency Status](https://gemnasium.com/RolifyCommunity/rolify.svg)](https://gemnasium.com/RolifyCommunity/rolify) [![Code Climate](https://codeclimate.com/github/RolifyCommunity/rolify.png)](https://codeclimate.com/github/RolifyCommunity/rolify) [![Coverage Status](https://coveralls.io/repos/RolifyCommunity/rolify/badge.svg?branch=master&service=github)](https://coveralls.io/github/RolifyCommunity/rolify?branch=master)
2
2
 
3
3
  Very simple Roles library without any authorization enforcement supporting scope on resource object.
4
4
 
@@ -221,6 +221,20 @@ end
221
221
  ```
222
222
  I.e. you get true only on a role that you manually add.
223
223
 
224
+ ### Cached Roles (to avoid N+1 issue)
225
+
226
+ ```ruby
227
+ @user.add_role :admin, Forum
228
+ @user.add_role :member, Forum
229
+
230
+ users = User.with_role(:admin, Forum).preload(:roles)
231
+ users.each do |user|
232
+ user.has_cached_role?(:member, Forum) # no extra queries
233
+ end
234
+ ```
235
+
236
+ This method should be used with caution. If you don't preload the roles, the `has_cached_role?` might return `false`. In the above example, it would return `false` for `@user.has_cached_role?(:member, Forum)`, because `User.with_role(:admin, Forum)` will load only the `:admin` roles.
237
+
224
238
  ## Resources
225
239
 
226
240
  * [Wiki](https://github.com/RolifyCommunity/rolify/wiki)
@@ -18,6 +18,28 @@ module Rolify
18
18
  relation.where(:name => args[:name], :resource_type => resource[:class], :resource_id => resource[:id])
19
19
  end
20
20
 
21
+ def find_cached(relation, args)
22
+ resource_id = (args[:resource].nil? || args[:resource].is_a?(Class) || args[:resource] == :any) ? nil : args[:resource].id
23
+ resource_type = args[:resource].is_a?(Class) ? args[:resource].to_s : args[:resource].class.name
24
+
25
+ return relation.find_all { |role| role.name == args[:name].to_s } if args[:resource] == :any
26
+
27
+ relation.find_all do |role|
28
+ (role.name == args[:name].to_s && role.resource_type == nil && role.resource_id == nil) ||
29
+ (role.name == args[:name].to_s && role.resource_type == resource_type && role.resource_id == nil) ||
30
+ (role.name == args[:name].to_s && role.resource_type == resource_type && role.resource_id == resource_id)
31
+ end
32
+ end
33
+
34
+ def find_cached_strict(relation, args)
35
+ resource_id = (args[:resource].nil? || args[:resource].is_a?(Class)) ? nil : args[:resource].id
36
+ resource_type = args[:resource].is_a?(Class) ? args[:resource].to_s : args[:resource].class.name
37
+
38
+ relation.find_all do |role|
39
+ role.resource_id == resource_id && role.resource_type == resource_type && role.name == args[:name].to_s
40
+ end
41
+ end
42
+
21
43
  def find_or_create_by(role_name, resource_type = nil, resource_id = nil)
22
44
  role_class.where(:name => role_name, :resource_type => resource_type, :resource_id => resource_id).first_or_create
23
45
  end
@@ -18,6 +18,28 @@ module Rolify
18
18
  relation.where(:name => args[:name], :resource_type => resource[:class], :resource_id => resource[:id])
19
19
  end
20
20
 
21
+ def find_cached(relation, args)
22
+ resource_id = (args[:resource].nil? || args[:resource].is_a?(Class) || args[:resource] == :any) ? nil : args[:resource].id
23
+ resource_type = args[:resource].is_a?(Class) ? args[:resource].to_s : args[:resource].class.name
24
+
25
+ return relation.find_all { |role| role.name == args[:name].to_s } if args[:resource] == :any
26
+
27
+ relation.find_all do |role|
28
+ (role.name == args[:name].to_s && role.resource_type == nil && role.resource_id == nil) ||
29
+ (role.name == args[:name].to_s && role.resource_type == resource_type && role.resource_id == nil) ||
30
+ (role.name == args[:name].to_s && role.resource_type == resource_type && role.resource_id == resource_id)
31
+ end
32
+ end
33
+
34
+ def find_cached_strict(relation, args)
35
+ resource_id = (args[:resource].nil? || args[:resource].is_a?(Class)) ? nil : args[:resource].id
36
+ resource_type = args[:resource].is_a?(Class) ? args[:resource].to_s : args[:resource].class.name
37
+
38
+ relation.find_all do |role|
39
+ role.resource_id == resource_id && role.resource_type == resource_type && role.name == args[:name].to_s
40
+ end
41
+ end
42
+
21
43
  def find_or_create_by(role_name, resource_type = nil, resource_id = nil)
22
44
  self.role_class.find_or_create_by(:name => role_name,
23
45
  :resource_type => resource_type,
@@ -52,9 +52,8 @@ module Rolify
52
52
  private
53
53
 
54
54
  def sanity_check(role_cnames)
55
- return true if (ARGV[0] =~ /assets:/) == 0
55
+ return true if ARGV.reduce(nil) { |acc,arg| arg =~ /assets:/ if acc.nil? } == 0
56
56
 
57
- role_cnames = [ "Role" ] if role_cnames.empty?
58
57
  role_cnames.each do |role_cname|
59
58
  role_class = role_cname.constantize
60
59
  if role_class.superclass.to_s == "ActiveRecord::Base" && role_table_missing?(role_class)
@@ -67,6 +66,9 @@ module Rolify
67
66
 
68
67
  def role_table_missing?(role_class)
69
68
  !role_class.table_exists?
69
+ rescue ActiveRecord::NoDatabaseError
70
+ true
70
71
  end
72
+
71
73
  end
72
74
  end
@@ -5,7 +5,7 @@ module Rolify
5
5
  def load_dynamic_methods
6
6
  if ENV['ADAPTER'] == 'active_record'
7
7
  # supported Rails version >= 3.2 with AR should use find_each, since use of .all.each is deprecated
8
- self.role_class.includes(:resource).find_each do |r|
8
+ self.role_class.group("name, resource_type").includes(:resource).find_each do |r|
9
9
  define_dynamic_method(r.name, r.resource)
10
10
  end
11
11
  else
@@ -5,11 +5,11 @@ RSpec::Matchers.define :have_role do |*args|
5
5
  resource.has_role?(*args)
6
6
  end
7
7
 
8
- failure_message_for_should do |resource|
8
+ failure_message do |resource|
9
9
  "expected to have role #{args.map(&:inspect).join(" ")}"
10
10
  end
11
11
 
12
- failure_message_for_should_not do |resource|
12
+ failure_message_when_negated do |resource|
13
13
  "expected not to have role #{args.map(&:inspect).join(" ")}"
14
14
  end
15
15
  end
@@ -44,6 +44,15 @@ module Rolify
44
44
  self.class.adapter.where_strict(self.roles, name: role_name, resource: resource).any?
45
45
  end
46
46
 
47
+ def has_cached_role?(role_name, resource = nil)
48
+ return has_strict_cached_role?(role_name, resource) if self.class.strict_rolify and resource and resource != :any
49
+ self.class.adapter.find_cached(self.roles, name: role_name, resource: resource).any?
50
+ end
51
+
52
+ def has_strict_cached_role?(role_name, resource = nil)
53
+ self.class.adapter.find_cached_strict(self.roles, name: role_name, resource: resource).any?
54
+ end
55
+
47
56
  def has_all_roles?(*args)
48
57
  args.each do |arg|
49
58
  if arg.is_a? Hash
@@ -1,3 +1,3 @@
1
1
  module Rolify
2
- VERSION = "4.1.1"
2
+ VERSION = "5.0.0"
3
3
  end
@@ -13,16 +13,22 @@ Gem::Specification.new do |s|
13
13
 
14
14
  s.license = 'MIT'
15
15
 
16
- s.authors = ['Florent Monbillard']
17
- s.email = ['f.monbillard@gmail.com']
16
+ s.authors = [
17
+ 'Florent Monbillard',
18
+ 'Wellington Cordeiro'
19
+ ]
20
+ s.email = [
21
+ 'f.monbillard@gmail.com',
22
+ 'wellington@wellingtoncordeiro.com'
23
+ ]
18
24
 
19
25
  s.files = `git ls-files`.split("\n")
20
26
  s.test_files = `git ls-files -- spec/*`.split("\n")
21
27
  s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
22
28
  s.require_paths = ['lib']
23
29
 
24
- s.add_development_dependency 'ammeter', '~> 1.1.2' # Spec generator
25
- s.add_development_dependency 'bundler', '>= 1.7.12' # packaging feature
26
- s.add_development_dependency 'rake', '~> 10.4.2' # Tasks manager
27
- s.add_development_dependency 'rspec-rails', '2.99.0'
30
+ s.add_development_dependency 'ammeter', '~> 1.1' # Spec generator
31
+ s.add_development_dependency 'bundler', '~> 1.10' # packaging feature
32
+ s.add_development_dependency 'rake', '~> 10.4' # Tasks manager
33
+ s.add_development_dependency 'rspec-rails', '~> 3.4'
28
34
  end
@@ -0,0 +1,16 @@
1
+ require 'test-unit'
2
+
3
+ begin
4
+ require 'pry'
5
+ rescue LoadError
6
+ end
7
+
8
+ # `Test::Unit::AutoRunner.need_auto_run=` was introduced to the test-unit
9
+ # gem in version 2.4.9. Previous to this version `Test::Unit.run=` was
10
+ # used. The implementation of test-unit included with Ruby has neither
11
+ # method.
12
+ if defined? Test::Unit::AutoRunner
13
+ Test::Unit::AutoRunner.need_auto_run = false
14
+ elsif defined?(Test::Unit)
15
+ Test::Unit.run = false
16
+ end
@@ -1,21 +1,28 @@
1
1
  require 'rubygems'
2
2
  require "bundler/setup"
3
3
 
4
+ require 'pry'
5
+
4
6
  require 'rolify'
5
7
  require 'rolify/matchers'
6
- require 'rails'
8
+ require 'rails/all'
7
9
  require_relative 'support/stream_helpers'
8
10
  include StreamHelpers
9
11
 
10
12
  require 'coveralls'
11
13
  Coveralls.wear_merged!
12
14
 
15
+ require 'common_helper'
16
+
13
17
  ENV['ADAPTER'] ||= 'active_record'
14
18
 
15
19
  if ENV['ADAPTER'] == 'active_record'
20
+ load File.dirname(__FILE__) + '/support/adapters/utils/active_record.rb'
16
21
  require 'active_record/railtie'
22
+ establish_connection
17
23
  else
18
- require 'mongoid'
24
+ load File.dirname(__FILE__) + '/support/adapters/utils/mongoid.rb'
25
+ load_mongoid_config
19
26
  end
20
27
 
21
28
  module TestApp
@@ -161,6 +161,8 @@ describe Rolify do
161
161
  config.orm = "mongoid"
162
162
  end
163
163
  end
164
+
165
+ subject { Rolify }
164
166
 
165
167
  its(:dynamic_shortcuts) { should be_truthy }
166
168
  its(:orm) { should eq("mongoid") }
@@ -17,4 +17,4 @@ describe "Using Rolify with custom User and Role class names" do
17
17
  it_behaves_like "Role.scopes"
18
18
  it_behaves_like Rolify::Dynamic
19
19
  it_behaves_like "Rolify.callbacks"
20
- end
20
+ end
@@ -542,29 +542,35 @@ describe Rolify::Resource do
542
542
  context "strict user" do
543
543
  before(:all) do
544
544
  @strict_user = StrictUser.first
545
+ @strict_user.role_ids
545
546
  @strict_user.add_role(:forum, Forum.first)
546
547
  @strict_user.add_role(:forum, Forum)
547
548
  end
548
549
 
549
550
  it "should return only strict forum" do
550
551
  @strict_user.has_role?(:forum, Forum.first).should be true
552
+ @strict_user.has_cached_role?(:forum, Forum.first).should be true
551
553
  end
552
554
 
553
555
  it "should return false on strict another forum" do
554
556
  @strict_user.has_role?(:forum, Forum.last).should be false
557
+ @strict_user.has_cached_role?(:forum, Forum.last).should be false
555
558
  end
556
559
 
557
560
  it "should return true if user has role on Forum model" do
558
561
  @strict_user.has_role?(:forum, Forum).should be true
562
+ @strict_user.has_cached_role?(:forum, Forum).should be true
559
563
  end
560
564
 
561
565
  it "should return true if user has role any forum name" do
562
566
  @strict_user.has_role?(:forum, :any).should be true
567
+ @strict_user.has_cached_role?(:forum, :any).should be true
563
568
  end
564
569
 
565
570
  it "should return false when deleted role on Forum model" do
566
571
  @strict_user.remove_role(:forum, Forum)
567
572
  @strict_user.has_role?(:forum, Forum).should be false
573
+ @strict_user.has_cached_role?(:forum, Forum).should be false
568
574
  end
569
575
  end
570
576
  end
@@ -3,10 +3,16 @@ shared_examples_for "#has_role?_examples" do |param_name, param_method|
3
3
  context "with a global role", :scope => :global do
4
4
  it { subject.has_role?("admin".send(param_method)).should be_truthy }
5
5
 
6
+ it { subject.has_cached_role?("admin".send(param_method)).should be_truthy }
7
+
6
8
  context "on resource request" do
7
9
  it { subject.has_role?("admin".send(param_method), Forum.first).should be_truthy }
8
10
  it { subject.has_role?("admin".send(param_method), Forum).should be_truthy }
9
11
  it { subject.has_role?("admin".send(param_method), :any).should be_truthy }
12
+
13
+ it { subject.has_cached_role?("admin".send(param_method), Forum.first).should be_truthy }
14
+ it { subject.has_cached_role?("admin".send(param_method), Forum).should be_truthy }
15
+ it { subject.has_cached_role?("admin".send(param_method), :any).should be_truthy }
10
16
  end
11
17
 
12
18
  context "with another global role" do
@@ -14,19 +20,29 @@ shared_examples_for "#has_role?_examples" do |param_name, param_method|
14
20
 
15
21
  it { subject.has_role?("global".send(param_method)).should be_falsey }
16
22
  it { subject.has_role?("global".send(param_method), :any).should be_falsey }
23
+
24
+ it { subject.has_cached_role?("global".send(param_method)).should be_falsey }
25
+ it { subject.has_cached_role?("global".send(param_method), :any).should be_falsey }
17
26
  end
18
27
 
19
28
  it "should not get an instance scoped role" do
20
29
  subject.has_role?("moderator".send(param_method), Group.first).should be_falsey
30
+
31
+ subject.has_cached_role?("moderator".send(param_method), Group.first).should be_falsey
21
32
  end
22
33
 
23
34
  it "should not get a class scoped role" do
24
35
  subject.has_role?("manager".send(param_method), Forum).should be_falsey
36
+
37
+ subject.has_cached_role?("manager".send(param_method), Forum).should be_falsey
25
38
  end
26
39
 
27
40
  context "using inexisting role" do
28
41
  it { subject.has_role?("dummy".send(param_method)).should be_falsey }
29
42
  it { subject.has_role?("dumber".send(param_method), Forum.first).should be_falsey }
43
+
44
+ it { subject.has_cached_role?("dummy".send(param_method)).should be_falsey }
45
+ it { subject.has_cached_role?("dumber".send(param_method), Forum.first).should be_falsey }
30
46
  end
31
47
  end
32
48
 
@@ -35,15 +51,23 @@ shared_examples_for "#has_role?_examples" do |param_name, param_method|
35
51
  it { subject.has_role?("manager".send(param_method), Forum).should be_truthy }
36
52
  it { subject.has_role?("manager".send(param_method), Forum.first).should be_truthy }
37
53
  it { subject.has_role?("manager".send(param_method), :any).should be_truthy }
54
+
55
+ it { subject.has_cached_role?("manager".send(param_method), Forum).should be_truthy }
56
+ it { subject.has_cached_role?("manager".send(param_method), Forum.first).should be_truthy }
57
+ it { subject.has_cached_role?("manager".send(param_method), :any).should be_truthy }
38
58
  end
39
59
 
40
60
  it "should not get a scoped role when asking for a global" do
41
61
  subject.has_role?("manager".send(param_method)).should be_falsey
62
+
63
+ subject.has_cached_role?("manager".send(param_method)).should be_falsey
42
64
  end
43
65
 
44
66
  it "should not get a global role" do
45
67
  role_class.create(:name => "admin")
46
68
  subject.has_role?("admin".send(param_method)).should be_falsey
69
+
70
+ subject.has_cached_role?("admin".send(param_method)).should be_falsey
47
71
  end
48
72
 
49
73
  context "with another class scoped role" do
@@ -52,6 +76,9 @@ shared_examples_for "#has_role?_examples" do |param_name, param_method|
52
76
 
53
77
  it { subject.has_role?("member".send(param_method), Forum).should be_falsey }
54
78
  it { subject.has_role?("member".send(param_method), :any).should be_falsey }
79
+
80
+ it { subject.has_cached_role?("member".send(param_method), Forum).should be_falsey }
81
+ it { subject.has_cached_role?("member".send(param_method), :any).should be_falsey }
55
82
  end
56
83
 
57
84
  context "on another resource with the same name" do
@@ -59,6 +86,9 @@ shared_examples_for "#has_role?_examples" do |param_name, param_method|
59
86
 
60
87
  it { subject.has_role?("manager".send(param_method), Group).should be_falsey }
61
88
  it { subject.has_role?("manager".send(param_method), :any).should be_truthy }
89
+
90
+ it { subject.has_cached_role?("manager".send(param_method), Group).should be_falsey }
91
+ it { subject.has_cached_role?("manager".send(param_method), :any).should be_truthy }
62
92
  end
63
93
 
64
94
  context "on another resource with another name" do
@@ -66,12 +96,18 @@ shared_examples_for "#has_role?_examples" do |param_name, param_method|
66
96
 
67
97
  it { subject.has_role?("defenders".send(param_method), Group).should be_falsey }
68
98
  it { subject.has_role?("defenders".send(param_method), :any).should be_falsey }
99
+
100
+ it { subject.has_cached_role?("defenders".send(param_method), Group).should be_falsey }
101
+ it { subject.has_cached_role?("defenders".send(param_method), :any).should be_falsey }
69
102
  end
70
103
  end
71
104
 
72
105
  context "using inexisting role" do
73
106
  it { subject.has_role?("dummy".send(param_method), Forum).should be_falsey }
74
107
  it { subject.has_role?("dumber".send(param_method)).should be_falsey }
108
+
109
+ it { subject.has_cached_role?("dummy".send(param_method), Forum).should be_falsey }
110
+ it { subject.has_cached_role?("dumber".send(param_method)).should be_falsey }
75
111
  end
76
112
  end
77
113
 
@@ -84,19 +120,33 @@ shared_examples_for "#has_role?_examples" do |param_name, param_method|
84
120
  m.add_role("moderator", Forum.first)
85
121
  m.has_role?("moderator".send(param_method), :any).should be_truthy
86
122
  }
123
+
124
+ it { subject.has_cached_role?("moderator".send(param_method), Forum.first).should be_truthy }
125
+ it { subject.has_cached_role?("moderator".send(param_method), :any).should be_truthy }
126
+ it {
127
+ m = subject.class.new
128
+ m.add_role("moderator", Forum.first)
129
+ m.has_cached_role?("moderator".send(param_method), :any).should be_truthy
130
+ }
87
131
  end
88
132
 
89
133
  it "should not get an instance scoped role when asking for a global" do
90
134
  subject.has_role?("moderator".send(param_method)).should be_falsey
135
+
136
+ subject.has_cached_role?("moderator".send(param_method)).should be_falsey
91
137
  end
92
138
 
93
139
  it "should not get an instance scoped role when asking for a class scoped" do
94
140
  subject.has_role?("moderator".send(param_method), Forum).should be_falsey
141
+
142
+ subject.has_cached_role?("moderator".send(param_method), Forum).should be_falsey
95
143
  end
96
144
 
97
145
  it "should not get a global role" do
98
146
  role_class.create(:name => "admin")
99
147
  subject.has_role?("admin".send(param_method)).should be_falsey
148
+
149
+ subject.has_cached_role?("admin".send(param_method)).should be_falsey
100
150
  end
101
151
 
102
152
  context "with another instance scoped role" do
@@ -105,6 +155,9 @@ shared_examples_for "#has_role?_examples" do |param_name, param_method|
105
155
 
106
156
  it { subject.has_role?("member".send(param_method), Forum.first).should be_falsey }
107
157
  it { subject.has_role?("member".send(param_method), :any).should be_falsey }
158
+
159
+ it { subject.has_cached_role?("member".send(param_method), Forum.first).should be_falsey }
160
+ it { subject.has_cached_role?("member".send(param_method), :any).should be_falsey }
108
161
  end
109
162
 
110
163
  context "on another resource of the same type but with the same role name" do
@@ -112,6 +165,9 @@ shared_examples_for "#has_role?_examples" do |param_name, param_method|
112
165
 
113
166
  it { subject.has_role?("moderator".send(param_method), Forum.last).should be_falsey }
114
167
  it { subject.has_role?("moderator".send(param_method), :any).should be_truthy }
168
+
169
+ it { subject.has_cached_role?("moderator".send(param_method), Forum.last).should be_falsey }
170
+ it { subject.has_cached_role?("moderator".send(param_method), :any).should be_truthy }
115
171
  end
116
172
 
117
173
  context "on another resource of different type but with the same role name" do
@@ -119,6 +175,9 @@ shared_examples_for "#has_role?_examples" do |param_name, param_method|
119
175
 
120
176
  it { subject.has_role?("moderator".send(param_method), Group.last).should be_falsey }
121
177
  it { subject.has_role?("moderator".send(param_method), :any).should be_truthy }
178
+
179
+ it { subject.has_cached_role?("moderator".send(param_method), Group.last).should be_falsey }
180
+ it { subject.has_cached_role?("moderator".send(param_method), :any).should be_truthy }
122
181
  end
123
182
 
124
183
  context "on another resource of the same type and with another role name" do
@@ -126,6 +185,9 @@ shared_examples_for "#has_role?_examples" do |param_name, param_method|
126
185
 
127
186
  it { subject.has_role?("member".send(param_method), Forum.last).should be_falsey }
128
187
  it { subject.has_role?("member".send(param_method), :any).should be_falsey }
188
+
189
+ it { subject.has_cached_role?("member".send(param_method), Forum.last).should be_falsey }
190
+ it { subject.has_cached_role?("member".send(param_method), :any).should be_falsey }
129
191
  end
130
192
 
131
193
  context "on another resource of different type and with another role name" do
@@ -133,6 +195,9 @@ shared_examples_for "#has_role?_examples" do |param_name, param_method|
133
195
 
134
196
  it { subject.has_role?("member".send(param_method), Group.first).should be_falsey }
135
197
  it { subject.has_role?("member".send(param_method), :any).should be_falsey }
198
+
199
+ it { subject.has_cached_role?("member".send(param_method), Group.first).should be_falsey }
200
+ it { subject.has_cached_role?("member".send(param_method), :any).should be_falsey }
136
201
  end
137
202
  end
138
203
  end
@@ -1,4 +1,5 @@
1
1
  require "codeclimate-test-reporter"
2
+
2
3
  CodeClimate::TestReporter.start
3
4
 
4
5
  require 'rubygems'
@@ -11,6 +12,8 @@ begin
11
12
  require 'its'
12
13
  rescue LoadError
13
14
  end
15
+ require 'database_cleaner'
16
+
14
17
  require 'coveralls'
15
18
  Coveralls.wear_merged!
16
19
 
@@ -19,10 +22,7 @@ ENV['ADAPTER'] ||= 'active_record'
19
22
  load File.dirname(__FILE__) + "/support/adapters/#{ENV['ADAPTER']}.rb"
20
23
  load File.dirname(__FILE__) + '/support/data.rb'
21
24
 
22
- begin
23
- require 'pry'
24
- rescue LoadError
25
- end
25
+
26
26
 
27
27
  def reset_defaults
28
28
  Rolify.use_defaults
@@ -47,3 +47,17 @@ def silence_warnings(&block)
47
47
  $VERBOSE = warn_level
48
48
  result
49
49
  end
50
+
51
+ RSpec.configure do |config|
52
+ config.expect_with(:rspec) { |c| c.syntax = [:should, :expect] }
53
+
54
+ config.before(:suite) do
55
+ DatabaseCleaner.strategy = :truncation
56
+ DatabaseCleaner.start
57
+ end
58
+
59
+ config.after(:suite) do |example|
60
+ DatabaseCleaner.clean
61
+ end
62
+
63
+ end
@@ -1,7 +1,8 @@
1
- require 'active_record'
1
+ load File.dirname(__FILE__) + '/utils/active_record.rb'
2
+
3
+ extend_rspec_with_activerecord_specific_matchers
4
+ establish_connection
2
5
 
3
- RSpec::Matchers::BuiltIn::OperatorMatcher.register(ActiveRecord::Relation, '=~', RSpec::Matchers::BuiltIn::MatchArray)
4
- ActiveRecord::Base.establish_connection(:adapter => "sqlite3", :database => ":memory:")
5
6
  ActiveRecord::Base.extend Rolify
6
7
 
7
8
  load File.dirname(__FILE__) + '/../schema.rb'
@@ -1,6 +1,11 @@
1
- require 'mongoid'
1
+ load File.dirname(__FILE__) + '/utils/mongoid.rb'
2
2
 
3
- Mongoid.load!("spec/support/adapters/mongoid.yml", :test)
3
+ load_mongoid_config
4
+
5
+ begin
6
+ Mongo::Logger.logger.level = ::Logger::FATAL
7
+ rescue NameError
8
+ end
4
9
 
5
10
  ::Mongoid::Document.module_eval do
6
11
  def self.included(base)
@@ -13,6 +18,8 @@ Rolify.use_mongoid
13
18
  # Standard user and role classes
14
19
  class User
15
20
  include Mongoid::Document
21
+ default_scope -> { order_by id: 'asc' }
22
+
16
23
  rolify
17
24
 
18
25
  field :login, :type => String
@@ -21,6 +28,7 @@ end
21
28
  # Standard user and role classes
22
29
  class StrictUser
23
30
  include Mongoid::Document
31
+ default_scope -> { order_by id: 'asc' }
24
32
  rolify strict: true
25
33
 
26
34
  field :login, :type => String
@@ -48,6 +56,7 @@ end
48
56
  # Resourcifed and rolifed at the same time
49
57
  class HumanResource
50
58
  include Mongoid::Document
59
+ default_scope -> { order_by id: 'asc' }
51
60
  resourcify :resources
52
61
  rolify
53
62
 
@@ -74,6 +83,7 @@ end
74
83
  # Custom role and class names
75
84
  class Customer
76
85
  include Mongoid::Document
86
+ default_scope -> { order_by id: 'asc' }
77
87
  rolify :role_cname => "Privilege"
78
88
 
79
89
  field :login, :type => String
@@ -81,6 +91,7 @@ end
81
91
 
82
92
  class Privilege
83
93
  include Mongoid::Document
94
+ default_scope -> { order_by id: 'asc' }
84
95
  has_and_belongs_to_many :customers
85
96
  belongs_to :resource, :polymorphic => true
86
97
  scopify
@@ -100,6 +111,7 @@ end
100
111
  module Admin
101
112
  class Moderator
102
113
  include Mongoid::Document
114
+ default_scope -> { order_by id: 'asc' }
103
115
  rolify :role_cname => "Admin::Right"
104
116
 
105
117
  field :login, :type => String
@@ -107,6 +119,7 @@ module Admin
107
119
 
108
120
  class Right
109
121
  include Mongoid::Document
122
+ default_scope -> { order_by id: 'asc' }
110
123
  has_and_belongs_to_many :moderators, :class_name => 'Admin::Moderator'
111
124
  belongs_to :resource, :polymorphic => true
112
125
  scopify
@@ -126,6 +139,7 @@ end
126
139
  # Resources classes
127
140
  class Forum
128
141
  include Mongoid::Document
142
+ default_scope -> { order_by id: 'asc' }
129
143
  #resourcify done during specs setup to be able to use custom user classes
130
144
 
131
145
  field :name, :type => String
@@ -133,6 +147,7 @@ end
133
147
 
134
148
  class Group
135
149
  include Mongoid::Document
150
+ default_scope -> { order_by id: 'asc' }
136
151
  #resourcify done during specs setup to be able to use custom user classes
137
152
 
138
153
  field :name, :type => String
@@ -145,6 +160,7 @@ end
145
160
 
146
161
  class Team
147
162
  include Mongoid::Document
163
+ default_scope -> { order_by id: 'asc' }
148
164
  #resourcify done during specs setup to be able to use custom user classes
149
165
 
150
166
  field :team_code, :type => Integer
@@ -153,6 +169,7 @@ end
153
169
 
154
170
  class Organization
155
171
  include Mongoid::Document
172
+ default_scope -> { order_by id: 'asc' }
156
173
  end
157
174
 
158
175
  class Company < Organization
@@ -0,0 +1,6 @@
1
+ test:
2
+ sessions:
3
+ default:
4
+ database: godfather
5
+ hosts:
6
+ - localhost:27017
@@ -0,0 +1,6 @@
1
+ test:
2
+ clients:
3
+ default:
4
+ database: godfather
5
+ hosts:
6
+ - localhost:27017
@@ -0,0 +1,12 @@
1
+ require 'active_record'
2
+
3
+ def establish_connection
4
+ ActiveRecord::Base.establish_connection(
5
+ :adapter => "sqlite3",
6
+ :database => ":memory:"
7
+ )
8
+ end
9
+
10
+ def extend_rspec_with_activerecord_specific_matchers
11
+ RSpec::Matchers::BuiltIn::OperatorMatcher.register(ActiveRecord::Relation, '=~', RSpec::Matchers::BuiltIn::ContainExactly)
12
+ end
@@ -0,0 +1,13 @@
1
+ require 'mongoid'
2
+
3
+ def mongoid_major_version
4
+ Mongoid::VERSION.split('.').first.to_i
5
+ end
6
+
7
+ def mongoid_config
8
+ "spec/support/adapters/mongoid_#{mongoid_major_version}.yml"
9
+ end
10
+
11
+ def load_mongoid_config
12
+ Mongoid.load!(mongoid_config, :test)
13
+ end
metadata CHANGED
@@ -1,14 +1,15 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rolify
3
3
  version: !ruby/object:Gem::Version
4
- version: 4.1.1
4
+ version: 5.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Florent Monbillard
8
+ - Wellington Cordeiro
8
9
  autorequire:
9
10
  bindir: bin
10
11
  cert_chain: []
11
- date: 2015-08-06 00:00:00.000000000 Z
12
+ date: 2015-11-18 00:00:00.000000000 Z
12
13
  dependencies:
13
14
  - !ruby/object:Gem::Dependency
14
15
  name: ammeter
@@ -16,61 +17,62 @@ dependencies:
16
17
  requirements:
17
18
  - - "~>"
18
19
  - !ruby/object:Gem::Version
19
- version: 1.1.2
20
+ version: '1.1'
20
21
  type: :development
21
22
  prerelease: false
22
23
  version_requirements: !ruby/object:Gem::Requirement
23
24
  requirements:
24
25
  - - "~>"
25
26
  - !ruby/object:Gem::Version
26
- version: 1.1.2
27
+ version: '1.1'
27
28
  - !ruby/object:Gem::Dependency
28
29
  name: bundler
29
30
  requirement: !ruby/object:Gem::Requirement
30
31
  requirements:
31
- - - ">="
32
+ - - "~>"
32
33
  - !ruby/object:Gem::Version
33
- version: 1.7.12
34
+ version: '1.10'
34
35
  type: :development
35
36
  prerelease: false
36
37
  version_requirements: !ruby/object:Gem::Requirement
37
38
  requirements:
38
- - - ">="
39
+ - - "~>"
39
40
  - !ruby/object:Gem::Version
40
- version: 1.7.12
41
+ version: '1.10'
41
42
  - !ruby/object:Gem::Dependency
42
43
  name: rake
43
44
  requirement: !ruby/object:Gem::Requirement
44
45
  requirements:
45
46
  - - "~>"
46
47
  - !ruby/object:Gem::Version
47
- version: 10.4.2
48
+ version: '10.4'
48
49
  type: :development
49
50
  prerelease: false
50
51
  version_requirements: !ruby/object:Gem::Requirement
51
52
  requirements:
52
53
  - - "~>"
53
54
  - !ruby/object:Gem::Version
54
- version: 10.4.2
55
+ version: '10.4'
55
56
  - !ruby/object:Gem::Dependency
56
57
  name: rspec-rails
57
58
  requirement: !ruby/object:Gem::Requirement
58
59
  requirements:
59
- - - '='
60
+ - - "~>"
60
61
  - !ruby/object:Gem::Version
61
- version: 2.99.0
62
+ version: '3.4'
62
63
  type: :development
63
64
  prerelease: false
64
65
  version_requirements: !ruby/object:Gem::Requirement
65
66
  requirements:
66
- - - '='
67
+ - - "~>"
67
68
  - !ruby/object:Gem::Version
68
- version: 2.99.0
69
+ version: '3.4'
69
70
  description: Very simple Roles library without any authorization enforcement supporting
70
71
  scope on resource objects (instance or class). Supports ActiveRecord and Mongoid
71
72
  ORMs.
72
73
  email:
73
74
  - f.monbillard@gmail.com
75
+ - wellington@wellingtoncordeiro.com
74
76
  executables: []
75
77
  extensions: []
76
78
  extra_rdoc_files: []
@@ -117,6 +119,7 @@ files:
117
119
  - lib/rolify/version.rb
118
120
  - rolify.gemspec
119
121
  - spec/README.rdoc
122
+ - spec/common_helper.rb
120
123
  - spec/generators/rolify/rolify_activerecord_generator_spec.rb
121
124
  - spec/generators/rolify/rolify_mongoid_generator_spec.rb
122
125
  - spec/generators_helper.rb
@@ -145,7 +148,11 @@ files:
145
148
  - spec/spec_helper.rb
146
149
  - spec/support/adapters/active_record.rb
147
150
  - spec/support/adapters/mongoid.rb
148
- - spec/support/adapters/mongoid.yml
151
+ - spec/support/adapters/mongoid_3.yml
152
+ - spec/support/adapters/mongoid_4.yml
153
+ - spec/support/adapters/mongoid_5.yml
154
+ - spec/support/adapters/utils/active_record.rb
155
+ - spec/support/adapters/utils/mongoid.rb
149
156
  - spec/support/data.rb
150
157
  - spec/support/schema.rb
151
158
  - spec/support/stream_helpers.rb
@@ -169,12 +176,13 @@ required_rubygems_version: !ruby/object:Gem::Requirement
169
176
  version: '0'
170
177
  requirements: []
171
178
  rubyforge_project: rolify
172
- rubygems_version: 2.4.5
179
+ rubygems_version: 2.4.5.1
173
180
  signing_key:
174
181
  specification_version: 4
175
182
  summary: Roles library with resource scoping
176
183
  test_files:
177
184
  - spec/README.rdoc
185
+ - spec/common_helper.rb
178
186
  - spec/generators/rolify/rolify_activerecord_generator_spec.rb
179
187
  - spec/generators/rolify/rolify_mongoid_generator_spec.rb
180
188
  - spec/generators_helper.rb
@@ -203,7 +211,11 @@ test_files:
203
211
  - spec/spec_helper.rb
204
212
  - spec/support/adapters/active_record.rb
205
213
  - spec/support/adapters/mongoid.rb
206
- - spec/support/adapters/mongoid.yml
214
+ - spec/support/adapters/mongoid_3.yml
215
+ - spec/support/adapters/mongoid_4.yml
216
+ - spec/support/adapters/mongoid_5.yml
217
+ - spec/support/adapters/utils/active_record.rb
218
+ - spec/support/adapters/utils/mongoid.rb
207
219
  - spec/support/data.rb
208
220
  - spec/support/schema.rb
209
221
  - spec/support/stream_helpers.rb