rolify 3.1.0 → 4.0.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 +7 -0
- data/.gitignore +4 -1
- data/.travis.yml +44 -11
- data/CHANGELOG.rdoc +70 -0
- data/CONTRIBUTORS +6 -0
- data/Gemfile +21 -2
- data/README.md +130 -68
- data/Rakefile +29 -4
- data/UPGRADE.rdoc +22 -0
- data/circle.yml +13 -0
- data/gemfiles/Gemfile.rails-3.2 +27 -0
- data/gemfiles/Gemfile.rails-4.0 +33 -0
- data/gemfiles/Gemfile.rails-4.1 +37 -0
- data/lib/generators/active_record/rolify_generator.rb +54 -0
- data/lib/generators/active_record/templates/README +8 -0
- data/lib/generators/active_record/templates/migration.rb +19 -0
- data/lib/generators/mongoid/rolify_generator.rb +55 -0
- data/lib/generators/mongoid/templates/README-mongoid +4 -0
- data/lib/generators/rolify/rolify_generator.rb +35 -0
- data/lib/generators/rolify/{role/templates/README-mongoid → templates/README} +1 -5
- data/lib/generators/rolify/templates/initializer.rb +7 -0
- data/lib/generators/rolify/templates/role-active_record.rb +11 -0
- data/lib/generators/rolify/{role/templates → templates}/role-mongoid.rb +9 -9
- data/lib/generators/rolify/user_generator.rb +39 -0
- data/lib/rolify/adapters/active_record/resource_adapter.rb +36 -6
- data/lib/rolify/adapters/active_record/role_adapter.rb +25 -13
- data/lib/rolify/adapters/active_record/scopes.rb +27 -0
- data/lib/rolify/adapters/base.rb +9 -0
- data/lib/rolify/adapters/mongoid/resource_adapter.rb +27 -6
- data/lib/rolify/adapters/mongoid/role_adapter.rb +35 -11
- data/lib/rolify/adapters/mongoid/scopes.rb +27 -0
- data/lib/rolify/configure.rb +36 -5
- data/lib/rolify/finders.rb +40 -0
- data/lib/rolify/matchers.rb +31 -0
- data/lib/rolify/railtie.rb +1 -1
- data/lib/rolify/resource.rb +19 -10
- data/lib/rolify/role.rb +33 -13
- data/lib/rolify/version.rb +1 -1
- data/lib/rolify.rb +46 -20
- data/rolify.gemspec +18 -24
- data/spec/README.rdoc +24 -0
- data/spec/generators/rolify/rolify_activerecord_generator_spec.rb +165 -0
- data/spec/generators/rolify/rolify_mongoid_generator_spec.rb +113 -0
- data/spec/generators_helper.rb +27 -0
- data/spec/rolify/config_spec.rb +20 -21
- data/spec/rolify/custom_spec.rb +11 -6
- data/spec/rolify/matchers_spec.rb +24 -0
- data/spec/rolify/namespace_spec.rb +24 -0
- data/spec/rolify/resource_spec.rb +135 -37
- data/spec/rolify/resourcifed_and_rolifed_spec.rb +24 -0
- data/spec/rolify/role_spec.rb +8 -9
- data/spec/rolify/shared_contexts.rb +22 -3
- data/spec/rolify/shared_examples/shared_examples_for_add_role.rb +10 -17
- data/spec/rolify/shared_examples/shared_examples_for_callbacks.rb +65 -0
- data/spec/rolify/shared_examples/shared_examples_for_dynamic.rb +64 -27
- data/spec/rolify/shared_examples/shared_examples_for_finders.rb +81 -0
- data/spec/rolify/shared_examples/shared_examples_for_has_all_roles.rb +29 -29
- data/spec/rolify/shared_examples/shared_examples_for_has_any_role.rb +35 -35
- data/spec/rolify/shared_examples/shared_examples_for_has_role.rb +38 -38
- data/spec/rolify/shared_examples/shared_examples_for_only_has_role.rb +174 -0
- data/spec/rolify/shared_examples/shared_examples_for_remove_role.rb +28 -59
- data/spec/rolify/shared_examples/shared_examples_for_roles.rb +58 -32
- data/spec/rolify/shared_examples/shared_examples_for_scopes.rb +38 -0
- data/spec/spec_helper.rb +39 -3
- data/spec/support/adapters/active_record.rb +58 -7
- data/spec/support/adapters/mongoid.rb +110 -17
- data/spec/support/adapters/mongoid.yml +6 -0
- data/spec/support/data.rb +18 -15
- data/spec/support/schema.rb +31 -16
- data/spec/support/stream_helpers.rb +18 -0
- metadata +88 -84
- data/lib/generators/rolify/role/role_generator.rb +0 -41
- data/lib/generators/rolify/role/templates/README +0 -11
- data/lib/generators/rolify/role/templates/README-active_record +0 -21
- data/lib/generators/rolify/role/templates/initializer.rb +0 -7
- data/lib/generators/rolify/role/templates/migration.rb +0 -19
- data/lib/generators/rolify/role/templates/role-active_record.rb +0 -4
- data/spec/generators/rolify/role/role_generator_spec.rb +0 -197
data/lib/rolify/configure.rb
CHANGED
|
@@ -2,8 +2,10 @@ module Rolify
|
|
|
2
2
|
module Configure
|
|
3
3
|
@@dynamic_shortcuts = false
|
|
4
4
|
@@orm = "active_record"
|
|
5
|
-
|
|
6
|
-
|
|
5
|
+
@@remove_role_if_empty = true
|
|
6
|
+
|
|
7
|
+
def configure(*role_cnames)
|
|
8
|
+
return if !sanity_check(role_cnames)
|
|
7
9
|
yield self if block_given?
|
|
8
10
|
end
|
|
9
11
|
|
|
@@ -26,9 +28,10 @@ module Rolify
|
|
|
26
28
|
def use_mongoid
|
|
27
29
|
self.orm = "mongoid"
|
|
28
30
|
end
|
|
29
|
-
|
|
31
|
+
|
|
30
32
|
def use_dynamic_shortcuts
|
|
31
|
-
|
|
33
|
+
return if !sanity_check([])
|
|
34
|
+
self.dynamic_shortcuts = true
|
|
32
35
|
end
|
|
33
36
|
|
|
34
37
|
def use_defaults
|
|
@@ -37,5 +40,33 @@ module Rolify
|
|
|
37
40
|
config.orm = "active_record"
|
|
38
41
|
end
|
|
39
42
|
end
|
|
43
|
+
|
|
44
|
+
def remove_role_if_empty=(is_remove)
|
|
45
|
+
@@remove_role_if_empty = is_remove
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
def remove_role_if_empty
|
|
49
|
+
@@remove_role_if_empty
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
private
|
|
53
|
+
|
|
54
|
+
def sanity_check(role_cnames)
|
|
55
|
+
return true if "assets:precompile"==ARGV[0]
|
|
56
|
+
|
|
57
|
+
role_cnames = [ "Role" ] if role_cnames.empty?
|
|
58
|
+
role_cnames.each do |role_cname|
|
|
59
|
+
role_class = role_cname.constantize
|
|
60
|
+
if role_class.superclass.to_s == "ActiveRecord::Base" && role_table_missing?(role_class)
|
|
61
|
+
warn "[WARN] table '#{role_cname}' doesn't exist. Did you run the migration ? Ignoring rolify config."
|
|
62
|
+
return false
|
|
63
|
+
end
|
|
64
|
+
end
|
|
65
|
+
true
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
def role_table_missing?(role_class)
|
|
69
|
+
!role_class.table_exists?
|
|
70
|
+
end
|
|
40
71
|
end
|
|
41
|
-
end
|
|
72
|
+
end
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
module Rolify
|
|
2
|
+
module Finders
|
|
3
|
+
def with_role(role_name, resource = nil)
|
|
4
|
+
self.adapter.scope(self, :name => role_name, :resource => resource)
|
|
5
|
+
end
|
|
6
|
+
|
|
7
|
+
def with_all_roles(*args)
|
|
8
|
+
users = []
|
|
9
|
+
parse_args(args, users) do |users_to_add|
|
|
10
|
+
users = users_to_add if users.empty?
|
|
11
|
+
users &= users_to_add
|
|
12
|
+
return [] if users.empty?
|
|
13
|
+
end
|
|
14
|
+
users
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
def with_any_role(*args)
|
|
18
|
+
users = []
|
|
19
|
+
parse_args(args, users) do |users_to_add|
|
|
20
|
+
users += users_to_add
|
|
21
|
+
end
|
|
22
|
+
users.uniq
|
|
23
|
+
end
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
private
|
|
27
|
+
|
|
28
|
+
def parse_args(args, users, &block)
|
|
29
|
+
args.each do |arg|
|
|
30
|
+
if arg.is_a? Hash
|
|
31
|
+
users_to_add = self.with_role(arg[:name], arg[:resource])
|
|
32
|
+
elsif arg.is_a?(String) || arg.is_a?(Symbol)
|
|
33
|
+
users_to_add = self.with_role(arg)
|
|
34
|
+
else
|
|
35
|
+
raise ArgumentError, "Invalid argument type: only hash or string or symbol allowed"
|
|
36
|
+
end
|
|
37
|
+
block.call(users_to_add)
|
|
38
|
+
end
|
|
39
|
+
end
|
|
40
|
+
end
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
require 'rspec/expectations'
|
|
2
|
+
|
|
3
|
+
RSpec::Matchers.define :have_role do |*args|
|
|
4
|
+
match do |resource|
|
|
5
|
+
resource.has_role?(*args)
|
|
6
|
+
end
|
|
7
|
+
|
|
8
|
+
failure_message_for_should do |resource|
|
|
9
|
+
"expected to have role #{args.map(&:inspect).join(" ")}"
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
failure_message_for_should_not do |resource|
|
|
13
|
+
"expected not to have role #{args.map(&:inspect).join(" ")}"
|
|
14
|
+
end
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
RSpec::Matchers.define :be_the_same_role do |*expected|
|
|
18
|
+
match do |actual|
|
|
19
|
+
if expected.size > 1
|
|
20
|
+
if expected[1].is_a? Class
|
|
21
|
+
actual[:name] == expected[0] && actual[:resource_type] == expected[1].to_s
|
|
22
|
+
else
|
|
23
|
+
actual[:name] == expected[0] &&
|
|
24
|
+
actual[:resource_type] == expected[1].class.name &&
|
|
25
|
+
actual[:resource_id] == expected[1].id
|
|
26
|
+
end
|
|
27
|
+
else
|
|
28
|
+
actual[:name] == expected[0]
|
|
29
|
+
end
|
|
30
|
+
end
|
|
31
|
+
end
|
data/lib/rolify/railtie.rb
CHANGED
data/lib/rolify/resource.rb
CHANGED
|
@@ -4,22 +4,31 @@ module Rolify
|
|
|
4
4
|
base.extend ClassMethods
|
|
5
5
|
end
|
|
6
6
|
|
|
7
|
-
module ClassMethods
|
|
7
|
+
module ClassMethods
|
|
8
8
|
def find_roles(role_name = nil, user = nil)
|
|
9
|
-
|
|
10
|
-
roles = roles.where(:resource_type => self.to_s)
|
|
11
|
-
roles = roles.where(:name => role_name) if role_name && (role_name != :any)
|
|
12
|
-
roles
|
|
9
|
+
self.adapter.find_roles(role_name, self, user)
|
|
13
10
|
end
|
|
14
11
|
|
|
15
12
|
def with_role(role_name, user = nil)
|
|
16
|
-
|
|
17
|
-
|
|
13
|
+
if role_name.is_a? Array
|
|
14
|
+
role_name.map!(&:to_s)
|
|
15
|
+
else
|
|
16
|
+
role_name = role_name.to_s
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
resources = self.adapter.resources_find(self.role_table_name, self, role_name) #.map(&:id)
|
|
20
|
+
user ? self.adapter.in(resources, user, role_name) : resources
|
|
21
|
+
end
|
|
22
|
+
alias :with_roles :with_role
|
|
23
|
+
|
|
24
|
+
def applied_roles(children = true)
|
|
25
|
+
self.adapter.applied_roles(self, children)
|
|
18
26
|
end
|
|
19
27
|
end
|
|
20
|
-
|
|
28
|
+
|
|
21
29
|
def applied_roles
|
|
22
|
-
self.roles + self.class.role_class.where(:resource_type => self.class.to_s, :resource_id => nil)
|
|
30
|
+
#self.roles + self.class.role_class.where(:resource_type => self.class.to_s, :resource_id => nil)
|
|
31
|
+
self.roles + self.class.applied_roles(true)
|
|
23
32
|
end
|
|
24
33
|
end
|
|
25
|
-
end
|
|
34
|
+
end
|
data/lib/rolify/role.rb
CHANGED
|
@@ -1,10 +1,17 @@
|
|
|
1
|
+
require "rolify/finders"
|
|
2
|
+
require "rolify/utils"
|
|
3
|
+
|
|
1
4
|
module Rolify
|
|
2
5
|
module Role
|
|
3
6
|
extend Utils
|
|
4
|
-
|
|
7
|
+
|
|
8
|
+
def self.included(base)
|
|
9
|
+
base.extend Finders
|
|
10
|
+
end
|
|
11
|
+
|
|
5
12
|
def add_role(role_name, resource = nil)
|
|
6
|
-
role = self.class.adapter.find_or_create_by(role_name,
|
|
7
|
-
(resource.is_a?(Class) ? resource.to_s : resource.class.name if resource),
|
|
13
|
+
role = self.class.adapter.find_or_create_by(role_name.to_s,
|
|
14
|
+
(resource.is_a?(Class) ? resource.to_s : resource.class.name if resource),
|
|
8
15
|
(resource.id if resource && !resource.is_a?(Class)))
|
|
9
16
|
|
|
10
17
|
if !roles.include?(role)
|
|
@@ -14,10 +21,16 @@ module Rolify
|
|
|
14
21
|
role
|
|
15
22
|
end
|
|
16
23
|
alias_method :grant, :add_role
|
|
17
|
-
deprecate :has_role, :add_role
|
|
18
24
|
|
|
19
25
|
def has_role?(role_name, resource = nil)
|
|
20
|
-
|
|
26
|
+
if new_record?
|
|
27
|
+
role_array = self.roles.detect { |r| r.name.to_s == role_name.to_s && (r.resource == resource || resource.nil?) }
|
|
28
|
+
else
|
|
29
|
+
role_array = self.class.adapter.where(self.roles, name: role_name, resource: resource)
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
return false if role_array.nil?
|
|
33
|
+
role_array != []
|
|
21
34
|
end
|
|
22
35
|
|
|
23
36
|
def has_all_roles?(*args)
|
|
@@ -34,12 +47,21 @@ module Rolify
|
|
|
34
47
|
end
|
|
35
48
|
|
|
36
49
|
def has_any_role?(*args)
|
|
37
|
-
|
|
50
|
+
if new_record?
|
|
51
|
+
args.any? { |r| self.has_role?(r) }
|
|
52
|
+
else
|
|
53
|
+
self.class.adapter.where(self.roles, *args).size > 0
|
|
54
|
+
end
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
def only_has_role?(role_name, resource = nil)
|
|
58
|
+
return self.has_role?(role_name,resource) && self.roles.count == 1
|
|
38
59
|
end
|
|
39
60
|
|
|
40
61
|
def remove_role(role_name, resource = nil)
|
|
41
|
-
self.class.adapter.remove(self, role_name, resource)
|
|
62
|
+
self.class.adapter.remove(self, role_name.to_s, resource)
|
|
42
63
|
end
|
|
64
|
+
|
|
43
65
|
alias_method :revoke, :remove_role
|
|
44
66
|
deprecate :has_no_role, :remove_role
|
|
45
67
|
|
|
@@ -49,11 +71,9 @@ module Rolify
|
|
|
49
71
|
|
|
50
72
|
def method_missing(method, *args, &block)
|
|
51
73
|
if method.to_s.match(/^is_(\w+)_of[?]$/) || method.to_s.match(/^is_(\w+)[?]$/)
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
return has_role?("#{$1}", resource)
|
|
56
|
-
end
|
|
74
|
+
resource = args.first
|
|
75
|
+
self.class.define_dynamic_method $1, resource
|
|
76
|
+
return has_role?("#{$1}", resource)
|
|
57
77
|
end unless !Rolify.dynamic_shortcuts
|
|
58
78
|
super
|
|
59
79
|
end
|
|
@@ -69,4 +89,4 @@ module Rolify
|
|
|
69
89
|
end
|
|
70
90
|
end
|
|
71
91
|
end
|
|
72
|
-
end
|
|
92
|
+
end
|
data/lib/rolify/version.rb
CHANGED
data/lib/rolify.rb
CHANGED
|
@@ -1,42 +1,68 @@
|
|
|
1
|
-
require 'rolify/
|
|
2
|
-
require 'rolify/utils'
|
|
3
|
-
require 'rolify/role'
|
|
1
|
+
require 'rolify/adapters/base'
|
|
4
2
|
require 'rolify/configure'
|
|
5
3
|
require 'rolify/dynamic'
|
|
4
|
+
require 'rolify/railtie' if defined?(Rails)
|
|
6
5
|
require 'rolify/resource'
|
|
7
|
-
require 'rolify/
|
|
6
|
+
require 'rolify/role'
|
|
8
7
|
|
|
9
8
|
module Rolify
|
|
10
9
|
extend Configure
|
|
11
10
|
|
|
12
|
-
attr_accessor :role_cname, :adapter
|
|
11
|
+
attr_accessor :role_cname, :adapter, :role_join_table_name, :role_table_name
|
|
12
|
+
@@resource_types = []
|
|
13
13
|
|
|
14
|
-
def rolify(options = {
|
|
14
|
+
def rolify(options = {})
|
|
15
15
|
include Role
|
|
16
16
|
extend Dynamic if Rolify.dynamic_shortcuts
|
|
17
|
-
|
|
17
|
+
|
|
18
|
+
options.reverse_merge!({:role_cname => 'Role'})
|
|
19
|
+
self.role_cname = options[:role_cname]
|
|
20
|
+
self.role_table_name = self.role_cname.tableize.gsub(/\//, "_")
|
|
21
|
+
|
|
22
|
+
default_join_table = "#{self.to_s.tableize.gsub(/\//, "_")}_#{self.role_table_name}"
|
|
23
|
+
options.reverse_merge!({:role_join_table_name => default_join_table})
|
|
24
|
+
self.role_join_table_name = options[:role_join_table_name]
|
|
25
|
+
|
|
18
26
|
rolify_options = { :class_name => options[:role_cname].camelize }
|
|
19
|
-
rolify_options.merge!({ :join_table =>
|
|
27
|
+
rolify_options.merge!({ :join_table => self.role_join_table_name }) if Rolify.orm == "active_record"
|
|
28
|
+
rolify_options.merge!(options.reject{ |k,v| ![ :before_add, :after_add, :before_remove, :after_remove ].include? k.to_sym })
|
|
29
|
+
|
|
20
30
|
has_and_belongs_to_many :roles, rolify_options
|
|
21
31
|
|
|
22
|
-
self.adapter = Rolify::Adapter::Base.create("role_adapter",
|
|
23
|
-
self.role_cname = options[:role_cname]
|
|
24
|
-
|
|
32
|
+
self.adapter = Rolify::Adapter::Base.create("role_adapter", self.role_cname, self.name)
|
|
25
33
|
load_dynamic_methods if Rolify.dynamic_shortcuts
|
|
26
34
|
end
|
|
27
35
|
|
|
28
|
-
def
|
|
36
|
+
def adapter
|
|
37
|
+
return self.superclass.adapter unless self.instance_variable_defined? '@adapter'
|
|
38
|
+
@adapter
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
def resourcify(association_name = :roles, options = {})
|
|
29
42
|
include Resource
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
resourcify_options
|
|
33
|
-
has_many :roles, resourcify_options
|
|
34
|
-
|
|
35
|
-
self.adapter = Rolify::Adapter::Base.create("resource_adapter", options[:role_cname], self.name)
|
|
43
|
+
|
|
44
|
+
options.reverse_merge!({ :role_cname => 'Role', :dependent => :destroy })
|
|
45
|
+
resourcify_options = { :class_name => options[:role_cname].camelize, :as => :resource, :dependent => options[:dependent] }
|
|
36
46
|
self.role_cname = options[:role_cname]
|
|
47
|
+
self.role_table_name = self.role_cname.tableize.gsub(/\//, "_")
|
|
48
|
+
|
|
49
|
+
has_many association_name, resourcify_options
|
|
50
|
+
|
|
51
|
+
self.adapter = Rolify::Adapter::Base.create("resource_adapter", self.role_cname, self.name)
|
|
52
|
+
@@resource_types << self.name
|
|
37
53
|
end
|
|
38
|
-
|
|
54
|
+
|
|
55
|
+
def scopify
|
|
56
|
+
require "rolify/adapters/#{Rolify.orm}/scopes.rb"
|
|
57
|
+
extend Rolify::Adapter::Scopes
|
|
58
|
+
end
|
|
59
|
+
|
|
39
60
|
def role_class
|
|
61
|
+
return self.superclass.role_class unless self.instance_variable_defined? '@role_cname'
|
|
40
62
|
self.role_cname.constantize
|
|
41
63
|
end
|
|
42
|
-
|
|
64
|
+
|
|
65
|
+
def self.resource_types
|
|
66
|
+
@@resource_types
|
|
67
|
+
end
|
|
68
|
+
end
|
data/rolify.gemspec
CHANGED
|
@@ -1,34 +1,28 @@
|
|
|
1
1
|
# -*- encoding: utf-8 -*-
|
|
2
|
-
$:.push File.expand_path(
|
|
3
|
-
require
|
|
2
|
+
$:.push File.expand_path('../lib', __FILE__)
|
|
3
|
+
require 'rolify/version'
|
|
4
4
|
|
|
5
5
|
Gem::Specification.new do |s|
|
|
6
|
-
s.name =
|
|
6
|
+
s.name = 'rolify'
|
|
7
|
+
s.summary = %q{Roles library with resource scoping}
|
|
8
|
+
s.description = %q{Very simple Roles library without any authorization enforcement supporting scope on resource objects (instance or class). Supports ActiveRecord and Mongoid ORMs.}
|
|
7
9
|
s.version = Rolify::VERSION
|
|
8
10
|
s.platform = Gem::Platform::RUBY
|
|
9
|
-
s.
|
|
10
|
-
s.email = ["f.monbillard@gmail.com"]
|
|
11
|
-
s.homepage = "https://github.com/EppO/rolify"
|
|
12
|
-
s.summary = %q{Roles library with resource scoping}
|
|
13
|
-
s.description = %q{Very simple Roles library without any authorization enforcement supporting scope on resource objects (instance or class)}
|
|
14
|
-
|
|
11
|
+
s.homepage = 'https://github.com/RolifyCommunity/rolify'
|
|
15
12
|
s.rubyforge_project = s.name
|
|
16
13
|
|
|
14
|
+
s.license = 'MIT'
|
|
15
|
+
|
|
16
|
+
s.authors = ['Florent Monbillard']
|
|
17
|
+
s.email = ['f.monbillard@gmail.com']
|
|
18
|
+
|
|
17
19
|
s.files = `git ls-files`.split("\n")
|
|
18
|
-
s.test_files = `git ls-files --
|
|
20
|
+
s.test_files = `git ls-files -- spec/*`.split("\n")
|
|
19
21
|
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
|
20
|
-
s.require_paths = [
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
end
|
|
27
|
-
s.add_development_dependency "activerecord", ">= 3.1.0"
|
|
28
|
-
s.add_development_dependency "mongoid", ">= 2.3"
|
|
29
|
-
s.add_development_dependency "bson_ext"
|
|
30
|
-
s.add_development_dependency "ammeter"
|
|
31
|
-
s.add_development_dependency "rake"
|
|
32
|
-
s.add_development_dependency "rspec"
|
|
33
|
-
s.add_development_dependency "bundler"
|
|
22
|
+
s.require_paths = ['lib']
|
|
23
|
+
|
|
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'
|
|
34
28
|
end
|
data/spec/README.rdoc
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
= Rolify Specs
|
|
2
|
+
|
|
3
|
+
== Running the specs
|
|
4
|
+
|
|
5
|
+
To run the specs first run the +bundle+ command to install the necessary gems and the +rake+ command to run the specs.
|
|
6
|
+
|
|
7
|
+
bundle
|
|
8
|
+
rake
|
|
9
|
+
|
|
10
|
+
== Model Adapters
|
|
11
|
+
|
|
12
|
+
Rolify currently supports 2 different ORMs: ActiveRecord and Mongoid. By default it will use Active Record but you can change this by setting the +ADAPTER+ environment variable before running the specs. You can run the +bundle+ command with this as well to ensure you have all the required gems.
|
|
13
|
+
|
|
14
|
+
ADAPTER=mongoid bundle
|
|
15
|
+
ADAPTER=mongoid rake
|
|
16
|
+
|
|
17
|
+
The different model adapters you can specify are:
|
|
18
|
+
|
|
19
|
+
* active_record (default)
|
|
20
|
+
* mongoid
|
|
21
|
+
|
|
22
|
+
You can also run the +spec_all+ rake task to run specs for each adapter.
|
|
23
|
+
|
|
24
|
+
rake spec_all
|
|
@@ -0,0 +1,165 @@
|
|
|
1
|
+
require 'generators_helper'
|
|
2
|
+
|
|
3
|
+
# Generators are not automatically loaded by Rails
|
|
4
|
+
require 'generators/rolify/rolify_generator'
|
|
5
|
+
|
|
6
|
+
describe Rolify::Generators::RolifyGenerator, :if => ENV['ADAPTER'] == 'active_record' do
|
|
7
|
+
# Tell the generator where to put its output (what it thinks of as Rails.root)
|
|
8
|
+
destination File.expand_path("../../../../tmp", __FILE__)
|
|
9
|
+
teardown :cleanup_destination_root
|
|
10
|
+
|
|
11
|
+
before {
|
|
12
|
+
prepare_destination
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
def cleanup_destination_root
|
|
16
|
+
FileUtils.rm_rf destination_root
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
describe 'specifying only Role class name' do
|
|
20
|
+
before(:all) { arguments %w(Role) }
|
|
21
|
+
|
|
22
|
+
before {
|
|
23
|
+
capture(:stdout) {
|
|
24
|
+
generator.create_file "app/models/user.rb" do
|
|
25
|
+
<<-RUBY
|
|
26
|
+
class User < ActiveRecord::Base
|
|
27
|
+
end
|
|
28
|
+
RUBY
|
|
29
|
+
end
|
|
30
|
+
}
|
|
31
|
+
require File.join(destination_root, "app/models/user.rb")
|
|
32
|
+
run_generator
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
describe 'config/initializers/rolify.rb' do
|
|
36
|
+
subject { file('config/initializers/rolify.rb') }
|
|
37
|
+
it { should exist }
|
|
38
|
+
it { should contain "Rolify.configure do |config|"}
|
|
39
|
+
it { should contain "# config.use_dynamic_shortcuts" }
|
|
40
|
+
it { should contain "# config.use_mongoid" }
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
describe 'app/models/role.rb' do
|
|
44
|
+
subject { file('app/models/role.rb') }
|
|
45
|
+
it { should exist }
|
|
46
|
+
it { should contain "class Role < ActiveRecord::Base" }
|
|
47
|
+
it { should contain "has_and_belongs_to_many :users, :join_table => :users_roles" }
|
|
48
|
+
it { should contain "belongs_to :resource, :polymorphic => true" }
|
|
49
|
+
it { should contain "validates :resource_type,\n"
|
|
50
|
+
" :inclusion => { :in => Rolify.resource_types },\n"
|
|
51
|
+
" :allow_nil => true" }
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
describe 'app/models/user.rb' do
|
|
55
|
+
subject { file('app/models/user.rb') }
|
|
56
|
+
it { should contain /class User < ActiveRecord::Base\n rolify\n/ }
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
describe 'migration file' do
|
|
60
|
+
subject { migration_file('db/migrate/rolify_create_roles.rb') }
|
|
61
|
+
|
|
62
|
+
it { should be_a_migration }
|
|
63
|
+
it { should contain "create_table(:roles) do" }
|
|
64
|
+
it { should contain "create_table(:users_roles, :id => false) do" }
|
|
65
|
+
end
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
describe 'specifying User and Role class names' do
|
|
69
|
+
before(:all) { arguments %w(AdminRole AdminUser) }
|
|
70
|
+
|
|
71
|
+
before {
|
|
72
|
+
capture(:stdout) {
|
|
73
|
+
generator.create_file "app/models/admin_user.rb" do
|
|
74
|
+
"class AdminUser < ActiveRecord::Base\nend"
|
|
75
|
+
end
|
|
76
|
+
}
|
|
77
|
+
require File.join(destination_root, "app/models/admin_user.rb")
|
|
78
|
+
run_generator
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
describe 'config/initializers/rolify.rb' do
|
|
82
|
+
subject { file('config/initializers/rolify.rb') }
|
|
83
|
+
|
|
84
|
+
it { should exist }
|
|
85
|
+
it { should contain "Rolify.configure(\"AdminRole\") do |config|"}
|
|
86
|
+
it { should contain "# config.use_dynamic_shortcuts" }
|
|
87
|
+
it { should contain "# config.use_mongoid" }
|
|
88
|
+
end
|
|
89
|
+
|
|
90
|
+
describe 'app/models/admin_role.rb' do
|
|
91
|
+
subject { file('app/models/admin_role.rb') }
|
|
92
|
+
|
|
93
|
+
it { should exist }
|
|
94
|
+
it { should contain "class AdminRole < ActiveRecord::Base" }
|
|
95
|
+
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" }
|
|
97
|
+
end
|
|
98
|
+
|
|
99
|
+
describe 'app/models/admin_user.rb' do
|
|
100
|
+
subject { file('app/models/admin_user.rb') }
|
|
101
|
+
|
|
102
|
+
it { should contain /class AdminUser < ActiveRecord::Base\n rolify :role_cname => 'AdminRole'\n/ }
|
|
103
|
+
end
|
|
104
|
+
|
|
105
|
+
describe 'migration file' do
|
|
106
|
+
subject { migration_file('db/migrate/rolify_create_admin_roles.rb') }
|
|
107
|
+
|
|
108
|
+
it { should be_a_migration }
|
|
109
|
+
it { should contain "create_table(:admin_roles)" }
|
|
110
|
+
it { should contain "create_table(:admin_users_admin_roles, :id => false) do" }
|
|
111
|
+
end
|
|
112
|
+
end
|
|
113
|
+
|
|
114
|
+
describe 'specifying namespaced User and Role class names' do
|
|
115
|
+
before(:all) { arguments %w(Admin::Role Admin::User) }
|
|
116
|
+
|
|
117
|
+
before {
|
|
118
|
+
capture(:stdout) {
|
|
119
|
+
generator.create_file "app/models/admin/user.rb" do
|
|
120
|
+
<<-RUBY
|
|
121
|
+
module Admin
|
|
122
|
+
class User < ActiveRecord::Base
|
|
123
|
+
self.table_name_prefix = 'admin_'
|
|
124
|
+
end
|
|
125
|
+
end
|
|
126
|
+
RUBY
|
|
127
|
+
end
|
|
128
|
+
}
|
|
129
|
+
require File.join(destination_root, "app/models/admin/user.rb")
|
|
130
|
+
run_generator
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
describe 'config/initializers/rolify.rb' do
|
|
134
|
+
subject { file('config/initializers/rolify.rb') }
|
|
135
|
+
|
|
136
|
+
it { should exist }
|
|
137
|
+
it { should contain "Rolify.configure(\"Admin::Role\") do |config|"}
|
|
138
|
+
it { should contain "# config.use_dynamic_shortcuts" }
|
|
139
|
+
it { should contain "# config.use_mongoid" }
|
|
140
|
+
end
|
|
141
|
+
|
|
142
|
+
describe 'app/models/admin/role.rb' do
|
|
143
|
+
subject { file('app/models/admin/role.rb') }
|
|
144
|
+
|
|
145
|
+
it { should exist }
|
|
146
|
+
it { should contain "class Admin::Role < ActiveRecord::Base" }
|
|
147
|
+
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" }
|
|
149
|
+
end
|
|
150
|
+
|
|
151
|
+
describe 'app/models/admin/user.rb' do
|
|
152
|
+
subject { file('app/models/admin/user.rb') }
|
|
153
|
+
|
|
154
|
+
it { should contain /class User < ActiveRecord::Base\n rolify :role_cname => 'Admin::Role'\n/ }
|
|
155
|
+
end
|
|
156
|
+
|
|
157
|
+
describe 'migration file' do
|
|
158
|
+
subject { migration_file('db/migrate/rolify_create_admin_roles.rb') }
|
|
159
|
+
|
|
160
|
+
it { should be_a_migration }
|
|
161
|
+
it { should contain "create_table(:admin_roles)" }
|
|
162
|
+
it { should contain "create_table(:admin_users_admin_roles, :id => false) do" }
|
|
163
|
+
end
|
|
164
|
+
end
|
|
165
|
+
end
|