mongoid-simple-roles 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +8 -0
- data/.travis.yml +4 -0
- data/CHANGELOG.rdoc +2 -0
- data/Gemfile +4 -0
- data/LICENSE +9 -0
- data/README.md +40 -0
- data/Rakefile +9 -0
- data/VERSION +1 -0
- data/lib/mongoid-simple-roles.rb +41 -0
- data/mongoid-simple-roles.gemspec +24 -0
- data/mongoid.yml +6 -0
- data/spec/mongoid-simple-roles_spec.rb +55 -0
- data/spec/spec_helper.rb +10 -0
- metadata +108 -0
data/.gitignore
ADDED
data/.travis.yml
ADDED
data/CHANGELOG.rdoc
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,9 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2012 drefined
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
6
|
+
|
7
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
8
|
+
|
9
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,40 @@
|
|
1
|
+
# mongoid-simple-roles [![build status](https://secure.travis-ci.org/drefined/mongoid-simple-roles.png?branch=master)](http://travis-ci.org/drefined/mongoid-simple-roles)
|
2
|
+
|
3
|
+
Very basic and simple roles system for mongoid v3. HEAVILY INSPIRED by [mongoid-simple-tags](https://github.com/chebyte/mongoid-simple-tags)
|
4
|
+
|
5
|
+
## Install
|
6
|
+
|
7
|
+
Add the following to Gemfile:
|
8
|
+
|
9
|
+
gem "mongoid-simple-roles", "0.0.1"
|
10
|
+
|
11
|
+
## Usage
|
12
|
+
|
13
|
+
### Model
|
14
|
+
|
15
|
+
```ruby
|
16
|
+
class User
|
17
|
+
include Mongoid::Document
|
18
|
+
include Mongoid::Document::Roleable
|
19
|
+
end
|
20
|
+
```
|
21
|
+
|
22
|
+
### Console
|
23
|
+
|
24
|
+
```ruby
|
25
|
+
u = User.create(:name => "Drefined", with_roles: ['superadmin', 'admin', 'user'])
|
26
|
+
|
27
|
+
u.roles # => ['superadmin', 'admin', 'user']
|
28
|
+
|
29
|
+
User.find_roles('superadmin') # => u
|
30
|
+
|
31
|
+
u2 = User.new(:name => "Quicksorter")
|
32
|
+
u2.add_role = 'admin'
|
33
|
+
u2.save
|
34
|
+
|
35
|
+
User.find_roles('admin') # => [u, u2]
|
36
|
+
```
|
37
|
+
|
38
|
+
## Questions or Problems?
|
39
|
+
|
40
|
+
If you have any issue or feature request with/for rolify, please add an [issue on GitHub](https://github.com/drefined/mongoid-simple-roles/issues) or fork the project and send a pull request.
|
data/Rakefile
ADDED
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.0.1
|
@@ -0,0 +1,41 @@
|
|
1
|
+
module Mongoid
|
2
|
+
module Document
|
3
|
+
module Roleable
|
4
|
+
def self.included(base)
|
5
|
+
base.class_eval do |klass|
|
6
|
+
klass.field :roles, :type => Array
|
7
|
+
klass.index({ roles: 1 }, { background: true })
|
8
|
+
|
9
|
+
include InstanceMethods
|
10
|
+
extend ClassMethods
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
module InstanceMethods
|
15
|
+
def with_roles=(roles)
|
16
|
+
self.roles = roles if roles.is_a? Array
|
17
|
+
end
|
18
|
+
|
19
|
+
def add_role(role)
|
20
|
+
self.roles = Array.new if self.roles.nil?
|
21
|
+
self.roles << role if role
|
22
|
+
end
|
23
|
+
|
24
|
+
def has_role?(role)
|
25
|
+
self.roles.include?(role)
|
26
|
+
end
|
27
|
+
|
28
|
+
def remove_role(role)
|
29
|
+
self.roles.delete_if { |r| r.casecmp(role) == 0 }
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
module ClassMethods
|
34
|
+
def find_roles(roles)
|
35
|
+
roles = [roles] unless roles.is_a? Array
|
36
|
+
criteria.in(:roles => roles).to_a
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
|
3
|
+
Gem::Specification.new do |s|
|
4
|
+
s.name = "mongoid-simple-roles"
|
5
|
+
s.version = "0.0.1"
|
6
|
+
s.platform = Gem::Platform::RUBY
|
7
|
+
s.authors = ["drefined"]
|
8
|
+
s.email = ["d.refined@gmail.com"]
|
9
|
+
s.homepage = "https://github.com/drefined/mongoid-simple-roles"
|
10
|
+
s.summary = %q{mongoid simple roles for sinatra}
|
11
|
+
s.description = %q{basic and simple roles system for mongoid v3}
|
12
|
+
|
13
|
+
s.rubyforge_project = s.name
|
14
|
+
|
15
|
+
s.files = `git ls-files`.split("\n")
|
16
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
17
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
18
|
+
s.require_paths = ["lib"]
|
19
|
+
|
20
|
+
s.add_dependency "mongoid", "~> 3.0.3"
|
21
|
+
s.add_dependency "bson_ext", "~> 1.6"
|
22
|
+
|
23
|
+
s.add_development_dependency "rspec", "~> 2.10.0"
|
24
|
+
end
|
data/mongoid.yml
ADDED
@@ -0,0 +1,55 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
class User
|
4
|
+
include Mongoid::Document
|
5
|
+
include Mongoid::Document::Roleable
|
6
|
+
field :name
|
7
|
+
end
|
8
|
+
|
9
|
+
describe "A Roleable model" do
|
10
|
+
let(:user) { User.new(name: 'Drefined') }
|
11
|
+
|
12
|
+
it "should have a add_role method" do
|
13
|
+
user.should respond_to :add_role
|
14
|
+
end
|
15
|
+
|
16
|
+
it "should have a has_role? method" do
|
17
|
+
user.should respond_to :has_role?
|
18
|
+
end
|
19
|
+
|
20
|
+
it "should have a remove_role method" do
|
21
|
+
user.should respond_to :remove_role
|
22
|
+
end
|
23
|
+
|
24
|
+
it "should be able to add a role" do
|
25
|
+
role = "superadmin"
|
26
|
+
user.add_role(role)
|
27
|
+
user.has_role?(role).should be true
|
28
|
+
end
|
29
|
+
|
30
|
+
it "should be able to remove a role" do
|
31
|
+
role = "superadmin"
|
32
|
+
user.add_role(role)
|
33
|
+
user.remove_role(role)
|
34
|
+
user.has_role?(role).should_not be true
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
describe "A Roleable model with roles assigned" do
|
39
|
+
before(:each) do
|
40
|
+
@user = User.create(name: 'Drefined', with_roles: ['superadmin', 'admin', 'user'])
|
41
|
+
end
|
42
|
+
|
43
|
+
it "should be able to find_roles" do
|
44
|
+
User.find_roles('superadmin').first.should eq(@user)
|
45
|
+
User.find_roles(['superadmin', 'admin']).first.should eq(@user)
|
46
|
+
end
|
47
|
+
|
48
|
+
it "should be able to find_roles objects if more than one object is present" do
|
49
|
+
user_2 = User.create!(name: 'Quicksorter', roles: 'admin')
|
50
|
+
|
51
|
+
users_with_roles = User.find_roles('admin')
|
52
|
+
users_with_roles.include?(@user).should be true
|
53
|
+
users_with_roles.include?(user_2).should be true
|
54
|
+
end
|
55
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,108 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: mongoid-simple-roles
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- drefined
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-08-14 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: mongoid
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ~>
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: 3.0.3
|
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.3
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: bson_ext
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ~>
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '1.6'
|
38
|
+
type: :runtime
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ~>
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '1.6'
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: rspec
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ~>
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: 2.10.0
|
54
|
+
type: :development
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ~>
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: 2.10.0
|
62
|
+
description: basic and simple roles system for mongoid v3
|
63
|
+
email:
|
64
|
+
- d.refined@gmail.com
|
65
|
+
executables: []
|
66
|
+
extensions: []
|
67
|
+
extra_rdoc_files: []
|
68
|
+
files:
|
69
|
+
- .gitignore
|
70
|
+
- .travis.yml
|
71
|
+
- CHANGELOG.rdoc
|
72
|
+
- Gemfile
|
73
|
+
- LICENSE
|
74
|
+
- README.md
|
75
|
+
- Rakefile
|
76
|
+
- VERSION
|
77
|
+
- lib/mongoid-simple-roles.rb
|
78
|
+
- mongoid-simple-roles.gemspec
|
79
|
+
- mongoid.yml
|
80
|
+
- spec/mongoid-simple-roles_spec.rb
|
81
|
+
- spec/spec_helper.rb
|
82
|
+
homepage: https://github.com/drefined/mongoid-simple-roles
|
83
|
+
licenses: []
|
84
|
+
post_install_message:
|
85
|
+
rdoc_options: []
|
86
|
+
require_paths:
|
87
|
+
- lib
|
88
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
89
|
+
none: false
|
90
|
+
requirements:
|
91
|
+
- - ! '>='
|
92
|
+
- !ruby/object:Gem::Version
|
93
|
+
version: '0'
|
94
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
95
|
+
none: false
|
96
|
+
requirements:
|
97
|
+
- - ! '>='
|
98
|
+
- !ruby/object:Gem::Version
|
99
|
+
version: '0'
|
100
|
+
requirements: []
|
101
|
+
rubyforge_project: mongoid-simple-roles
|
102
|
+
rubygems_version: 1.8.24
|
103
|
+
signing_key:
|
104
|
+
specification_version: 3
|
105
|
+
summary: mongoid simple roles for sinatra
|
106
|
+
test_files:
|
107
|
+
- spec/mongoid-simple-roles_spec.rb
|
108
|
+
- spec/spec_helper.rb
|