roles_for_mongoid 0.1.1 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (41) hide show
  1. data/.rspec +1 -0
  2. data/README.markdown +14 -109
  3. data/Rakefile +5 -1
  4. data/VERSION +1 -1
  5. data/lib/generators/mongoid/roles/roles_generator.rb +48 -25
  6. data/lib/roles_mongoid.rb +9 -0
  7. data/lib/roles_mongoid/base.rb +35 -0
  8. data/lib/roles_mongoid/namespaces.rb +12 -0
  9. data/lib/roles_mongoid/role.rb +28 -0
  10. data/lib/roles_mongoid/strategy.rb +15 -0
  11. data/lib/roles_mongoid/strategy/multi/many_roles.rb +65 -0
  12. data/lib/roles_mongoid/strategy/multi/role_strings.rb +63 -0
  13. data/lib/roles_mongoid/strategy/multi/roles_mask.rb +81 -0
  14. data/lib/roles_mongoid/strategy/single/admin_flag.rb +53 -0
  15. data/lib/roles_mongoid/strategy/single/one_role.rb +56 -0
  16. data/lib/roles_mongoid/strategy/single/role_string.rb +51 -0
  17. data/roles_for_mongoid.gemspec +43 -24
  18. data/sandbox/roles_assign.rb +54 -0
  19. data/sandbox/single_role.rb +68 -0
  20. data/spec/roles_mongoid/admin_flag_spec.rb +74 -0
  21. data/spec/roles_mongoid/many_roles_spec.rb +74 -0
  22. data/spec/roles_mongoid/one_role_spec.rb +74 -0
  23. data/spec/roles_mongoid/role_string_spec.rb +73 -0
  24. data/spec/roles_mongoid/role_strings_spec.rb +73 -0
  25. data/spec/roles_mongoid/roles_mask_spec.rb +73 -0
  26. data/spec/spec_helper.rb +17 -7
  27. metadata +94 -25
  28. data/lib/roles_for_mongoid.rb +0 -1
  29. data/lib/roles_for_mongoid/role.rb +0 -4
  30. data/lib/roles_for_mongoid/strategies/admin_flag.rb +0 -17
  31. data/lib/roles_for_mongoid/strategies/inline_role.rb +0 -6
  32. data/lib/roles_for_mongoid/strategies/inline_roles.rb +0 -13
  33. data/lib/roles_for_mongoid/strategies/role_relations.rb +0 -34
  34. data/lib/roles_for_mongoid/strategies/roles_mask.rb +0 -52
  35. data/lib/roles_for_mongoid/user.rb +0 -12
  36. data/spec/roles_for_mongoid/user_admin_field_spec.rb +0 -24
  37. data/spec/roles_for_mongoid/user_inline_role_spec.rb +0 -23
  38. data/spec/roles_for_mongoid/user_inline_roles_spec.rb +0 -27
  39. data/spec/roles_for_mongoid/user_role_relations_spec.rb +0 -37
  40. data/spec/roles_for_mongoid/user_roles_mask_spec.rb +0 -29
  41. data/spec/spec.opts +0 -1
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --format nested --color
data/README.markdown CHANGED
@@ -1,125 +1,30 @@
1
1
  # Roles for Mongoid
2
2
 
3
- # Install
4
-
5
- <code>gem install roles_for_mongoid</code>
6
-
7
- # Usage
8
-
9
- The library comes with the following role models built-in:
10
-
11
- * Single inline role (string)
12
- * Multiple inline role (strings)
13
- * Multiple role relations (many relationship using array of object ids)
14
-
15
- Note: The following examples use RSpec to demonstrate usage scenarios.
16
-
17
- ## Example : Admin flag
18
-
19
- <pre>
20
- require 'roles_for_mongoid'
21
-
22
- User.role_strategy :admin_flag
23
-
24
- before :each do
25
- guest_user = User.new :name => 'Kristian', :role => 'guest'
26
- admin_user = User.new :name => 'Admin user', :role => 'admin'
27
- admin_user.save
28
- guest_user.save
29
- end
30
-
31
- it "should return first user maching role" do
32
- User.in_role('guest').first.name.should == 'Kristian'
33
- User.in_role('admin').first.name.should == 'Admin user'
34
- end
35
- </pre>
36
-
37
-
38
- ## Example : Single inline role
39
-
40
- <pre>
41
- User.role_strategy :inline_role
3
+ A Mongoid implementation of [roles generic](http://github.com/kristianmandrup/roles_generic)
42
4
 
43
- before :each do
44
- guest_user = User.new :name => 'Kristian', :role => 'guest'
45
- admin_user = User.new :name => 'Admin user', :role => 'admin'
46
- admin_user.save
47
- guest_user.save
48
- end
49
-
50
- it "should return first user maching role" do
51
- User.in_role('guest').first.name.should == 'Kristian'
52
- User.in_role('admin').first.name.should == 'Admin user'
53
- end
54
- </pre>
55
-
56
- ## Example : Multiple inline roles
57
-
58
- <pre>
59
- User.role_strategy :inline_roles
60
-
61
- before :each do
62
- guest_user = User.new(:name => 'Kristian', :roles => ['guest']).save
63
- admin_user = User.new(:name => 'Admin user', :roles => ['admin']).save
64
- author_user = User.new(:name => 'Author', :roles => ['author']).save
65
- end
66
-
67
- it "should return first user matching role" do
68
- User.in_role('admin').first.name.should == 'Admin user'
69
- end
70
-
71
- it "should return all users matching any of the roles" do
72
- User.in_roles(['guest', 'admin']).all.size.should == 2
73
- User.in_roles('guest', 'admin').all.size.should == 2
74
- end
75
- </pre>
76
-
77
- ## Example : Multiple role relations
78
-
79
- <pre>
80
- User.role_strategy :role_relations
81
-
82
- before :each do
83
- guest_user = User.new(:name => 'Kristian')
84
- admin_user = User.new(:name => 'Admin user')
85
- author_user = User.new(:name => 'Author')
5
+ # Install
86
6
 
87
- guest_user.roles << Role.new(:name => 'guest')
88
- guest_user.save
7
+ <code>gem install roles_mongoid</code>
89
8
 
90
- admin_user.roles << Role.new(:name => 'admin')
91
- admin_user.save
92
-
93
- author_user.roles << Role.new(:name => 'author')
94
- author_user.save
95
- end
9
+ ## Status: Sept 6, 2010
96
10
 
97
- it "should return first user matching role" do
98
- User.in_role('guest').first.name.should == 'Kristian'
99
- User.in_role('admin').first.name.should == 'Admin user'
100
- end
11
+ Implements the [roles generic](http://github.com/kristianmandrup/roles_generic) Roles API
12
+ It also implements the following Role strategies:
101
13
 
102
- it "should return all users matching any of the roles" do
103
- User.in_roles(['guest', 'admin']).all.size.should == 2
104
- User.in_roles('guest', 'admin').all.size.should == 2
105
- end
106
- </pre>
14
+ * admin_flag
15
+ * many_roles
16
+ * one_role
17
+ * roles_mask
18
+ * role_string
19
+ * role_strings
107
20
 
108
21
  ## Rails generator
109
22
 
110
- The library comes with a Rails 3 generator that lets you populate a user model with a given role strategy
111
- The following role strategies are included by default. Add your own by adding extra files inside the strategy folder, one file for each role strategy is recommended.
112
-
113
- * admin_flag
114
- * inline_role
115
- * inline_roles
116
- * role_relations
117
- * role_mask
23
+ Might work but needs testing ;)
118
24
 
119
25
  Example:
120
26
 
121
- <code>$ rails g mongoid:roles user admin_flag</code>
122
-
27
+ <code>$ rails g mongoid:roles User --strategy admin_flag --roles admin guest</code>
123
28
 
124
29
  ## Note on Patches/Pull Requests
125
30
 
data/Rakefile CHANGED
@@ -7,7 +7,11 @@ begin
7
7
  gem.email = "kmandrup@gmail.com"
8
8
  gem.homepage = "http://github.com/kristianmandrup/roles_for_mm"
9
9
  gem.authors = ["Kristian Mandrup"]
10
- gem.add_development_dependency "rspec", ">= 2.0.0.beta.17"
10
+ gem.add_development_dependency "rspec", ">= 2.0.0.beta.19"
11
+ gem.add_dependency "mongoid", '~> 2.0.0.beta.17'
12
+ gem.add_dependency "require_all", '~> 1.1.0'
13
+ gem.add_dependency "sugar-high", '~> 0.2.3'
14
+ gem.add_dependency "roles_generic", '~> 0.2.2'
11
15
  # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
12
16
  end
13
17
  Jeweler::GemcutterTasks.new
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.1
1
+ 0.2.0
@@ -1,45 +1,68 @@
1
- module Mongoid
1
+ require 'rails3_artifactor'
2
+
3
+ module RolesModel
2
4
  module Generators
3
5
  class RolesGenerator < Rails::Generators::NamedBase
4
- desc "Generate roles model for User"
6
+ include Rails3::Assist::Artifact::Model
7
+
8
+ desc "Add role strategy to a model"
5
9
 
6
- argument :role_strategy, :type => :string, :aliases => "-r", :default => 'inline_role', :desc => "Create roles model for user"
10
+ class_option :strategy, :type => :string, :aliases => "-s", :default => 'role_string',
11
+ :desc => "Role strategy to use (admin_flag, role_string, roles_string, role_strings, one_role, many_roles, roles_mask)"
12
+
13
+
14
+ class_option :roles, :type => :array, :aliases => "-r", :default => [], :desc => "Valid roles"
7
15
 
8
- hook_for :orm
16
+ # hook_for :orm
9
17
 
10
- def apply_role_strategy
11
- insert_into_model(model_name, role_strategy_statement)
18
+ # def self.source_root
19
+ # @source_root ||= File.expand_path("../../templates", __FILE__)
20
+ # end
21
+
22
+ def apply_role_strategy
23
+ self.class.use_orm orm if orm
24
+ insert_into_model name do
25
+ insertion_text
26
+ end
12
27
  end
13
28
 
14
29
  protected
15
30
 
16
- def model_name
17
- name
31
+ def orm
32
+ :mongoid
33
+ end
34
+
35
+ def default_roles
36
+ [:admin, :guest]
18
37
  end
19
38
 
20
- def match_expr
21
- /include Mongoid::Document/
39
+ def roles_to_add
40
+ default_roles.concat(options[:roles]).compact.uniq
41
+ end
42
+
43
+ def roles
44
+ @roles ||= roles_to_add.map{|r| ":#{r}" }
22
45
  end
23
46
 
24
47
  def role_strategy_statement
25
- "role_strategy #{role_strategy}"
48
+ "strategy :#{strategy}\n"
26
49
  end
27
50
 
28
- def role_strategy
29
- options[:role_strategy]
51
+ def roles_statement
52
+ roles ? "valid_roles #{roles.join(',')}" : ''
53
+ end
54
+
55
+ def insertion_text
56
+ %Q{
57
+ include Roles::#{orm.to_s.camelize}
58
+ #{role_strategy_statement}
59
+ #{roles_statement}
60
+ }
61
+ end
62
+
63
+ def strategy
64
+ options[:strategy]
30
65
  end
31
-
32
- def model_file(name)
33
- File.join(Rails.root, "app/models/#{name.to_s}.rb")
34
- end
35
-
36
- def insert_into_model(m_name, insert_text)
37
- file = File.new(model_file(m_name))
38
- return if (file.read =~ /#{insert_text}/)
39
- gsub_file model_file(m_name), match_expr do |match|
40
- match << insert_text
41
- end
42
- end
43
66
  end
44
67
  end
45
68
  end
@@ -0,0 +1,9 @@
1
+ require 'roles_generic'
2
+ require 'require_all'
3
+ require 'set'
4
+ require 'sugar-high/array'
5
+ require 'mongoid'
6
+ require 'bson'
7
+ require 'roles_mongoid/namespaces'
8
+ require 'roles_mongoid/base'
9
+ require 'roles_mongoid/strategy'
@@ -0,0 +1,35 @@
1
+ module Roles::Base
2
+ def valid_roles_are(*role_list)
3
+ strategy_class.valid_roles = role_list.to_symbols
4
+ end
5
+ end
6
+
7
+ module Roles
8
+ module Mongoid
9
+ def self.included(base)
10
+ base.extend Roles::Base
11
+ base.extend ClassMethods
12
+ base.orm_name = :mongoid
13
+ end
14
+
15
+ module ClassMethods
16
+
17
+ MAP = {
18
+ :admin_flag => "field :admin_flag, :type => Boolean",
19
+ :many_roles => "references_many :many_roles, :stored_as => :array, :class_name => 'Role', :default => []",
20
+ :one_role => "references_one :one_role, :class_name => 'Role'",
21
+ :roles_mask => "field :roles_mask, :type => Integer, :default => 1",
22
+ :role_string => "field :role_string, :type => String",
23
+ :role_strings => "field :role_strings, :type => Array",
24
+ :roles_string => "field :roles_string, :type => String"
25
+ }
26
+
27
+ def strategy name, options=nil
28
+ if options == :default && MAP[name]
29
+ instance_eval MAP[name]
30
+ end
31
+ role_strategy name, options
32
+ end
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,12 @@
1
+ require 'sugar-high/module'
2
+
3
+ module Roles
4
+ modules :mongoid do
5
+ nested_modules :user, :role
6
+ end
7
+ modules :base, :strategy
8
+ end
9
+
10
+ module RoleStrategy
11
+ modules :mongoid
12
+ end
@@ -0,0 +1,28 @@
1
+ module Roles::Base
2
+ def valid_roles_are(*role_list)
3
+ strategy_class.valid_roles = role_list.to_symbols
4
+ if role_class_name
5
+ role_list.each do |name|
6
+ role_class_name.create(:name => name.to_s)
7
+ end
8
+ end
9
+ end
10
+ end
11
+
12
+ class Role
13
+ include Mongoid::Document
14
+ field :name, :type => String
15
+ referenced_in :user
16
+
17
+ class << self
18
+ def find_roles(*role_names)
19
+ where(:name.in => role_names.flatten).to_a
20
+ end
21
+
22
+ def find_role role_name
23
+ raise ArgumentError, "#find_role takes a single role name as argument, not: #{role_name.inspect}" if !role_name.kind_of_label?
24
+ res = find_roles(role_name)
25
+ res ? res.first : res
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,15 @@
1
+ require 'sugar-high/file'
2
+ require 'sugar-high/array'
3
+
4
+ module Roles::Strategy
5
+ class << self
6
+ def role_dir
7
+ File.dirname(__FILE__)
8
+ end
9
+
10
+ def gem_name
11
+ :roles_mongoid
12
+ end
13
+ end
14
+ end
15
+
@@ -0,0 +1,65 @@
1
+ module RoleStrategy::Mongoid
2
+ module ManyRoles
3
+ def self.default_role_attribute
4
+ :many_roles
5
+ end
6
+
7
+ def self.included base
8
+ base.extend Roles::Generic::Role::ClassMethods
9
+ base.extend ClassMethods
10
+ end
11
+
12
+ module ClassMethods
13
+ def role_attribute
14
+ "#{strategy_class.roles_attribute_name.to_s.singularize}_ids".to_sym
15
+ end
16
+
17
+ def in_role(role_name)
18
+ in_roles(role_name)
19
+ end
20
+
21
+ def in_roles(*role_names)
22
+ begin
23
+ role_ids = Role.where(:name.in => role_names.to_strings).to_a.map(&:_id)
24
+ criteria.in(role_attribute => role_ids).to_a
25
+ rescue
26
+ return []
27
+ end
28
+ end
29
+ end
30
+
31
+ module Implementation
32
+ def role_attribute
33
+ strategy_class.roles_attribute_name
34
+ end
35
+
36
+ # assign roles
37
+ def roles=(*roles)
38
+ raise "Role class #{role_class} does not have a #find_role(role) method" if !role_class.respond_to? :find_role
39
+ role_relations = role_class.find_roles(*roles)
40
+ self.send("#{role_attribute}=", role_relations)
41
+ save
42
+ end
43
+
44
+ def add_roles(*roles)
45
+ raise "Role class #{role_class} does not have a #find_role(role) method" if !role_class.respond_to? :find_role
46
+ role_relations = role_class.find_roles(*roles)
47
+ self.many_roles << role_relations
48
+ save
49
+ end
50
+
51
+ # query assigned roles
52
+ def roles
53
+ self.send(role_attribute)
54
+ end
55
+
56
+ def roles_list
57
+ [roles].flatten.map{|r| r.name }.compact.to_symbols
58
+ end
59
+ end
60
+
61
+ extend Roles::Generic::User::Configuration
62
+ configure :type => :role_class
63
+ end
64
+ end
65
+
@@ -0,0 +1,63 @@
1
+ module RoleStrategy::Mongoid
2
+ module RoleStrings
3
+ def self.default_role_attribute
4
+ :role_strings
5
+ end
6
+
7
+ def self.included base
8
+ base.extend ClassMethods
9
+ end
10
+
11
+ module ClassMethods
12
+ def role_attribute
13
+ strategy_class.roles_attribute_name.to_sym
14
+ end
15
+
16
+ def in_role(role_name)
17
+ in_roles(role_name)
18
+ end
19
+
20
+ def in_roles(*role_names)
21
+ begin
22
+ where(role_attribute.in => role_names).to_a
23
+ rescue
24
+ return []
25
+ end
26
+ end
27
+ end
28
+
29
+ module Implementation
30
+ def role_attribute
31
+ strategy_class.roles_attribute_name
32
+ end
33
+
34
+ # assign roles
35
+ def roles=(*new_roles)
36
+ new_roles = new_roles.flatten.map{|r| r.to_s if valid_role?(r)}.compact
37
+ if new_roles && new_roles.not.empty?
38
+ self.send("#{role_attribute}=", new_roles.compact.uniq)
39
+ end
40
+ end
41
+ alias_method :role=, :roles=
42
+
43
+ def add_roles(*roles_to_add)
44
+ roles_to_add = roles_to_add.flatten.map{|r| r.to_s if valid_role?(r)}.compact
45
+ if new_roles && new_roles.not.empty?
46
+ self.send(role_attribute) << new_roles.compact.uniq
47
+ end
48
+ end
49
+
50
+ # query assigned roles
51
+ def roles
52
+ self.send(role_attribute).map{|r| r.to_sym}
53
+ end
54
+
55
+ def roles_list
56
+ [roles].flatten
57
+ end
58
+ end
59
+
60
+ extend Roles::Generic::User::Configuration
61
+ configure
62
+ end
63
+ end