groupify 0.1.1 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +49 -16
- data/groupify.gemspec +1 -0
- data/lib/groupify/version.rb +1 -1
- data/lib/groupify.rb +75 -10
- data/spec/groupify/mongoid_spec.rb +37 -0
- data/spec/spec_helper.rb +18 -2
- metadata +117 -74
data/README.md
CHANGED
@@ -20,28 +20,61 @@ Or install it yourself as:
|
|
20
20
|
## Getting Started
|
21
21
|
In your group model:
|
22
22
|
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
23
|
+
```ruby
|
24
|
+
class Group
|
25
|
+
include Mongoid::Document
|
26
|
+
|
27
|
+
acts_as_group
|
28
|
+
end
|
29
|
+
```
|
28
30
|
|
29
31
|
In your user model:
|
30
32
|
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
33
|
+
```ruby
|
34
|
+
class User
|
35
|
+
include Mongoid::Document
|
36
|
+
|
37
|
+
acts_as_group_member
|
38
|
+
end
|
39
|
+
```
|
40
|
+
|
41
|
+
## Usage
|
36
42
|
|
37
43
|
Create groups and add members:
|
38
44
|
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
+
```ruby
|
46
|
+
group = Group.new
|
47
|
+
user = User.new
|
48
|
+
|
49
|
+
user.groups << group
|
50
|
+
or
|
51
|
+
group.add user
|
52
|
+
|
53
|
+
user.in_group?(group) => true
|
54
|
+
```
|
55
|
+
|
56
|
+
Add to named groups:
|
57
|
+
|
58
|
+
```ruby
|
59
|
+
user.groups << :admin
|
60
|
+
user.in_group?(:admin) => true
|
61
|
+
```
|
62
|
+
|
63
|
+
Check if two group members share any of the same groups:
|
64
|
+
|
65
|
+
```ruby
|
66
|
+
user1.shares_any_group?(user2)
|
67
|
+
```
|
68
|
+
|
69
|
+
Query for groups & members:
|
70
|
+
|
71
|
+
```ruby
|
72
|
+
User.in_group(group) # Find all users in this group
|
73
|
+
Group.with_member(user) # Find all groups with this user
|
74
|
+
|
75
|
+
User.shares_any_group(user) # Find all users that share any groups with this user
|
76
|
+
User.shares_any_named_group(user) # Find all users that share any named groups with this user
|
77
|
+
```
|
45
78
|
|
46
79
|
Check the specs for more details.
|
47
80
|
|
data/groupify.gemspec
CHANGED
data/lib/groupify/version.rb
CHANGED
data/lib/groupify.rb
CHANGED
@@ -1,4 +1,3 @@
|
|
1
|
-
require "groupify/version"
|
2
1
|
require 'active_support'
|
3
2
|
require 'mongoid'
|
4
3
|
|
@@ -79,14 +78,45 @@ module Groupify
|
|
79
78
|
|
80
79
|
included do
|
81
80
|
group_class_name='Group' unless defined?(@group_class_name)
|
82
|
-
|
81
|
+
field :named_groups, :type => Array
|
82
|
+
has_and_belongs_to_many :groups, :autosave => true, :inverse_of => nil, :class_name => @group_class_name do
|
83
|
+
def <<(group)
|
84
|
+
case group
|
85
|
+
when self.metadata.klass
|
86
|
+
super
|
87
|
+
else
|
88
|
+
(self.base.named_groups ||= []) << group.to_s
|
89
|
+
self.base.save if self.metadata.autosave
|
90
|
+
end
|
91
|
+
end
|
92
|
+
end
|
93
|
+
end
|
94
|
+
|
95
|
+
def in_group?(group)
|
96
|
+
case group
|
97
|
+
when groups.metadata.klass
|
98
|
+
self.groups.include?(group)
|
99
|
+
else
|
100
|
+
self.named_groups ? self.named_groups.include?(group.to_s) : false
|
101
|
+
end
|
102
|
+
end
|
103
|
+
|
104
|
+
def in_any_group?(*groups)
|
105
|
+
groups.flatten.each do |group|
|
106
|
+
return true if in_group?(group)
|
107
|
+
end
|
108
|
+
return false
|
109
|
+
end
|
110
|
+
|
111
|
+
def in_all_groups?(*groups)
|
112
|
+
groups.flatten.each do |group|
|
113
|
+
return false unless in_group?(group)
|
114
|
+
end
|
115
|
+
return true
|
83
116
|
end
|
84
117
|
|
85
|
-
def in_group?(group); self.groups.include? group; end
|
86
|
-
def in_any_group?(*groups); (self.groups & groups.flatten).present?; end
|
87
|
-
def in_all_groups?(*groups); self.groups == groups.flatten; end
|
88
118
|
def shares_any_group?(other)
|
89
|
-
in_any_group?(other.groups)
|
119
|
+
in_any_group?(other.groups + (other.named_groups || []))
|
90
120
|
end
|
91
121
|
|
92
122
|
module ClassMethods
|
@@ -94,10 +124,45 @@ module Groupify
|
|
94
124
|
def group_class_name=(klass); @group_class_name = klass; end
|
95
125
|
|
96
126
|
def none; where(:id => nil); end
|
97
|
-
|
98
|
-
def
|
99
|
-
|
100
|
-
|
127
|
+
|
128
|
+
def in_group(group)
|
129
|
+
return none unless group.present?
|
130
|
+
case group
|
131
|
+
when self.reflect_on_association(:groups).klass
|
132
|
+
where(:group_ids.in => [group.id])
|
133
|
+
else
|
134
|
+
in_named_group(group)
|
135
|
+
end
|
136
|
+
end
|
137
|
+
|
138
|
+
def in_named_group(named_group)
|
139
|
+
named_group.present? ? where(:named_groups.in => [named_group.to_s]) : none
|
140
|
+
end
|
141
|
+
|
142
|
+
def in_any_group(*groups)
|
143
|
+
groups.present? ? where(:group_ids.in => groups.flatten.map{|g|g.id}) : none
|
144
|
+
end
|
145
|
+
|
146
|
+
def in_any_named_group(*named_groups)
|
147
|
+
named_groups.present? ? where(:named_groups.in => named_groups.flatten.map{|g|g.to_s}) : none
|
148
|
+
end
|
149
|
+
|
150
|
+
def in_all_groups(*groups)
|
151
|
+
groups.present? ? where(:group_ids => groups.flatten.map{|g|g.id}) : none
|
152
|
+
end
|
153
|
+
|
154
|
+
def in_all_named_groups(*named_groups)
|
155
|
+
named_groups.present? ? where(:named_groups => named_groups.flatten.map{|g|g.to_s}) : none
|
156
|
+
end
|
157
|
+
|
158
|
+
def shares_any_group(other)
|
159
|
+
in_any_group(other.groups)
|
160
|
+
end
|
161
|
+
|
162
|
+
def shares_any_named_group(other)
|
163
|
+
in_any_named_group(other.named_groups)
|
164
|
+
end
|
165
|
+
|
101
166
|
end
|
102
167
|
end
|
103
168
|
end
|
@@ -22,6 +22,11 @@ class MongoidGroup
|
|
22
22
|
alias_method :users, :mongoid_users
|
23
23
|
end
|
24
24
|
|
25
|
+
class MongoidProject < MongoidGroup
|
26
|
+
|
27
|
+
alias_method :tasks, :mongoid_tasks
|
28
|
+
end
|
29
|
+
|
25
30
|
describe MongoidGroup do
|
26
31
|
it { should respond_to :members}
|
27
32
|
it { should respond_to :add }
|
@@ -82,4 +87,36 @@ describe "MongoidUser" do
|
|
82
87
|
MongoidUser.in_all_groups(group, group2).first.should eql(user)
|
83
88
|
MongoidUser.in_all_groups([group, group2]).first.should eql(user)
|
84
89
|
end
|
90
|
+
|
91
|
+
it "can have named groups" do
|
92
|
+
user.groups << :admin
|
93
|
+
user.groups << :user
|
94
|
+
user.named_groups.should include('admin')
|
95
|
+
|
96
|
+
user.in_group?(:admin).should be_true
|
97
|
+
user.in_any_group?(:admin, :user, :test).should be_true
|
98
|
+
user.in_all_groups?(:admin, :user).should be_true
|
99
|
+
user.in_all_groups?(:admin, 'user', :test).should be_false
|
100
|
+
|
101
|
+
MongoidUser.in_group(:admin).first.should eql(user)
|
102
|
+
MongoidUser.in_named_group(:admin).first.should eql(user)
|
103
|
+
MongoidUser.in_any_named_group(:admin, :test).first.should eql(user)
|
104
|
+
MongoidUser.in_all_named_groups(:admin, :user).first.should eql(user)
|
105
|
+
end
|
106
|
+
|
107
|
+
it "can check if groups are shared" do
|
108
|
+
user.groups << group
|
109
|
+
user2 = MongoidUser.create(:groups => [group])
|
110
|
+
|
111
|
+
user.shares_any_group?(user2).should be_true
|
112
|
+
MongoidUser.shares_any_group(user).to_a.should include(user2)
|
113
|
+
end
|
114
|
+
|
115
|
+
it "can check if named groups are shared" do
|
116
|
+
user.groups << :admin
|
117
|
+
user2 = MongoidUser.create(:named_groups => [:admin])
|
118
|
+
|
119
|
+
user.shares_any_group?(user2).should be_true
|
120
|
+
MongoidUser.shares_any_named_group(user).to_a.should include(user2)
|
121
|
+
end
|
85
122
|
end
|
data/spec/spec_helper.rb
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
require 'rubygems'
|
2
2
|
require 'bundler/setup'
|
3
3
|
|
4
|
-
Bundler.require(:default)
|
4
|
+
Bundler.require(:default, :test, :development)
|
5
5
|
|
6
6
|
require 'active_support'
|
7
7
|
require 'active_support/all'
|
@@ -15,4 +15,20 @@ if Mongoid::VERSION < '3'
|
|
15
15
|
else
|
16
16
|
Mongoid.load!('./spec/mongoid3.yml', :test)
|
17
17
|
end
|
18
|
-
#Mongoid.logger.level = :info
|
18
|
+
#Mongoid.logger.level = :info
|
19
|
+
|
20
|
+
RSpec.configure do |config|
|
21
|
+
config.order = "random"
|
22
|
+
|
23
|
+
config.before(:suite) do
|
24
|
+
DatabaseCleaner[:mongoid].strategy = :truncation
|
25
|
+
end
|
26
|
+
|
27
|
+
config.before(:each) do
|
28
|
+
DatabaseCleaner[:mongoid].start
|
29
|
+
end
|
30
|
+
|
31
|
+
config.after(:each) do
|
32
|
+
DatabaseCleaner[:mongoid].clean
|
33
|
+
end
|
34
|
+
end
|
metadata
CHANGED
@@ -1,64 +1,100 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: groupify
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
|
5
|
-
|
4
|
+
hash: 23
|
5
|
+
prerelease: false
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 2
|
9
|
+
- 0
|
10
|
+
version: 0.2.0
|
6
11
|
platform: ruby
|
7
12
|
authors:
|
8
|
-
|
13
|
+
- dwbutler
|
9
14
|
autorequire:
|
10
15
|
bindir: bin
|
11
16
|
cert_chain: []
|
12
17
|
|
13
|
-
date: 2012-
|
18
|
+
date: 2012-09-05 00:00:00 -07:00
|
19
|
+
default_executable:
|
14
20
|
dependencies:
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
21
|
+
- !ruby/object:Gem::Dependency
|
22
|
+
name: rails
|
23
|
+
prerelease: false
|
24
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ~>
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
hash: 3
|
30
|
+
segments:
|
31
|
+
- 3
|
32
|
+
- 2
|
33
|
+
version: "3.2"
|
34
|
+
type: :development
|
35
|
+
version_requirements: *id001
|
36
|
+
- !ruby/object:Gem::Dependency
|
37
|
+
name: rspec
|
38
|
+
prerelease: false
|
39
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
40
|
+
none: false
|
41
|
+
requirements:
|
42
|
+
- - ">="
|
43
|
+
- !ruby/object:Gem::Version
|
44
|
+
hash: 3
|
45
|
+
segments:
|
46
|
+
- 0
|
47
|
+
version: "0"
|
48
|
+
type: :development
|
49
|
+
version_requirements: *id002
|
50
|
+
- !ruby/object:Gem::Dependency
|
51
|
+
name: mongoid
|
52
|
+
prerelease: false
|
53
|
+
requirement: &id003 !ruby/object:Gem::Requirement
|
54
|
+
none: false
|
55
|
+
requirements:
|
56
|
+
- - ~>
|
57
|
+
- !ruby/object:Gem::Version
|
58
|
+
hash: 3
|
59
|
+
segments:
|
60
|
+
- 2
|
61
|
+
- 0
|
62
|
+
version: "2.0"
|
63
|
+
type: :development
|
64
|
+
version_requirements: *id003
|
65
|
+
- !ruby/object:Gem::Dependency
|
66
|
+
name: mongoid-rspec
|
67
|
+
prerelease: false
|
68
|
+
requirement: &id004 !ruby/object:Gem::Requirement
|
69
|
+
none: false
|
70
|
+
requirements:
|
71
|
+
- - "="
|
72
|
+
- !ruby/object:Gem::Version
|
73
|
+
hash: 13
|
74
|
+
segments:
|
75
|
+
- 1
|
76
|
+
- 4
|
77
|
+
- 5
|
78
|
+
version: 1.4.5
|
79
|
+
type: :development
|
80
|
+
version_requirements: *id004
|
81
|
+
- !ruby/object:Gem::Dependency
|
82
|
+
name: database_cleaner
|
83
|
+
prerelease: false
|
84
|
+
requirement: &id005 !ruby/object:Gem::Requirement
|
85
|
+
none: false
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
hash: 3
|
90
|
+
segments:
|
91
|
+
- 0
|
92
|
+
version: "0"
|
93
|
+
type: :development
|
94
|
+
version_requirements: *id005
|
59
95
|
description: Adds group and membership functionality to Rails models
|
60
96
|
email:
|
61
|
-
|
97
|
+
- dwbutler@ucla.edu
|
62
98
|
executables: []
|
63
99
|
|
64
100
|
extensions: []
|
@@ -66,19 +102,20 @@ extensions: []
|
|
66
102
|
extra_rdoc_files: []
|
67
103
|
|
68
104
|
files:
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
105
|
+
- .gitignore
|
106
|
+
- .travis.yml
|
107
|
+
- Gemfile
|
108
|
+
- LICENSE
|
109
|
+
- README.md
|
110
|
+
- Rakefile
|
111
|
+
- groupify.gemspec
|
112
|
+
- lib/groupify.rb
|
113
|
+
- lib/groupify/version.rb
|
114
|
+
- spec/groupify/mongoid_spec.rb
|
115
|
+
- spec/mongoid2.yml
|
116
|
+
- spec/mongoid3.yml
|
117
|
+
- spec/spec_helper.rb
|
118
|
+
has_rdoc: true
|
82
119
|
homepage: https://github.com/dwbutler/groupify
|
83
120
|
licenses: []
|
84
121
|
|
@@ -86,28 +123,34 @@ post_install_message:
|
|
86
123
|
rdoc_options: []
|
87
124
|
|
88
125
|
require_paths:
|
89
|
-
|
126
|
+
- lib
|
90
127
|
required_ruby_version: !ruby/object:Gem::Requirement
|
91
128
|
none: false
|
92
129
|
requirements:
|
93
|
-
|
94
|
-
|
95
|
-
|
130
|
+
- - ">="
|
131
|
+
- !ruby/object:Gem::Version
|
132
|
+
hash: 3
|
133
|
+
segments:
|
134
|
+
- 0
|
135
|
+
version: "0"
|
96
136
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
97
137
|
none: false
|
98
138
|
requirements:
|
99
|
-
|
100
|
-
|
101
|
-
|
139
|
+
- - ">="
|
140
|
+
- !ruby/object:Gem::Version
|
141
|
+
hash: 3
|
142
|
+
segments:
|
143
|
+
- 0
|
144
|
+
version: "0"
|
102
145
|
requirements: []
|
103
146
|
|
104
147
|
rubyforge_project:
|
105
|
-
rubygems_version: 1.
|
148
|
+
rubygems_version: 1.3.7
|
106
149
|
signing_key:
|
107
150
|
specification_version: 3
|
108
151
|
summary: Group functionality for Rails
|
109
152
|
test_files:
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
153
|
+
- spec/groupify/mongoid_spec.rb
|
154
|
+
- spec/mongoid2.yml
|
155
|
+
- spec/mongoid3.yml
|
156
|
+
- spec/spec_helper.rb
|