roles_active_record 0.2.5 → 0.3.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (34) hide show
  1. data/Gemfile +13 -5
  2. data/Rakefile +13 -14
  3. data/VERSION +1 -1
  4. data/development.sqlite3 +0 -0
  5. data/lib/roles_active_record/role.rb +3 -0
  6. data/lib/roles_active_record/strategy/multi/many_roles.rb +38 -41
  7. data/lib/roles_active_record/strategy/multi/roles_mask.rb +22 -23
  8. data/lib/roles_active_record/strategy/multi.rb +54 -0
  9. data/lib/roles_active_record/strategy/shared.rb +26 -0
  10. data/lib/roles_active_record/strategy/single/admin_flag.rb +19 -18
  11. data/lib/roles_active_record/strategy/single/one_role.rb +35 -26
  12. data/lib/roles_active_record/strategy/single/role_string.rb +24 -19
  13. data/lib/roles_active_record/strategy/single.rb +34 -0
  14. data/roles_active_record.gemspec +54 -49
  15. data/spec/fixtures/many_roles_setup.rb +1 -1
  16. data/spec/fixtures/one_role_setup.rb +1 -1
  17. data/spec/migrations/many_roles/002_create_roles.rb +0 -1
  18. data/spec/migrations/roles_mask/005_add_roles_mask_to_users.rb +1 -1
  19. data/spec/roles_active_record/strategy/api.rb +214 -0
  20. data/spec/roles_active_record/strategy/api_examples.rb +213 -0
  21. data/spec/roles_active_record/strategy/multi/many_roles_spec.rb +27 -0
  22. data/spec/roles_active_record/strategy/multi/roles_mask_spec.rb +23 -0
  23. data/spec/roles_active_record/strategy/single/admin_flag_spec.rb +27 -0
  24. data/spec/roles_active_record/strategy/single/one_role_spec.rb +19 -0
  25. data/spec/roles_active_record/strategy/single/role_string_spec.rb +23 -0
  26. data/spec/roles_active_record/strategy/user_setup.rb +20 -0
  27. data/spec/spec_helper.rb +6 -1
  28. metadata +49 -63
  29. data/roles_for_ar.gemspec +0 -165
  30. data/spec/roles_active_record/admin_flag_spec.rb +0 -71
  31. data/spec/roles_active_record/many_roles_spec.rb +0 -69
  32. data/spec/roles_active_record/one_role_spec.rb +0 -68
  33. data/spec/roles_active_record/role_string_spec.rb +0 -68
  34. data/spec/roles_active_record/roles_mask_spec.rb +0 -68
data/Gemfile CHANGED
@@ -1,13 +1,21 @@
1
1
  source :rubygems
2
2
 
3
- gem "active_record", ">= 3.0.0.rc"
4
- gem "active_support", ">= 3.0.0.rc"
5
- gem "meta_where", ">= 0.6.0"
3
+ gem "active_record", ">= 3.0.0"
4
+ gem "active_support", ">= 3.0.0"
5
+ gem "meta_where", ">= 0.9"
6
+ gem "arel", "~> 1.0"
7
+ gem "meta_where", ">= 0.9"
8
+ gem "sugar-high", "~> 0.3.0"
9
+ gem "require_all", '~> 1.2.0'
10
+ gem "roles_generic", '~> 0.2.5'
11
+
12
+ gem 'rails3_artifactor', '~> 0.3.0'
13
+ gem 'logging_assist', '~> 0.1.5'
6
14
 
7
15
  group :test do
8
- gem "rspec", ">= 2.0.0.beta.19"
16
+ gem "rspec", ">= 2.0.0"
9
17
  gem "yaml"
10
18
  gem "logger"
11
19
  gem 'database_cleaner'
12
- gem "generator_spec", '>= 0.4.5'
20
+ gem "generator_spec", '>= 0.7.0'
13
21
  end
data/Rakefile CHANGED
@@ -7,21 +7,20 @@ begin
7
7
  gem.email = "kmandrup@gmail.com"
8
8
  gem.homepage = "http://github.com/kristianmandrup/roles_for_dm"
9
9
  gem.authors = ["Kristian Mandrup"]
10
- gem.add_development_dependency "rspec", '>= 2.0.0.rc'
11
- gem.add_development_dependency 'database_cleaner', '~> 0.5.2'
12
- gem.add_development_dependency "generator-spec", '~> 0.6.5'
13
-
14
- gem.add_dependency "activerecord", "~> 3.0.0"
15
- gem.add_dependency "activesupport", "~> 3.0.0"
16
- gem.add_dependency "arel", "~> 1.0.0"
17
- gem.add_dependency "meta_where", "~> 0.9.3"
18
- gem.add_dependency "sugar-high", "~> 0.2.11"
19
- gem.add_dependency "require_all", '~> 1.2.0'
20
- gem.add_dependency "roles_generic", '~> 0.2.5'
21
- gem.add_dependency 'rails3_artifactor', '>= 0.2.6'
22
- gem.add_dependency 'logging_assist', '>= 0.1.5'
23
- gem.add_dependency 'migration_assist', '>= 0.1.5'
10
+ gem.add_development_dependency "rspec", '>= 2.0'
11
+ gem.add_development_dependency 'database_cleaner', '>= 0.5'
12
+ gem.add_development_dependency "generator-spec", '>= 0.7.0'
24
13
 
14
+ gem.add_dependency "activerecord", "~> 3.0"
15
+ gem.add_dependency "activesupport", "~> 3.0"
16
+ gem.add_dependency "arel", "~> 1.0"
17
+ gem.add_dependency "meta_where", ">= 0.9"
18
+ gem.add_dependency "sugar-high", "~> 0.3.0"
19
+ gem.add_dependency "require_all", '~> 1.2.0'
20
+ gem.add_dependency "roles_generic", '>= 0.3.0'
21
+
22
+ gem.add_dependency 'rails3_artifactor', '~> 0.3.1'
23
+ gem.add_dependency 'logging_assist', '~> 0.1.6'
25
24
  # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
26
25
  end
27
26
  Jeweler::GemcutterTasks.new
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.2.5
1
+ 0.3.1
data/development.sqlite3 CHANGED
Binary file
@@ -2,9 +2,12 @@ module Roles::Base
2
2
  def valid_roles_are(*role_list)
3
3
  strategy_class.valid_roles = role_list.to_symbols
4
4
  if role_class_name
5
+ "Create Roles: #{role_list}"
5
6
  role_list.each do |name|
6
7
  role_class_name.create(:name => name.to_s)
7
8
  end
9
+ else
10
+ "Using Inline roles"
8
11
  end
9
12
  end
10
13
  end
@@ -1,13 +1,15 @@
1
- class Role < ActiveRecord::Base
2
- scope :named, lambda{|role_names| where(:name.in => role_names.flatten)}
1
+ require 'roles_active_record/strategy/multi'
2
+
3
+ class Role < ActiveRecord::Base
4
+ scope :named, lambda{|role_names| where(:name.in => role_names.flatten)}
3
5
  has_many :users, :through => :user_roles
4
- has_many :user_roles
5
- end
6
+ has_many :user_roles
7
+ end
6
8
 
7
- class UserRole < ActiveRecord::Base
9
+ class UserRole < ActiveRecord::Base
8
10
  belongs_to :user
9
11
  belongs_to :role
10
- end
12
+ end
11
13
 
12
14
  module RoleStrategy::ActiveRecord
13
15
  module ManyRoles
@@ -17,58 +19,53 @@ module RoleStrategy::ActiveRecord
17
19
 
18
20
  def self.included base
19
21
  base.extend Roles::Generic::Role::ClassMethods
20
- base.extend ClassMethods
22
+ base.extend ClassMethods
21
23
  base.has_many :many_roles, :through => :user_roles, :source => :role
22
- base.has_many :user_roles
24
+ base.has_many :user_roles
23
25
  end
24
26
 
25
- module ClassMethods
26
- def in_role(role_name)
27
- in_roles(role_name)
27
+ module ClassMethods
28
+ def in_role(role_name)
29
+ in_any_role(role_name)
28
30
  end
29
31
 
30
- def in_roles(*role_names)
31
- joins(:many_roles) & Role.named(role_names)
32
+ def in_any_role(*role_names)
33
+ joins(:many_roles) & Role.named(role_names.to_strings)
32
34
  end
33
35
  end
34
-
36
+
35
37
  module Implementation
36
- def role_attribute
37
- strategy_class.roles_attribute_name
38
- end
39
-
40
- # assign roles
41
- def roles=(*_roles)
42
- _roles = get_roles(_roles)
43
- return nil if _roles.none?
38
+ include Roles::ActiveRecord::Strategy::Multi
44
39
 
45
- role_relations = role_class.find_roles(_roles)
46
- self.send("#{role_attribute}=", role_relations)
47
- save
40
+ # assign multiple roles
41
+ def roles=(*role_names)
42
+ puts "Assign roles #{role_names}"
43
+ role_names = role_names.flat_uniq
44
+ role_names = extract_roles(role_names)
45
+ return nil if role_names.empty?
46
+ puts "role names #{role_names}"
47
+ valids = role_class.find_roles(role_names).to_a
48
+ puts "valids: #{valids}"
49
+ vrs = select_valid_roles role_names
50
+ puts "Set valid roles #{vrs}"
51
+ set_roles(vrs)
48
52
  end
49
53
 
50
- def add_roles(*_roles)
51
- _roles = get_roles(_roles)
52
- return nil if _roles.none?
53
54
 
54
- role_relations = role_class.find_roles(_roles)
55
- puts "role_relations: #{role_relations.inspect}"
56
- self.send(role_attribute) << role_relations
57
- save
55
+ def new_roles *role_names
56
+ role_class.find_roles(extract_roles role_names)
58
57
  end
59
58
 
60
- # query assigned roles
61
- def roles
62
- self.send(role_attribute)
59
+ def present_roles roles_names
60
+ roles_names.to_a.map{|role| role.name.to_s.to_sym}
63
61
  end
64
62
 
65
- def roles_list
66
- [roles].flatten.map{|r| r.name }.compact.to_symbols
63
+ def set_empty_roles
64
+ self.send("#{role_attribute}=", [])
67
65
  end
68
- end
69
-
66
+ end
67
+
70
68
  extend Roles::Generic::User::Configuration
71
- configure :type => :role_class
69
+ configure :type => :role_class
72
70
  end
73
71
  end
74
-
@@ -1,3 +1,5 @@
1
+ require 'roles_active_record/strategy/multi'
2
+
1
3
  module RoleStrategy::ActiveRecord
2
4
  module RolesMask
3
5
  def self.default_role_attribute
@@ -20,7 +22,7 @@ module RoleStrategy::ActiveRecord
20
22
  end
21
23
  end
22
24
 
23
- def in_roles(*roles)
25
+ def in_any_role(*roles)
24
26
  all.select do |user|
25
27
  roles.flatten.any? do |role|
26
28
  mask = calc_index(role.to_s)
@@ -34,7 +36,9 @@ module RoleStrategy::ActiveRecord
34
36
  end
35
37
  end
36
38
 
37
- module Implementation
39
+ module Implementation
40
+ include Roles::ActiveRecord::Strategy::Multi
41
+
38
42
  class Roles < ::Set # :nodoc:
39
43
  attr_reader :model_instance
40
44
 
@@ -49,32 +53,27 @@ module RoleStrategy::ActiveRecord
49
53
  end
50
54
  end
51
55
 
52
- def role_attribute
53
- strategy_class.roles_attribute_name
54
- end
56
+ protected
57
+
58
+ def calc_index(r)
59
+ 2**strategy_class.valid_roles.index(r)
60
+ end
55
61
 
56
- # assign roles
57
- def roles=(*roles)
58
- the_roles = (roles.flatten.map { |r| r.to_sym } & strategy_class.valid_roles)
59
- value = the_roles.map{ |r| calc_index(r) }.inject{ |sum, bitvalue| sum + bitvalue }
60
- puts "value: #{value}"
61
- self.send "#{role_attribute}=", value
62
+ def get_roles
63
+ strategy_class::Roles.new(self, strategy_class.valid_roles.reject { |r| ((get_role || 0) & calc_index(r)).zero? })
62
64
  end
63
- alias_method :role=, :roles=
64
65
 
65
- # query assigned roles
66
- def roles
67
- strategy_class::Roles.new(self, strategy_class.valid_roles.reject { |r| ((self.send(role_attribute) || 0) & calc_index(r)).zero? })
66
+ def new_roles *role_names
67
+ role_names = role_names.flatten.map{ |r| r.to_sym } & strategy_class.valid_roles
68
+ role_names.map { |r| calc_index(r) }.inject { |sum, bitvalue| sum + bitvalue }
68
69
  end
69
70
 
70
- def roles_list
71
- roles.to_a
72
- end
73
-
74
- protected
75
-
76
- def calc_index(r)
77
- 2**strategy_class.valid_roles.index(r)
71
+ def set_empty_roles
72
+ self.send("#{role_attribute}=", 0)
73
+ end
74
+
75
+ def present_roles *role_names
76
+ role_names.to_a.to_symbols
78
77
  end
79
78
  end
80
79
 
@@ -0,0 +1,54 @@
1
+ require 'roles_active_record/strategy/shared'
2
+
3
+ module Roles::ActiveRecord
4
+ module Strategy
5
+ module Multi
6
+ # assign multiple roles
7
+ def roles=(*role_names)
8
+ extracted_roles = extract_roles(role_names)
9
+ return nil if extracted_roles.empty?
10
+ set_roles(select_valid_roles extracted_roles)
11
+ end
12
+
13
+ def add_roles *roles
14
+ new_roles = select_valid_roles(roles)
15
+ if !new_roles.empty?
16
+ self.roles = self.roles + new_roles
17
+ end
18
+ end
19
+
20
+ # should remove the current single role (set = nil)
21
+ # only if it is contained in the list of roles to be removed
22
+ def remove_roles *role_names
23
+ role_names = role_names.flat_uniq
24
+ set_empty_roles and return if roles_diff(role_names).empty?
25
+ roles_to_remove = select_valid_roles(role_names)
26
+ set_roles roles_diff(role_names)
27
+ true
28
+ end
29
+
30
+ # query assigned roles
31
+ def roles
32
+ get_roles.map do |role|
33
+ role.respond_to?(:sym) ? role.to_sym : role
34
+ end
35
+ end
36
+
37
+ def roles_list
38
+ my_roles = [roles].flat_uniq
39
+ return [] if my_roles.empty?
40
+ has_role_class? ? my_roles.map{|r| r.name.to_sym } : my_roles
41
+ end
42
+
43
+ protected
44
+
45
+ def set_roles *roles
46
+ self.send("#{role_attribute}=", new_roles(roles))
47
+ end
48
+
49
+ def get_roles
50
+ self.send(role_attribute)
51
+ end
52
+ end
53
+ end
54
+ end
@@ -0,0 +1,26 @@
1
+ module Roles::ActiveRecord
2
+ module Strategy
3
+ module Shared
4
+ def set_role role
5
+ vr = new_role(role)
6
+ # self.send("#{role_attribute}=", vr)
7
+ update_attributes(role_attribute => vr)
8
+ end
9
+ alias_method :set_roles, :set_role
10
+
11
+ def get_role
12
+ r = self.send(role_attribute)
13
+ # respond_to?(:present_role) ? present_role(r) : r
14
+ end
15
+
16
+ def get_roles
17
+ r = self.send(role_attribute)
18
+ end
19
+
20
+ def select_valid_roles *roles
21
+ roles.flat_uniq.select{|role| valid_role? role }
22
+ has_role_class? ? role_class.find_roles(roles).to_a.flat_uniq : roles.flat_uniq
23
+ end
24
+ end
25
+ end
26
+ end
@@ -1,3 +1,5 @@
1
+ require 'roles_active_record/strategy/single'
2
+
1
3
  module RoleStrategy::ActiveRecord
2
4
  module AdminFlag
3
5
  def self.default_role_attribute
@@ -24,26 +26,25 @@ module RoleStrategy::ActiveRecord
24
26
  end
25
27
 
26
28
  module Implementation
27
- def role_attribute
28
- strategy_class.roles_attribute_name
29
- end
30
-
31
- # assign roles
32
- def roles=(*new_roles)
33
- first_role = new_roles.flatten.first
34
- if valid_role?(first_role)
35
- self.send("#{role_attribute}=", new_roles.flatten.first.admin?)
36
- else
37
- raise ArgumentError, "The role #{first_role} is not a valid role"
38
- end
39
- end
29
+ include Roles::ActiveRecord::Strategy::Single
40
30
 
41
- # query assigned roles
42
- def roles
43
- role = self.send(role_attribute) ? strategy_class.admin_role_key : strategy_class.default_role_key
44
- [role]
31
+ def new_role role
32
+ role = role.kind_of?(Array) ? role.flatten.first : role
33
+ role.admin?
45
34
  end
46
- alias_method :roles_list, :roles
35
+
36
+ def get_role
37
+ self.send(role_attribute) ? strategy_class.admin_role_key : strategy_class.default_role_key
38
+ end
39
+
40
+ def present_roles *roles
41
+ roles = roles.flat_uniq
42
+ roles.map{|role| role ? :admin : :guest}
43
+ end
44
+
45
+ def set_empty_role
46
+ self.send("#{role_attribute}=", false)
47
+ end
47
48
 
48
49
  end # Implementation
49
50
 
@@ -1,7 +1,9 @@
1
- class Role < ActiveRecord::Base
2
- scope :named, lambda{|role_names| where(:name.in => role_names.flatten)}
3
- belongs_to :user
4
- end
1
+ require 'roles_active_record/strategy/single'
2
+
3
+ class Role < ActiveRecord::Base
4
+ scope :named, lambda{|role_names| where(:name.in => role_names.flatten)}
5
+ has_many :users
6
+ end
5
7
 
6
8
  module RoleStrategy::ActiveRecord
7
9
  module OneRole
@@ -11,40 +13,47 @@ module RoleStrategy::ActiveRecord
11
13
 
12
14
  def self.included base
13
15
  base.extend Roles::Generic::Role::ClassMethods
14
- base.extend ClassMethods
15
- base.has_one :one_role, :foreign_key => :role_id, :class_name => 'Role'
16
+ base.extend ClassMethods
17
+ base.belongs_to :one_role, :foreign_key => :role_id, :class_name => 'Role'
16
18
  end
17
19
 
18
- module ClassMethods
19
- def in_role(role_name)
20
- in_roles(role_name)
20
+ module ClassMethods
21
+ def in_role(role_name)
22
+ in_any_role(role_name)
21
23
  end
22
24
 
23
- def in_roles(*role_names)
25
+ def in_any_role(*role_names)
24
26
  joins(:one_role) & Role.named(role_names)
25
27
  end
26
28
  end
27
29
 
28
- module Implementation
29
- # assign roles
30
- def roles=(*_roles)
31
- _roles = get_roles(_roles)
32
- return nil if _roles.none?
30
+ module Implementation
31
+ include Roles::ActiveRecord::Strategy::Single
32
+
33
+ def new_role role
34
+ role_class.find_role(extract_role role)
35
+ end
36
+
37
+ def new_roles *roles
38
+ new_role roles.flatten.first
39
+ end
40
+
41
+ def remove_roles *role_names
42
+ roles = role_names.flat_uniq
43
+ set_empty_role if roles_diff(roles).empty?
44
+ true
45
+ end
33
46
 
34
- role_relation = role_class.find_role(_roles.first)
35
- self.send("#{role_attribute}=", role_relation)
47
+ def present_roles *roles
48
+ roles.map{|role| extract_role role}
36
49
  end
37
- alias_method :role=, :roles=
38
-
39
- # query assigned roles
40
- def roles
41
- [self.send(role_attribute).name.to_sym]
50
+
51
+ def set_empty_role
52
+ self.send("#{role_attribute}=", nil)
42
53
  end
43
- alias_method :roles_list, :roles
44
-
45
54
  end
46
55
 
47
56
  extend Roles::Generic::User::Configuration
48
- configure :num => :single, :type => :role_class
49
- end
57
+ configure :num => :single, :type => :role_class
58
+ end
50
59
  end
@@ -1,3 +1,5 @@
1
+ require 'roles_active_record/strategy/single'
2
+
1
3
  module RoleStrategy::ActiveRecord
2
4
  module RoleString
3
5
  def self.default_role_attribute
@@ -15,32 +17,35 @@ module RoleStrategy::ActiveRecord
15
17
 
16
18
  def in_role(role_name)
17
19
  where(role_attribute => role_name)
18
- end
20
+ end
21
+
22
+ def in_any_role(*role_names)
23
+ where(:role_attribute.in => role_names)
24
+ end
19
25
  end
20
-
21
26
 
22
27
  module Implementation
23
- def role_attribute
24
- strategy_class.roles_attribute_name
25
- end
26
-
27
- # assign roles
28
- def roles=(*roles)
29
- self.role = roles.select_labels.first.to_s
30
- end
28
+ include Roles::ActiveRecord::Strategy::Single
29
+
30
+ def new_role role
31
+ role = role.kind_of?(Array) ? role.first : role
32
+ role.to_s
33
+ end
34
+
35
+ def new_roles *roles
36
+ new_role roles.flatten.first
37
+ end
31
38
 
32
- def role= role_name
33
- if role_name.kind_of_label? && valid_role?(role_name)
34
- self.send("#{role_attribute}=", role_name.to_s)
35
- end
39
+ def present_role role
40
+ return role if role.kind_of? Array
41
+ role.split(',').map(&:to_sym)
36
42
  end
37
43
 
38
- # query assigned roles
39
- def roles
40
- role = self.send(role_attribute)
41
- [role.to_sym]
44
+ def set_empty_role
45
+ self.send("#{role_attribute}=", "")
42
46
  end
43
- alias_method :roles_list, :roles
47
+
48
+ alias_method :present_roles, :present_role
44
49
  end
45
50
 
46
51
  extend Roles::Generic::User::Configuration
@@ -0,0 +1,34 @@
1
+ require 'roles_active_record/strategy/shared'
2
+
3
+ class Symbol
4
+ def admin?
5
+ self == :admin
6
+ end
7
+ end
8
+
9
+ module Roles::ActiveRecord
10
+ module Strategy
11
+ module Single
12
+ include Shared
13
+ # assigns first valid role from list of roles
14
+ def add_roles *role_names
15
+ new_roles = select_valid_roles(role_names)
16
+ new_role = new_roles.first if !new_roles.empty?
17
+ set_role new_role
18
+ end
19
+
20
+ # should remove the current single role (set = nil)
21
+ # only if it is contained in the list of roles to be removed
22
+ def remove_roles *role_names
23
+ roles = role_names.flat_uniq
24
+ set_empty_role if roles_diff(roles).empty?
25
+ true
26
+ end
27
+
28
+ def roles_list
29
+ raise 'the method #roles should be present' if !respond_to? :roles
30
+ present_roles(roles)
31
+ end
32
+ end
33
+ end
34
+ end