easy_roles 1.2.0 → 2.0.0.beta1

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG.rdoc CHANGED
@@ -1,3 +1,7 @@
1
+ === 18 August 2011 - Version 2.0.0.beta
2
+ Rewrite the whole gem to be more modular, API is exactly the same
3
+ Only supports Rails 3.x
4
+
1
5
  === 06 September 2010 - Version 1.1.0
2
6
  Fixed the issue where the is_user? method was not working through association, if the is_user? method had not been called before
3
7
  Wrote tests (FINALLY!!!)
data/Manifest CHANGED
@@ -11,6 +11,8 @@ lib/generators/active_record/templates/migration_bitmask.rb
11
11
  lib/generators/active_record/templates/migration_non_bitmask.rb
12
12
  lib/generators/easy_roles/easy_roles_generator.rb
13
13
  lib/generators/templates/README
14
+ lib/methods/bitmask.rb
15
+ lib/methods/serialize.rb
14
16
  spec/easy_roles_spec.rb
15
17
  spec/spec_helper.rb
16
18
  Manifest
data/README.rdoc CHANGED
@@ -8,8 +8,6 @@ Please read the CHANGELOG.rdoc file.
8
8
 
9
9
  == Install
10
10
 
11
- gem install gemcutter
12
- gem tumble
13
11
  gem install easy_roles
14
12
 
15
13
  or it can be installed as a rails plugin.
@@ -20,19 +18,17 @@ or it can be installed as a rails plugin.
20
18
 
21
19
  === Serialize Method
22
20
 
23
- Add the following to your enviroment.rb in the rails initializer block
21
+ Add the following to your Gemfile:
22
+ gem 'easy_roles'
24
23
 
25
- config.gem 'easy_roles', :source => 'http://gemcutter.org'
26
-
27
- (from version 0.3.0) use the migration generator
28
-
29
- script/generate easy_roles user roles
24
+ Then generate the migration:
25
+ rails g easy_roles user roles
30
26
 
31
27
  Or add a "roles" column to your users model, and set the default value to "--- []". Please note you can call this column anything you like, I like to use the name "roles".
32
28
 
33
29
  t.string :roles, :default => "--- []"
34
30
 
35
- Then you need to add "easy_roles :column_name" to your model.
31
+ Then you need to add "easy_roles :column_name" to your model:
36
32
 
37
33
  class User < ActiveRecord::Base
38
34
  easy_roles :roles
@@ -40,17 +36,15 @@ Then you need to add "easy_roles :column_name" to your model.
40
36
 
41
37
  === Bitmask Method
42
38
 
43
- ==== WARNING: If you plan on using the column name 'roles' for the bitmask method your getter and setter actions will be 'easy_roles' and 'easy_roles=', instead of 'roles' and 'roles='. I would suggest using 'roles_mask' as your roles column for this method.
44
-
45
- Add the following to your enviroment.rb in the rails initializer block
46
-
47
- config.gem 'easy_roles', :source => 'http://gemcutter.org'
39
+ Add the following to your Gemfile:
48
40
 
49
- (from version 0.4.1) use the migration generator to generate the roles column for your users table
41
+ gem 'easy_roles'
50
42
 
51
- E.G. script/generate easy_bitmask_roles user roles_mask
43
+ Then generate the migration:
44
+
45
+ rails g easy_bitmask_roles user roles
52
46
 
53
- Or add a "roles_mask" column to your users model of type 'integer', and set the default value to 0. Please note you can call this column anything you like, I like to use the name "roles_mask".
47
+ Or add a "roles_mask" column to your users model of type 'integer', and set the default value to 0. Please note you can call this column anything you like, I like to use the name "roles_mask":
54
48
 
55
49
  t.integer :roles_mask, :default => 0
56
50
 
@@ -142,16 +136,13 @@ Then in your AdminsController or any controller that you only want admins to vie
142
136
  before_filter :admin_required, :only => :create, :update
143
137
  end
144
138
 
145
- Check out http://blog.platform45.com/2009/10/05/howto-basic-roles-for-users for implementation, and http://blog.platform45.com/2009/10/07/easy-roles-gem-released for usage.
146
-
147
139
  Follow me on twitter: http://twitter.com/ryan_za
148
140
 
149
141
  Email: ryan *at* platform45.com
150
142
 
151
-
152
143
  == License
153
144
 
154
- Copyright (c) 2009 Platform45
145
+ Copyright (c) 2011 Platform45
155
146
 
156
147
  Permission is hereby granted, free of charge, to any person obtaining
157
148
  a copy of this software and associated documentation files (the
data/Rakefile CHANGED
@@ -2,11 +2,11 @@ require 'rubygems'
2
2
  require 'rake'
3
3
  require 'echoe'
4
4
 
5
- Echoe.new('easy_roles', '1.2.0') do |p|
5
+ Echoe.new('easy_roles', '2.0.0.beta1') do |p|
6
6
  p.description = "Easy role authorization in rails"
7
7
  p.url = "http://github.com/platform45/easy_roles"
8
8
  p.author = "Platform45"
9
9
  p.email = "ryan@platform45.com"
10
10
  p.ignore_pattern = ["tmp/*", "script/*"]
11
11
  p.development_dependencies = ["rspec", "activerecord", "sqlite3"]
12
- end
12
+ end
data/easy_roles.gemspec CHANGED
@@ -2,15 +2,15 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = %q{easy_roles}
5
- s.version = "1.2.0"
5
+ s.version = "2.0.0.beta1"
6
6
 
7
7
  s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
8
8
  s.authors = [%q{Platform45}]
9
9
  s.date = %q{2011-08-18}
10
10
  s.description = %q{Easy role authorization in rails}
11
11
  s.email = %q{ryan@platform45.com}
12
- s.extra_rdoc_files = [%q{CHANGELOG.rdoc}, %q{README.rdoc}, %q{lib/easy_roles.rb}, %q{lib/generators/active_record/easy_roles_generator.rb}, %q{lib/generators/active_record/templates/migration_bitmask.rb}, %q{lib/generators/active_record/templates/migration_non_bitmask.rb}, %q{lib/generators/easy_roles/easy_roles_generator.rb}, %q{lib/generators/templates/README}]
13
- s.files = [%q{CHANGELOG.rdoc}, %q{Gemfile}, %q{Gemfile.lock}, %q{README.rdoc}, %q{Rakefile}, %q{easy_roles.gemspec}, %q{init.rb}, %q{lib/easy_roles.rb}, %q{lib/generators/active_record/easy_roles_generator.rb}, %q{lib/generators/active_record/templates/migration_bitmask.rb}, %q{lib/generators/active_record/templates/migration_non_bitmask.rb}, %q{lib/generators/easy_roles/easy_roles_generator.rb}, %q{lib/generators/templates/README}, %q{spec/easy_roles_spec.rb}, %q{spec/spec_helper.rb}, %q{Manifest}]
12
+ s.extra_rdoc_files = [%q{CHANGELOG.rdoc}, %q{README.rdoc}, %q{lib/easy_roles.rb}, %q{lib/generators/active_record/easy_roles_generator.rb}, %q{lib/generators/active_record/templates/migration_bitmask.rb}, %q{lib/generators/active_record/templates/migration_non_bitmask.rb}, %q{lib/generators/easy_roles/easy_roles_generator.rb}, %q{lib/generators/templates/README}, %q{lib/methods/bitmask.rb}, %q{lib/methods/serialize.rb}]
13
+ s.files = [%q{CHANGELOG.rdoc}, %q{Gemfile}, %q{Gemfile.lock}, %q{README.rdoc}, %q{Rakefile}, %q{easy_roles.gemspec}, %q{init.rb}, %q{lib/easy_roles.rb}, %q{lib/generators/active_record/easy_roles_generator.rb}, %q{lib/generators/active_record/templates/migration_bitmask.rb}, %q{lib/generators/active_record/templates/migration_non_bitmask.rb}, %q{lib/generators/easy_roles/easy_roles_generator.rb}, %q{lib/generators/templates/README}, %q{lib/methods/bitmask.rb}, %q{lib/methods/serialize.rb}, %q{spec/easy_roles_spec.rb}, %q{spec/spec_helper.rb}, %q{Manifest}]
14
14
  s.homepage = %q{http://github.com/platform45/easy_roles}
15
15
  s.rdoc_options = [%q{--line-numbers}, %q{--inline-source}, %q{--title}, %q{Easy_roles}, %q{--main}, %q{README.rdoc}]
16
16
  s.require_paths = [%q{lib}]
data/lib/easy_roles.rb CHANGED
@@ -1,107 +1,31 @@
1
+ require 'active_support'
2
+
1
3
  module EasyRoles
2
- def self.included(base)
3
- base.extend ClassMethods
4
+ extend ActiveSupport::Concern
5
+
6
+ included do |base|
4
7
  base.send :alias_method_chain, :method_missing, :roles
5
8
  base.send :alias_method_chain, :respond_to?, :roles
6
9
  end
10
+
11
+ ALLOWED_METHODS = [:serialize, :bitmask]
12
+
13
+ ALLOWED_METHODS.each do |method|
14
+ autoload method.to_s.capitalize.to_sym, "methods/#{method}"
15
+ end
7
16
 
8
17
  module ClassMethods
9
18
  def easy_roles(name, options = {})
10
-
11
19
  options[:method] ||= :serialize
12
-
13
- if options[:method] == :serialize
14
- serialize name.to_sym, Array
15
-
16
- ActiveSupport::Deprecation.silence do
17
- respond_to?(:before_validation_on_create) ? before_validation_on_create(:make_default_roles) : before_validation(:make_default_roles, :on => :create)
18
- end
19
-
20
- class_eval <<-EOC
21
- def has_role?(role)
22
- #{name}.include?(role)
23
- end
24
-
25
- def add_role(role)
26
- clear_roles if self.#{name}.nil?
27
- has_role?(role) ? false : self.#{name} << role
28
- end
20
+
21
+ begin
22
+ raise NameError unless ALLOWED_METHODS.include? options[:method]
29
23
 
30
- def add_role!(role)
31
- add_role(role)
32
- self.save!
33
- end
34
-
35
- def remove_role(role)
36
- self.#{name}.delete(role)
37
- end
38
-
39
- def remove_role!(role)
40
- remove_role(role)
41
- self.save!
42
- end
43
-
44
- def clear_roles
45
- self.#{name} = []
46
- end
47
-
48
- def make_default_roles
49
- clear_roles if #{name}.nil?
50
- end
24
+ "EasyRoles::#{options[:method].to_s.camelize}".constantize.new(self, name, options)
25
+ rescue NameError
26
+ puts "[Easy Roles] Storage method does not exist reverting to Serialize"
51
27
 
52
- private :make_default_roles
53
- EOC
54
- elsif options[:method] == :bitmask
55
-
56
- def_name = (name == :roles) ? :easy_roles : :roles
57
-
58
- class_eval <<-EOC
59
- def self.list_roles
60
- #{name.to_s.upcase}
61
- end
62
-
63
- def #{def_name}=(roles)
64
- self.#{name} = (roles & #{name.to_s.upcase}).map { |r| 2**#{name.to_s.upcase}.index(r) }.sum
65
- end
66
-
67
- def #{def_name}
68
- #{name.to_s.upcase}.reject { |r| ((#{name} || 0) & 2**#{name.to_s.upcase}.index(r)).zero? }
69
- end
70
-
71
- def has_role?(role)
72
- #{def_name}.include?(role)
73
- end
74
-
75
- def add_role(role)
76
- new_roles = #{def_name}.push(role).uniq
77
- self.#{def_name} = new_roles
78
- end
79
-
80
- def add_role!(role)
81
- add_role(role)
82
- self.save!
83
- end
84
-
85
- def remove_role(role)
86
- new_roles = #{def_name}
87
- new_roles.delete(role)
88
- self.#{def_name} = new_roles
89
- end
90
-
91
- def remove_role!(role)
92
- remove_role(role)
93
- self.save!
94
- end
95
-
96
- def clear_roles
97
- self.#{name} = 0
98
- end
99
-
100
- def clear_roles!
101
- self.#{name} = 0
102
- self.save!
103
- end
104
- EOC
28
+ EasyRoles::Serialize.new(self, name, options)
105
29
  end
106
30
  end
107
31
  end
@@ -0,0 +1,55 @@
1
+ module EasyRoles
2
+ class Bitmask
3
+ def initialize(base, column_name, options)
4
+ base.send :define_method, :_roles= do |roles|
5
+ states = base.const_get(column_name.upcase.to_sym)
6
+
7
+ self[column_name.to_sym] = (roles & states).map { |r| 2**states.index(r) }.sum
8
+ end
9
+
10
+ base.send :define_method, :_roles do
11
+ states = base.const_get(column_name.upcase.to_sym)
12
+
13
+ states.reject { |r| ((self[column_name.to_sym] || 0) & 2**states.index(r)).zero? }
14
+ end
15
+
16
+ base.send :define_method, :has_role? do |role|
17
+ self._roles.inspect
18
+
19
+ self._roles.include?(role)
20
+ end
21
+
22
+ base.send :define_method, :add_role do |role|
23
+ new_roles = self._roles.push(role).uniq
24
+ self._roles = new_roles
25
+ end
26
+
27
+ base.send :define_method, :add_role! do |role|
28
+ add_role(role)
29
+ self.save!
30
+ end
31
+
32
+ base.send :define_method, :remove_role do |role|
33
+ new_roles = self._roles
34
+ new_roles.delete(role)
35
+
36
+ self._roles = new_roles
37
+ end
38
+
39
+ base.send :define_method, :remove_role! do |role|
40
+ remove_role(role)
41
+ self.save!
42
+ end
43
+
44
+ base.send :define_method, :clear_roles do
45
+ self[column_name.to_sym] = 0
46
+ end
47
+
48
+ base.send :define_method, :clear_roles! do
49
+ self.clear_roles
50
+
51
+ self.save!
52
+ end
53
+ end
54
+ end
55
+ end
@@ -0,0 +1,45 @@
1
+ module EasyRoles
2
+ class Serialize
3
+ def initialize(base, column_name, options)
4
+ base.serialize column_name.to_sym, Array
5
+
6
+ ActiveSupport::Deprecation.silence do
7
+ base.before_validation(:make_default_roles, :on => :create)
8
+ end
9
+
10
+ base.send :define_method, :has_role? do |role|
11
+ self[column_name.to_sym].include?(role)
12
+ end
13
+
14
+ base.send :define_method, :add_role do |role|
15
+ clear_roles if self[column_name.to_sym].blank?
16
+
17
+ has_role?(role) ? false : self[column_name.to_sym] << role
18
+ end
19
+
20
+ base.send :define_method, :add_role! do |role|
21
+ add_role(role)
22
+ self.save!
23
+ end
24
+
25
+ base.send :define_method, :remove_role do |role|
26
+ self[column_name.to_sym].delete(role)
27
+ end
28
+
29
+ base.send :define_method, :remove_role! do |role|
30
+ remove_role(role)
31
+ self.save!
32
+ end
33
+
34
+ base.send :define_method, :clear_roles do
35
+ self[column_name.to_sym] = []
36
+ end
37
+
38
+ base.send :define_method, :make_default_roles do
39
+ clear_roles if self[column_name.to_sym].blank?
40
+ end
41
+
42
+ base.send :private, :make_default_roles
43
+ end
44
+ end
45
+ end
@@ -174,7 +174,7 @@ describe EasyRoles do
174
174
  it "should allow me to set a users role" do
175
175
  user = BitmaskUser.new
176
176
  user.add_role 'admin'
177
- user.roles.include?("admin").should be_true
177
+ user._roles.include?("admin").should be_true
178
178
  end
179
179
 
180
180
  it "should return true for is_admin? if the admin role is added to the user" do
metadata CHANGED
@@ -1,8 +1,8 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: easy_roles
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.0
5
- prerelease:
4
+ version: 2.0.0.beta1
5
+ prerelease: 6
6
6
  platform: ruby
7
7
  authors:
8
8
  - Platform45
@@ -13,7 +13,7 @@ date: 2011-08-18 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rspec
16
- requirement: &2153550700 !ruby/object:Gem::Requirement
16
+ requirement: &2153037320 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: '0'
22
22
  type: :development
23
23
  prerelease: false
24
- version_requirements: *2153550700
24
+ version_requirements: *2153037320
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: activerecord
27
- requirement: &2153550220 !ruby/object:Gem::Requirement
27
+ requirement: &2153036840 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: '0'
33
33
  type: :development
34
34
  prerelease: false
35
- version_requirements: *2153550220
35
+ version_requirements: *2153036840
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: sqlite3
38
- requirement: &2153549740 !ruby/object:Gem::Requirement
38
+ requirement: &2153036360 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ! '>='
@@ -43,7 +43,7 @@ dependencies:
43
43
  version: '0'
44
44
  type: :development
45
45
  prerelease: false
46
- version_requirements: *2153549740
46
+ version_requirements: *2153036360
47
47
  description: Easy role authorization in rails
48
48
  email: ryan@platform45.com
49
49
  executables: []
@@ -57,6 +57,8 @@ extra_rdoc_files:
57
57
  - lib/generators/active_record/templates/migration_non_bitmask.rb
58
58
  - lib/generators/easy_roles/easy_roles_generator.rb
59
59
  - lib/generators/templates/README
60
+ - lib/methods/bitmask.rb
61
+ - lib/methods/serialize.rb
60
62
  files:
61
63
  - CHANGELOG.rdoc
62
64
  - Gemfile
@@ -71,6 +73,8 @@ files:
71
73
  - lib/generators/active_record/templates/migration_non_bitmask.rb
72
74
  - lib/generators/easy_roles/easy_roles_generator.rb
73
75
  - lib/generators/templates/README
76
+ - lib/methods/bitmask.rb
77
+ - lib/methods/serialize.rb
74
78
  - spec/easy_roles_spec.rb
75
79
  - spec/spec_helper.rb
76
80
  - Manifest