bitroles 0.0.4 → 0.1.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.
- data/README.md +16 -8
- data/bitroles.gemspec +0 -1
- data/lib/bitroles/version.rb +1 -1
- data/lib/bitroles.rb +41 -33
- metadata +2 -18
data/README.md
CHANGED
@@ -20,11 +20,7 @@ Or install it yourself as:
|
|
20
20
|
|
21
21
|
```ruby
|
22
22
|
class MyModel < ActiveRecord::Base
|
23
|
-
|
24
|
-
include Bitroles
|
25
|
-
|
26
|
-
roles :admin, :moderator
|
27
|
-
|
23
|
+
has_roles :admin, :moderator
|
28
24
|
end
|
29
25
|
```
|
30
26
|
By default bitroles uses roles_mask integer column in your table to store the roles. You can change column name pretty
|
@@ -32,12 +28,24 @@ easy:
|
|
32
28
|
|
33
29
|
```ruby
|
34
30
|
class MyModel < ActiveRecord::Base
|
31
|
+
has_roles :admin, :moderator, mask_column: :someothercolumn
|
32
|
+
end
|
33
|
+
```
|
34
|
+
## Available class methods
|
35
|
+
```ruby
|
36
|
+
MyModel.with_role(:admin) # Scope that finds all MyModel objects with admin role
|
37
|
+
```
|
35
38
|
|
36
|
-
|
39
|
+
## Available instance methods
|
37
40
|
|
38
|
-
|
41
|
+
```ruby
|
42
|
+
has_role? :admin # Checks if the object has admin role
|
43
|
+
is_admin? # Checks the above. It can be is_moderator? or is_whatever? depends on roles given to has_role
|
44
|
+
roles # Shows all roles of object (strings array)
|
45
|
+
obj.roles = [:admin, :moderator] # Sets admin and moderator role to obj
|
46
|
+
obj.admin = true # obj became admin (same for other roles)
|
47
|
+
obj.admin = false # obj is no longer an admin (same for other roles)
|
39
48
|
|
40
|
-
end
|
41
49
|
```
|
42
50
|
|
43
51
|
## Contributing
|
data/bitroles.gemspec
CHANGED
data/lib/bitroles/version.rb
CHANGED
data/lib/bitroles.rb
CHANGED
@@ -1,49 +1,57 @@
|
|
1
1
|
require "bitroles/version"
|
2
2
|
|
3
3
|
module Bitroles
|
4
|
-
|
5
|
-
|
6
|
-
included do
|
7
|
-
scope :with_role, -> role { where("#{mask_column_name} & #{2**roles.index(role.to_s)} > 0") }
|
4
|
+
def self.included(base)
|
5
|
+
base.extend(ClassMethods)
|
8
6
|
end
|
9
7
|
|
10
8
|
module ClassMethods
|
11
|
-
def
|
9
|
+
def has_roles(*args)
|
12
10
|
if args.any?
|
11
|
+
roles_mask = 'roles_mask'
|
13
12
|
if args.last.is_a?(Hash)
|
14
13
|
options = args.pop if args.last.is_a?(Hash)
|
15
|
-
|
14
|
+
roles_mask = options[:mask_column].to_s
|
15
|
+
end
|
16
|
+
roles = args.map(&:to_s)
|
17
|
+
|
18
|
+
roles.each do |role|
|
19
|
+
class_eval <<-EVAL, __FILE__, __LINE__
|
20
|
+
scope :with_role, -> role { where(["#{roles_mask} & ? > 0", 2**#{roles}.index(role.to_s)]) }
|
21
|
+
|
22
|
+
def is_#{role}?
|
23
|
+
role? role
|
24
|
+
end
|
25
|
+
|
26
|
+
def #{role}=(val)
|
27
|
+
if val
|
28
|
+
self.roles += [#{role}] unless has_role? #{role}
|
29
|
+
else
|
30
|
+
self.roles -= [#{role}] if has_role? #{role}
|
31
|
+
end
|
32
|
+
end
|
33
|
+
EVAL
|
16
34
|
end
|
17
|
-
@@roles = args.map(&:to_s)
|
18
|
-
else
|
19
|
-
@@roles
|
20
|
-
end
|
21
|
-
end
|
22
|
-
|
23
|
-
def mask_column_name
|
24
|
-
defined?(@@mask_column_name) ? @@mask_column_name : 'roles_mask'
|
25
|
-
end
|
26
|
-
end
|
27
35
|
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
super
|
34
|
-
end
|
35
|
-
end
|
36
|
+
class_eval <<-EVAL, __FILE__, __LINE__
|
37
|
+
def roles=(roles)
|
38
|
+
roles = (roles.map(&:to_s) & #{roles}).map { |r| 2**#{roles}.index(r) }.sum
|
39
|
+
self.#{roles_mask} = roles
|
40
|
+
end
|
36
41
|
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
end
|
42
|
+
def roles
|
43
|
+
#{roles}.reject { |r| ((#{roles_mask} || 0) & 2**#{roles}.index(r)).zero? }
|
44
|
+
end
|
41
45
|
|
42
|
-
|
43
|
-
|
46
|
+
def has_role?(role)
|
47
|
+
roles.include? role.to_s
|
48
|
+
end
|
49
|
+
EVAL
|
50
|
+
end
|
51
|
+
end
|
44
52
|
end
|
53
|
+
end
|
45
54
|
|
46
|
-
|
47
|
-
|
48
|
-
end
|
55
|
+
class ActiveRecord::Base
|
56
|
+
include Bitroles
|
49
57
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: bitroles
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 0.1.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -11,22 +11,6 @@ bindir: bin
|
|
11
11
|
cert_chain: []
|
12
12
|
date: 2012-05-07 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
|
-
- !ruby/object:Gem::Dependency
|
15
|
-
name: activesupport
|
16
|
-
requirement: !ruby/object:Gem::Requirement
|
17
|
-
none: false
|
18
|
-
requirements:
|
19
|
-
- - ! '>='
|
20
|
-
- !ruby/object:Gem::Version
|
21
|
-
version: 3.0.0
|
22
|
-
type: :runtime
|
23
|
-
prerelease: false
|
24
|
-
version_requirements: !ruby/object:Gem::Requirement
|
25
|
-
none: false
|
26
|
-
requirements:
|
27
|
-
- - ! '>='
|
28
|
-
- !ruby/object:Gem::Version
|
29
|
-
version: 3.0.0
|
30
14
|
- !ruby/object:Gem::Dependency
|
31
15
|
name: activerecord
|
32
16
|
requirement: !ruby/object:Gem::Requirement
|
@@ -81,5 +65,5 @@ rubyforge_project:
|
|
81
65
|
rubygems_version: 1.8.24
|
82
66
|
signing_key:
|
83
67
|
specification_version: 3
|
84
|
-
summary: Bitroles gem v0.0
|
68
|
+
summary: Bitroles gem v0.1.0
|
85
69
|
test_files: []
|