rolify 3.4.1 → 3.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: cf5b355e4195b739b029a8567fd142b8e35bb12d
4
+ data.tar.gz: 9e9ed19d10b9eb0d41b1319602b69ded63bb16d6
5
+ SHA512:
6
+ metadata.gz: f1d687a86a7050e389650808e2abb072e1c7aa4387d4a4d6465e9c57091198f196a0dc2b84a1d2e5c0ebbf95db14c8212ee19597f353bac5b803f6331310c34f
7
+ data.tar.gz: 3a7cc52b331ad63e658a9027385efc5bb60266112633d2d6e01d3146b6b01112e8c7e02489d2ac551da275e5e3187c0821da5f6283f1e6baf292cca7422af1ae
data/.travis.yml CHANGED
@@ -11,6 +11,9 @@ env:
11
11
 
12
12
  services: mongodb
13
13
 
14
+ addons:
15
+ code_climate:
16
+ repo_token: 6bd8d374b120a5449b9a4b7dfda40cc0609dbade48a1b6655f04a9bc8de3a3ee
14
17
  # language: ruby
15
18
  #
16
19
  # rvm:
data/CHANGELOG.rdoc CHANGED
@@ -1,4 +1,11 @@
1
- = 3.4.1 (Sep 07, 2014)
1
+ = 3.5.0 (Jan 08, 2015)
2
+ * Removed `has_role` method alias for `add_role`
3
+ * Fixed regression in caching system
4
+ * Fix deprecation warning about timestamp on specs
5
+ * Copy Kernel#capture helper from Rails
6
+ * Add validation on Role.resource_type
7
+
8
+ = 3.4.1 (Sep 08, 2014)
2
9
  * Fixed issue with migrations being generated without a file extension.
3
10
  * Updated spec tests to work with Travis CI on Ruby up to 2.1.2 and Rails 4.1.x
4
11
  * Made it possible for inheritance of resources.
data/CONTRIBUTORS ADDED
@@ -0,0 +1,5 @@
1
+ Florent Monbillard (@EppO) - Original Author
2
+ Wellington Cordeiro (@wldcordeiro)
3
+ Joel Azemar (@joel)
4
+ Alex Klim (@AlexKlim)
5
+ Mauro George (@MauroGeorge)
data/Gemfile CHANGED
@@ -16,6 +16,7 @@ group :test do
16
16
  gem 'coveralls', :require => false
17
17
  gem 'its'
18
18
  gem 'pry'
19
+ gem 'codeclimate-test-reporter', :require => nil
19
20
  end
20
21
 
21
22
  gemspec
data/README.md CHANGED
@@ -1,4 +1,4 @@
1
- # rolify [![Gem Version](https://badge.fury.io/rb/rolify.png)](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/EppO/rolify.png)](https://gemnasium.com/EppO/rolify) [![Code Climate](https://codeclimate.com/github/EppO/rolify.png)](https://codeclimate.com/github/EppO/rolify) [![Coverage Status](https://coveralls.io/repos/EppO/rolify/badge.png?branch=master)](https://coveralls.io/r/EppO/rolify)
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)
2
2
 
3
3
 
4
4
  Very simple Roles library without any authorization enforcement supporting scope on resource object.
@@ -10,7 +10,7 @@ user.has_role?(:moderator, Forum.first)
10
10
  => false # if user is moderator of another Forum
11
11
  ```
12
12
 
13
- This library can be easily integrated with any authentication gem ([devise](https://github.com/plataformatec/devise), [Authlogic](https://github.com/binarylogic/authlogic), [Clearance](https://github.com/thoughtbot/clearance)) and authorization gem<span style="color: red"><strong>*</strong></span> ([CanCan](https://github.com/ryanb/cancan), [authority](https://github.com/nathanl/authority))
13
+ This library can be easily integrated with any authentication gem ([devise](https://github.com/plataformatec/devise), [Authlogic](https://github.com/binarylogic/authlogic), [Clearance](https://github.com/thoughtbot/clearance)) and authorization gem<span style="color: red"><strong>*</strong></span> ([CanCanCan](https://github.com/CanCanCommunity/cancancan), [authority](https://github.com/nathanl/authority))
14
14
 
15
15
  <span style="color: red"><strong>*</strong></span>: authorization gem that doesn't provide a role class
16
16
 
@@ -184,6 +184,8 @@ Forum.with_role(:admin)
184
184
  # => [ list of Forum instances that has role "admin" binded to it ]
185
185
  Forum.with_role(:admin, current_user)
186
186
  # => [ list of Forum instances that has role "admin" binded to it and belongs to current_user roles ]
187
+ Forum.with_roles([:admin, :user], current_user)
188
+ # => [ list of Forum instances that has role "admin" or "user" binded to it and belongs to current_user roles ]
187
189
 
188
190
  User.with_any_role(:user, :admin)
189
191
  # => [ list of User instances that has role "admin" or "user" binded to it ]
@@ -202,11 +204,11 @@ Forum.find_roles(:admin, current_user)
202
204
 
203
205
  ## Resources
204
206
 
205
- * [Wiki](https://github.com/EppO/rolify/wiki)
206
- * [Usage](https://github.com/EppO/rolify/wiki/Usage): all the available commands
207
- * [Tutorials](https://github.com/EppO/rolify/wiki#wiki-tutorials):
208
- * [How-To use rolify with Devise and CanCan](https://github.com/EppO/rolify/wiki/Tutorial)
209
- * [Using rolify with Devise and Authority](https://github.com/EppO/rolify/wiki/Using-rolify-with-Devise-and-Authority)
207
+ * [Wiki](https://github.com/RolifyCommunity/rolify/wiki)
208
+ * [Usage](https://github.com/RolifyCommunity/rolify/wiki/Usage): all the available commands
209
+ * [Tutorials](https://github.com/RolifyCommunity/rolify/wiki#wiki-tutorials):
210
+ * [How-To use rolify with Devise and CanCan](https://github.com/RolifyCommunity/rolify/wiki/Tutorial)
211
+ * [Using rolify with Devise and Authority](https://github.com/RolifyCommunity/rolify/wiki/Using-rolify-with-Devise-and-Authority)
210
212
  * [Step-by-step tutorial](http://railsapps.github.com/tutorial-rails-bootstrap-devise-cancan.html) provided by [RailsApps](http://railsapps.github.com/)
211
213
 
212
214
  ## Upgrade from previous versions
@@ -215,12 +217,12 @@ Please read the [upgrade instructions](UPGRADE.rdoc).
215
217
 
216
218
  ## Known issues
217
219
 
218
- * If you are using Mongoid and/or less-rails gem, please read [this](https://github.com/EppO/rolify/wiki/FAQ#when-i-start-rails-using-server-console-whatever-i-get-this-error)
220
+ * If you are using Mongoid and/or less-rails gem, please read [this](https://github.com/RolifyCommunity/rolify/wiki/FAQ#when-i-start-rails-using-server-console-whatever-i-get-this-error)
219
221
  * Moped library (ruby driver for Mongodb used by Mongoid) doesn't support rubinius 2.2 yet (see https://github.com/mongoid/moped/issues/231)
220
- * If you use Rails 4 and Mongoid, use Mongoid ~> 4 (not released yet). rolify is fully tested with Rails 4 and Mongoid master branch
222
+ * If you use Rails 4 and Mongoid, use Mongoid ~> 4. rolify is fully tested with Rails 4 and Mongoid 4.
221
223
 
222
224
  ## Questions or Problems?
223
225
 
224
- If you have any issue or feature request with/for rolify, please create an new [issue on GitHub](https://github.com/EppO/rolify/issues) **specifying the ruby runtime, rails and rolify versions you're using and the gems listed in your Gemfile**, or fork the project and send a pull request.
226
+ If you have any issue or feature request with/for rolify, please create an new [issue on GitHub](https://github.com/RolifyCommunity/rolify/issues) **specifying the ruby runtime, rails and rolify versions you're using and the gems listed in your Gemfile**, or fork the project and send a pull request.
225
227
 
226
228
  To get the specs running you should call `bundle` and then `rake`. See the spec/README for more information.
data/circle.yml ADDED
@@ -0,0 +1,11 @@
1
+ machine:
2
+ ruby:
3
+ version: 2.1.3
4
+ environment:
5
+ CODECLIMATE_REPO_TOKEN: 6bd8d374b120a5449b9a4b7dfda40cc0609dbade48a1b6655f04a9bc8de3a3ee
6
+ ADAPTER: active_record
7
+ ADAPTER: mongoid
8
+
9
+ test:
10
+ override:
11
+ - bundle exec rake:spec_all
@@ -5,42 +5,44 @@ module ActiveRecord
5
5
  module Generators
6
6
  class RolifyGenerator < ActiveRecord::Generators::Base
7
7
  source_root File.expand_path("../templates", __FILE__)
8
-
8
+
9
9
  argument :user_cname, :type => :string, :default => "User", :banner => "User"
10
-
10
+
11
11
  def generate_model
12
12
  invoke "active_record:model", [ name ], :migration => false
13
13
  end
14
-
14
+
15
15
  def inject_role_class
16
16
  inject_into_class(model_path, class_name, model_content)
17
17
  end
18
-
18
+
19
19
  def copy_rolify_migration
20
20
  migration_template "migration.rb", "db/migrate/rolify_create_#{table_name}.rb"
21
21
  end
22
-
22
+
23
23
  def join_table
24
24
  user_cname.constantize.table_name + "_" + table_name
25
25
  end
26
-
26
+
27
27
  def user_reference
28
28
  user_cname.demodulize.underscore
29
29
  end
30
-
30
+
31
31
  def role_reference
32
32
  class_name.demodulize.underscore
33
33
  end
34
-
34
+
35
35
  def model_path
36
36
  File.join("app", "models", "#{file_path}.rb")
37
37
  end
38
-
38
+
39
39
  def model_content
40
40
  content = <<RUBY
41
41
  has_and_belongs_to_many :%{user_cname}, :join_table => :%{join_table}
42
42
  belongs_to :resource, :polymorphic => true
43
-
43
+
44
+ validates :resource_type, :inclusion => { :in => Rolify.resource_types }
45
+
44
46
  scopify
45
47
  RUBY
46
48
  content % { :user_cname => user_cname.constantize.table_name, :join_table => "#{user_cname.constantize.table_name}_#{table_name}"}
@@ -5,34 +5,34 @@ module Mongoid
5
5
  module Generators
6
6
  class RolifyGenerator < Rails::Generators::NamedBase
7
7
  source_root File.expand_path("../templates", __FILE__)
8
-
8
+
9
9
  argument :user_cname, :type => :string, :default => "User", :banner => "User"
10
-
10
+
11
11
  def generate_model
12
12
  invoke "mongoid:model", [ name ]
13
13
  end
14
-
14
+
15
15
  def inject_role_class
16
16
  inject_into_file(model_path, model_contents, :after => "include Mongoid::Document\n")
17
17
  end
18
-
18
+
19
19
  def user_reference
20
20
  user_cname.demodulize.underscore
21
21
  end
22
-
22
+
23
23
  def role_reference
24
24
  class_name.demodulize.underscore
25
25
  end
26
-
26
+
27
27
  def model_path
28
28
  File.join("app", "models", "#{file_path}.rb")
29
29
  end
30
-
30
+
31
31
  def model_contents
32
32
  content = <<RUBY
33
33
  has_and_belongs_to_many :%{user_cname}
34
34
  belongs_to :resource, :polymorphic => true
35
-
35
+
36
36
  field :name, :type => String
37
37
 
38
38
  index({
@@ -41,11 +41,13 @@ module Mongoid
41
41
  :resource_id => 1
42
42
  },
43
43
  { :unique => true})
44
-
44
+
45
+ validates :resource_type, :inclusion => { :in => Rolify.resource_types }
46
+
45
47
  scopify
46
48
  RUBY
47
49
  content % { :user_cname => user_cname.constantize.collection_name }
48
50
  end
49
51
  end
50
52
  end
51
- end
53
+ end
data/lib/rolify.rb CHANGED
@@ -10,6 +10,7 @@ module Rolify
10
10
  extend Configure
11
11
 
12
12
  attr_accessor :role_cname, :adapter, :role_join_table_name, :role_table_name
13
+ @@resource_types = []
13
14
 
14
15
  def rolify(options = {})
15
16
  include Role
@@ -49,6 +50,7 @@ module Rolify
49
50
  has_many association_name, resourcify_options
50
51
 
51
52
  self.adapter = Rolify::Adapter::Base.create("resource_adapter", self.role_cname, self.name)
53
+ @@resource_types << self.name
52
54
  end
53
55
 
54
56
  def scopify
@@ -60,4 +62,8 @@ module Rolify
60
62
  return self.superclass.role_class unless self.instance_variable_defined? '@role_cname'
61
63
  self.role_cname.constantize
62
64
  end
65
+
66
+ def self.resource_types
67
+ @@resource_types
68
+ end
63
69
  end
@@ -25,7 +25,7 @@ module Rolify
25
25
  relation.roles.delete(roles)
26
26
  roles.each do |role|
27
27
  role.destroy if role.send(ActiveSupport::Inflector.demodulize(user_class).tableize.to_sym).limit(1).empty?
28
- end
28
+ end if Rolify.remove_role_if_empty
29
29
  end
30
30
  roles
31
31
  end
@@ -9,8 +9,8 @@ module Rolify
9
9
  end
10
10
 
11
11
  def find_or_create_by(role_name, resource_type = nil, resource_id = nil)
12
- self.role_class.find_or_create_by(:name => role_name,
13
- :resource_type => resource_type,
12
+ self.role_class.find_or_create_by(:name => role_name,
13
+ :resource_type => resource_type,
14
14
  :resource_id => resource_id)
15
15
  end
16
16
 
@@ -24,7 +24,7 @@ module Rolify
24
24
  #roles.merge!({ :resource_id => resource.id }) if resource && !resource.is_a?(Class)
25
25
  #roles_to_remove = relation.roles.where(roles)
26
26
  #roles_to_remove.each do |role|
27
- # # Deletion in n-n relations is unreliable. Sometimes it works, sometimes not.
27
+ # # Deletion in n-n relations is unreliable. Sometimes it works, sometimes not.
28
28
  # # So, this does not work all the time: `relation.roles.delete(role)`
29
29
  # # @see http://stackoverflow.com/questions/9132596/rails3-mongoid-many-to-many-relation-and-delete-operation
30
30
  # # We instead remove ids from the Role object and the relation object.
@@ -37,10 +37,12 @@ module Rolify
37
37
  cond[:resource_type] = (resource.is_a?(Class) ? resource.to_s : resource.class.name) if resource
38
38
  cond[:resource_id] = resource.id if resource && !resource.is_a?(Class)
39
39
  roles = relation.roles.where(cond)
40
- roles.each do |role|
40
+ roles.each do |role|
41
41
  relation.roles.delete(role)
42
42
  role.send(ActiveSupport::Inflector.demodulize(user_class).tableize.to_sym).delete(relation)
43
- role.destroy if role.send(ActiveSupport::Inflector.demodulize(user_class).tableize.to_sym).empty?
43
+ if Rolify.remove_role_if_empty && role.send(ActiveSupport::Inflector.demodulize(user_class).tableize.to_sym).empty?
44
+ role.destroy
45
+ end
44
46
  end if roles
45
47
  roles
46
48
  end
@@ -48,7 +50,7 @@ module Rolify
48
50
  def exists?(relation, column)
49
51
  relation.where(column.to_sym.ne => nil)
50
52
  end
51
-
53
+
52
54
  def scope(relation, conditions)
53
55
  roles = where(role_class, conditions).map { |role| role.id }
54
56
  return [] if roles.size.zero?
@@ -86,4 +88,4 @@ module Rolify
86
88
  end
87
89
  end
88
90
  end
89
- end
91
+ end
@@ -2,7 +2,8 @@ module Rolify
2
2
  module Configure
3
3
  @@dynamic_shortcuts = false
4
4
  @@orm = "active_record"
5
-
5
+ @@remove_role_if_empty = true
6
+
6
7
  def configure(*role_cnames)
7
8
  return if !sanity_check(role_cnames)
8
9
  yield self if block_given?
@@ -27,7 +28,7 @@ module Rolify
27
28
  def use_mongoid
28
29
  self.orm = "mongoid"
29
30
  end
30
-
31
+
31
32
  def use_dynamic_shortcuts
32
33
  self.dynamic_shortcuts = true
33
34
  end
@@ -38,10 +39,20 @@ module Rolify
38
39
  config.orm = "active_record"
39
40
  end
40
41
  end
41
-
42
+
43
+ def remove_role_if_empty=(is_remove)
44
+ @@remove_role_if_empty = is_remove
45
+ end
46
+
47
+ def remove_role_if_empty
48
+ @@remove_role_if_empty
49
+ end
50
+
42
51
  private
43
-
52
+
44
53
  def sanity_check(role_cnames)
54
+ return true if "assets:precompile"==ARGV[0]
55
+
45
56
  role_cnames = [ "Role" ] if role_cnames.empty?
46
57
  role_cnames.each do |role_cname|
47
58
  role_class = role_cname.constantize
@@ -52,7 +63,7 @@ module Rolify
52
63
  end
53
64
  true
54
65
  end
55
-
66
+
56
67
  def role_table_missing?(role_class)
57
68
  role_class.connected? && !role_class.table_exists?
58
69
  end
data/lib/rolify/role.rb CHANGED
@@ -3,11 +3,11 @@ require "rolify/finders"
3
3
  module Rolify
4
4
  module Role
5
5
  extend Utils
6
-
6
+
7
7
  def self.included(base)
8
8
  base.extend Finders
9
9
  end
10
-
10
+
11
11
  def add_role(role_name, resource = nil)
12
12
  role = self.class.adapter.find_or_create_by(role_name.to_s,
13
13
  (resource.is_a?(Class) ? resource.to_s : resource.class.name if resource),
@@ -20,20 +20,13 @@ module Rolify
20
20
  role
21
21
  end
22
22
  alias_method :grant, :add_role
23
- deprecate :has_role, :add_role
24
23
 
25
24
  def has_role?(role_name, resource = nil)
26
- @r_map ||= {}
27
- role_n_resource = role_name.to_s + resource.to_s
28
- @r_map[role_n_resource].nil? ? @r_map[role_n_resource] = has_role_helper(role_name, resource) : @r_map[role_n_resource]
29
- end
30
-
31
- def has_role_helper(role_name, resource = nil)
32
25
  if new_record?
33
- self.roles.detect { |r| r.name == role_name.to_s && (r.resource == resource || resource.nil?) }.present?
26
+ self.roles.detect { |r| r.name.to_s == role_name.to_s && (r.resource == resource || resource.nil?) }
34
27
  else
35
- self.class.adapter.where(self.roles, :name => role_name, :resource => resource).size > 0
36
- end
28
+ self.class.adapter.where(self.roles, name: role_name, resource: resource)
29
+ end.present?
37
30
  end
38
31
 
39
32
  def has_all_roles?(*args)
@@ -56,7 +49,7 @@ module Rolify
56
49
  self.class.adapter.where(self.roles, *args).size > 0
57
50
  end
58
51
  end
59
-
52
+
60
53
  def only_has_role?(role_name, resource = nil)
61
54
  return self.has_role?(role_name,resource) && self.roles.count == 1
62
55
  end
@@ -64,7 +57,7 @@ module Rolify
64
57
  def remove_role(role_name, resource = nil)
65
58
  self.class.adapter.remove(self, role_name.to_s, resource)
66
59
  end
67
-
60
+
68
61
  alias_method :revoke, :remove_role
69
62
  deprecate :has_no_role, :remove_role
70
63
 
@@ -1,3 +1,3 @@
1
1
  module Rolify
2
- VERSION = "3.4.1"
2
+ VERSION = "3.5.0"
3
3
  end
data/rolify.gemspec CHANGED
@@ -24,6 +24,6 @@ Gem::Specification.new do |s|
24
24
  s.add_development_dependency 'rake', '~> 10.3.2'
25
25
  s.add_development_dependency 'rspec', '2.99.0'
26
26
  s.add_development_dependency 'rspec-rails', '2.99.0'
27
- s.add_development_dependency 'bundler', '~> 1.6.2'
27
+ s.add_development_dependency 'bundler', '~> 1.7.0'
28
28
  s.add_development_dependency 'fuubar', '~> 1.3.3'
29
29
  end
@@ -7,11 +7,11 @@ describe Rolify::Generators::RolifyGenerator, :if => ENV['ADAPTER'] == 'active_r
7
7
  # Tell the generator where to put its output (what it thinks of as Rails.root)
8
8
  destination File.expand_path("../../../../tmp", __FILE__)
9
9
  teardown :cleanup_destination_root
10
-
11
- before {
10
+
11
+ before {
12
12
  prepare_destination
13
13
  }
14
-
14
+
15
15
  def cleanup_destination_root
16
16
  FileUtils.rm_rf destination_root
17
17
  end
@@ -19,7 +19,7 @@ describe Rolify::Generators::RolifyGenerator, :if => ENV['ADAPTER'] == 'active_r
19
19
  describe 'specifying only Role class name' do
20
20
  before(:all) { arguments %w(Role) }
21
21
 
22
- before {
22
+ before {
23
23
  capture(:stdout) {
24
24
  generator.create_file "app/models/user.rb" do
25
25
  <<-RUBY
@@ -29,9 +29,9 @@ describe Rolify::Generators::RolifyGenerator, :if => ENV['ADAPTER'] == 'active_r
29
29
  end
30
30
  }
31
31
  require File.join(destination_root, "app/models/user.rb")
32
- run_generator
32
+ run_generator
33
33
  }
34
-
34
+
35
35
  describe 'config/initializers/rolify.rb' do
36
36
  subject { file('config/initializers/rolify.rb') }
37
37
  it { should exist }
@@ -39,23 +39,24 @@ describe Rolify::Generators::RolifyGenerator, :if => ENV['ADAPTER'] == 'active_r
39
39
  it { should contain "# config.use_dynamic_shortcuts" }
40
40
  it { should contain "# config.use_mongoid" }
41
41
  end
42
-
42
+
43
43
  describe 'app/models/role.rb' do
44
44
  subject { file('app/models/role.rb') }
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
48
  it { should contain "belongs_to :resource, :polymorphic => true" }
49
+ it { should contain "validates :resource_type, :inclusion => { :in => Rolify.resource_types }" }
49
50
  end
50
-
51
+
51
52
  describe 'app/models/user.rb' do
52
53
  subject { file('app/models/user.rb') }
53
54
  it { should contain /class User < ActiveRecord::Base\n rolify\n/ }
54
55
  end
55
-
56
+
56
57
  describe 'migration file' do
57
58
  subject { migration_file('db/migrate/rolify_create_roles.rb') }
58
-
59
+
59
60
  it { should be_a_migration }
60
61
  it { should contain "create_table(:roles) do" }
61
62
  it { should contain "create_table(:users_roles, :id => false) do" }
@@ -64,8 +65,8 @@ describe Rolify::Generators::RolifyGenerator, :if => ENV['ADAPTER'] == 'active_r
64
65
 
65
66
  describe 'specifying User and Role class names' do
66
67
  before(:all) { arguments %w(AdminRole AdminUser) }
67
-
68
- before {
68
+
69
+ before {
69
70
  capture(:stdout) {
70
71
  generator.create_file "app/models/admin_user.rb" do
71
72
  "class AdminUser < ActiveRecord::Base\nend"
@@ -74,44 +75,44 @@ describe Rolify::Generators::RolifyGenerator, :if => ENV['ADAPTER'] == 'active_r
74
75
  require File.join(destination_root, "app/models/admin_user.rb")
75
76
  run_generator
76
77
  }
77
-
78
+
78
79
  describe 'config/initializers/rolify.rb' do
79
80
  subject { file('config/initializers/rolify.rb') }
80
-
81
+
81
82
  it { should exist }
82
83
  it { should contain "Rolify.configure(\"AdminRole\") do |config|"}
83
84
  it { should contain "# config.use_dynamic_shortcuts" }
84
85
  it { should contain "# config.use_mongoid" }
85
86
  end
86
-
87
+
87
88
  describe 'app/models/admin_role.rb' do
88
89
  subject { file('app/models/admin_role.rb') }
89
-
90
+
90
91
  it { should exist }
91
92
  it { should contain "class AdminRole < ActiveRecord::Base" }
92
93
  it { should contain "has_and_belongs_to_many :admin_users, :join_table => :admin_users_admin_roles" }
93
94
  it { should contain "belongs_to :resource, :polymorphic => true" }
94
95
  end
95
-
96
+
96
97
  describe 'app/models/admin_user.rb' do
97
98
  subject { file('app/models/admin_user.rb') }
98
-
99
+
99
100
  it { should contain /class AdminUser < ActiveRecord::Base\n rolify :role_cname => 'AdminRole'\n/ }
100
101
  end
101
-
102
+
102
103
  describe 'migration file' do
103
104
  subject { migration_file('db/migrate/rolify_create_admin_roles.rb') }
104
-
105
+
105
106
  it { should be_a_migration }
106
107
  it { should contain "create_table(:admin_roles)" }
107
108
  it { should contain "create_table(:admin_users_admin_roles, :id => false) do" }
108
109
  end
109
110
  end
110
-
111
+
111
112
  describe 'specifying namespaced User and Role class names' do
112
113
  before(:all) { arguments %w(Admin::Role Admin::User) }
113
-
114
- before {
114
+
115
+ before {
115
116
  capture(:stdout) {
116
117
  generator.create_file "app/models/admin/user.rb" do
117
118
  <<-RUBY
@@ -126,34 +127,34 @@ describe Rolify::Generators::RolifyGenerator, :if => ENV['ADAPTER'] == 'active_r
126
127
  require File.join(destination_root, "app/models/admin/user.rb")
127
128
  run_generator
128
129
  }
129
-
130
+
130
131
  describe 'config/initializers/rolify.rb' do
131
132
  subject { file('config/initializers/rolify.rb') }
132
-
133
+
133
134
  it { should exist }
134
135
  it { should contain "Rolify.configure(\"Admin::Role\") do |config|"}
135
136
  it { should contain "# config.use_dynamic_shortcuts" }
136
137
  it { should contain "# config.use_mongoid" }
137
138
  end
138
-
139
+
139
140
  describe 'app/models/admin/role.rb' do
140
141
  subject { file('app/models/admin/role.rb') }
141
-
142
+
142
143
  it { should exist }
143
144
  it { should contain "class Admin::Role < ActiveRecord::Base" }
144
145
  it { should contain "has_and_belongs_to_many :admin_users, :join_table => :admin_users_admin_roles" }
145
146
  it { should contain "belongs_to :resource, :polymorphic => true" }
146
147
  end
147
-
148
+
148
149
  describe 'app/models/admin/user.rb' do
149
150
  subject { file('app/models/admin/user.rb') }
150
-
151
+
151
152
  it { should contain /class User < ActiveRecord::Base\n rolify :role_cname => 'Admin::Role'\n/ }
152
153
  end
153
-
154
+
154
155
  describe 'migration file' do
155
156
  subject { migration_file('db/migrate/rolify_create_admin_roles.rb') }
156
-
157
+
157
158
  it { should be_a_migration }
158
159
  it { should contain "create_table(:admin_roles)" }
159
160
  it { should contain "create_table(:admin_users_admin_roles, :id => false) do" }
@@ -7,19 +7,19 @@ describe Rolify::Generators::RolifyGenerator, :if => ENV['ADAPTER'] == 'mongoid'
7
7
  # Tell the generator where to put its output (what it thinks of as Rails.root)
8
8
  destination File.expand_path("../../../../tmp", __FILE__)
9
9
  teardown :cleanup_destination_root
10
-
11
- before {
10
+
11
+ before {
12
12
  prepare_destination
13
13
  }
14
-
14
+
15
15
  def cleanup_destination_root
16
16
  FileUtils.rm_rf destination_root
17
17
  end
18
18
 
19
- describe 'specifying ORM adapter' do
19
+ describe 'specifying ORM adapter' do
20
20
  before(:all) { arguments [ "Role", "User", "--orm=mongoid" ] }
21
-
22
- before {
21
+
22
+ before {
23
23
  capture(:stdout) {
24
24
  generator.create_file "app/models/user.rb" do
25
25
  <<-RUBY
@@ -34,7 +34,7 @@ RUBY
34
34
  require File.join(destination_root, "app/models/user.rb")
35
35
  run_generator
36
36
  }
37
-
37
+
38
38
  describe 'config/initializers/rolify.rb' do
39
39
  subject { file('config/initializers/rolify.rb') }
40
40
  it { should exist }
@@ -42,7 +42,7 @@ RUBY
42
42
  it { should_not contain "# config.use_mongoid" }
43
43
  it { should contain "# config.use_dynamic_shortcuts" }
44
44
  end
45
-
45
+
46
46
  describe 'app/models/role.rb' do
47
47
  subject { file('app/models/role.rb') }
48
48
  it { should exist }
@@ -56,18 +56,19 @@ RUBY
56
56
  " { :resource_id => 1 }\n"
57
57
  " },\n"
58
58
  " { unique => true })"}
59
+ it { should contain "validates :resource_type, :inclusion => { :in => Rolify.resource_types }" }
59
60
  end
60
-
61
+
61
62
  describe 'app/models/user.rb' do
62
63
  subject { file('app/models/user.rb') }
63
64
  it { should contain /class User\n include Mongoid::Document\n rolify\n/ }
64
65
  end
65
66
  end
66
-
67
+
67
68
  describe 'specifying namespaced User and Role class names and ORM adapter' do
68
69
  before(:all) { arguments %w(Admin::Role Admin::User --orm=mongoid) }
69
-
70
- before {
70
+
71
+ before {
71
72
  capture(:stdout) {
72
73
  generator.create_file "app/models/admin/user.rb" do
73
74
  <<-RUBY
@@ -82,29 +83,29 @@ RUBY
82
83
  require File.join(destination_root, "app/models/admin/user.rb")
83
84
  run_generator
84
85
  }
85
-
86
+
86
87
  describe 'config/initializers/rolify.rb' do
87
88
  subject { file('config/initializers/rolify.rb') }
88
-
89
+
89
90
  it { should exist }
90
91
  it { should contain "Rolify.configure(\"Admin::Role\") do |config|"}
91
92
  it { should contain "# config.use_dynamic_shortcuts" }
92
93
  it { should_not contain "# config.use_mongoid" }
93
94
  end
94
-
95
+
95
96
  describe 'app/models/admin/role.rb' do
96
97
  subject { file('app/models/admin/role.rb') }
97
-
98
+
98
99
  it { should exist }
99
100
  it { should contain "class Admin::Role" }
100
101
  it { should contain "has_and_belongs_to_many :admin_users" }
101
102
  it { should contain "belongs_to :resource, :polymorphic => true" }
102
103
  end
103
-
104
+
104
105
  describe 'app/models/admin/user.rb' do
105
106
  subject { file('app/models/admin/user.rb') }
106
-
107
+
107
108
  it { should contain /class User\n include Mongoid::Document\n rolify :role_cname => 'Admin::Role'\n/ }
108
109
  end
109
110
  end
110
- end
111
+ end
@@ -4,6 +4,8 @@ require "bundler/setup"
4
4
  require 'rolify'
5
5
  require 'rolify/matchers'
6
6
  require 'rails'
7
+ require_relative 'support/stream_helpers'
8
+ include StreamHelpers
7
9
 
8
10
  require 'coveralls'
9
11
  Coveralls.wear_merged!
@@ -22,4 +24,4 @@ module TestApp
22
24
  end
23
25
  end
24
26
 
25
- require 'ammeter/init'
27
+ require 'ammeter/init'
@@ -403,4 +403,12 @@ describe Rolify::Resource do
403
403
  its(:applied_roles) { should_not include(forum_role, godfather_role, sneaky_role, tourist_role) }
404
404
  end
405
405
  end
406
+
407
+ describe '.resource_types' do
408
+
409
+ it 'include all models that call resourcify' do
410
+ Rolify.resource_types.should include("HumanResource", "Forum", "Group",
411
+ "Team", "Organization")
412
+ end
413
+ end
406
414
  end
@@ -12,9 +12,19 @@ describe Rolify do
12
12
  def role_class
13
13
  Role
14
14
  end
15
-
15
+
16
16
  it_behaves_like Rolify::Role
17
17
  it_behaves_like "Role.scopes"
18
18
  it_behaves_like Rolify::Dynamic
19
19
  it_behaves_like "Rolify.callbacks"
20
+
21
+ context 'cache', pending: true do
22
+ let(:user) { User.first }
23
+ before { user.grant(:zombie) }
24
+ specify do
25
+ expect(user).to have_role(:zombie)
26
+ user.remove_role(:zombie)
27
+ expect(user).to_not have_role(:zombie)
28
+ end
29
+ end
20
30
  end
@@ -29,7 +29,7 @@ shared_examples_for Rolify::Role do
29
29
 
30
30
  subject { user_class.first }
31
31
 
32
- [ :has_role, :grant, :add_role ].each do |method_alias|
32
+ [ :grant, :add_role ].each do |method_alias|
33
33
  it { should respond_to(method_alias.to_sym).with(1).arguments }
34
34
  it { should respond_to(method_alias.to_sym).with(2).arguments }
35
35
  end
@@ -51,11 +51,6 @@ shared_examples_for Rolify::Role do
51
51
  it { should_not respond_to(:is_admin?) }
52
52
  it { should_not respond_to(:is_moderator_of?) }
53
53
 
54
- describe "#has_role" do
55
- it_should_behave_like "#add_role_examples", "String", :to_s
56
- it_should_behave_like "#add_role_examples", "Symbol", :to_sym
57
- end
58
-
59
54
  describe "#has_role?" do
60
55
  it_should_behave_like "#has_role?_examples", "String", :to_s
61
56
  it_should_behave_like "#has_role?_examples", "Symbol", :to_sym
data/spec/spec_helper.rb CHANGED
@@ -1,3 +1,6 @@
1
+ require "codeclimate-test-reporter"
2
+ CodeClimate::TestReporter.start
3
+
1
4
  require 'rubygems'
2
5
  require "bundler/setup"
3
6
 
@@ -5,8 +5,8 @@ ActiveRecord::Schema.define do
5
5
  create_table(table) do |t|
6
6
  t.string :name
7
7
  t.references :resource, :polymorphic => true
8
-
9
- t.timestamps
8
+
9
+ t.timestamps null: false
10
10
  end
11
11
  end
12
12
 
@@ -20,17 +20,17 @@ ActiveRecord::Schema.define do
20
20
  t.references :user
21
21
  t.references :role
22
22
  end
23
-
23
+
24
24
  create_table(:human_resources_roles, :id => false) do |t|
25
25
  t.references :human_resource
26
26
  t.references :role
27
27
  end
28
-
28
+
29
29
  create_table(:customers_privileges, :id => false) do |t|
30
30
  t.references :customer
31
31
  t.references :privilege
32
32
  end
33
-
33
+
34
34
  create_table(:moderators_rights, :id => false) do |t|
35
35
  t.references :moderator
36
36
  t.references :right
@@ -44,7 +44,7 @@ ActiveRecord::Schema.define do
44
44
  t.integer :parent_id
45
45
  t.string :name
46
46
  end
47
-
47
+
48
48
  create_table(:teams, :id => false) do |t|
49
49
  t.primary_key :team_code
50
50
  t.string :name
@@ -0,0 +1,18 @@
1
+ module StreamHelpers
2
+ def capture(stream)
3
+ stream = stream.to_s
4
+ captured_stream = Tempfile.new(stream)
5
+ stream_io = eval("$#{stream}")
6
+ origin_stream = stream_io.dup
7
+ stream_io.reopen(captured_stream)
8
+
9
+ yield
10
+
11
+ stream_io.rewind
12
+ return captured_stream.read
13
+ ensure
14
+ captured_stream.close
15
+ captured_stream.unlink
16
+ stream_io.reopen(origin_stream)
17
+ end
18
+ end
metadata CHANGED
@@ -1,52 +1,46 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rolify
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.4.1
5
- prerelease:
4
+ version: 3.5.0
6
5
  platform: ruby
7
6
  authors:
8
7
  - Florent Monbillard
9
8
  autorequire:
10
9
  bindir: bin
11
10
  cert_chain: []
12
- date: 2014-09-08 00:00:00.000000000 Z
11
+ date: 2015-01-08 00:00:00.000000000 Z
13
12
  dependencies:
14
13
  - !ruby/object:Gem::Dependency
15
14
  name: ammeter
16
15
  requirement: !ruby/object:Gem::Requirement
17
- none: false
18
16
  requirements:
19
- - - ~>
17
+ - - "~>"
20
18
  - !ruby/object:Gem::Version
21
19
  version: 1.1.2
22
20
  type: :development
23
21
  prerelease: false
24
22
  version_requirements: !ruby/object:Gem::Requirement
25
- none: false
26
23
  requirements:
27
- - - ~>
24
+ - - "~>"
28
25
  - !ruby/object:Gem::Version
29
26
  version: 1.1.2
30
27
  - !ruby/object:Gem::Dependency
31
28
  name: rake
32
29
  requirement: !ruby/object:Gem::Requirement
33
- none: false
34
30
  requirements:
35
- - - ~>
31
+ - - "~>"
36
32
  - !ruby/object:Gem::Version
37
33
  version: 10.3.2
38
34
  type: :development
39
35
  prerelease: false
40
36
  version_requirements: !ruby/object:Gem::Requirement
41
- none: false
42
37
  requirements:
43
- - - ~>
38
+ - - "~>"
44
39
  - !ruby/object:Gem::Version
45
40
  version: 10.3.2
46
41
  - !ruby/object:Gem::Dependency
47
42
  name: rspec
48
43
  requirement: !ruby/object:Gem::Requirement
49
- none: false
50
44
  requirements:
51
45
  - - '='
52
46
  - !ruby/object:Gem::Version
@@ -54,7 +48,6 @@ dependencies:
54
48
  type: :development
55
49
  prerelease: false
56
50
  version_requirements: !ruby/object:Gem::Requirement
57
- none: false
58
51
  requirements:
59
52
  - - '='
60
53
  - !ruby/object:Gem::Version
@@ -62,7 +55,6 @@ dependencies:
62
55
  - !ruby/object:Gem::Dependency
63
56
  name: rspec-rails
64
57
  requirement: !ruby/object:Gem::Requirement
65
- none: false
66
58
  requirements:
67
59
  - - '='
68
60
  - !ruby/object:Gem::Version
@@ -70,7 +62,6 @@ dependencies:
70
62
  type: :development
71
63
  prerelease: false
72
64
  version_requirements: !ruby/object:Gem::Requirement
73
- none: false
74
65
  requirements:
75
66
  - - '='
76
67
  - !ruby/object:Gem::Version
@@ -78,33 +69,29 @@ dependencies:
78
69
  - !ruby/object:Gem::Dependency
79
70
  name: bundler
80
71
  requirement: !ruby/object:Gem::Requirement
81
- none: false
82
72
  requirements:
83
- - - ~>
73
+ - - "~>"
84
74
  - !ruby/object:Gem::Version
85
- version: 1.6.2
75
+ version: 1.7.0
86
76
  type: :development
87
77
  prerelease: false
88
78
  version_requirements: !ruby/object:Gem::Requirement
89
- none: false
90
79
  requirements:
91
- - - ~>
80
+ - - "~>"
92
81
  - !ruby/object:Gem::Version
93
- version: 1.6.2
82
+ version: 1.7.0
94
83
  - !ruby/object:Gem::Dependency
95
84
  name: fuubar
96
85
  requirement: !ruby/object:Gem::Requirement
97
- none: false
98
86
  requirements:
99
- - - ~>
87
+ - - "~>"
100
88
  - !ruby/object:Gem::Version
101
89
  version: 1.3.3
102
90
  type: :development
103
91
  prerelease: false
104
92
  version_requirements: !ruby/object:Gem::Requirement
105
- none: false
106
93
  requirements:
107
- - - ~>
94
+ - - "~>"
108
95
  - !ruby/object:Gem::Version
109
96
  version: 1.3.3
110
97
  description: Very simple Roles library without any authorization enforcement supporting
@@ -116,14 +103,16 @@ executables: []
116
103
  extensions: []
117
104
  extra_rdoc_files: []
118
105
  files:
119
- - .gitignore
120
- - .travis.yml
106
+ - ".gitignore"
107
+ - ".travis.yml"
121
108
  - CHANGELOG.rdoc
109
+ - CONTRIBUTORS
122
110
  - Gemfile
123
111
  - LICENSE
124
112
  - README.md
125
113
  - Rakefile
126
114
  - UPGRADE.rdoc
115
+ - circle.yml
127
116
  - gemfiles/Gemfile.rails-3.2
128
117
  - gemfiles/Gemfile.rails-4.0
129
118
  - gemfiles/Gemfile.rails-4.1
@@ -185,30 +174,30 @@ files:
185
174
  - spec/support/adapters/mongoid.yml
186
175
  - spec/support/data.rb
187
176
  - spec/support/schema.rb
177
+ - spec/support/stream_helpers.rb
188
178
  homepage: http://eppo.github.com/rolify/
189
179
  licenses:
190
180
  - MIT
181
+ metadata: {}
191
182
  post_install_message:
192
183
  rdoc_options: []
193
184
  require_paths:
194
185
  - lib
195
186
  required_ruby_version: !ruby/object:Gem::Requirement
196
- none: false
197
187
  requirements:
198
- - - ! '>='
188
+ - - ">="
199
189
  - !ruby/object:Gem::Version
200
190
  version: '0'
201
191
  required_rubygems_version: !ruby/object:Gem::Requirement
202
- none: false
203
192
  requirements:
204
- - - ! '>='
193
+ - - ">="
205
194
  - !ruby/object:Gem::Version
206
195
  version: '0'
207
196
  requirements: []
208
197
  rubyforge_project: rolify
209
- rubygems_version: 1.8.23
198
+ rubygems_version: 2.2.2
210
199
  signing_key:
211
- specification_version: 3
200
+ specification_version: 4
212
201
  summary: Roles library with resource scoping
213
202
  test_files:
214
203
  - spec/README.rdoc
@@ -240,3 +229,4 @@ test_files:
240
229
  - spec/support/adapters/mongoid.yml
241
230
  - spec/support/data.rb
242
231
  - spec/support/schema.rb
232
+ - spec/support/stream_helpers.rb