mongoid_roles_zhd 0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (51) hide show
  1. data/.bundle/config +2 -0
  2. data/.document +5 -0
  3. data/.rspec +1 -0
  4. data/Gemfile +17 -0
  5. data/LICENSE +20 -0
  6. data/README.textile +99 -0
  7. data/Rakefile +26 -0
  8. data/VERSION +1 -0
  9. data/lib/generators/mongoid/roles/core_ext.rb +11 -0
  10. data/lib/generators/mongoid/roles/roles_generator.rb +173 -0
  11. data/lib/generators/mongoid/roles/templates/role.rb +8 -0
  12. data/lib/mongoid_roles.rb +10 -0
  13. data/lib/mongoid_roles/base.rb +91 -0
  14. data/lib/mongoid_roles/base_role.rb +27 -0
  15. data/lib/mongoid_roles/embedded_role.rb +24 -0
  16. data/lib/mongoid_roles/namespaces.rb +12 -0
  17. data/lib/mongoid_roles/role.rb +10 -0
  18. data/lib/mongoid_roles/strategy.rb +15 -0
  19. data/lib/mongoid_roles/strategy/multi.rb +59 -0
  20. data/lib/mongoid_roles/strategy/multi/embed_many_roles.rb +124 -0
  21. data/lib/mongoid_roles/strategy/multi/many_roles.rb +83 -0
  22. data/lib/mongoid_roles/strategy/multi/role_strings.rb +54 -0
  23. data/lib/mongoid_roles/strategy/multi/roles_mask.rb +130 -0
  24. data/lib/mongoid_roles/strategy/shared.rb +29 -0
  25. data/lib/mongoid_roles/strategy/single.rb +34 -0
  26. data/lib/mongoid_roles/strategy/single/admin_flag.rb +55 -0
  27. data/lib/mongoid_roles/strategy/single/embed_one_role.rb +83 -0
  28. data/lib/mongoid_roles/strategy/single/one_role.rb +66 -0
  29. data/lib/mongoid_roles/strategy/single/role_string.rb +61 -0
  30. data/mongoid_roles.gemspec +125 -0
  31. data/sandbox/roles_assign.rb +54 -0
  32. data/sandbox/single_role.rb +82 -0
  33. data/sandbox/test.rb +47 -0
  34. data/sandbox/test_query.rb +49 -0
  35. data/spec/generator_spec_helper.rb +12 -0
  36. data/spec/mongoid_roles/generators/roles_generator_spec.rb +67 -0
  37. data/spec/mongoid_roles/strategy/api_examples.rb +200 -0
  38. data/spec/mongoid_roles/strategy/multi/embed_many_roles_spec.rb +15 -0
  39. data/spec/mongoid_roles/strategy/multi/many_roles_old_spec.rb +22 -0
  40. data/spec/mongoid_roles/strategy/multi/many_roles_spec.rb +19 -0
  41. data/spec/mongoid_roles/strategy/multi/role_strings_spec.rb +16 -0
  42. data/spec/mongoid_roles/strategy/multi/roles_mask_spec.rb +20 -0
  43. data/spec/mongoid_roles/strategy/single/admin_flag_spec.rb +18 -0
  44. data/spec/mongoid_roles/strategy/single/embed_one_role_spec.rb +18 -0
  45. data/spec/mongoid_roles/strategy/single/one_role_spec.rb +18 -0
  46. data/spec/mongoid_roles/strategy/single/role_string_spec.rb +18 -0
  47. data/spec/mongoid_roles/strategy/user_setup.rb +13 -0
  48. data/spec/spec_helper.rb +15 -0
  49. data/tmp/rails/app/models/user.rb +8 -0
  50. data/tmp/rails/config/routes.rb +2 -0
  51. metadata +256 -0
data/.bundle/config ADDED
@@ -0,0 +1,2 @@
1
+ --- {}
2
+
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --format nested --color
data/Gemfile ADDED
@@ -0,0 +1,17 @@
1
+ source :rubygems
2
+ source 'http://gems.github.com/'
3
+
4
+ # group :default do
5
+ # gem "mongoid", '~> 2.0.0.rc.5'
6
+ # gem 'bson', '>= 1.1.5'
7
+ # gem "activesupport", '~> 3.0.3'
8
+ # gem "require_all", '~> 1.2.0'
9
+ # gem "sugar-high", '>= 0.3.5'
10
+ # gem "roles_generic", '>= 0.3.8'
11
+ # # gem 'logging_assist', '>= 0.2.1'
12
+ # end
13
+ #
14
+ # group :test do
15
+ # gem 'rspec', '>= 2.4.0'
16
+ # gem 'generator-spec', '>= 0.7.0'
17
+ # end
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Kristian Mandrup
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.textile ADDED
@@ -0,0 +1,99 @@
1
+ h1. Fork Info
2
+
3
+ Update Mongoid to 2.0.2 support.
4
+
5
+ h1. Roles for Mongoid
6
+
7
+ A Mongoid implementation of [Roles Generic](http://github.com/kristianmandrup/roles_generic).
8
+ See the Roles [wiki](http://github.com/kristianmandrup/roles_generic/wiki) for an overview of the API and how to use it.
9
+
10
+ h2. Role strategies
11
+
12
+ Role strategies implemented:
13
+
14
+ Inline attribute on User
15
+
16
+ * admin_flag
17
+ * roles_mask
18
+ * role_string
19
+ * role_strings
20
+
21
+ Reference Role
22
+
23
+ * many_roles
24
+ * one_role
25
+
26
+ Embedded Role
27
+
28
+ * embed_many_roles
29
+ * embed_one_role
30
+
31
+ h2. Install
32
+
33
+ <code>gem install mongoid_roles</code>
34
+
35
+ h3. Install in Rails app
36
+
37
+ Insert in Gemfile:
38
+
39
+ <code>gem 'mongoid_roles'</code>
40
+
41
+ Run <code>$ bundle install</code> from terminal
42
+
43
+ Alternatively install using [Cream](http://github.com/kristianmandrup/cream)
44
+
45
+ h2. Strategy and roles configuration
46
+
47
+ Example: _role_string_ strategy
48
+
49
+ <pre>class User
50
+ include Mongoid::Document
51
+ include Mongoid::Roles
52
+
53
+ strategy :one_role
54
+ valid_roles_are :admin, :guest, :user
55
+ end
56
+ </pre>
57
+
58
+ Example: _one_role_ strategy
59
+
60
+ <pre>class User
61
+ include Mongoid::Document
62
+ include Mongoid::Roles
63
+
64
+ strategy :one_role
65
+ valid_roles_are :admin, :guest, :user
66
+ end
67
+ </pre>
68
+
69
+ h2. Rails generators
70
+
71
+ The library comes with a Rails 3 generator that lets you populate a user model with a role strategy of your choice.
72
+
73
+ 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.
74
+
75
+ Apply :admin_flag Role strategy to User model using default roles :admin and :guest (default)
76
+
77
+ <code>$ rails g mongoid:roles User --strategy admin_flag</code>
78
+
79
+ Apply :admin_flag Role strategy to User model using default roles and extra role :author
80
+
81
+ <code>$ rails g mongoid:roles_migration User --strategy admin_flag --roles author</code>
82
+
83
+ Apply :one_role Role strategy to User model without default roles, only with roles :user, :special and :editor
84
+
85
+ <code>$ rails g mongoid:roles_migration User --strategy one_role --roles user special editor --no-default-roles</code>
86
+
87
+ h2. Note on Patches/Pull Requests
88
+
89
+ * Fork the project.
90
+ * Make your feature addition or bug fix.
91
+ * Add tests for it. This is important so I don't break it in a
92
+ future version unintentionally.
93
+ * Commit, do not mess with rakefile, version, or history.
94
+ (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
95
+ * Send me a pull request. Bonus points for topic branches.
96
+
97
+ h2. Copyright
98
+
99
+ Copyright (c) 2010 Kristian Mandrup. See LICENSE for details.
data/Rakefile ADDED
@@ -0,0 +1,26 @@
1
+ begin
2
+ require 'jeweler'
3
+ Jeweler::Tasks.new do |gem|
4
+ gem.name = "mongoid_roles_zhd"
5
+ gem.summary = %Q{Implementation of Roles generic API for Mongoid}
6
+ gem.description = %Q{Makes it easy to set a role strategy on your User model in Mongoid}
7
+ gem.email = "blackanger.z@gmail.com"
8
+ gem.homepage = "https://github.com/ZhangHanDong/roles_mongoid"
9
+ gem.authors = ["ZhangHanDong"]
10
+ gem.add_development_dependency 'rspec', '>= 2.4.1'
11
+ gem.add_development_dependency 'generator-spec', '>= 0.7.3'
12
+
13
+ gem.add_dependency 'mongoid', '>= 2.0.2'
14
+ gem.add_dependency 'bson', '>= 1.3.1'
15
+
16
+ gem.add_dependency 'sugar-high', '~> 0.4.0'
17
+ gem.add_dependency 'require_all', '~> 1.2.0'
18
+ gem.add_dependency "roles_generic", '~> 0.3.8'
19
+
20
+ gem.add_dependency 'rails_artifactor', '>= 0.3.5'
21
+ end
22
+ Jeweler::GemcutterTasks.new
23
+ rescue LoadError
24
+ puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
25
+ end
26
+
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1
@@ -0,0 +1,11 @@
1
+ class String
2
+ def as_filename
3
+ self.underscore
4
+ end
5
+ end
6
+
7
+ class Symbol
8
+ def as_filename
9
+ self.to_s.underscore
10
+ end
11
+ end
@@ -0,0 +1,173 @@
1
+ require 'rails_artifactor'
2
+ require 'generators/mongoid/roles/core_ext'
3
+
4
+ module Mongoid
5
+ module Generators
6
+ class RolesGenerator < Rails::Generators::Base
7
+ desc "Add role strategy to a Mongoid User model"
8
+
9
+ argument :user_class, :type => :string, :default => 'User', :desc => "User class name"
10
+
11
+ class_option :strategy, :type => :string, :aliases => "-s", :default => 'role_string',
12
+ :desc => "Role strategy to use (admin_flag, role_string, roles_string, role_strings, roles_mask, one_role, many_roles, embed_one_role, embed_many_roles)"
13
+
14
+ class_option :roles, :type => :array, :aliases => "-r", :default => [], :desc => "Valid roles"
15
+ class_option :role_class, :type => :string, :aliases => "-rc", :default => 'Role', :desc => "Role class name"
16
+
17
+ class_option :logfile, :type => :string, :aliases => "-l", :default => nil, :desc => "Logfile location"
18
+
19
+ source_root File.dirname(__FILE__) + '/templates'
20
+
21
+ def apply_role_strategy
22
+ if logfile?
23
+ require 'logging_assist'
24
+
25
+ class_eval do
26
+ send :include, RailsAssist::BasicLogger
27
+ end
28
+
29
+ logger.add_logfile :logfile => logfile if logfile
30
+ debug "apply_role_strategy for : #{strategy} in model #{user_file}"
31
+ end
32
+
33
+ if !valid_strategy?
34
+ error "Strategy #{strategy} is not currently supported, please try one of #{valid_strategies.join(', ')}"
35
+ end
36
+
37
+ if !has_model_file?(user_file)
38
+ error "User model #{user_file} not found"
39
+ return
40
+ end
41
+
42
+ if !is_mongoid_model?(user_file)
43
+ error "User model #{user_file} is not a Mongoid Document"
44
+ return
45
+ end
46
+
47
+ begin
48
+ debug "Trying to insert roles code into #{user_file}"
49
+
50
+ insert_into_model user_file, :after => /include Mongoid::\w+/ do
51
+ insertion_text
52
+ end
53
+ rescue Exception => e
54
+ error "Error: #{e.message}" if logger
55
+ end
56
+
57
+ copy_role_class if role_class_strategy?
58
+ end
59
+
60
+ protected
61
+
62
+ def logger
63
+ nil
64
+ end
65
+
66
+ def debug msg
67
+ logger.debug msg if logger
68
+ end
69
+
70
+ def info msg
71
+ logger.info msg if logger
72
+ end
73
+
74
+ def error msg
75
+ logger.error msg if logger
76
+ end
77
+
78
+
79
+ extend RailsAssist::UseMacro
80
+
81
+ use_orm :mongoid
82
+
83
+ def user_file
84
+ user_class.as_filename
85
+ end
86
+
87
+ def role_file
88
+ role_class.as_filename
89
+ end
90
+
91
+ def is_mongoid_model? name
92
+ read_model(name) =~ /include Mongoid::\w+/
93
+ end
94
+
95
+ def valid_strategy?
96
+ valid_strategies.include? strategy.to_sym
97
+ end
98
+
99
+ def valid_strategies
100
+ [:admin_flag, :one_role, :embed_one_role, :role_string, :embed_many_roles, :many_roles, :role_strings, :roles_mask]
101
+ end
102
+
103
+ def logfile?
104
+ logfile
105
+ end
106
+
107
+ def logfile
108
+ options[:logfile]
109
+ end
110
+
111
+ def orm
112
+ :mongoid
113
+ end
114
+
115
+ def orm_class
116
+ orm.to_s.camelize
117
+ end
118
+
119
+ def default_roles
120
+ [:admin, :guest]
121
+ end
122
+
123
+ def copy_role_class
124
+ debug "generating role model: #{role_file}"
125
+ template 'role.rb', "app/models/#{role_file}.rb"
126
+ end
127
+
128
+ def roles_to_add
129
+ @roles_to_add ||= default_roles.concat(options[:roles]).to_symbols.uniq
130
+ end
131
+
132
+ def roles
133
+ roles_to_add.map{|r| ":#{r}" }
134
+ end
135
+
136
+ def role_strategy_statement
137
+ "strategy :#{strategy} #{strategy_option_arg}"
138
+ end
139
+
140
+ def strategy_option_arg
141
+ return ", :role_class => '#{role_class}'" if role_class_strategy? && role_class.to_s != 'Role'
142
+ ''
143
+ end
144
+
145
+ def role_class_strategy?
146
+ [:one_role, :many_roles, :embed_one_role, :embed_many_roles].include? strategy.to_sym
147
+ end
148
+
149
+ def valid_roles_statement
150
+ return '' if has_valid_roles_statement?
151
+ roles ? "valid_roles_are #{roles.join(', ')}" : ''
152
+ end
153
+
154
+ def has_valid_roles_statement?
155
+ !(read_model(user_class) =~ /valid_roles_are/).nil?
156
+ end
157
+
158
+ def insertion_text
159
+ %Q{include Roles::#{orm_class}
160
+ #{role_strategy_statement}
161
+ #{valid_roles_statement}}
162
+ end
163
+
164
+ def role_class
165
+ options[:role_class].classify || 'Role'
166
+ end
167
+
168
+ def strategy
169
+ options[:strategy]
170
+ end
171
+ end
172
+ end
173
+ end
@@ -0,0 +1,8 @@
1
+ class <%= role_class %>
2
+ include Mongoid::Document
3
+ field :name, :type => String
4
+
5
+ validates_uniqueness_of :name
6
+
7
+ extend RoleClass::ClassMethods
8
+ end
@@ -0,0 +1,10 @@
1
+ require 'roles_generic'
2
+ require 'require_all'
3
+ require 'set'
4
+ require 'sugar-high/array'
5
+ require 'sugar-high/kind_of'
6
+ require 'mongoid'
7
+ require 'bson'
8
+ require 'mongoid_roles/namespaces'
9
+ require 'mongoid_roles/base'
10
+ require 'mongoid_roles/strategy'
@@ -0,0 +1,91 @@
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 Mongoid
8
+ module Roles
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
+
20
+ :many_roles_old => "references_many :many_roles, :stored_as => :array, :class_name => 'Role'",
21
+ :many_roles => "references_and_referenced_in_many :many_roles, :class_name => 'Role'",
22
+ :one_role => "referenced_in :one_role, :class_name => 'Role'",
23
+
24
+ :embed_many_roles => "embeds_many :many_roles, :class_name => 'Role'",
25
+ :embed_one_role => "embeds_one :one_role, :class_name => 'Role'",
26
+
27
+ :roles_mask => "field :roles_mask, :type => Integer, :default => 0",
28
+ :role_string => "field :role_string, :type => String",
29
+ :role_strings => "field :role_strings, :type => Array",
30
+ :roles_string => "field :roles_string, :type => String"
31
+ }
32
+
33
+ def strategy name, options = {}
34
+ strategy_name = name.to_sym
35
+ raise ArgumentError, "Unknown role strategy #{strategy_name}" if !MAP.keys.include? strategy_name
36
+ use_roles_strategy strategy_name
37
+
38
+ set_role_class(strategy_name, options) if strategies_with_role_class.include? strategy_name
39
+
40
+ if default_options?(options) && MAP[strategy_name]
41
+ instance_eval statement(MAP[strategy_name])
42
+ end
43
+
44
+ case name
45
+ when :embed_one_role
46
+ raise ArgumentError, "#strategy class method must take :role_class option when using an embedded role strategy" if !@role_class_name
47
+ @role_class_name.embedded_in :user, :inverse_of => :one_role
48
+ when :embed_many_roles
49
+ raise ArgumentError, "#strategy class method must take :role_class option when using an embedded role strategy" if !@role_class_name
50
+ @role_class_name.embedded_in :user, :inverse_of => :many_roles
51
+ end
52
+
53
+ set_role_strategy strategy_name, options
54
+ end
55
+
56
+ private
57
+
58
+ def set_role_class strategy_name, options = {}
59
+ @role_class_name = !options.kind_of?(Symbol) ? get_role_class(strategy_name, options) : default_role_class(strategy_name)
60
+ end
61
+
62
+ def default_options? options = {}
63
+ return true if options == :default
64
+ if options.kind_of? Hash
65
+ return true # if options[:config] == :default || options == {}
66
+ end
67
+ false
68
+ end
69
+
70
+ def statement code_str
71
+ code_str.gsub /Role/, @role_class_name.to_s
72
+ end
73
+
74
+ def default_role_class strategy_name
75
+ if defined? ::Role
76
+ require "mongoid_roles/role"
77
+ return ::Role
78
+ end
79
+ raise "Default Role class not defined"
80
+ end
81
+
82
+ def strategies_with_role_class
83
+ [:one_role, :embed_one_role, :many_roles,:embed_many_roles]
84
+ end
85
+
86
+ def get_role_class strategy_name, options
87
+ options[:role_class] ? options[:role_class].to_s.camelize.constantize : default_role_class(strategy_name)
88
+ end
89
+ end
90
+ end
91
+ end