groupify 0.2.1 → 0.3.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 +5 -2
- data/lib/groupify.rb +68 -47
- data/lib/groupify/version.rb +1 -1
- data/spec/groupify/mongoid_spec.rb +11 -10
- metadata +99 -86
data/README.md
CHANGED
@@ -35,6 +35,7 @@ class User
|
|
35
35
|
include Mongoid::Document
|
36
36
|
|
37
37
|
acts_as_group_member
|
38
|
+
acts_as_named_group_member
|
38
39
|
end
|
39
40
|
```
|
40
41
|
|
@@ -56,20 +57,22 @@ user.in_group?(group) => true
|
|
56
57
|
Add to named groups:
|
57
58
|
|
58
59
|
```ruby
|
59
|
-
user.
|
60
|
-
user.
|
60
|
+
user.named_groups << :admin
|
61
|
+
user.in_named_group?(:admin) => true
|
61
62
|
```
|
62
63
|
|
63
64
|
Check if two group members share any of the same groups:
|
64
65
|
|
65
66
|
```ruby
|
66
67
|
user1.shares_any_group?(user2)
|
68
|
+
user2.shares_any_named_group?(user1)
|
67
69
|
```
|
68
70
|
|
69
71
|
Query for groups & members:
|
70
72
|
|
71
73
|
```ruby
|
72
74
|
User.in_group(group) # Find all users in this group
|
75
|
+
User.in_named_group(:admin) # Find all users in this named group
|
73
76
|
Group.with_member(user) # Find all groups with this user
|
74
77
|
|
75
78
|
User.shares_any_group(user) # Find all users that share any groups with this user
|
data/lib/groupify.rb
CHANGED
@@ -1,10 +1,15 @@
|
|
1
1
|
require 'active_support'
|
2
2
|
require 'mongoid'
|
3
|
+
require 'set'
|
3
4
|
|
4
5
|
# Groups and members
|
5
6
|
module Groupify
|
6
7
|
extend ActiveSupport::Concern
|
7
8
|
|
9
|
+
included do
|
10
|
+
def none; where(:id => nil); end
|
11
|
+
end
|
12
|
+
|
8
13
|
module ClassMethods
|
9
14
|
def acts_as_group(opts = {})
|
10
15
|
include Groupify::Group
|
@@ -24,15 +29,20 @@ module Groupify
|
|
24
29
|
class_eval { @group_class_name = opts[:class_name] || 'Group' }
|
25
30
|
include Groupify::GroupMember
|
26
31
|
end
|
32
|
+
|
33
|
+
def acts_as_named_group_member(opts = {})
|
34
|
+
include Groupify::NamedGroupMember
|
35
|
+
end
|
27
36
|
end
|
28
37
|
|
29
|
-
# Functionality related to groups.
|
30
38
|
# Usage:
|
31
39
|
# class Group
|
32
40
|
# acts_as_group, :members => [:users]
|
33
41
|
# ...
|
34
42
|
# end
|
35
43
|
#
|
44
|
+
# group.add(member)
|
45
|
+
#
|
36
46
|
module Group
|
37
47
|
extend ActiveSupport::Concern
|
38
48
|
|
@@ -66,39 +76,24 @@ module Groupify
|
|
66
76
|
end
|
67
77
|
end
|
68
78
|
|
69
|
-
# Functionality related to group members.
|
70
79
|
# Usage:
|
71
80
|
# class User
|
72
81
|
# acts_as_group_member
|
73
82
|
# ...
|
74
83
|
# end
|
75
84
|
#
|
85
|
+
# user.groups << group
|
86
|
+
#
|
76
87
|
module GroupMember
|
77
88
|
extend ActiveSupport::Concern
|
78
89
|
|
79
90
|
included do
|
80
91
|
group_class_name='Group' unless defined?(@group_class_name)
|
81
|
-
|
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
|
89
|
-
self.base.save if self.metadata.autosave
|
90
|
-
end
|
91
|
-
end
|
92
|
-
end
|
92
|
+
has_and_belongs_to_many :groups, :autosave => true, :inverse_of => nil, :class_name => @group_class_name
|
93
93
|
end
|
94
94
|
|
95
95
|
def in_group?(group)
|
96
|
-
|
97
|
-
when groups.metadata.klass
|
98
|
-
self.groups.include?(group)
|
99
|
-
else
|
100
|
-
self.named_groups ? self.named_groups.include?(group) : false
|
101
|
-
end
|
96
|
+
self.groups.include?(group)
|
102
97
|
end
|
103
98
|
|
104
99
|
def in_any_group?(*groups)
|
@@ -109,60 +104,86 @@ module Groupify
|
|
109
104
|
end
|
110
105
|
|
111
106
|
def in_all_groups?(*groups)
|
112
|
-
groups.flatten
|
113
|
-
return false unless in_group?(group)
|
114
|
-
end
|
115
|
-
return true
|
107
|
+
Set.new(groups.flatten) == Set.new(self.named_groups)
|
116
108
|
end
|
117
109
|
|
118
110
|
def shares_any_group?(other)
|
119
|
-
in_any_group?(other.groups
|
111
|
+
in_any_group?(other.groups)
|
120
112
|
end
|
121
113
|
|
122
114
|
module ClassMethods
|
123
115
|
def group_class_name; @group_class_name || 'Group'; end
|
124
116
|
def group_class_name=(klass); @group_class_name = klass; end
|
125
117
|
|
126
|
-
def none; where(:id => nil); end
|
127
|
-
|
128
118
|
def in_group(group)
|
129
|
-
|
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]) : none
|
119
|
+
group.present? ? where(:group_ids.in => [group.id]) : none
|
140
120
|
end
|
141
121
|
|
142
122
|
def in_any_group(*groups)
|
143
123
|
groups.present? ? where(:group_ids.in => groups.flatten.map{|g|g.id}) : none
|
144
124
|
end
|
145
125
|
|
146
|
-
def in_any_named_group(*named_groups)
|
147
|
-
named_groups.present? ? where(:named_groups.in => named_groups.flatten) : none
|
148
|
-
end
|
149
|
-
|
150
126
|
def in_all_groups(*groups)
|
151
127
|
groups.present? ? where(:group_ids => groups.flatten.map{|g|g.id}) : none
|
152
128
|
end
|
153
129
|
|
154
|
-
def in_all_named_groups(*named_groups)
|
155
|
-
named_groups.present? ? where(:named_groups => named_groups.flatten) : none
|
156
|
-
end
|
157
|
-
|
158
130
|
def shares_any_group(other)
|
159
131
|
in_any_group(other.groups)
|
160
132
|
end
|
161
133
|
|
134
|
+
end
|
135
|
+
end
|
136
|
+
|
137
|
+
# Usage:
|
138
|
+
# class User
|
139
|
+
# acts_as_named_group_member
|
140
|
+
# ...
|
141
|
+
# end
|
142
|
+
#
|
143
|
+
# user.named_groups << :admin
|
144
|
+
#
|
145
|
+
module NamedGroupMember
|
146
|
+
extend ActiveSupport::Concern
|
147
|
+
|
148
|
+
included do
|
149
|
+
field :named_groups, :type => Array, :default => []
|
150
|
+
end
|
151
|
+
|
152
|
+
def in_named_group?(group)
|
153
|
+
self.named_groups.include?(group)
|
154
|
+
end
|
155
|
+
|
156
|
+
def in_any_named_group?(*groups)
|
157
|
+
groups.flatten.each do |group|
|
158
|
+
return true if in_named_group?(group)
|
159
|
+
end
|
160
|
+
return false
|
161
|
+
end
|
162
|
+
|
163
|
+
def in_all_named_groups?(*groups)
|
164
|
+
Set.new(groups.flatten) == Set.new(self.named_groups)
|
165
|
+
end
|
166
|
+
|
167
|
+
def shares_any_named_group?(other)
|
168
|
+
in_any_named_group?(other.named_groups)
|
169
|
+
end
|
170
|
+
|
171
|
+
module ClassMethods
|
172
|
+
def in_named_group(named_group)
|
173
|
+
named_group.present? ? where(:named_groups.in => [named_group]) : none
|
174
|
+
end
|
175
|
+
|
176
|
+
def in_any_named_group(*named_groups)
|
177
|
+
named_groups.present? ? where(:named_groups.in => named_groups.flatten) : none
|
178
|
+
end
|
179
|
+
|
180
|
+
def in_all_named_groups(*named_groups)
|
181
|
+
named_groups.present? ? where(:named_groups => named_groups.flatten) : none
|
182
|
+
end
|
183
|
+
|
162
184
|
def shares_any_named_group(other)
|
163
185
|
in_any_named_group(other.named_groups)
|
164
186
|
end
|
165
|
-
|
166
187
|
end
|
167
188
|
end
|
168
189
|
end
|
data/lib/groupify/version.rb
CHANGED
@@ -7,6 +7,7 @@ class MongoidUser
|
|
7
7
|
include Mongoid::Document
|
8
8
|
|
9
9
|
acts_as_group_member :class_name => 'MongoidGroup'
|
10
|
+
acts_as_named_group_member
|
10
11
|
end
|
11
12
|
|
12
13
|
class MongoidTask
|
@@ -89,16 +90,16 @@ describe "MongoidUser" do
|
|
89
90
|
end
|
90
91
|
|
91
92
|
it "can have named groups" do
|
92
|
-
user.
|
93
|
-
user.
|
93
|
+
user.named_groups << :admin
|
94
|
+
user.named_groups << :user
|
95
|
+
user.save
|
94
96
|
user.named_groups.should include(:admin)
|
95
97
|
|
96
|
-
user.
|
97
|
-
user.
|
98
|
-
user.
|
99
|
-
user.
|
100
|
-
|
101
|
-
MongoidUser.in_group(:admin).first.should eql(user)
|
98
|
+
user.in_named_group?(:admin).should be_true
|
99
|
+
user.in_any_named_group?(:admin, :user, :test).should be_true
|
100
|
+
user.in_all_named_groups?(:admin, :user).should be_true
|
101
|
+
user.in_all_named_groups?(:admin, :user, :test).should be_false
|
102
|
+
|
102
103
|
MongoidUser.in_named_group(:admin).first.should eql(user)
|
103
104
|
MongoidUser.in_any_named_group(:admin, :test).first.should eql(user)
|
104
105
|
MongoidUser.in_all_named_groups(:admin, :user).first.should eql(user)
|
@@ -113,10 +114,10 @@ describe "MongoidUser" do
|
|
113
114
|
end
|
114
115
|
|
115
116
|
it "can check if named groups are shared" do
|
116
|
-
user.
|
117
|
+
user.named_groups << :admin
|
117
118
|
user2 = MongoidUser.create(:named_groups => [:admin])
|
118
119
|
|
119
|
-
user.
|
120
|
+
user.shares_any_named_group?(user2).should be_true
|
120
121
|
MongoidUser.shares_any_named_group(user).to_a.should include(user2)
|
121
122
|
end
|
122
123
|
end
|
metadata
CHANGED
@@ -1,103 +1,107 @@
|
|
1
|
-
--- !ruby/object:Gem::Specification
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
2
|
name: groupify
|
3
|
-
version: !ruby/object:Gem::Version
|
4
|
-
|
5
|
-
prerelease:
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 19
|
5
|
+
prerelease: false
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 3
|
9
|
+
- 0
|
10
|
+
version: 0.3.0
|
6
11
|
platform: ruby
|
7
|
-
authors:
|
12
|
+
authors:
|
8
13
|
- dwbutler
|
9
14
|
autorequire:
|
10
15
|
bindir: bin
|
11
16
|
cert_chain: []
|
12
|
-
|
13
|
-
|
14
|
-
|
17
|
+
|
18
|
+
date: 2012-09-06 00:00:00 -07:00
|
19
|
+
default_executable:
|
20
|
+
dependencies:
|
21
|
+
- !ruby/object:Gem::Dependency
|
15
22
|
name: rails
|
16
|
-
requirement: !ruby/object:Gem::Requirement
|
17
|
-
none: false
|
18
|
-
requirements:
|
19
|
-
- - ~>
|
20
|
-
- !ruby/object:Gem::Version
|
21
|
-
version: '3.2'
|
22
|
-
type: :development
|
23
23
|
prerelease: false
|
24
|
-
|
24
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
25
25
|
none: false
|
26
|
-
requirements:
|
26
|
+
requirements:
|
27
27
|
- - ~>
|
28
|
-
- !ruby/object:Gem::Version
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
requirements:
|
35
|
-
- - ! '>='
|
36
|
-
- !ruby/object:Gem::Version
|
37
|
-
version: '0'
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
hash: 3
|
30
|
+
segments:
|
31
|
+
- 3
|
32
|
+
- 2
|
33
|
+
version: "3.2"
|
38
34
|
type: :development
|
35
|
+
version_requirements: *id001
|
36
|
+
- !ruby/object:Gem::Dependency
|
37
|
+
name: rspec
|
39
38
|
prerelease: false
|
40
|
-
|
39
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
41
40
|
none: false
|
42
|
-
requirements:
|
43
|
-
- -
|
44
|
-
- !ruby/object:Gem::Version
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
none: false
|
50
|
-
requirements:
|
51
|
-
- - ~>
|
52
|
-
- !ruby/object:Gem::Version
|
53
|
-
version: '3.0'
|
41
|
+
requirements:
|
42
|
+
- - ">="
|
43
|
+
- !ruby/object:Gem::Version
|
44
|
+
hash: 3
|
45
|
+
segments:
|
46
|
+
- 0
|
47
|
+
version: "0"
|
54
48
|
type: :development
|
49
|
+
version_requirements: *id002
|
50
|
+
- !ruby/object:Gem::Dependency
|
51
|
+
name: mongoid
|
55
52
|
prerelease: false
|
56
|
-
|
53
|
+
requirement: &id003 !ruby/object:Gem::Requirement
|
57
54
|
none: false
|
58
|
-
requirements:
|
55
|
+
requirements:
|
59
56
|
- - ~>
|
60
|
-
- !ruby/object:Gem::Version
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
requirements:
|
67
|
-
- - ~>
|
68
|
-
- !ruby/object:Gem::Version
|
69
|
-
version: 1.5.1
|
57
|
+
- !ruby/object:Gem::Version
|
58
|
+
hash: 3
|
59
|
+
segments:
|
60
|
+
- 2
|
61
|
+
- 0
|
62
|
+
version: "2.0"
|
70
63
|
type: :development
|
64
|
+
version_requirements: *id003
|
65
|
+
- !ruby/object:Gem::Dependency
|
66
|
+
name: mongoid-rspec
|
71
67
|
prerelease: false
|
72
|
-
|
68
|
+
requirement: &id004 !ruby/object:Gem::Requirement
|
73
69
|
none: false
|
74
|
-
requirements:
|
75
|
-
- -
|
76
|
-
- !ruby/object:Gem::Version
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
- - ! '>='
|
84
|
-
- !ruby/object:Gem::Version
|
85
|
-
version: '0'
|
70
|
+
requirements:
|
71
|
+
- - "="
|
72
|
+
- !ruby/object:Gem::Version
|
73
|
+
hash: 13
|
74
|
+
segments:
|
75
|
+
- 1
|
76
|
+
- 4
|
77
|
+
- 5
|
78
|
+
version: 1.4.5
|
86
79
|
type: :development
|
80
|
+
version_requirements: *id004
|
81
|
+
- !ruby/object:Gem::Dependency
|
82
|
+
name: database_cleaner
|
87
83
|
prerelease: false
|
88
|
-
|
84
|
+
requirement: &id005 !ruby/object:Gem::Requirement
|
89
85
|
none: false
|
90
|
-
requirements:
|
91
|
-
- -
|
92
|
-
- !ruby/object:Gem::Version
|
93
|
-
|
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
|
94
95
|
description: Adds group and membership functionality to Rails models
|
95
|
-
email:
|
96
|
+
email:
|
96
97
|
- dwbutler@ucla.edu
|
97
98
|
executables: []
|
99
|
+
|
98
100
|
extensions: []
|
101
|
+
|
99
102
|
extra_rdoc_files: []
|
100
|
-
|
103
|
+
|
104
|
+
files:
|
101
105
|
- .gitignore
|
102
106
|
- .travis.yml
|
103
107
|
- Gemfile
|
@@ -111,33 +115,42 @@ files:
|
|
111
115
|
- spec/mongoid2.yml
|
112
116
|
- spec/mongoid3.yml
|
113
117
|
- spec/spec_helper.rb
|
118
|
+
has_rdoc: true
|
114
119
|
homepage: https://github.com/dwbutler/groupify
|
115
120
|
licenses: []
|
121
|
+
|
116
122
|
post_install_message:
|
117
123
|
rdoc_options: []
|
118
|
-
|
124
|
+
|
125
|
+
require_paths:
|
119
126
|
- lib
|
120
|
-
required_ruby_version: !ruby/object:Gem::Requirement
|
127
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
121
128
|
none: false
|
122
|
-
requirements:
|
123
|
-
- -
|
124
|
-
- !ruby/object:Gem::Version
|
125
|
-
|
126
|
-
|
129
|
+
requirements:
|
130
|
+
- - ">="
|
131
|
+
- !ruby/object:Gem::Version
|
132
|
+
hash: 3
|
133
|
+
segments:
|
134
|
+
- 0
|
135
|
+
version: "0"
|
136
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
127
137
|
none: false
|
128
|
-
requirements:
|
129
|
-
- -
|
130
|
-
- !ruby/object:Gem::Version
|
131
|
-
|
138
|
+
requirements:
|
139
|
+
- - ">="
|
140
|
+
- !ruby/object:Gem::Version
|
141
|
+
hash: 3
|
142
|
+
segments:
|
143
|
+
- 0
|
144
|
+
version: "0"
|
132
145
|
requirements: []
|
146
|
+
|
133
147
|
rubyforge_project:
|
134
|
-
rubygems_version: 1.
|
148
|
+
rubygems_version: 1.3.7
|
135
149
|
signing_key:
|
136
150
|
specification_version: 3
|
137
151
|
summary: Group functionality for Rails
|
138
|
-
test_files:
|
152
|
+
test_files:
|
139
153
|
- spec/groupify/mongoid_spec.rb
|
140
154
|
- spec/mongoid2.yml
|
141
155
|
- spec/mongoid3.yml
|
142
156
|
- spec/spec_helper.rb
|
143
|
-
has_rdoc:
|