rolify 5.0.0 → 5.1.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: ecd709c2e7e50d751a0161d8f0ab5b02ee5fd1aa
4
- data.tar.gz: f0454a8410eba30cf7aff44ee27ffc70ee999182
3
+ metadata.gz: 8b81efc86ee3d4c660521e8b8cb7d1a5045b2ce7
4
+ data.tar.gz: 84f19deb5072b9d46dcade6c8d5db085dd8cfb60
5
5
  SHA512:
6
- metadata.gz: 350c0f03b4d1a7764fee723122aad166d38728dd0ebeed8b39b6cd1523f03bfeb3cc5ef40b813b8d37e84ab28a37088f59fabac7cf3271c34badc3910b77762b
7
- data.tar.gz: dbfcaebac05d8b0dc0d5cdca10f847d84080e28ef08b2473c759cf7cc1eba4bcae80eea2bf77cc0cec3f180a3e43f04b029247b2d894fb68f34e2fed2f8dc3d2
6
+ metadata.gz: d0209a0003ff41826654b23c1ebd6eeb556aef5d8e6554dd86a63362e82a69cc624abdc903063d65f4ab706a42a4c908cb52662c6db9cec1a8a38725f4274afe
7
+ data.tar.gz: 8aee6f2b0fa61dfe9c04aa42c2a496b7b907cb74f44b4c542a6b0587f46686085f5a30457e4168ba1485ed24eb3dd089e17e99a66d86da21e8c724767a2bf5d1
data/.travis.yml CHANGED
@@ -1,12 +1,6 @@
1
1
  language: ruby
2
2
  sudo: false
3
3
 
4
- rvm:
5
- - 2.2.3
6
- - 2.2.2
7
- - 2.1.2
8
- - 2.0.0
9
-
10
4
  before_install:
11
5
  - gem update --system
12
6
  - gem install bundler
@@ -14,11 +8,80 @@ before_install:
14
8
 
15
9
  script: bundle exec rake
16
10
 
17
- env:
18
- - ADAPTER=active_record
19
- - ADAPTER=mongoid MONGOID_VERSION=3
20
- - ADAPTER=mongoid MONGOID_VERSION=4
21
- - ADAPTER=mongoid MONGOID_VERSION=5
11
+ matrix:
12
+ include:
13
+ - rvm: 2.3.0
14
+ gemfile: gemfiles/activerecord_3.gemfile
15
+ env: ADAPTER=active_record
16
+ - rvm: 2.2.4
17
+ gemfile: gemfiles/activerecord_3.gemfile
18
+ env: ADAPTER=active_record
19
+ - rvm: 2.1.8
20
+ gemfile: gemfiles/activerecord_3.gemfile
21
+ env: ADAPTER=active_record
22
+ - rvm: 2.0.0
23
+ gemfile: gemfiles/activerecord_3.gemfile
24
+ env: ADAPTER=active_record
25
+
26
+ - rvm: 2.3.0
27
+ gemfile: gemfiles/activerecord_4.gemfile
28
+ env: ADAPTER=active_record
29
+ - rvm: 2.2.4
30
+ gemfile: gemfiles/activerecord_4.gemfile
31
+ env: ADAPTER=active_record
32
+ - rvm: 2.1.8
33
+ gemfile: gemfiles/activerecord_4.gemfile
34
+ env: ADAPTER=active_record
35
+ - rvm: 2.0.0
36
+ gemfile: gemfiles/activerecord_4.gemfile
37
+ env: ADAPTER=active_record
38
+
39
+ - rvm: 2.3.0
40
+ gemfile: gemfiles/activerecord_5.gemfile
41
+ env: ADAPTER=active_record
42
+ - rvm: 2.2.4
43
+ gemfile: gemfiles/activerecord_5.gemfile
44
+ env: ADAPTER=active_record
45
+
46
+ - rvm: 2.3.0
47
+ gemfile: gemfiles/mongoid_3.gemfile
48
+ env: ADAPTER=mongoid
49
+ - rvm: 2.2.4
50
+ gemfile: gemfiles/mongoid_3.gemfile
51
+ env: ADAPTER=mongoid
52
+ - rvm: 2.1.8
53
+ gemfile: gemfiles/mongoid_3.gemfile
54
+ env: ADAPTER=mongoid
55
+ - rvm: 2.0.0
56
+ gemfile: gemfiles/mongoid_3.gemfile
57
+ env: ADAPTER=mongoid
58
+
59
+ - rvm: 2.3.0
60
+ gemfile: gemfiles/mongoid_4.gemfile
61
+ env: ADAPTER=mongoid
62
+ - rvm: 2.2.4
63
+ gemfile: gemfiles/mongoid_4.gemfile
64
+ env: ADAPTER=mongoid
65
+ - rvm: 2.1.8
66
+ gemfile: gemfiles/mongoid_4.gemfile
67
+ env: ADAPTER=mongoid
68
+ - rvm: 2.0.0
69
+ gemfile: gemfiles/mongoid_4.gemfile
70
+ env: ADAPTER=mongoid
71
+
72
+ - rvm: 2.3.0
73
+ gemfile: gemfiles/mongoid_5.gemfile
74
+ env: ADAPTER=mongoid
75
+ - rvm: 2.2.4
76
+ gemfile: gemfiles/mongoid_5.gemfile
77
+ env: ADAPTER=mongoid
78
+ - rvm: 2.1.8
79
+ gemfile: gemfiles/mongoid_5.gemfile
80
+ env: ADAPTER=mongoid
81
+ - rvm: 2.0.0
82
+ gemfile: gemfiles/mongoid_5.gemfile
83
+ env: ADAPTER=mongoid
84
+
22
85
 
23
86
  services: mongodb
24
87
 
data/Appraisals ADDED
@@ -0,0 +1,41 @@
1
+ appraise 'mongoid-3' do
2
+ gem "mongoid", "~> 3"
3
+ gem "bson_ext", :platform => "ruby"
4
+ end
5
+
6
+ appraise 'mongoid-4' do
7
+ gem "mongoid", "~> 4"
8
+ gem "bson_ext", :platform => "ruby"
9
+ end
10
+
11
+ appraise 'mongoid-5' do
12
+ gem "mongoid", "~> 5"
13
+ gem "bson_ext", :platform => "ruby"
14
+ end
15
+
16
+ appraise 'activerecord-3' do
17
+ gem "sqlite3", :platform => "ruby"
18
+ gem "activerecord", ">= 3.2.0", :require => "active_record"
19
+ end
20
+
21
+ appraise 'activerecord-4' do
22
+ gem "sqlite3", :platform => "ruby"
23
+ gem "activerecord", ">= 4.2.5", :require => "active_record"
24
+ end
25
+
26
+ appraise 'activerecord-5' do
27
+ gem "sqlite3", :platform => "ruby"
28
+ gem "activerecord", ">= 5.0.0.beta", :require => "active_record"
29
+
30
+ # Ammeter dependencies:
31
+ gem "actionpack", ">= 5.0.0.beta2"
32
+ gem "activemodel", ">= 5.0.0.beta2"
33
+ gem "railties", ">= 5.0.0.beta2"
34
+
35
+ gem 'rspec-rails' , github: 'rspec/rspec-rails'
36
+ gem 'rspec-core' , github: 'rspec/rspec-core'
37
+ gem 'rspec-expectations', github: 'rspec/rspec-expectations'
38
+ gem 'rspec-mocks' , github: 'rspec/rspec-mocks'
39
+ gem 'rspec-support' , github: 'rspec/rspec-support'
40
+
41
+ end
data/CHANGELOG.rdoc CHANGED
@@ -1,3 +1,9 @@
1
+ = 5.1.0 (Mar 19, 2016)
2
+ * Rails 5 Support (thanks @lorefnon)
3
+ * Fix user handling in generator (thanks @lorefnon)
4
+ * Fix quoting issues (thanks @lorefnon)
5
+ * Improvements to dynamic module loading (thanks to @DmitryKK)
6
+
1
7
  = 5.0.0 (Nov 17, 2015)
2
8
  * Fix migration warning showing when it shouldn't
3
9
  * Add role lazy-loading
data/CONTRIBUTORS CHANGED
@@ -4,3 +4,5 @@ Joel Azemar (@joel)
4
4
  Alex Klim (@AlexKlim)
5
5
  Mauro George (@MauroGeorge)
6
6
  Cheri Allen(@cherimarie)
7
+ Gaurab Paul (@lorefnon)
8
+ Dmitry Krakosevich (@DmitryKK)
data/Gemfile CHANGED
@@ -1,25 +1,7 @@
1
1
  source "https://rubygems.org"
2
2
 
3
3
  group :test do
4
- case ENV["ADAPTER"]
5
- when nil, "active_record"
6
- gem "activerecord-jdbcsqlite3-adapter", ">= 1.3.0.rc", :platform => "jruby"
7
- gem "sqlite3", :platform => "ruby"
8
- gem "activerecord", ">= 3.2.0", :require => "active_record"
9
- when "mongoid"
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
18
- gem "bson_ext", :platform => "ruby"
19
- else
20
- raise "Unknown model adapter: #{ENV["ADAPTER"]}"
21
- end
22
-
4
+ gem 'appraisal'
23
5
  gem 'coveralls', :require => false
24
6
  gem 'its'
25
7
  gem 'byebug'
data/LICENSE CHANGED
@@ -1,4 +1,4 @@
1
- Copyright (c) 2011 Florent Monbillard
1
+ Copyright (c) 2016 Florent Monbillard
2
2
 
3
3
  Permission is hereby granted, free of charge, to any person obtaining
4
4
  a copy of this software and associated documentation files (the
data/README.md CHANGED
@@ -44,14 +44,14 @@ rails g rolify Role User
44
44
  rails g rolify:role Role User
45
45
  ```
46
46
 
47
- Role and User classes are the default. You can specify any Role class name you want. This is completly a new file so any name can do the job.
47
+ Role and User classes are the default. You can specify any Role class name you want. This is a completely new file so any name will do the job.
48
48
  For the User class name, you would probably use the one provided by your authentication solution. rolify just adds some class methods in an existing User class.
49
49
 
50
50
  If you want to use Mongoid instead of ActiveRecord, just add `--orm=mongoid` argument, and skip to step #3
51
51
 
52
52
  ### 2. Run the migration (only required when using ActiveRecord)
53
53
 
54
- Let's migrate !
54
+ Let's migrate!
55
55
 
56
56
  ```
57
57
  rake db:migrate
@@ -100,21 +100,21 @@ user = User.find(1)
100
100
  user.add_role :admin
101
101
  ```
102
102
 
103
- To define a role scoped to a resource instance
103
+ To define a role scoped to a resource instance:
104
104
 
105
105
  ```ruby
106
106
  user = User.find(2)
107
107
  user.add_role :moderator, Forum.first
108
108
  ```
109
109
 
110
- To define a role scoped to a resource class
110
+ To define a role scoped to a resource class:
111
111
 
112
112
  ```ruby
113
113
  user = User.find(3)
114
114
  user.add_role :moderator, Forum
115
115
  ```
116
116
 
117
- That's it !
117
+ That's it!
118
118
 
119
119
  ### 5. Role queries
120
120
 
@@ -171,36 +171,36 @@ Starting from rolify 3.0, you can search roles on instance level or class level
171
171
  ```ruby
172
172
  forum = Forum.first
173
173
  forum.roles
174
- # => [ list of roles that are only binded to forum instance ]
174
+ # => [ list of roles that are only bound to forum instance ]
175
175
  forum.applied_roles
176
- # => [ list of roles binded to forum instance and to the Forum class ]
176
+ # => [ list of roles bound to forum instance and to the Forum class ]
177
177
  ```
178
178
 
179
179
  #### Class level
180
180
 
181
181
  ```ruby
182
182
  Forum.with_role(:admin)
183
- # => [ list of Forum instances that has role "admin" binded to it ]
183
+ # => [ list of Forum instances that have role "admin" bound to them ]
184
184
  Forum.with_role(:admin, current_user)
185
- # => [ list of Forum instances that has role "admin" binded to it and belongs to current_user roles ]
185
+ # => [ list of Forum instances that have role "admin" bound to them and belong to current_user roles ]
186
186
  Forum.with_roles([:admin, :user], current_user)
187
- # => [ list of Forum instances that has role "admin" or "user" binded to it and belongs to current_user roles ]
187
+ # => [ list of Forum instances that have role "admin" or "user" bound to them and belong to current_user roles ]
188
188
 
189
189
  User.with_any_role(:user, :admin)
190
- # => [ list of User instances that has role "admin" or "user" binded to it ]
190
+ # => [ list of User instances that have role "admin" or "user" bound to them ]
191
191
  User.with_role(:site_admin, current_site)
192
192
  # => [ list of User instances that have a scoped role of "site_admin" to a site instance ]
193
193
  User.with_role(:site_admin, :any)
194
194
  # => [ list of User instances that have a scoped role of "site_admin" for any site instances ]
195
195
  User.with_all_roles(:site_admin, :admin)
196
- # => [ list of User instances that have a role of "site_admin" and a role of "admin" binded to it ]
196
+ # => [ list of User instances that have a role of "site_admin" and a role of "admin" bound to it ]
197
197
 
198
198
  Forum.find_roles
199
- # => [ list of roles that binded to any Forum instance or to the Forum class ]
199
+ # => [ list of roles that are bound to any Forum instance or to the Forum class ]
200
200
  Forum.find_roles(:admin)
201
- # => [ list of roles that binded to any Forum instance or to the Forum class with "admin" as a role name ]
201
+ # => [ list of roles that are bound to any Forum instance or to the Forum class, with "admin" as a role name ]
202
202
  Forum.find_roles(:admin, current_user)
203
- # => [ list of roles that binded to any Forum instance or to the Forum class with "admin" as a role name and belongs to current_user roles ]
203
+ # => [ list of roles that are bound to any Forum instance, or to the Forum class with "admin" as a role name, and belongs to current_user ]
204
204
  ```
205
205
 
206
206
  ### Strict Mode
data/Rakefile CHANGED
@@ -14,7 +14,11 @@ RSpec::Core::RakeTask.new(:rolify) do |task|
14
14
  task.pattern = 'spec/rolify/**/*_spec.rb'
15
15
  end
16
16
 
17
- task :default => [ :spec, 'coveralls:push' ]
17
+ if !ENV["APPRAISAL_INITIALIZED"] && !ENV["TRAVIS"]
18
+ task :default => :appraisal
19
+ else
20
+ task :default => [ :spec, 'coveralls:push' ]
21
+ end
18
22
 
19
23
  desc 'Run all specs'
20
24
  task 'spec' do
@@ -0,0 +1,19 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "https://rubygems.org"
4
+
5
+ gem "sqlite3", :platform => "ruby"
6
+ gem "activerecord", ">= 3.2.0", :require => "active_record"
7
+
8
+ group :test do
9
+ gem "appraisal"
10
+ gem "coveralls", :require => false
11
+ gem "its"
12
+ gem "byebug"
13
+ gem "pry-byebug"
14
+ gem "test-unit"
15
+ gem "database_cleaner"
16
+ gem "codeclimate-test-reporter", :require => nil
17
+ end
18
+
19
+ gemspec :path => "../"
@@ -0,0 +1,19 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "https://rubygems.org"
4
+
5
+ gem "sqlite3", :platform => "ruby"
6
+ gem "activerecord", ">= 4.2.5", :require => "active_record"
7
+
8
+ group :test do
9
+ gem "appraisal"
10
+ gem "coveralls", :require => false
11
+ gem "its"
12
+ gem "byebug"
13
+ gem "pry-byebug"
14
+ gem "test-unit"
15
+ gem "database_cleaner"
16
+ gem "codeclimate-test-reporter", :require => nil
17
+ end
18
+
19
+ gemspec :path => "../"
@@ -0,0 +1,27 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "https://rubygems.org"
4
+
5
+ gem "sqlite3", :platform => "ruby"
6
+ gem "activerecord", ">= 5.0.0.beta", :require => "active_record"
7
+ gem "actionpack", ">= 5.0.0.beta2"
8
+ gem "activemodel", ">= 5.0.0.beta2"
9
+ gem "railties", ">= 5.0.0.beta2"
10
+ gem "rspec-rails", :github => "rspec/rspec-rails"
11
+ gem "rspec-core", :github => "rspec/rspec-core"
12
+ gem "rspec-expectations", :github => "rspec/rspec-expectations"
13
+ gem "rspec-mocks", :github => "rspec/rspec-mocks"
14
+ gem "rspec-support", :github => "rspec/rspec-support"
15
+
16
+ group :test do
17
+ gem "appraisal"
18
+ gem "coveralls", :require => false
19
+ gem "its"
20
+ gem "byebug"
21
+ gem "pry-byebug"
22
+ gem "test-unit"
23
+ gem "database_cleaner"
24
+ gem "codeclimate-test-reporter", :require => nil
25
+ end
26
+
27
+ gemspec :path => "../"
@@ -0,0 +1,19 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "https://rubygems.org"
4
+
5
+ gem "mongoid", "~> 3"
6
+ gem "bson_ext", :platform => "ruby"
7
+
8
+ group :test do
9
+ gem "appraisal"
10
+ gem "coveralls", :require => false
11
+ gem "its"
12
+ gem "byebug"
13
+ gem "pry-byebug"
14
+ gem "test-unit"
15
+ gem "database_cleaner"
16
+ gem "codeclimate-test-reporter", :require => nil
17
+ end
18
+
19
+ gemspec :path => "../"
@@ -0,0 +1,19 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "https://rubygems.org"
4
+
5
+ gem "mongoid", "~> 4"
6
+ gem "bson_ext", :platform => "ruby"
7
+
8
+ group :test do
9
+ gem "appraisal"
10
+ gem "coveralls", :require => false
11
+ gem "its"
12
+ gem "byebug"
13
+ gem "pry-byebug"
14
+ gem "test-unit"
15
+ gem "database_cleaner"
16
+ gem "codeclimate-test-reporter", :require => nil
17
+ end
18
+
19
+ gemspec :path => "../"
@@ -0,0 +1,19 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "https://rubygems.org"
4
+
5
+ gem "mongoid", "~> 5"
6
+ gem "bson_ext", :platform => "ruby"
7
+
8
+ group :test do
9
+ gem "appraisal"
10
+ gem "coveralls", :require => false
11
+ gem "its"
12
+ gem "byebug"
13
+ gem "pry-byebug"
14
+ gem "test-unit"
15
+ gem "database_cleaner"
16
+ gem "codeclimate-test-reporter", :require => nil
17
+ end
18
+
19
+ gemspec :path => "../"
@@ -8,6 +8,13 @@ module ActiveRecord
8
8
 
9
9
  argument :user_cname, :type => :string, :default => "User", :banner => "User"
10
10
 
11
+ def ensure_user_class_defined
12
+ unless user_class_defined?
13
+ prompt_missing_user
14
+ abort
15
+ end
16
+ end
17
+
11
18
  def generate_model
12
19
  invoke "active_record:model", [ name ], :migration => false
13
20
  end
@@ -22,6 +29,7 @@ module ActiveRecord
22
29
  require "#{ENGINE_ROOT}/app/models/#{user_cname.downcase}.rb"
23
30
  end
24
31
  end
32
+
25
33
  inject_into_class(model_path, class_name, model_content)
26
34
  end
27
35
 
@@ -29,8 +37,10 @@ module ActiveRecord
29
37
  migration_template "migration.rb", "db/migrate/rolify_create_#{table_name}.rb"
30
38
  end
31
39
 
40
+ private
41
+
32
42
  def join_table
33
- user_cname.constantize.table_name + "_" + table_name
43
+ user_class.table_name + "_" + table_name
34
44
  end
35
45
 
36
46
  def user_reference
@@ -48,7 +58,10 @@ module ActiveRecord
48
58
  def model_content
49
59
  content = <<RUBY
50
60
  has_and_belongs_to_many :%{user_cname}, :join_table => :%{join_table}
51
- belongs_to :resource, :polymorphic => true
61
+
62
+ belongs_to :resource,
63
+ :polymorphic => true,
64
+ :optional => true
52
65
 
53
66
  validates :resource_type,
54
67
  :inclusion => { :in => Rolify.resource_types },
@@ -56,8 +69,31 @@ module ActiveRecord
56
69
 
57
70
  scopify
58
71
  RUBY
59
- content % { :user_cname => user_cname.constantize.table_name, :join_table => "#{user_cname.constantize.table_name}_#{table_name}"}
72
+ content % { :user_cname => user_class.table_name, :join_table => "#{user_cname.constantize.table_name}_#{table_name}"}
73
+ end
74
+
75
+ def user_class
76
+ user_cname.constantize
77
+ end
78
+
79
+ def user_class_defined?
80
+ user_class
81
+ true
82
+ rescue NameError => ex
83
+ if ex.missing_name == user_cname
84
+ false
85
+ else
86
+ raise ex
87
+ end
60
88
  end
89
+
90
+ def prompt_missing_user
91
+ puts <<MSG
92
+ Rolify expected a model named #{user_cname} to be defined but could not find one.
93
+ Please ensure that this model exists and is not mis-spelled and re-run the generator.
94
+ MSG
95
+ end
96
+
61
97
  end
62
98
  end
63
99
  end
data/lib/rolify.rb CHANGED
@@ -30,7 +30,6 @@ module Rolify
30
30
  has_and_belongs_to_many :roles, rolify_options
31
31
 
32
32
  self.adapter = Rolify::Adapter::Base.create("role_adapter", self.role_cname, self.name)
33
- load_dynamic_methods if Rolify.dynamic_shortcuts
34
33
 
35
34
  #use strict roles
36
35
  self.strict_rolify = true if options[:strict]
@@ -17,16 +17,17 @@ module Rolify
17
17
  str << ', ' unless klass == klasses.last
18
18
  str
19
19
  end
20
- resources = relation.joins("INNER JOIN #{quote(roles_table)} ON #{quote(roles_table)}.resource_type IN (#{relations}) AND
21
- (#{quote(roles_table)}.resource_id IS NULL OR #{quote(roles_table)}.resource_id = #{quote(relation.table_name)}.#{relation.primary_key})")
22
- resources = resources.where("#{quote(roles_table)}.name IN (?) AND #{quote(roles_table)}.resource_type IN (?)", Array(role_name), klasses)
23
- resources = resources.select("#{quote(relation.table_name)}.*")
20
+
21
+ resources = relation.joins("INNER JOIN #{quote_table(roles_table)} ON #{quote_table(roles_table)}.resource_type IN (#{relations}) AND
22
+ (#{quote_table(roles_table)}.resource_id IS NULL OR #{quote_table(roles_table)}.resource_id = #{quote_table(relation.table_name)}.#{quote_column(relation.primary_key)})")
23
+ resources = resources.where("#{quote_table(roles_table)}.name IN (?) AND #{quote_table(roles_table)}.resource_type IN (?)", Array(role_name), klasses)
24
+ resources = resources.select("#{quote_table(relation.table_name)}.*")
24
25
  resources
25
26
  end
26
27
 
27
28
  def in(relation, user, role_names)
28
- roles = user.roles.where(:name => role_names).select("#{quote(role_class.table_name)}.#{role_class.primary_key}")
29
- relation.where("#{quote(role_class.table_name)}.#{role_class.primary_key} IN (?) AND ((resource_id = #{quote(relation.table_name)}.#{relation.primary_key}) OR (resource_id IS NULL))", roles)
29
+ roles = user.roles.where(:name => role_names).select("#{quote_table(role_class.table_name)}.#{quote_column(role_class.primary_key)}")
30
+ relation.where("#{quote_table(role_class.table_name)}.#{quote_column(role_class.primary_key)} IN (?) AND ((resource_id = #{quote_table(relation.table_name)}.#{quote_column(relation.primary_key)}) OR (resource_id IS NULL))", roles)
30
31
  end
31
32
 
32
33
  def applied_roles(relation, children)
@@ -44,9 +45,14 @@ module Rolify
44
45
 
45
46
  private
46
47
 
47
- def quote(column)
48
+ def quote_column(column)
48
49
  ActiveRecord::Base.connection.quote_column_name column
49
50
  end
51
+
52
+ def quote_table(table)
53
+ ActiveRecord::Base.connection.quote_table_name table
54
+ end
55
+
50
56
  end
51
57
  end
52
58
  end
@@ -9,6 +9,7 @@ module Rolify
9
9
  end
10
10
 
11
11
  def where_strict(relation, args)
12
+ return relation.where(:name => args[:name]) if args[:resource].blank?
12
13
  resource = if args[:resource].is_a?(Class)
13
14
  {class: args[:resource].to_s, id: nil}
14
15
  else
@@ -9,13 +9,14 @@ module Rolify
9
9
  end
10
10
 
11
11
  def where_strict(relation, args)
12
+ return relation.where(:name => args[:name]) if args[:resource].blank?
12
13
  resource = if args[:resource].is_a?(Class)
13
14
  {class: args[:resource].to_s, id: nil}
14
15
  else
15
16
  {class: args[:resource].class.name, id: args[:resource].id}
16
17
  end
17
18
 
18
- relation.where(:name => args[:name], :resource_type => resource[:class], :resource_id => resource[:id])
19
+ relation.where(:name => args[:name], :resource_type => resource[:class], :resource_id => resource[:id])
19
20
  end
20
21
 
21
22
  def find_cached(relation, args)
@@ -2,29 +2,15 @@ require "rolify/configure"
2
2
 
3
3
  module Rolify
4
4
  module Dynamic
5
- def load_dynamic_methods
6
- if ENV['ADAPTER'] == 'active_record'
7
- # supported Rails version >= 3.2 with AR should use find_each, since use of .all.each is deprecated
8
- self.role_class.group("name, resource_type").includes(:resource).find_each do |r|
9
- define_dynamic_method(r.name, r.resource)
10
- end
11
- else
12
- # for compatibility with MongoidDB and older Rails AR - does not support polymorphic includes
13
- self.role_class.all.each do |r|
14
- define_dynamic_method(r.name, r.resource)
15
- end
16
- end
17
- end
18
-
19
5
  def define_dynamic_method(role_name, resource)
20
6
  class_eval do
21
7
  define_method("is_#{role_name}?".to_sym) do
22
8
  has_role?("#{role_name}")
23
- end if !method_defined?("is_#{role_name}?".to_sym)
9
+ end if !method_defined?("is_#{role_name}?".to_sym) && self.adapter.where_strict(self.role_class, name: role_name).exists?
24
10
 
25
11
  define_method("is_#{role_name}_of?".to_sym) do |arg|
26
12
  has_role?("#{role_name}", arg)
27
- end if !method_defined?("is_#{role_name}_of?".to_sym) && resource
13
+ end if !method_defined?("is_#{role_name}_of?".to_sym) && resource && self.adapter.where_strict(self.role_class, name: role_name, resource: resource).exists?
28
14
  end
29
15
  end
30
16
  end
data/lib/rolify/role.rb CHANGED
@@ -94,7 +94,7 @@ module Rolify
94
94
  resource = args.first
95
95
  self.class.define_dynamic_method $1, resource
96
96
  return has_role?("#{$1}", resource)
97
- end unless !Rolify.dynamic_shortcuts
97
+ end if Rolify.dynamic_shortcuts
98
98
  super
99
99
  end
100
100
 
@@ -1,3 +1,3 @@
1
1
  module Rolify
2
- VERSION = "5.0.0"
2
+ VERSION = "5.1.0"
3
3
  end
@@ -45,7 +45,10 @@ describe Rolify::Generators::RolifyGenerator, :if => ENV['ADAPTER'] == 'active_r
45
45
  it { should exist }
46
46
  it { should contain "class Role < ActiveRecord::Base" }
47
47
  it { should contain "has_and_belongs_to_many :users, :join_table => :users_roles" }
48
- it { should contain "belongs_to :resource, :polymorphic => true" }
48
+ it { should contain "belongs_to :resource,\n"
49
+ " :polymorphic => true,\n"
50
+ " :optional => true"
51
+ }
49
52
  it { should contain "validates :resource_type,\n"
50
53
  " :inclusion => { :in => Rolify.resource_types },\n"
51
54
  " :allow_nil => true" }
@@ -93,7 +96,10 @@ describe Rolify::Generators::RolifyGenerator, :if => ENV['ADAPTER'] == 'active_r
93
96
  it { should exist }
94
97
  it { should contain "class AdminRole < ActiveRecord::Base" }
95
98
  it { should contain "has_and_belongs_to_many :admin_users, :join_table => :admin_users_admin_roles" }
96
- it { should contain "belongs_to :resource, :polymorphic => true" }
99
+ it { should contain "belongs_to :resource,\n"
100
+ " :polymorphic => true,\n"
101
+ " :optional => true"
102
+ }
97
103
  end
98
104
 
99
105
  describe 'app/models/admin_user.rb' do
@@ -145,7 +151,10 @@ describe Rolify::Generators::RolifyGenerator, :if => ENV['ADAPTER'] == 'active_r
145
151
  it { should exist }
146
152
  it { should contain "class Admin::Role < ActiveRecord::Base" }
147
153
  it { should contain "has_and_belongs_to_many :admin_users, :join_table => :admin_users_admin_roles" }
148
- it { should contain "belongs_to :resource, :polymorphic => true" }
154
+ it { should contain "belongs_to :resource,\n"
155
+ " :polymorphic => true,\n"
156
+ " :optional => true"
157
+ }
149
158
  end
150
159
 
151
160
  describe 'app/models/admin/user.rb' do
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rolify
3
3
  version: !ruby/object:Gem::Version
4
- version: 5.0.0
4
+ version: 5.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Florent Monbillard
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2015-11-18 00:00:00.000000000 Z
12
+ date: 2016-03-19 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: ammeter
@@ -79,6 +79,7 @@ extra_rdoc_files: []
79
79
  files:
80
80
  - ".gitignore"
81
81
  - ".travis.yml"
82
+ - Appraisals
82
83
  - CHANGELOG.rdoc
83
84
  - CONTRIBUTORS
84
85
  - Gemfile
@@ -89,6 +90,12 @@ files:
89
90
  - gemfiles/Gemfile.rails-3.2
90
91
  - gemfiles/Gemfile.rails-4.0
91
92
  - gemfiles/Gemfile.rails-4.1
93
+ - gemfiles/activerecord_3.gemfile
94
+ - gemfiles/activerecord_4.gemfile
95
+ - gemfiles/activerecord_5.gemfile
96
+ - gemfiles/mongoid_3.gemfile
97
+ - gemfiles/mongoid_4.gemfile
98
+ - gemfiles/mongoid_5.gemfile
92
99
  - lib/generators/active_record/rolify_generator.rb
93
100
  - lib/generators/active_record/templates/README
94
101
  - lib/generators/active_record/templates/migration.rb