groupify 0.7.2 → 0.8.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.ruby-gemset +1 -0
- data/CHANGELOG.md +19 -3
- data/README.md +15 -2
- data/Rakefile +1 -1
- data/lib/groupify/adapter/active_record/group.rb +13 -11
- data/lib/groupify/adapter/active_record/group_member.rb +14 -14
- data/lib/groupify/adapter/active_record/model.rb +1 -3
- data/lib/groupify/adapter/active_record/named_group_collection.rb +5 -5
- data/lib/groupify/adapter/active_record/named_group_member.rb +9 -9
- data/lib/groupify/adapter/mongoid/group.rb +5 -3
- data/lib/groupify/adapter/mongoid/model.rb +1 -3
- data/lib/groupify/version.rb +1 -1
- data/spec/active_record_spec.rb +38 -29
- data/spec/mongoid_spec.rb +32 -21
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: dcc33f0b1cc986893a05a5fdc5e916bd7e3b9837
|
4
|
+
data.tar.gz: bbe9793b57fe7c7256193694aed87e7d1de7adf7
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: bfa5473767c6bf658020a14ba3f480d0b04ace3997c34377b4dca702dbeab9c4ef3c11354da9264ea453986b20af09472ea513c7ca807e0bc8d3e7f0111935bd
|
7
|
+
data.tar.gz: e12229d766b2f5abead3b6d7d6848fb6f7c3b40a7889fe03734e589c35a5b4d017efb0628d902755d51f355264b5b049b160ded876a1fbbfbcd3d0b0f168d1c1
|
data/.ruby-gemset
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
gem_groupify
|
data/CHANGELOG.md
CHANGED
@@ -1,7 +1,23 @@
|
|
1
1
|
# Change Log
|
2
2
|
|
3
|
-
## [
|
4
|
-
[Full Changelog](https://github.com/dwbutler/groupify/compare/v0.7.
|
3
|
+
## [v0.8.0](https://github.com/dwbutler/groupify/tree/v0.8.0) (2016-06-10)
|
4
|
+
[Full Changelog](https://github.com/dwbutler/groupify/compare/v0.7.2...v0.8.0)
|
5
|
+
|
6
|
+
**Fixed bugs:**
|
7
|
+
|
8
|
+
- Setting a group as a group member breaks has\_members associations on that group [\#45](https://github.com/dwbutler/groupify/issues/45)
|
9
|
+
- Error on add user a team on Rails 5 Beta [\#39](https://github.com/dwbutler/groupify/issues/39)
|
10
|
+
- Split group memberships [\#46](https://github.com/dwbutler/groupify/pull/46) ([juhazi](https://github.com/juhazi))
|
11
|
+
|
12
|
+
## [v0.7.2](https://github.com/dwbutler/groupify/tree/v0.7.2) (2016-05-21)
|
13
|
+
[Full Changelog](https://github.com/dwbutler/groupify/compare/v0.7.1...v0.7.2)
|
14
|
+
|
15
|
+
**Merged pull requests:**
|
16
|
+
|
17
|
+
- Some fixes to prep for rails5 [\#44](https://github.com/dwbutler/groupify/pull/44) ([wadestuart](https://github.com/wadestuart))
|
18
|
+
|
19
|
+
## [v0.7.1](https://github.com/dwbutler/groupify/tree/v0.7.1) (2015-11-19)
|
20
|
+
[Full Changelog](https://github.com/dwbutler/groupify/compare/v0.7.0...v0.7.1)
|
5
21
|
|
6
22
|
**Fixed bugs:**
|
7
23
|
|
@@ -107,4 +123,4 @@
|
|
107
123
|
## [v0.4.0](https://github.com/dwbutler/groupify/tree/v0.4.0) (2013-07-01)
|
108
124
|
|
109
125
|
|
110
|
-
\* *This Change Log was automatically generated by [github_changelog_generator](https://github.com/skywinder/Github-Changelog-Generator)*
|
126
|
+
\* *This Change Log was automatically generated by [github_changelog_generator](https://github.com/skywinder/Github-Changelog-Generator)*
|
data/README.md
CHANGED
@@ -129,11 +129,11 @@ Example:
|
|
129
129
|
|
130
130
|
```ruby
|
131
131
|
class Organization < Group
|
132
|
-
has_members
|
132
|
+
has_members :offices, :equipment
|
133
133
|
end
|
134
134
|
```
|
135
135
|
|
136
|
-
Mongoid works the same way by creating Mongoid relations.
|
136
|
+
Mongoid works the same way by creating Mongoid relations.
|
137
137
|
|
138
138
|
## Usage
|
139
139
|
|
@@ -359,6 +359,15 @@ end
|
|
359
359
|
|
360
360
|
## Upgrading
|
361
361
|
|
362
|
+
### 0.8+ - Name Change for `group_memberships` Associations (ActiveRecord only)
|
363
|
+
|
364
|
+
Groupify 0.8+ changed the ActiveRecord adapter to support configuring the same
|
365
|
+
model as both a group and a group member. To accomplish this, the internal `group_memberships`
|
366
|
+
association was renamed to be different for groups and members. If you were
|
367
|
+
using it, please be aware that you will need to change your code. This
|
368
|
+
association is considered to be an internal implementation details and not part
|
369
|
+
of the public API, so please don't rely on it if you can avoid it.
|
370
|
+
|
362
371
|
### 0.7+ - Polymorphic Groups (ActiveRecord only)
|
363
372
|
Groupify < 0.7 required a single `Group` model used for all group memberships.
|
364
373
|
Groupify 0.7+ supports using multiple models as groups by implementing polymorphic associations.
|
@@ -381,3 +390,7 @@ table is large.
|
|
381
390
|
3. Commit your changes (`git commit -am 'Added some feature'`)
|
382
391
|
4. Push to the branch (`git push origin my-new-feature`)
|
383
392
|
5. Create new Pull Request
|
393
|
+
|
394
|
+
## Contributors
|
395
|
+
|
396
|
+
See a list of contributors [here](https://github.com/dwbutler/groupify/graphs/contributors).
|
data/Rakefile
CHANGED
@@ -15,7 +15,7 @@ module Groupify
|
|
15
15
|
included do
|
16
16
|
@default_member_class = nil
|
17
17
|
@member_klasses ||= Set.new
|
18
|
-
has_many :
|
18
|
+
has_many :group_memberships_as_group,
|
19
19
|
dependent: :destroy,
|
20
20
|
as: :group,
|
21
21
|
class_name: Groupify.group_membership_class_name
|
@@ -37,7 +37,7 @@ module Groupify
|
|
37
37
|
members.each do |member|
|
38
38
|
member.groups << self unless member.groups.include?(self)
|
39
39
|
if membership_type
|
40
|
-
member.
|
40
|
+
member.group_memberships_as_member.where(group_id: id, group_type: self.class.model_name.to_s, membership_type: membership_type).first_or_create!
|
41
41
|
end
|
42
42
|
member.__send__(:clear_association_cache)
|
43
43
|
end
|
@@ -68,9 +68,11 @@ module Groupify
|
|
68
68
|
end
|
69
69
|
|
70
70
|
# Define which classes are members of this group
|
71
|
-
def has_members(
|
72
|
-
|
73
|
-
|
71
|
+
def has_members(*names)
|
72
|
+
Array.wrap(names.flatten).each do |name|
|
73
|
+
klass = name.to_s.classify.constantize
|
74
|
+
register(klass)
|
75
|
+
end
|
74
76
|
end
|
75
77
|
|
76
78
|
# Merge two groups. The members of the source become members of the destination, and the source is destroyed.
|
@@ -78,13 +80,13 @@ module Groupify
|
|
78
80
|
# Ensure that all the members of the source can be members of the destination
|
79
81
|
invalid_member_classes = (source_group.member_classes - destination_group.member_classes)
|
80
82
|
invalid_member_classes.each do |klass|
|
81
|
-
if klass.joins(:
|
83
|
+
if klass.joins(:group_memberships_as_member).where(:group_memberships => {:group_id => source_group.id}).count > 0
|
82
84
|
raise ArgumentError.new("#{source_group.class} has members that cannot belong to #{destination_group.class}")
|
83
85
|
end
|
84
86
|
end
|
85
87
|
|
86
88
|
source_group.transaction do
|
87
|
-
source_group.
|
89
|
+
source_group.group_memberships_as_group.update_all(:group_id => destination_group.id)
|
88
90
|
source_group.destroy
|
89
91
|
end
|
90
92
|
end
|
@@ -109,7 +111,7 @@ module Groupify
|
|
109
111
|
members = args
|
110
112
|
|
111
113
|
if opts[:as]
|
112
|
-
proxy_association.owner.
|
114
|
+
proxy_association.owner.group_memberships_as_group.
|
113
115
|
where(member_id: members.map(&:id), member_type: proxy_association.reflection.options[:source_type]).
|
114
116
|
as(opts[:as]).
|
115
117
|
delete_all
|
@@ -123,7 +125,7 @@ module Groupify
|
|
123
125
|
members = args
|
124
126
|
|
125
127
|
if opts[:as]
|
126
|
-
proxy_association.owner.
|
128
|
+
proxy_association.owner.group_memberships_as_group.
|
127
129
|
where(member_id: members.map(&:id), member_type: proxy_association.reflection.options[:source_type]).
|
128
130
|
as(opts[:as]).
|
129
131
|
destroy_all
|
@@ -148,14 +150,14 @@ module Groupify
|
|
148
150
|
if ActiveSupport::VERSION::MAJOR > 3
|
149
151
|
has_many association_name,
|
150
152
|
->{ uniq },
|
151
|
-
through: :
|
153
|
+
through: :group_memberships_as_group,
|
152
154
|
source: :member,
|
153
155
|
source_type: source_type,
|
154
156
|
extend: MemberAssociationExtensions
|
155
157
|
else
|
156
158
|
has_many association_name,
|
157
159
|
uniq: true,
|
158
|
-
through: :
|
160
|
+
through: :group_memberships_as_group,
|
159
161
|
source: :member,
|
160
162
|
source_type: source_type,
|
161
163
|
extend: MemberAssociationExtensions
|
@@ -13,8 +13,8 @@ module Groupify
|
|
13
13
|
extend ActiveSupport::Concern
|
14
14
|
|
15
15
|
included do
|
16
|
-
unless respond_to?(:
|
17
|
-
has_many :
|
16
|
+
unless respond_to?(:group_memberships_as_member)
|
17
|
+
has_many :group_memberships_as_member,
|
18
18
|
as: :member,
|
19
19
|
autosave: true,
|
20
20
|
dependent: :destroy,
|
@@ -23,14 +23,14 @@ module Groupify
|
|
23
23
|
|
24
24
|
if ActiveSupport::VERSION::MAJOR > 3
|
25
25
|
has_many :groups, ->{ uniq },
|
26
|
-
through: :
|
26
|
+
through: :group_memberships_as_member,
|
27
27
|
as: :group,
|
28
28
|
source_type: @group_class_name,
|
29
29
|
extend: GroupAssociationExtensions
|
30
30
|
else
|
31
31
|
has_many :groups,
|
32
32
|
uniq: true,
|
33
|
-
through: :
|
33
|
+
through: :group_memberships_as_member,
|
34
34
|
as: :group,
|
35
35
|
source_type: @group_class_name,
|
36
36
|
extend: GroupAssociationExtensions
|
@@ -48,7 +48,7 @@ module Groupify
|
|
48
48
|
groups = args.flatten
|
49
49
|
|
50
50
|
if opts[:as]
|
51
|
-
proxy_association.owner.
|
51
|
+
proxy_association.owner.group_memberships_as_member.where(group_id: groups.map(&:id)).as(opts[:as]).delete_all
|
52
52
|
else
|
53
53
|
super(*groups)
|
54
54
|
end
|
@@ -59,7 +59,7 @@ module Groupify
|
|
59
59
|
groups = args.flatten
|
60
60
|
|
61
61
|
if opts[:as]
|
62
|
-
proxy_association.owner.
|
62
|
+
proxy_association.owner.group_memberships_as_member.where(group_id: groups.map(&:id)).as(opts[:as]).destroy_all
|
63
63
|
else
|
64
64
|
super(*groups)
|
65
65
|
end
|
@@ -73,7 +73,7 @@ module Groupify
|
|
73
73
|
criteria.merge!(membership_type: opts[:as])
|
74
74
|
end
|
75
75
|
|
76
|
-
|
76
|
+
group_memberships_as_member.exists?(criteria)
|
77
77
|
end
|
78
78
|
|
79
79
|
def in_any_group?(*args)
|
@@ -106,30 +106,30 @@ module Groupify
|
|
106
106
|
|
107
107
|
module ClassMethods
|
108
108
|
def as(membership_type)
|
109
|
-
joins(:
|
109
|
+
joins(:group_memberships_as_member).where(group_memberships: { membership_type: membership_type })
|
110
110
|
end
|
111
111
|
|
112
112
|
def in_group(group)
|
113
113
|
return none unless group.present?
|
114
114
|
|
115
|
-
joins(:
|
115
|
+
joins(:group_memberships_as_member).where(group_memberships: { group_id: group.id }).uniq
|
116
116
|
end
|
117
117
|
|
118
118
|
def in_any_group(*groups)
|
119
119
|
groups = groups.flatten
|
120
120
|
return none unless groups.present?
|
121
121
|
|
122
|
-
joins(:
|
122
|
+
joins(:group_memberships_as_member).where(group_memberships: { group_id: groups.map(&:id) }).uniq
|
123
123
|
end
|
124
124
|
|
125
125
|
def in_all_groups(*groups)
|
126
126
|
groups = groups.flatten
|
127
127
|
return none unless groups.present?
|
128
128
|
|
129
|
-
joins(:
|
129
|
+
joins(:group_memberships_as_member).
|
130
130
|
group("#{quoted_table_name}.#{connection.quote_column_name('id')}").
|
131
131
|
where(group_memberships: {group_id: groups.map(&:id)}).
|
132
|
-
having("COUNT(#{reflect_on_association(:
|
132
|
+
having("COUNT(#{reflect_on_association(:group_memberships_as_member).klass.quoted_table_name}.#{connection.quote_column_name('group_id')}) = ?", groups.count).
|
133
133
|
uniq
|
134
134
|
end
|
135
135
|
|
@@ -137,9 +137,9 @@ module Groupify
|
|
137
137
|
groups = groups.flatten
|
138
138
|
return none unless groups.present?
|
139
139
|
|
140
|
-
joins(:
|
140
|
+
joins(:group_memberships_as_member).
|
141
141
|
group("#{quoted_table_name}.#{connection.quote_column_name('id')}").
|
142
|
-
having("COUNT(DISTINCT #{reflect_on_association(:
|
142
|
+
having("COUNT(DISTINCT #{reflect_on_association(:group_memberships_as_member).klass.quoted_table_name}.#{connection.quote_column_name('group_id')}) = ?", groups.count).
|
143
143
|
uniq
|
144
144
|
end
|
145
145
|
|
@@ -4,7 +4,7 @@ module Groupify
|
|
4
4
|
class NamedGroupCollection < Set
|
5
5
|
def initialize(member)
|
6
6
|
@member = member
|
7
|
-
@named_group_memberships = member.
|
7
|
+
@named_group_memberships = member.group_memberships_as_member.named
|
8
8
|
@group_names = @named_group_memberships.pluck(:group_name).map(&:to_sym)
|
9
9
|
super(@group_names)
|
10
10
|
end
|
@@ -14,18 +14,18 @@ module Groupify
|
|
14
14
|
membership_type = opts[:as]
|
15
15
|
|
16
16
|
if @member.new_record?
|
17
|
-
@member.
|
17
|
+
@member.group_memberships_as_member.build(group_name: named_group, membership_type: nil)
|
18
18
|
else
|
19
19
|
@member.transaction do
|
20
|
-
@member.
|
20
|
+
@member.group_memberships_as_member.where(group_name: named_group, membership_type: nil).first_or_create!
|
21
21
|
end
|
22
22
|
end
|
23
23
|
|
24
24
|
if membership_type
|
25
25
|
if @member.new_record?
|
26
|
-
@member.
|
26
|
+
@member.group_memberships_as_member.build(group_name: named_group, membership_type: membership_type)
|
27
27
|
else
|
28
|
-
@member.
|
28
|
+
@member.group_memberships_as_member.where(group_name: named_group, membership_type: membership_type).first_or_create!
|
29
29
|
end
|
30
30
|
end
|
31
31
|
|
@@ -13,8 +13,8 @@ module Groupify
|
|
13
13
|
extend ActiveSupport::Concern
|
14
14
|
|
15
15
|
included do
|
16
|
-
unless respond_to?(:
|
17
|
-
has_many :
|
16
|
+
unless respond_to?(:group_memberships_as_member)
|
17
|
+
has_many :group_memberships_as_member,
|
18
18
|
as: :member,
|
19
19
|
autosave: true,
|
20
20
|
dependent: :destroy,
|
@@ -63,30 +63,30 @@ module Groupify
|
|
63
63
|
|
64
64
|
module ClassMethods
|
65
65
|
def as(membership_type)
|
66
|
-
joins(:
|
66
|
+
joins(:group_memberships_as_member).where(group_memberships: {membership_type: membership_type})
|
67
67
|
end
|
68
68
|
|
69
69
|
def in_named_group(named_group)
|
70
70
|
return none unless named_group.present?
|
71
71
|
|
72
|
-
joins(:
|
72
|
+
joins(:group_memberships_as_member).where(group_memberships: {group_name: named_group}).uniq
|
73
73
|
end
|
74
74
|
|
75
75
|
def in_any_named_group(*named_groups)
|
76
76
|
named_groups.flatten!
|
77
77
|
return none unless named_groups.present?
|
78
78
|
|
79
|
-
joins(:
|
79
|
+
joins(:group_memberships_as_member).where(group_memberships: {group_name: named_groups.flatten}).uniq
|
80
80
|
end
|
81
81
|
|
82
82
|
def in_all_named_groups(*named_groups)
|
83
83
|
named_groups.flatten!
|
84
84
|
return none unless named_groups.present?
|
85
85
|
|
86
|
-
joins(:
|
86
|
+
joins(:group_memberships_as_member).
|
87
87
|
group("#{quoted_table_name}.#{connection.quote_column_name('id')}").
|
88
88
|
where(:group_memberships => {:group_name => named_groups}).
|
89
|
-
having("COUNT(DISTINCT #{reflect_on_association(:
|
89
|
+
having("COUNT(DISTINCT #{reflect_on_association(:group_memberships_as_member).klass.quoted_table_name}.#{connection.quote_column_name('group_name')}) = ?", named_groups.count).
|
90
90
|
uniq
|
91
91
|
end
|
92
92
|
|
@@ -94,9 +94,9 @@ module Groupify
|
|
94
94
|
named_groups.flatten!
|
95
95
|
return none unless named_groups.present?
|
96
96
|
|
97
|
-
joins(:
|
97
|
+
joins(:group_memberships_as_member).
|
98
98
|
group("#{quoted_table_name}.#{connection.quote_column_name('id')}").
|
99
|
-
having("COUNT(DISTINCT #{reflect_on_association(:
|
99
|
+
having("COUNT(DISTINCT #{reflect_on_association(:group_memberships_as_member).klass.quoted_table_name}.#{connection.quote_column_name('group_name')}) = ?", named_groups.count).
|
100
100
|
uniq
|
101
101
|
end
|
102
102
|
|
@@ -65,9 +65,11 @@ module Groupify
|
|
65
65
|
end
|
66
66
|
|
67
67
|
# Define which classes are members of this group
|
68
|
-
def has_members(
|
69
|
-
|
70
|
-
|
68
|
+
def has_members(*names)
|
69
|
+
Array.wrap(names.flatten).each do |name|
|
70
|
+
klass = name.to_s.classify.constantize
|
71
|
+
register(klass)
|
72
|
+
end
|
71
73
|
end
|
72
74
|
|
73
75
|
# Merge two groups. The members of the source become members of the destination, and the source is destroyed.
|
data/lib/groupify/version.rb
CHANGED
data/spec/active_record_spec.rb
CHANGED
@@ -36,7 +36,7 @@ end
|
|
36
36
|
|
37
37
|
require 'groupify/adapter/active_record'
|
38
38
|
|
39
|
-
class User < ActiveRecord::Base
|
39
|
+
class User < ActiveRecord::Base
|
40
40
|
groupify :group_member
|
41
41
|
groupify :named_group_member
|
42
42
|
end
|
@@ -58,15 +58,17 @@ class Project < ActiveRecord::Base
|
|
58
58
|
groupify :named_group_member
|
59
59
|
end
|
60
60
|
|
61
|
-
class Group < ActiveRecord::Base
|
61
|
+
class Group < ActiveRecord::Base
|
62
62
|
groupify :group, members: [:users, :widgets, "namespaced/members"], default_members: :users
|
63
63
|
end
|
64
64
|
|
65
65
|
class Organization < Group
|
66
|
-
|
66
|
+
groupify :group_member
|
67
|
+
|
68
|
+
has_members :managers, :organizations
|
67
69
|
end
|
68
70
|
|
69
|
-
class GroupMembership < ActiveRecord::Base
|
71
|
+
class GroupMembership < ActiveRecord::Base
|
70
72
|
groupify :group_membership
|
71
73
|
end
|
72
74
|
|
@@ -176,7 +178,7 @@ describe Groupify::ActiveRecord do
|
|
176
178
|
it "only adds a member to a group once" do
|
177
179
|
group.add user
|
178
180
|
group.add user
|
179
|
-
expect(user.
|
181
|
+
expect(user.group_memberships_as_member.count).to eq(1)
|
180
182
|
end
|
181
183
|
|
182
184
|
it "adds a namespaced member to a group" do
|
@@ -199,24 +201,31 @@ describe Groupify::ActiveRecord do
|
|
199
201
|
expect { classroom.add(user) }.to raise_error(ActiveRecord::AssociationTypeMismatch)
|
200
202
|
expect { user.groups << classroom }.to raise_error(ActiveRecord::AssociationTypeMismatch)
|
201
203
|
end
|
204
|
+
|
205
|
+
it "allows a group to also act as a member" do
|
206
|
+
parent_org = Organization.create!
|
207
|
+
child_org = Organization.create!
|
208
|
+
parent_org.add(child_org)
|
209
|
+
expect(parent_org.organizations).to include(child_org)
|
210
|
+
end
|
202
211
|
end
|
203
212
|
|
204
213
|
it "lists which member classes can belong to this group" do
|
205
214
|
expect(group.class.member_classes).to include(User, Widget)
|
206
215
|
expect(group.member_classes).to include(User, Widget)
|
207
|
-
|
216
|
+
|
208
217
|
expect(Organization.member_classes).to include(User, Widget, Manager)
|
209
218
|
end
|
210
|
-
|
219
|
+
|
211
220
|
it "finds members by group" do
|
212
221
|
group.add user
|
213
|
-
|
222
|
+
|
214
223
|
expect(User.in_group(group).first).to eql(user)
|
215
224
|
end
|
216
225
|
|
217
226
|
it "finds the group a member belongs to" do
|
218
227
|
group.add user
|
219
|
-
|
228
|
+
|
220
229
|
expect(Group.with_member(user).first).to eq(group)
|
221
230
|
end
|
222
231
|
|
@@ -275,14 +284,14 @@ describe Groupify::ActiveRecord do
|
|
275
284
|
group2 = Group.create!
|
276
285
|
user.groups << group2
|
277
286
|
group3 = Group.create!
|
278
|
-
|
287
|
+
|
279
288
|
expect(user.groups).to include(group)
|
280
289
|
expect(user.groups).to include(group2)
|
281
|
-
|
290
|
+
|
282
291
|
expect(User.in_group(group).first).to eql(user)
|
283
292
|
expect(User.in_group(group2).first).to eql(user)
|
284
293
|
expect(user.in_group?(group)).to be true
|
285
|
-
|
294
|
+
|
286
295
|
expect(User.in_any_group(group).first).to eql(user)
|
287
296
|
expect(User.in_any_group(group3)).to be_empty
|
288
297
|
expect(user.in_any_group?(group2, group3)).to be true
|
@@ -299,7 +308,7 @@ describe Groupify::ActiveRecord do
|
|
299
308
|
widget.groups << group
|
300
309
|
user2 = User.create!
|
301
310
|
user2.groups << group
|
302
|
-
|
311
|
+
|
303
312
|
expect(user.shares_any_group?(widget)).to be true
|
304
313
|
expect(Widget.shares_any_group(user).to_a).to include(widget)
|
305
314
|
expect(User.shares_any_group(widget).to_a).to include(user, user2)
|
@@ -322,7 +331,7 @@ describe Groupify::ActiveRecord do
|
|
322
331
|
|
323
332
|
destination.merge!(source)
|
324
333
|
expect(source.destroyed?).to be true
|
325
|
-
|
334
|
+
|
326
335
|
expect(destination.users).to include(user, manager)
|
327
336
|
expect(destination.managers).to include(manager)
|
328
337
|
end
|
@@ -368,8 +377,8 @@ describe Groupify::ActiveRecord do
|
|
368
377
|
|
369
378
|
context "when using membership types with groups" do
|
370
379
|
it 'adds groups to a member with a specific membership type' do
|
371
|
-
user.
|
372
|
-
|
380
|
+
user.group_memberships_as_member.create!(group: group, as: :admin)
|
381
|
+
|
373
382
|
expect(user.groups).to include(group)
|
374
383
|
expect(group.members).to include(user)
|
375
384
|
expect(group.users).to include(user)
|
@@ -416,13 +425,13 @@ describe Groupify::ActiveRecord do
|
|
416
425
|
it "finds the group a member belongs to with a membership type" do
|
417
426
|
group.add user, as: Manager
|
418
427
|
user.groups.create!
|
419
|
-
|
428
|
+
|
420
429
|
expect(Group.with_member(user).as(Manager)).to eq([group])
|
421
430
|
end
|
422
431
|
|
423
432
|
it "checks if members belong to any groups with a certain membership type" do
|
424
433
|
group2 = Group.create!
|
425
|
-
user.
|
434
|
+
user.group_memberships_as_member.create!([{group: group, as: 'employee'}, {group: group2}])
|
426
435
|
|
427
436
|
expect(User.in_any_group(group, group2).as('employee').first).to eql(user)
|
428
437
|
end
|
@@ -437,8 +446,8 @@ describe Groupify::ActiveRecord do
|
|
437
446
|
it "checks if members belong to all groups with a certain membership type" do
|
438
447
|
group2 = Group.create!
|
439
448
|
group3 = Group.create!
|
440
|
-
user.
|
441
|
-
|
449
|
+
user.group_memberships_as_member.create!([{group: group, as: 'employee'}, {group: group2, as: 'employee'}, {group: group3, as: 'contractor'}])
|
450
|
+
|
442
451
|
expect(User.in_all_groups(group, group2).as('employee').first).to eql(user)
|
443
452
|
expect(User.in_all_groups([group, group2]).as('employee').first).to eql(user)
|
444
453
|
expect(User.in_all_groups(group, group3).as('employee')).to be_empty
|
@@ -452,7 +461,7 @@ describe Groupify::ActiveRecord do
|
|
452
461
|
group2 = Group.create!
|
453
462
|
group3 = Group.create!
|
454
463
|
group4 = Group.create!
|
455
|
-
user.
|
464
|
+
user.group_memberships_as_member.create!([{group: group, as: 'employee'}, {group: group2, as: 'employee'}, {group: group3, as: 'contractor'}, {group: group4, as: 'employee'}])
|
456
465
|
|
457
466
|
expect(User.in_only_groups(group, group2, group4).as('employee').first).to eql(user)
|
458
467
|
expect(User.in_only_groups([group, group2]).as('employee')).to be_empty
|
@@ -467,7 +476,7 @@ describe Groupify::ActiveRecord do
|
|
467
476
|
it "members can check if groups are shared with the same membership type" do
|
468
477
|
user2 = User.create!
|
469
478
|
group.add(user, user2, widget, as: "Sub Group #1")
|
470
|
-
|
479
|
+
|
471
480
|
expect(user.shares_any_group?(widget, as: "Sub Group #1")).to be true
|
472
481
|
expect(Widget.shares_any_group(user).as("Sub Group #1").to_a).to include(widget)
|
473
482
|
expect(User.shares_any_group(widget).as("Sub Group #1").to_a).to include(user, user2)
|
@@ -515,7 +524,7 @@ describe Groupify::ActiveRecord do
|
|
515
524
|
end
|
516
525
|
end
|
517
526
|
end
|
518
|
-
|
527
|
+
|
519
528
|
context 'when using named groups' do
|
520
529
|
before(:each) do
|
521
530
|
user.named_groups.concat :admin, :user, :poster
|
@@ -543,7 +552,7 @@ describe Groupify::ActiveRecord do
|
|
543
552
|
user.named_groups.destroy_all
|
544
553
|
expect(user.named_groups).to be_empty
|
545
554
|
end
|
546
|
-
|
555
|
+
|
547
556
|
it "checks if a member belongs to one named group" do
|
548
557
|
expect(user.in_named_group?(:admin)).to be true
|
549
558
|
expect(User.in_named_group(:admin).first).to eql(user)
|
@@ -576,7 +585,7 @@ describe Groupify::ActiveRecord do
|
|
576
585
|
|
577
586
|
it "checks if named groups are shared" do
|
578
587
|
user2 = User.create!(:named_groups => [:admin])
|
579
|
-
|
588
|
+
|
580
589
|
expect(user.shares_any_named_group?(user2)).to be true
|
581
590
|
expect(User.shares_any_named_group(user).to_a).to include(user2)
|
582
591
|
end
|
@@ -596,15 +605,15 @@ describe Groupify::ActiveRecord do
|
|
596
605
|
it "enforces uniqueness of named groups" do
|
597
606
|
user.named_groups << :team1
|
598
607
|
expect(user.named_groups.count{|g| g == :team1}).to eq(1)
|
599
|
-
expect(user.
|
600
|
-
expect(user.
|
608
|
+
expect(user.group_memberships_as_member.where(group_name: :team1, membership_type: nil).count).to eq(1)
|
609
|
+
expect(user.group_memberships_as_member.where(group_name: :team1, membership_type: 'employee').count).to eq(1)
|
601
610
|
expect(user.named_groups.as('employee').count{|g| g == :team1}).to eq(1)
|
602
611
|
end
|
603
612
|
|
604
613
|
it "enforces uniqueness of group name and membership type for group memberships" do
|
605
614
|
user.named_groups.push :team1, as: 'employee'
|
606
|
-
expect(user.
|
607
|
-
expect(user.
|
615
|
+
expect(user.group_memberships_as_member.where(group_name: :team1, membership_type: nil).count).to eq(1)
|
616
|
+
expect(user.group_memberships_as_member.where(group_name: :team1, membership_type: 'employee').count).to eq(1)
|
608
617
|
expect(user.named_groups.count{|g| g == :team1}).to eq(1)
|
609
618
|
expect(user.named_groups.as('employee').count{|g| g == :team1}).to eq(1)
|
610
619
|
end
|
data/spec/mongoid_spec.rb
CHANGED
@@ -65,7 +65,9 @@ end
|
|
65
65
|
class MongoidGroup
|
66
66
|
include Mongoid::Document
|
67
67
|
|
68
|
-
groupify :group, members: [:mongoid_users, :mongoid_tasks, :mongoid_widgets], default_members: :mongoid_users
|
68
|
+
groupify :group, members: [:mongoid_users, :mongoid_tasks, :mongoid_widgets, :mongoid_groups], default_members: :mongoid_users
|
69
|
+
groupify :group_member, group_class_name: "MongoidGroup"
|
70
|
+
|
69
71
|
alias_method :users, :mongoid_users
|
70
72
|
alias_method :tasks, :mongoid_tasks
|
71
73
|
alias_method :widgets, :mongoid_widgets
|
@@ -97,28 +99,37 @@ describe Groupify::Mongoid do
|
|
97
99
|
expect(group.members).to be_empty
|
98
100
|
end
|
99
101
|
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
102
|
+
context "when adding" do
|
103
|
+
it "adds a group to a member" do
|
104
|
+
user.groups << group
|
105
|
+
expect(user.groups).to include(group)
|
106
|
+
expect(group.members).to include(user)
|
107
|
+
expect(group.users).to include(user)
|
108
|
+
end
|
109
|
+
|
110
|
+
it "adds a member to a group" do
|
111
|
+
expect(user.groups).to be_empty
|
112
|
+
group.add user
|
113
|
+
expect(user.groups).to include(group)
|
114
|
+
expect(group.members).to include(user)
|
115
|
+
end
|
113
116
|
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
117
|
+
it "adds multiple members to a group" do
|
118
|
+
group.add(user, task)
|
119
|
+
expect(group.users).to include(user)
|
120
|
+
expect(group.tasks).to include(task)
|
118
121
|
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
+
users = [MongoidUser.create!, MongoidUser.create!]
|
123
|
+
group.add(users)
|
124
|
+
expect(group.users).to include(*users)
|
125
|
+
end
|
126
|
+
|
127
|
+
it "allows a group to also act as a member" do
|
128
|
+
parent_group = MongoidGroup.create!
|
129
|
+
child_group = MongoidGroup.create!
|
130
|
+
parent_group.add(child_group)
|
131
|
+
expect(parent_group.mongoid_groups).to include(child_group)
|
132
|
+
end
|
122
133
|
end
|
123
134
|
|
124
135
|
it 'lists which member classes can belong to this group' do
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: groupify
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.8.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- dwbutler
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-
|
11
|
+
date: 2016-06-11 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: mongoid
|
@@ -124,6 +124,7 @@ files:
|
|
124
124
|
- ".coveralls.yml"
|
125
125
|
- ".gitignore"
|
126
126
|
- ".rspec"
|
127
|
+
- ".ruby-gemset"
|
127
128
|
- ".travis.yml"
|
128
129
|
- Appraisals
|
129
130
|
- CHANGELOG.md
|