roles_active_record 0.3.6 → 0.4.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.
Files changed (30) hide show
  1. data/README.textile +139 -0
  2. data/VERSION +1 -1
  3. data/lib/generators/active_record/roles/roles_generator.rb +58 -14
  4. data/lib/generators/active_record/roles/templates/many_roles/role.rb +7 -0
  5. data/lib/generators/active_record/roles/templates/many_roles/user_role.rb +4 -0
  6. data/lib/generators/active_record/roles/templates/one_role/role.rb +6 -0
  7. data/lib/roles_active_record/base.rb +52 -1
  8. data/lib/roles_active_record/many_roles.rb +11 -0
  9. data/lib/roles_active_record/one_role.rb +5 -0
  10. data/lib/roles_active_record/strategy/multi/many_roles.rb +0 -12
  11. data/lib/roles_active_record/strategy/single/one_role.rb +1 -7
  12. data/roles_active_record.gemspec +18 -18
  13. data/spec/fixtures/one_role_setup.rb +1 -1
  14. data/spec/generator_spec_helper.rb +1 -1
  15. data/spec/roles_active_record/generators/{roles_generator_spec.rb → admin_flag_gen_spec.rb} +11 -26
  16. data/spec/roles_active_record/generators/many_roles_gen_spec.rb +55 -0
  17. data/spec/roles_active_record/generators/{setup_generator_spec.rb → migration_generator_spec.rb} +0 -0
  18. data/spec/roles_active_record/generators/one_role_gen_spec.rb +49 -0
  19. data/spec/roles_active_record/strategy/single/one_role_spec.rb +2 -1
  20. metadata +19 -19
  21. data/README.markdown +0 -191
  22. data/logging.log +0 -26
  23. data/tmp/rails/app/models/user.rb +0 -7
  24. data/tmp/rails/config/routes.rb +0 -2
  25. data/tmp/rails/db/migrations/20100912101428_add_admin_flag_strategy.rb +0 -14
  26. data/tmp/rails/db/migrations/20100912101429_remove_admin_flag_strategy.rb +0 -14
  27. data/tmp/rails/db/migrations/20100912105055_add_many_roles_strategy.rb +0 -40
  28. data/tmp/rails/db/migrations/20100912105323_add_one_role_strategy.rb +0 -40
  29. data/tmp/rails/db/migrations/20100912110044_add_role_string_strategy.rb +0 -13
  30. data/tmp/rails/db/migrations/20100912110249_add_roles_mask_strategy.rb +0 -13
data/README.textile ADDED
@@ -0,0 +1,139 @@
1
+ h1. Roles for Active Record
2
+
3
+ This is an Active Record implementation of the "Roles generic API":https://github.com/kristianmandrup/roles_generic/wiki
4
+ Please also see the "Roles generic README":https://github.com/kristianmandrup/roles_generic
5
+
6
+ Roles lets you add a _role strategy_ of choice to your user model.
7
+ Roles supports strategies with either an inline attribute on user or a separate Role model (see below).
8
+
9
+ h2. Install
10
+
11
+ <code>gem install roles_active_record</code>
12
+
13
+ h3. Install in Rails app
14
+
15
+ Insert in Gemfile:
16
+
17
+ <code>gem 'roles_active_record'</code>
18
+
19
+ Run <code>$ bundle install</code> from terminal
20
+
21
+ Alternatively install using "Cream":http://github.com/kristianmandrup/cream
22
+
23
+ h2. Role strategies
24
+
25
+ The following Role strategies are available for Active Record:
26
+
27
+ h3. Inline attribute on User
28
+
29
+ * admin_flag
30
+ * roles_mask
31
+ * role_string
32
+
33
+ These strategies all use an inline attribute on the User model.
34
+
35
+ h3. Reference to Role
36
+
37
+ * many_roles
38
+ * one_role
39
+
40
+ These strategies use a separate Role model (class and table).
41
+
42
+ h2. Role strategy configuration
43
+
44
+ The following demonstrates some examples of role strategy configuration.
45
+
46
+ h3. Strategy: admin_flag
47
+
48
+ <pre>class User < ActiveRecord::Base
49
+ include Roles::ActiveRecord
50
+
51
+ strategy :admin_flag, :default
52
+ valid_roles_are :admin, :guest
53
+ end
54
+ </pre>
55
+
56
+ h3. Strategy: one_role
57
+
58
+ For strategies that use a separate Role model you must call the class method #role_class with the name of the role class
59
+
60
+ <pre>class Bruger < ActiveRecord::Base
61
+ include Roles::ActiveRecord
62
+
63
+ strategy :one_role, :role_class => :rolle
64
+ valid_roles_are :admin, :guest
65
+ ...
66
+ end
67
+ </pre>
68
+
69
+ Here the role names have been customized
70
+
71
+ h3. Default Role classes
72
+
73
+ The default role classes can currently be included by:
74
+
75
+ One role:
76
+
77
+ <code>require 'roles_active_record/one_role'</code>
78
+
79
+ Many roles:
80
+
81
+ <code>require 'roles_active_record/many_roles'</code>
82
+
83
+ Note: These files will automatically be included when needed under normal conditions.
84
+
85
+ h2. Roles generators
86
+
87
+ The gem includes these Rails 3 generators:
88
+
89
+ * active_record:roles
90
+ * active_record:roles_migration
91
+
92
+ h3. Roles generator
93
+
94
+ Let you populate a User model with a given role strategy
95
+
96
+ Example: Apply Role strategy _admin_flag_ to the User and make the default roles :admin and :guest available
97
+
98
+ <code>$ rails g active_record:roles User --strategy admin_flag</code>
99
+
100
+ Example: Apply Role strategy _role_string_ to the User and make the roles :admin, :guest and :author available
101
+
102
+ <code>$ rails g active_record:roles_migration User --strategy role_string --roles author</code>
103
+
104
+ Example: Apply Role strategy _one_role_ to the User model with roles :user, :special and :editor
105
+
106
+ <code>$ rails g active_record:roles_migration User --strategy one_role --roles user special editor --no-default-roles</code>
107
+
108
+ Example: Apply Role strategy _many_role_ to the User model with default roles and customizing role class names to BrugerRolle and Rolle
109
+
110
+ <code>$ rails g active_record:roles_migration User -s one_role -r user editor -rc Rolle -urc BrugerRolle</code>
111
+
112
+ For the strategies _one_role_ and _many_roles_ the generator also generates the Role and UserRole classes in the app/models dir. If you customize the names of these
113
+ classes (using generator arguments), the customized classes will be generated and "linked up" correctly (with has and belongs_to statements).
114
+
115
+ h3. Roles migration generator
116
+
117
+ In case you only want to generate Role migrations.
118
+
119
+ Example: admin_flag Role strategy
120
+
121
+ <code>$ rails g active_record:roles_migration User --strategy admin_flag</code>
122
+
123
+ Create reverse migration
124
+
125
+ <code>$ rails g active_record:roles_migration User --strategy admin_flag --reverse</code>
126
+
127
+ h2. Note on Patches/Pull Requests
128
+
129
+ * Fork the project.
130
+ * Make your feature addition or bug fix.
131
+ * Add tests for it. This is important so I don't break it in a
132
+ future version unintentionally.
133
+ * Commit, do not mess with rakefile, version, or history.
134
+ (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)
135
+ * Send me a pull request. Bonus points for topic branches.
136
+
137
+ h2. Copyright
138
+
139
+ Copyright (c) 2010 Kristian Mandrup. See LICENSE for details.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.3.6
1
+ 0.4.0
@@ -11,9 +11,14 @@ module ActiveRecord
11
11
  class_option :strategy, :type => :string, :aliases => "-s", :default => 'role_string',
12
12
  :desc => "Role strategy to use (admin_flag, role_string, roles_string, role_strings, one_role, many_roles, roles_mask)"
13
13
 
14
- class_option :roles, :type => :array, :aliases => "-r", :default => [], :desc => "Valid roles"
15
- class_option :default_roles, :type => :boolean, :default => true, :desc => "Use default roles :admin and :base"
16
- class_option :logfile, :type => :string, :default => nil, :desc => "Logfile location"
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"
16
+ class_option :user_role_class, :type => :string, :aliases => "-urc", :default => 'UserRole', :desc => "User Role join class"
17
+
18
+ class_option :default_roles, :type => :boolean, :default => true, :desc => "Use default roles :admin and :base"
19
+ class_option :logfile, :type => :string, :default => nil, :desc => "Logfile location"
20
+
21
+ source_root File.dirname(__FILE__) + '/templates'
17
22
 
18
23
  def apply_role_strategy
19
24
  logger.add_logfile :logfile => logfile if logfile
@@ -23,21 +28,57 @@ module ActiveRecord
23
28
  insertion_text
24
29
  end
25
30
  rescue
31
+ logger.debug "Model #{name} not found"
26
32
  say "Model #{name} not found"
27
33
  end
28
34
  end
29
-
35
+
36
+ def copy_role_models
37
+ logger.debug 'copy_role_models'
38
+ case strategy.to_sym
39
+ when :one_role
40
+ copy_one_role_model
41
+ when :many_roles
42
+ copy_many_roles_models
43
+ end
44
+ end
45
+
30
46
  protected
31
47
 
32
48
  extend Rails3::Assist::UseMacro
33
49
  use_orm :active_record
34
50
  include Rails3::Assist::BasicLogger
35
51
 
52
+ def copy_one_role_model
53
+ logger.debug "copy_one_role_model: #{role_class.underscore}"
54
+
55
+ template 'one_role/role.rb', "app/models/#{role_class.underscore}.rb"
56
+ end
57
+
58
+ def copy_many_roles_models
59
+ logger.debug "copy_many_roles_models: #{role_class.underscore} and #{user_role_class.underscore}"
60
+
61
+ template 'many_roles/role.rb', "app/models/#{role_class.underscore}.rb"
62
+ template 'many_roles/user_role.rb', "app/models/#{user_role_class.underscore}.rb"
63
+ end
64
+
65
+ def user_class
66
+ options[:user_class].classify || 'User'
67
+ end
68
+
69
+ def role_class
70
+ options[:role_class].classify || 'Role'
71
+ end
72
+
73
+ def user_role_class
74
+ options[:user_role_class].classify || 'UserRole'
75
+ end
76
+
36
77
  def logfile
37
78
  options[:logfile]
38
79
  end
39
80
 
40
- def user_model_name
81
+ def user_class
41
82
  name || 'User'
42
83
  end
43
84
 
@@ -57,27 +98,30 @@ module ActiveRecord
57
98
  roles_to_add.map{|r| ":#{r}" }
58
99
  end
59
100
 
60
- def roles_statement
61
- return '' if has_valid_roles_statement?
62
- roles ? "valid_roles_are #{roles.join(', ')}" : ''
101
+ def has_valid_roles_statement?
102
+ !(read_model(user_class) =~ /valid_roles_are/).nil?
63
103
  end
64
104
 
65
- def has_valid_roles_statement?
66
- !(read_model(user_model_name) =~ /valid_roles_are/).nil?
105
+ def valid_roles_statement
106
+ return '' if has_valid_roles_statement?
107
+ roles ? "valid_roles_are #{roles.join(', ')}" : ''
67
108
  end
68
109
 
69
110
  def role_strategy_statement
70
- "strategy :#{strategy}, :default\n#{role_class_stmt}"
111
+ "strategy :#{strategy}, #{strategy_options}"
71
112
  end
72
113
 
73
- def role_class_stmt
74
- " role_class :role" if [:one_role, :many_roles].include?(strategy.to_sym)
114
+ def strategy_options
115
+ if role_class != 'Role' || user_role_class != 'UserRole' && role_ref_strategy?
116
+ return ":role_class => '#{role_class}', :user_role_class => 'user_role_class'"
117
+ end
118
+ ":default"
75
119
  end
76
120
 
77
121
  def insertion_text
78
122
  %Q{include Roles::#{orm.to_s.camelize}
79
123
  #{role_strategy_statement}
80
- #{roles_statement}}
124
+ #{valid_roles_statement}}
81
125
  end
82
126
 
83
127
  def strategy
@@ -0,0 +1,7 @@
1
+ class <%= role_class %> < ActiveRecord::Base
2
+ scope :named, lambda{|role_names| where(:name.in => role_names.flatten)}
3
+ has_many :<%= user_class.pluralize.underscore %>, :through => :<%= user_role_class.pluralize.underscore %>
4
+ has_many :<%= user_role_class.pluralize.underscore %>
5
+
6
+ validates :name, :uniqueness => true
7
+ end
@@ -0,0 +1,4 @@
1
+ class <%= user_role_class %> < ActiveRecord::Base
2
+ belongs_to :<%= user_class.underscore %>
3
+ belongs_to :<%= role_class.underscore %>
4
+ end
@@ -0,0 +1,6 @@
1
+ class <%= role_class %> < ActiveRecord::Base
2
+ scope :named, lambda{|role_names| where(:name.in => role_names.flatten)}
3
+ has_many :<%= user_class.pluralize.underscore %>
4
+
5
+ validates :name, :uniqueness => true
6
+ end
@@ -12,8 +12,59 @@ module Roles::ActiveRecord
12
12
  end
13
13
 
14
14
  module ClassMethods
15
- def strategy name, options=nil
15
+ MAP = {
16
+ :admin_flag => "attr_accessor :admin_flag",
17
+
18
+ :many_roles => "attr_accessor :many_roles",
19
+ :one_role => "belongs_to :one_role, :foreign_key => :role_id, :class_name => 'Role'",
20
+
21
+ :embed_many_roles => "attr_accessor :many_roles",
22
+ :embed_one_role => "attr_accessor :one_role",
23
+
24
+ :roles_mask => "attr_accessor :roles_mask",
25
+ :role_string => "attr_accessor :role_string",
26
+ :role_strings => "attr_accessor :role_strings",
27
+ :roles_string => "attr_accessor :roles_string"
28
+ }
29
+
30
+ def strategy name, options = {}
31
+ strategy_name = name.to_sym
32
+ raise ArgumentError, "Unknown role strategy #{strategy_name}" if !MAP.keys.include? strategy_name
33
+ use_roles_strategy strategy_name
34
+
35
+ if !options.kind_of? Symbol
36
+ @role_class_name = get_role_class(strategy_name, options)
37
+ else
38
+ @role_class_name = default_role_class(strategy_name) if strategies_with_role_class.include? strategy_name
39
+ end
40
+
41
+ if (options == :default || options[:config] == :default) && MAP[name]
42
+ instance_eval statement(MAP[strategy_name])
43
+ end
44
+
16
45
  set_role_strategy name, options
17
46
  end
47
+
48
+ private
49
+
50
+ def statement code_str
51
+ code_str.gsub /Role/, @role_class_name.to_s
52
+ end
53
+
54
+ def default_role_class strategy_name
55
+ if defined? ::Role
56
+ require "roles_active_record/#{strategy_name}"
57
+ return ::Role
58
+ end
59
+ raise Error, "Default Role class not defined"
60
+ end
61
+
62
+ def strategies_with_role_class
63
+ [:one_role, :embed_one_role, :many_roles,:embed_many_roles]
64
+ end
65
+
66
+ def get_role_class strategy_name, options
67
+ options[:role_class] ? options[:role_class].to_s.camelize.constantize : default_role_class(strategy_name)
68
+ end
18
69
  end
19
70
  end
@@ -0,0 +1,11 @@
1
+ class Role < ActiveRecord::Base
2
+ scope :named, lambda{|role_names| where(:name.in => role_names.flatten)}
3
+ has_many :users, :through => :user_roles
4
+ has_many :user_roles
5
+ validates :name, :uniqueness => true
6
+ end
7
+
8
+ class UserRole < ActiveRecord::Base
9
+ belongs_to :user
10
+ belongs_to :role
11
+ end
@@ -0,0 +1,5 @@
1
+ class Role < ActiveRecord::Base
2
+ scope :named, lambda{|role_names| where(:name.in => role_names.flatten)}
3
+ has_many :users
4
+ validates :name, :uniqueness => true
5
+ end
@@ -1,17 +1,5 @@
1
1
  require 'roles_active_record/strategy/multi'
2
2
 
3
- class Role < ActiveRecord::Base
4
- scope :named, lambda{|role_names| where(:name.in => role_names.flatten)}
5
- has_many :users, :through => :user_roles
6
- has_many :user_roles
7
- validates :name, :uniqueness => true
8
- end
9
-
10
- class UserRole < ActiveRecord::Base
11
- belongs_to :user
12
- belongs_to :role
13
- end
14
-
15
3
  module RoleStrategy::ActiveRecord
16
4
  module ManyRoles
17
5
  def self.default_role_attribute
@@ -1,11 +1,5 @@
1
1
  require 'roles_active_record/strategy/single'
2
2
 
3
- class Role < ActiveRecord::Base
4
- scope :named, lambda{|role_names| where(:name.in => role_names.flatten)}
5
- has_many :users
6
- validates :name, :uniqueness => true
7
- end
8
-
9
3
  module RoleStrategy::ActiveRecord
10
4
  module OneRole
11
5
  def self.default_role_attribute
@@ -15,7 +9,7 @@ module RoleStrategy::ActiveRecord
15
9
  def self.included base
16
10
  base.extend Roles::Generic::Role::ClassMethods
17
11
  base.extend ClassMethods
18
- base.belongs_to :one_role, :foreign_key => :role_id, :class_name => 'Role'
12
+ # base.belongs_to :one_role, :foreign_key => :role_id, :class_name => 'Role'
19
13
  end
20
14
 
21
15
  module ClassMethods
@@ -5,28 +5,31 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{roles_active_record}
8
- s.version = "0.3.6"
8
+ s.version = "0.4.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Kristian Mandrup"]
12
- s.date = %q{2010-12-20}
12
+ s.date = %q{2010-12-25}
13
13
  s.description = %q{Makes it easy to set a role strategy on your User model in Active Record}
14
14
  s.email = %q{kmandrup@gmail.com}
15
15
  s.extra_rdoc_files = [
16
16
  "LICENSE",
17
- "README.markdown"
17
+ "README.textile"
18
18
  ]
19
19
  s.files = [
20
20
  ".document",
21
21
  ".rspec",
22
22
  "Gemfile",
23
23
  "LICENSE",
24
- "README.markdown",
24
+ "README.textile",
25
25
  "Rakefile",
26
26
  "VERSION",
27
27
  "config/database.yml",
28
28
  "development.sqlite3",
29
29
  "lib/generators/active_record/roles/roles_generator.rb",
30
+ "lib/generators/active_record/roles/templates/many_roles/role.rb",
31
+ "lib/generators/active_record/roles/templates/many_roles/user_role.rb",
32
+ "lib/generators/active_record/roles/templates/one_role/role.rb",
30
33
  "lib/generators/active_record/roles_migration/roles_migration_generator.rb",
31
34
  "lib/generators/active_record/roles_migration/templates/add_admin_flag_strategy.erb",
32
35
  "lib/generators/active_record/roles_migration/templates/add_many_roles_strategy.erb",
@@ -35,7 +38,9 @@ Gem::Specification.new do |s|
35
38
  "lib/generators/active_record/roles_migration/templates/add_roles_mask_strategy.erb",
36
39
  "lib/roles_active_record.rb",
37
40
  "lib/roles_active_record/base.rb",
41
+ "lib/roles_active_record/many_roles.rb",
38
42
  "lib/roles_active_record/namespaces.rb",
43
+ "lib/roles_active_record/one_role.rb",
39
44
  "lib/roles_active_record/role.rb",
40
45
  "lib/roles_active_record/strategy.rb",
41
46
  "lib/roles_active_record/strategy/multi.rb",
@@ -48,7 +53,6 @@ Gem::Specification.new do |s|
48
53
  "lib/roles_active_record/strategy/single/role_string.rb",
49
54
  "lib/views/_multi_role_selector.erb.html",
50
55
  "lib/views/_single_role_selector.erb.html",
51
- "logging.log",
52
56
  "roles_active_record.gemspec",
53
57
  "sandbox/Rakefile",
54
58
  "sandbox/add_role_to_users_migration.erb",
@@ -74,13 +78,15 @@ Gem::Specification.new do |s|
74
78
  "spec/migrations/role_string/002_add_role_string_strategy.rb",
75
79
  "spec/migrations/roles_mask/002_add_roles_mask_strategy.rb",
76
80
  "spec/migrations/users/001_create_users.rb",
77
- "spec/roles_active_record/generators/roles_generator_spec.rb",
81
+ "spec/roles_active_record/generators/admin_flag_gen_spec.rb",
82
+ "spec/roles_active_record/generators/many_roles_gen_spec.rb",
83
+ "spec/roles_active_record/generators/migration_generator_spec.rb",
84
+ "spec/roles_active_record/generators/one_role_gen_spec.rb",
78
85
  "spec/roles_active_record/generators/roles_migration/admin_flag_spec.rb",
79
86
  "spec/roles_active_record/generators/roles_migration/many_roles_spec.rb",
80
87
  "spec/roles_active_record/generators/roles_migration/one_role_spec.rb",
81
88
  "spec/roles_active_record/generators/roles_migration/role_string_spec.rb",
82
89
  "spec/roles_active_record/generators/roles_migration/roles_mask_spec.rb",
83
- "spec/roles_active_record/generators/setup_generator_spec.rb",
84
90
  "spec/roles_active_record/strategy/api.rb",
85
91
  "spec/roles_active_record/strategy/api_examples.rb",
86
92
  "spec/roles_active_record/strategy/multi/many_roles_spec.rb",
@@ -91,15 +97,7 @@ Gem::Specification.new do |s|
91
97
  "spec/roles_active_record/strategy/single/one_role_unique_spec.rb",
92
98
  "spec/roles_active_record/strategy/single/role_string_spec.rb",
93
99
  "spec/roles_active_record/strategy/user_setup.rb",
94
- "spec/spec_helper.rb",
95
- "tmp/rails/app/models/user.rb",
96
- "tmp/rails/config/routes.rb",
97
- "tmp/rails/db/migrations/20100912101428_add_admin_flag_strategy.rb",
98
- "tmp/rails/db/migrations/20100912101429_remove_admin_flag_strategy.rb",
99
- "tmp/rails/db/migrations/20100912105055_add_many_roles_strategy.rb",
100
- "tmp/rails/db/migrations/20100912105323_add_one_role_strategy.rb",
101
- "tmp/rails/db/migrations/20100912110044_add_role_string_strategy.rb",
102
- "tmp/rails/db/migrations/20100912110249_add_roles_mask_strategy.rb"
100
+ "spec/spec_helper.rb"
103
101
  ]
104
102
  s.homepage = %q{http://github.com/kristianmandrup/roles_active_record}
105
103
  s.require_paths = ["lib"]
@@ -118,13 +116,15 @@ Gem::Specification.new do |s|
118
116
  "spec/migrations/role_string/002_add_role_string_strategy.rb",
119
117
  "spec/migrations/roles_mask/002_add_roles_mask_strategy.rb",
120
118
  "spec/migrations/users/001_create_users.rb",
121
- "spec/roles_active_record/generators/roles_generator_spec.rb",
119
+ "spec/roles_active_record/generators/admin_flag_gen_spec.rb",
120
+ "spec/roles_active_record/generators/many_roles_gen_spec.rb",
121
+ "spec/roles_active_record/generators/migration_generator_spec.rb",
122
+ "spec/roles_active_record/generators/one_role_gen_spec.rb",
122
123
  "spec/roles_active_record/generators/roles_migration/admin_flag_spec.rb",
123
124
  "spec/roles_active_record/generators/roles_migration/many_roles_spec.rb",
124
125
  "spec/roles_active_record/generators/roles_migration/one_role_spec.rb",
125
126
  "spec/roles_active_record/generators/roles_migration/role_string_spec.rb",
126
127
  "spec/roles_active_record/generators/roles_migration/roles_mask_spec.rb",
127
- "spec/roles_active_record/generators/setup_generator_spec.rb",
128
128
  "spec/roles_active_record/strategy/api.rb",
129
129
  "spec/roles_active_record/strategy/api_examples.rb",
130
130
  "spec/roles_active_record/strategy/multi/many_roles_spec.rb",
@@ -2,7 +2,7 @@ class User < ActiveRecord::Base
2
2
  include Roles::ActiveRecord
3
3
 
4
4
  strategy :one_role, :default
5
- role_class :role
5
+ # role_class :role
6
6
 
7
7
  valid_roles_are :admin, :guest, :user
8
8
  end
@@ -6,7 +6,7 @@ require 'roles_generic'
6
6
 
7
7
  RSpec::Generator.configure do |config|
8
8
  config.debug = false
9
- config.remove_temp_dir = false # true
9
+ config.remove_temp_dir = true
10
10
  config.default_rails_root(__FILE__)
11
11
  config.lib = File.dirname(__FILE__) + '/../lib'
12
12
  config.logger = :stdout
@@ -1,15 +1,18 @@
1
1
  require 'generator_spec_helper'
2
- require_generator :active_record => :roles
2
+
3
+ # require_generator :active_record => :roles
4
+ require 'generators/active_record/roles/roles_generator'
3
5
 
4
6
  # root_dir = Rails3::Assist::Directory.rails_root
5
- root_dir = File.join(Rails.application.config.root_dir, 'rails')
7
+ # root_dir = File.join(Rails.application.config.root_dir, 'rails')
8
+ root_dir = Rails.root
6
9
 
7
- describe 'role strategy generator: admin_flag' do
10
+ describe 'roles generator' do
8
11
  describe 'ORM: active_record' do
9
12
  use_orm :active_record
10
13
 
11
14
  before do
12
- setup_generator 'datamapper_roles_generator' do
15
+ setup_generator 'AR_generator' do
13
16
  tests ActiveRecord::Generators::RolesGenerator
14
17
  end
15
18
  end
@@ -19,10 +22,10 @@ describe 'role strategy generator: admin_flag' do
19
22
  end
20
23
 
21
24
  after :each do
22
- # remove_model :user
25
+ remove_model :user
23
26
  end
24
27
 
25
- it "should configure 'admin_flag' strategy without default roles" do
28
+ it "should configure 'admin_flag' strategy without default roles" do
26
29
  create_model :user do
27
30
  '# content'
28
31
  end
@@ -33,12 +36,12 @@ describe 'role strategy generator: admin_flag' do
33
36
  root_dir.should have_model :user do |clazz|
34
37
  clazz.should include_module 'Roles::ActiveRecord'
35
38
  clazz.should have_call :valid_roles_are, :args => ':special'
36
- clazz.should have_call :strategy, :args => ":admin_flag"
39
+ clazz.should have_call :strategy, :args => ":admin_flag"
37
40
  end
38
41
  end
39
42
  end
40
43
 
41
- it "should configure 'admin_flag' strategy" do
44
+ it "should configure 'admin_flag' strategy" do
42
45
  create_model :user do
43
46
  '# content'
44
47
  end
@@ -52,24 +55,6 @@ describe 'role strategy generator: admin_flag' do
52
55
  clazz.should have_call :strategy, :args => ":admin_flag"
53
56
  end
54
57
  end
55
- end
56
-
57
- it "should configure 'one_role' strategy" do
58
- create_model :user do
59
- '# content'
60
- end
61
- with_generator do |g|
62
- arguments = "User --strategy one_role --roles admin user"
63
- puts "arguments: #{arguments}"
64
- g.run_generator arguments.args
65
- root_dir.should have_model :user do |clazz|
66
- clazz.should include_module 'Roles::ActiveRecord'
67
- clazz.should have_call :valid_roles_are, :args => ':admin, :guest, :user'
68
- clazz.should have_call :role_class, :args => ":role"
69
- clazz.should have_call :strategy, :args => ":one_role"
70
- end
71
- end
72
58
  end
73
59
  end
74
60
  end
75
-
@@ -0,0 +1,55 @@
1
+ require 'generator_spec_helper'
2
+
3
+ # require_generator :active_record => :roles
4
+ require 'generators/active_record/roles/roles_generator'
5
+
6
+ # root_dir = Rails3::Assist::Directory.rails_root
7
+ # root_dir = File.join(Rails.application.config.root_dir, 'rails')
8
+ root_dir = Rails.root
9
+
10
+ describe 'roles generator' do
11
+ describe 'ORM: active_record' do
12
+ use_orm :active_record
13
+
14
+ before do
15
+ setup_generator 'AR_generator' do
16
+ tests ActiveRecord::Generators::RolesGenerator
17
+ end
18
+ end
19
+
20
+ before :each do
21
+ remove_model :user
22
+ end
23
+
24
+ after :each do
25
+ remove_model :user
26
+ end
27
+
28
+ it "should configure 'many_roles' strategy" do
29
+ create_model :user do
30
+ '# content'
31
+ end
32
+ with_generator do |g|
33
+ arguments = "User --strategy many_roles --roles admin user"
34
+ puts "arguments: #{arguments}"
35
+ g.run_generator arguments.args
36
+ root_dir.should have_model :user do |clazz|
37
+ clazz.should include_module 'Roles::ActiveRecord'
38
+ clazz.should have_call :valid_roles_are, :args => ':admin, :guest, :user'
39
+ clazz.should have_call :strategy, :args => ":many_roles"
40
+ end
41
+
42
+ root_dir.should have_model :role do |clazz|
43
+ clazz.should have_call :validates, :args => ':name, :uniqueness => true'
44
+ clazz.should have_call :has_many, :args => ':users, :through => :user_roles'
45
+ clazz.should have_call :has_many, :args => ':user_roles'
46
+ end
47
+
48
+ root_dir.should have_model :user_role do |clazz|
49
+ clazz.should have_call :belongs_to, :args => ':user'
50
+ clazz.should have_call :belongs_to, :args => ':role'
51
+ end
52
+ end
53
+ end
54
+ end
55
+ end
@@ -0,0 +1,49 @@
1
+ require 'generator_spec_helper'
2
+
3
+ # require_generator :active_record => :roles
4
+ require 'generators/active_record/roles/roles_generator'
5
+
6
+ # root_dir = Rails3::Assist::Directory.rails_root
7
+ # root_dir = File.join(Rails.application.config.root_dir, 'rails')
8
+ root_dir = Rails.root
9
+
10
+ describe 'roles generator' do
11
+ describe 'ORM: active_record' do
12
+ use_orm :active_record
13
+
14
+ before do
15
+ setup_generator 'AR_generator' do
16
+ tests ActiveRecord::Generators::RolesGenerator
17
+ end
18
+ end
19
+
20
+ before :each do
21
+ remove_model :user
22
+ end
23
+
24
+ after :each do
25
+ remove_model :user
26
+ end
27
+
28
+ it "should configure 'one_role' strategy" do
29
+ create_model :user do
30
+ '# content'
31
+ end
32
+ with_generator do |g|
33
+ arguments = "User --strategy one_role --roles admin user"
34
+ puts "arguments: #{arguments}"
35
+ g.run_generator arguments.args
36
+ root_dir.should have_model :user do |clazz|
37
+ clazz.should include_module 'Roles::ActiveRecord'
38
+ clazz.should have_call :valid_roles_are, :args => ':admin, :guest, :user'
39
+ clazz.should have_call :strategy, :args => ":one_role"
40
+ end
41
+
42
+ root_dir.should have_model :role do |clazz|
43
+ clazz.should have_call :validates, :args => ':name, :uniqueness => true'
44
+ clazz.should have_call :has_many, :args => ':users'
45
+ end
46
+ end
47
+ end
48
+ end
49
+ end
@@ -1,5 +1,6 @@
1
1
  require 'spec_helper'
2
- use_roles_strategy :one_role
2
+
3
+ #use_roles_strategy :one_role
3
4
 
4
5
  def api_fixture
5
6
  load 'fixtures/one_role_setup.rb'
metadata CHANGED
@@ -4,9 +4,9 @@ version: !ruby/object:Gem::Version
4
4
  prerelease: false
5
5
  segments:
6
6
  - 0
7
- - 3
8
- - 6
9
- version: 0.3.6
7
+ - 4
8
+ - 0
9
+ version: 0.4.0
10
10
  platform: ruby
11
11
  authors:
12
12
  - Kristian Mandrup
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2010-12-20 00:00:00 +01:00
17
+ date: 2010-12-25 00:00:00 +01:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
@@ -337,18 +337,21 @@ extensions: []
337
337
 
338
338
  extra_rdoc_files:
339
339
  - LICENSE
340
- - README.markdown
340
+ - README.textile
341
341
  files:
342
342
  - .document
343
343
  - .rspec
344
344
  - Gemfile
345
345
  - LICENSE
346
- - README.markdown
346
+ - README.textile
347
347
  - Rakefile
348
348
  - VERSION
349
349
  - config/database.yml
350
350
  - development.sqlite3
351
351
  - lib/generators/active_record/roles/roles_generator.rb
352
+ - lib/generators/active_record/roles/templates/many_roles/role.rb
353
+ - lib/generators/active_record/roles/templates/many_roles/user_role.rb
354
+ - lib/generators/active_record/roles/templates/one_role/role.rb
352
355
  - lib/generators/active_record/roles_migration/roles_migration_generator.rb
353
356
  - lib/generators/active_record/roles_migration/templates/add_admin_flag_strategy.erb
354
357
  - lib/generators/active_record/roles_migration/templates/add_many_roles_strategy.erb
@@ -357,7 +360,9 @@ files:
357
360
  - lib/generators/active_record/roles_migration/templates/add_roles_mask_strategy.erb
358
361
  - lib/roles_active_record.rb
359
362
  - lib/roles_active_record/base.rb
363
+ - lib/roles_active_record/many_roles.rb
360
364
  - lib/roles_active_record/namespaces.rb
365
+ - lib/roles_active_record/one_role.rb
361
366
  - lib/roles_active_record/role.rb
362
367
  - lib/roles_active_record/strategy.rb
363
368
  - lib/roles_active_record/strategy/multi.rb
@@ -370,7 +375,6 @@ files:
370
375
  - lib/roles_active_record/strategy/single/role_string.rb
371
376
  - lib/views/_multi_role_selector.erb.html
372
377
  - lib/views/_single_role_selector.erb.html
373
- - logging.log
374
378
  - roles_active_record.gemspec
375
379
  - sandbox/Rakefile
376
380
  - sandbox/add_role_to_users_migration.erb
@@ -396,13 +400,15 @@ files:
396
400
  - spec/migrations/role_string/002_add_role_string_strategy.rb
397
401
  - spec/migrations/roles_mask/002_add_roles_mask_strategy.rb
398
402
  - spec/migrations/users/001_create_users.rb
399
- - spec/roles_active_record/generators/roles_generator_spec.rb
403
+ - spec/roles_active_record/generators/admin_flag_gen_spec.rb
404
+ - spec/roles_active_record/generators/many_roles_gen_spec.rb
405
+ - spec/roles_active_record/generators/migration_generator_spec.rb
406
+ - spec/roles_active_record/generators/one_role_gen_spec.rb
400
407
  - spec/roles_active_record/generators/roles_migration/admin_flag_spec.rb
401
408
  - spec/roles_active_record/generators/roles_migration/many_roles_spec.rb
402
409
  - spec/roles_active_record/generators/roles_migration/one_role_spec.rb
403
410
  - spec/roles_active_record/generators/roles_migration/role_string_spec.rb
404
411
  - spec/roles_active_record/generators/roles_migration/roles_mask_spec.rb
405
- - spec/roles_active_record/generators/setup_generator_spec.rb
406
412
  - spec/roles_active_record/strategy/api.rb
407
413
  - spec/roles_active_record/strategy/api_examples.rb
408
414
  - spec/roles_active_record/strategy/multi/many_roles_spec.rb
@@ -414,14 +420,6 @@ files:
414
420
  - spec/roles_active_record/strategy/single/role_string_spec.rb
415
421
  - spec/roles_active_record/strategy/user_setup.rb
416
422
  - spec/spec_helper.rb
417
- - tmp/rails/app/models/user.rb
418
- - tmp/rails/config/routes.rb
419
- - tmp/rails/db/migrations/20100912101428_add_admin_flag_strategy.rb
420
- - tmp/rails/db/migrations/20100912101429_remove_admin_flag_strategy.rb
421
- - tmp/rails/db/migrations/20100912105055_add_many_roles_strategy.rb
422
- - tmp/rails/db/migrations/20100912105323_add_one_role_strategy.rb
423
- - tmp/rails/db/migrations/20100912110044_add_role_string_strategy.rb
424
- - tmp/rails/db/migrations/20100912110249_add_roles_mask_strategy.rb
425
423
  has_rdoc: true
426
424
  homepage: http://github.com/kristianmandrup/roles_active_record
427
425
  licenses: []
@@ -467,13 +465,15 @@ test_files:
467
465
  - spec/migrations/role_string/002_add_role_string_strategy.rb
468
466
  - spec/migrations/roles_mask/002_add_roles_mask_strategy.rb
469
467
  - spec/migrations/users/001_create_users.rb
470
- - spec/roles_active_record/generators/roles_generator_spec.rb
468
+ - spec/roles_active_record/generators/admin_flag_gen_spec.rb
469
+ - spec/roles_active_record/generators/many_roles_gen_spec.rb
470
+ - spec/roles_active_record/generators/migration_generator_spec.rb
471
+ - spec/roles_active_record/generators/one_role_gen_spec.rb
471
472
  - spec/roles_active_record/generators/roles_migration/admin_flag_spec.rb
472
473
  - spec/roles_active_record/generators/roles_migration/many_roles_spec.rb
473
474
  - spec/roles_active_record/generators/roles_migration/one_role_spec.rb
474
475
  - spec/roles_active_record/generators/roles_migration/role_string_spec.rb
475
476
  - spec/roles_active_record/generators/roles_migration/roles_mask_spec.rb
476
- - spec/roles_active_record/generators/setup_generator_spec.rb
477
477
  - spec/roles_active_record/strategy/api.rb
478
478
  - spec/roles_active_record/strategy/api_examples.rb
479
479
  - spec/roles_active_record/strategy/multi/many_roles_spec.rb
data/README.markdown DELETED
@@ -1,191 +0,0 @@
1
- # Roles for Active Record
2
-
3
- An Active Record implementation of [roles generic](http://github.com/kristianmandrup/roles_generic)
4
-
5
- ## Install
6
-
7
- <code>gem install roles_active_record</code>
8
-
9
- ## Update!
10
-
11
- Now implements the [roles generic](http://github.com/kristianmandrup/roles_generic) Roles API
12
- It also implements the following Role strategies:
13
-
14
- * admin_flag
15
- * many_roles
16
- * one_role
17
- * roles_mask
18
- * role_string
19
-
20
- ## Rails generator
21
-
22
- Will create admin and guest roles by default
23
-
24
- <code>$ rails g active_record:roles User --strategy admin_flag --roles admin guest</code>
25
-
26
- ## Usage
27
-
28
- Example: admin_flag Role strategy - generate migrations and model files
29
-
30
- <code>$ rails g active_record:roles User admin_flag</code>
31
-
32
- Example: admin_flag Role strategy - generate migrations only
33
-
34
- <code>$ rails g active_record:roles_migration User admin_flag</code>
35
-
36
- ## Role strategies
37
-
38
- The library comes with the following role strategies built-in:
39
-
40
- Single role:
41
-
42
- * admin_flag
43
- * role_string
44
- * one_role
45
-
46
- Multi role:
47
-
48
- * many_roles
49
- * roles_mask
50
-
51
- _Note_ The strategies *one_role* and *many_roles* both use a separate Role model (roles table). The others use an inline strategy with an attribute in the User model.
52
-
53
- ### Admin flag
54
-
55
- Boolean *admin_flag* on User to indicate if user role is admin or normal user
56
-
57
- ### Role string
58
-
59
- String *role_string* on User that names the role
60
-
61
- ### One role
62
-
63
- *role_id* relation to *id* of *Roles* table that contain all valid roles
64
-
65
- ### Many roles
66
-
67
- *role_id* relation to *UserRoles* table that is the join table that joins a user to multiple roles in the *Roles* tables.
68
-
69
- ### Roles mask
70
-
71
- *roles_mask* integer that as a bitmask indicating which roles out of a set of valid roles that the user has.
72
-
73
- Note: The following examples use RSpec to demonstrate usage scenarios.
74
-
75
- ## Example : admin_flag
76
-
77
- <pre>use_roles_strategy :admin_flag
78
-
79
- class User < ActiveRecord::Base
80
- include Roles::ActiveRecord
81
-
82
- strategy :admin_flag, :default
83
- valid_roles_are :admin, :guest
84
- end
85
- </pre>
86
-
87
- ## Example : role_string
88
-
89
- <pre>use_roles_strategy :role_string
90
-
91
- class User < ActiveRecord::Base
92
- include Roles::ActiveRecord
93
-
94
- strategy :role_string, :default
95
- valid_roles_are :admin, :guest
96
- end
97
- </pre>
98
-
99
- ## Example : one_role
100
-
101
- <pre>use_roles_strategy :one_role
102
- class User < ActiveRecord::Base
103
- include Roles::ActiveRecord
104
-
105
- strategy :one_role, :default
106
- role_class :role
107
-
108
- valid_roles_are :admin, :guest
109
- end
110
- </pre>
111
-
112
- ## Example : many_roles
113
-
114
- <pre>use_roles_strategy :many_roles
115
- class User < ActiveRecord::Base
116
- include Roles::ActiveRecord
117
-
118
- strategy :many_roles, :default
119
- role_class :role
120
-
121
- valid_roles_are :admin, :guest
122
- end
123
- </pre>
124
-
125
- ## Example : roles_mask
126
-
127
- <pre>use_roles_strategy :roles_mask
128
-
129
- class User < ActiveRecord::Base
130
- include Roles::ActiveRecord
131
-
132
- strategy :roles_mask, :default
133
- valid_roles_are :admin, :guest
134
- end
135
-
136
- </pre>
137
-
138
- ## Rails generators
139
-
140
- The library comes with a Rails 3 generator that lets you populate a user model with a given role strategy
141
- 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.
142
-
143
- * admin_flag
144
- * role_string
145
- * roles_mask
146
- * one_role
147
- * many_roles
148
-
149
-
150
- ### Generators
151
-
152
- * active_record:roles
153
- * active_record:roles_migration
154
-
155
- *Roles*
156
-
157
- Apply :admin_flag Role strategy to User model using default roles :admin and :guest (default)
158
-
159
- <code>$ rails g active_record:roles User --strategy admin_flag</code>
160
-
161
- Apply :admin_flag Role strategy to User model using default roles and extra role :author
162
-
163
- <code>$ rails g active_record:roles_migration User --strategy admin_flag --roles author</code>
164
-
165
- Apply :one_role Role strategy to User model without default roles, only with roles :user, :special and :editor
166
-
167
- <code>$ rails g active_record:roles_migration User --strategy one_role --roles user special editor --no-default-roles</code>
168
-
169
- *Roles Migration*
170
-
171
- Example: admin_flag Role strategy - generate migrations and model files
172
-
173
- <code>$ rails g active_record:roles_migration User --strategy admin_flag</code>
174
-
175
- Create reverse migration
176
-
177
- <code>$ rails g active_record:roles_migration User --strategy admin_flag --reverse</code>
178
-
179
- ## Note on Patches/Pull Requests
180
-
181
- * Fork the project.
182
- * Make your feature addition or bug fix.
183
- * Add tests for it. This is important so I don't break it in a
184
- future version unintentionally.
185
- * Commit, do not mess with rakefile, version, or history.
186
- (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)
187
- * Send me a pull request. Bonus points for topic branches.
188
-
189
- ## Copyright
190
-
191
- Copyright (c) 2010 Kristian Mandrup. See LICENSE for details.
data/logging.log DELETED
@@ -1,26 +0,0 @@
1
- [Rails::Assist::Logging]
2
- apply_role_strategy for : admin_flag in model User
3
- [Rails::Assist::Logging]
4
- apply_role_strategy for : one_role in model User
5
- [Rails::Assist::Logging]
6
- apply_role_strategy for : admin_flag in model User
7
- [Rails::Assist::Logging]
8
- apply_role_strategy for : admin_flag in model User
9
- [Rails::Assist::Logging]
10
- apply_role_strategy for : one_role in model User
11
- [Rails::Assist::Logging]
12
- apply_role_strategy for : admin_flag in model User
13
- [Rails::Assist::Logging]
14
- apply_role_strategy for : admin_flag in model User
15
- [Rails::Assist::Logging]
16
- apply_role_strategy for : admin_flag in model User
17
- [Rails::Assist::Logging]
18
- apply_role_strategy for : admin_flag in model User
19
- [Rails::Assist::Logging]
20
- apply_role_strategy for : admin_flag in model User
21
- [Rails::Assist::Logging]
22
- apply_role_strategy for : admin_flag in model User
23
- [Rails::Assist::Logging]
24
- apply_role_strategy for : admin_flag in model User
25
- [Rails::Assist::Logging]
26
- apply_role_strategy for : one_role in model User
@@ -1,7 +0,0 @@
1
- class User < ActiveRecord::Base
2
- include Roles::ActiveRecord
3
- strategy :one_role, :default
4
- role_class :role
5
- valid_roles_are :admin, :guest, :user
6
-
7
- end
@@ -1,2 +0,0 @@
1
- Rails.application.routes.draw do
2
- end
@@ -1,14 +0,0 @@
1
- class AddAdminFlagStrategy < ActiveRecord::Migration
2
-
3
- def self.up
4
- change_table :users do |t|
5
- t.boolean :admin_flag, :default => false
6
- end
7
- end
8
-
9
- def self.down
10
- change_table :users do |t|
11
- t.remove :admin_flag
12
- end
13
- end
14
- end
@@ -1,14 +0,0 @@
1
- class RemoveAdminFlagStrategy < ActiveRecord::Migration
2
-
3
- def self.down
4
- change_table :users do |t|
5
- t.boolean :admin_flag, :default => false
6
- end
7
- end
8
-
9
- def self.up
10
- change_table :users do |t|
11
- t.remove :admin_flag
12
- end
13
- end
14
- end
@@ -1,40 +0,0 @@
1
- class AddManyRolesStrategy < ActiveRecord::Migration
2
- class << self
3
-
4
- def up
5
- create_roles
6
- create_user_roles
7
- end
8
-
9
- def down
10
- drop_roles
11
- drop_user_roles
12
- end
13
-
14
- protected
15
-
16
- def create_user_roles
17
- create_table :user_roles do |t|
18
- t.integer :user_id
19
- t.integer :role_id
20
- t.timestamps
21
- end
22
- end
23
-
24
- def drop_user_roles
25
- drop_table :user_roles
26
- end
27
-
28
-
29
- def create_roles
30
- create_table :roles do |t|
31
- t.string :name
32
- t.timestamps
33
- end
34
- end
35
-
36
- def drop_roles
37
- drop_table :roles
38
- end
39
- end
40
- end
@@ -1,40 +0,0 @@
1
- class AddOneRoleStrategy < ActiveRecord::Migration
2
- class << self
3
-
4
- def up
5
- create_roles
6
- add_user_role
7
- end
8
-
9
- def down
10
- drop_roles
11
- remove_user_role
12
- end
13
-
14
- protected
15
-
16
- def add_user_role
17
- change_table :users do |t|
18
- t.integer :role_id
19
- end
20
- end
21
-
22
- def remove_user_role
23
- change_table :users do |t|
24
- t.remove :role_id
25
- end
26
- end
27
-
28
-
29
- def create_roles
30
- create_table :roles do |t|
31
- t.string :name
32
- t.timestamps
33
- end
34
- end
35
-
36
- def drop_roles
37
- drop_table :roles
38
- end
39
- end
40
- end
@@ -1,13 +0,0 @@
1
- class AddRoleStringStrategy < ActiveRecord::Migration
2
- def self.up
3
- change_table :users do |t|
4
- t.string :role, :default => 'guest'
5
- end
6
- end
7
-
8
- def self.down
9
- change_table :users do |t|
10
- t.remove :role
11
- end
12
- end
13
- end
@@ -1,13 +0,0 @@
1
- class AddRolesMaskStrategy < ActiveRecord::Migration
2
- def self.up
3
- change_table :users do |t|
4
- t.integer :roles_mask, :default => 1
5
- end
6
- end
7
-
8
- def self.down
9
- change_table :users do |t|
10
- t.remove :roles_mask
11
- end
12
- end
13
- end