groupify 0.4.2 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: f67a4e4fc525ccf5ec5d481bad9faef6a49e9c0e
4
- data.tar.gz: 452badaf83a0fd0e5e1eecd39d7e3341c00ad8e5
3
+ metadata.gz: 14495b01edcc63635a6d0b077f5fb0227d7d6022
4
+ data.tar.gz: 4adb33988bdc07fbe2d4a64d4d59c806b51274a9
5
5
  SHA512:
6
- metadata.gz: fc58d2e804fd86761855d4a85154f7cfeddc714be60c009484e6f3ca316ee3980ca1b8fe244bb15f27e2b21dfcfb916e6aa60cc0e829cdc3d90500b04c5c8778
7
- data.tar.gz: 857bbc22e823eed129a2b281e611b1f1903424af03c62e2cdea36e9e063ac9b323563bf3541148978a5af147fcaeb68d6967aa247c0a40771a656075ec4928b3
6
+ metadata.gz: df8017bb4f1e2b123b1bcad6c209f744c03b06161d70fa1a7153f89d34f36bdc4ec79a4cfbda27b665263aecf03bad850fc0ada10fb67570b8b73bda03aedb2b
7
+ data.tar.gz: 010dd7484806af265d6e869dd98de25ff07e51c527dc80231ebde7928913ee73c77df5b7607c580a5fa6fb3afe7311f224f28145ab9eb2eef8def37d175099a7
@@ -3,8 +3,10 @@ services: mongodb
3
3
  rvm:
4
4
  - 1.9.3
5
5
  - 2.0.0
6
- - jruby-19mode # JRuby in 1.9 mode
7
- - rbx-19mode
6
+ - 2.1.0
7
+ - 2.1.1
8
+ - jruby-19mode
9
+ #- rbx
8
10
  gemfile:
9
11
  - gemfiles/activesupport3.gemfile
10
12
  - gemfiles/activesupport4.gemfile
data/Gemfile CHANGED
@@ -1,5 +1,9 @@
1
1
  source 'https://rubygems.org'
2
2
 
3
+ group :development do
4
+ gem 'pry'
5
+ end
6
+
3
7
  # Specify your gem's dependencies in groupify.gemspec
4
8
  gemspec
5
9
 
data/README.md CHANGED
@@ -1,11 +1,13 @@
1
- # Groupify [![Build Status](https://secure.travis-ci.org/dwbutler/groupify.png)](http://travis-ci.org/dwbutler/groupify) [![Dependency Status](https://gemnasium.com/dwbutler/groupify.png)](https://gemnasium.com/dwbutler/groupify)
1
+ # Groupify [![Build Status](https://secure.travis-ci.org/dwbutler/groupify.png)](http://travis-ci.org/dwbutler/groupify) [![Dependency Status](https://gemnasium.com/dwbutler/groupify.png)](https://gemnasium.com/dwbutler/groupify) [![Code Climate](https://codeclimate.com/github/dwbutler/groupify.png)](https://codeclimate.com/github/dwbutler/groupify)
2
+
2
3
  Adds group and membership functionality to Rails models.
3
4
 
4
5
  The following ORMs are supported:
5
- Mongoid 3.1 & 4.0, ActiveRecord 3.2 & 4.0
6
+ Mongoid 3.1 & 4.0, ActiveRecord 3.2 & 4.x
6
7
 
7
8
  The following Rubies are supported:
8
- Ruby 1.9.3, 2.0.0 (MRI, REE, JRuby, and Rubinius).
9
+ * MRI Ruby 1.9.3, 2.0.x, 2.1.x
10
+ * JRuby (1.9 mode)
9
11
 
10
12
  ## Installation
11
13
 
@@ -21,8 +23,6 @@ Or install it yourself as:
21
23
 
22
24
  $ gem install groupify
23
25
 
24
- ## Getting Started
25
-
26
26
  ### Active Record
27
27
  Add a migration similar to the following:
28
28
 
@@ -68,6 +68,14 @@ class Assignment < ActiveRecord::Base
68
68
  end
69
69
  ```
70
70
 
71
+ You will also need to define a `GroupMembership` model to join groups to members:
72
+
73
+ ```ruby
74
+ class GroupMembership < ActiveRecord::Base
75
+ acts_as_group_membership
76
+ end
77
+ ```
78
+
71
79
  ### Mongoid
72
80
  In your group model:
73
81
 
@@ -104,6 +112,9 @@ group.add user
104
112
 
105
113
  user.in_group?(group)
106
114
  # => true
115
+
116
+ # Add multiple members at once
117
+ group.add(user, widget, task)
107
118
  ```
108
119
 
109
120
  Add to named groups:
@@ -5,7 +5,8 @@ gem 'activerecord', "~> 4"
5
5
  # TODO: Enable once Mongoid 4 is released
6
6
  #gem "mongoid", ">= 4.0"
7
7
 
8
- gem "mongoid", :github => "mongoid/mongoid"
8
+ #gem "mongoid", :github => "mongoid/mongoid"
9
+ gem "mongoid", "4.0.0.beta1"
9
10
  gem 'mongoid-rspec'
10
11
 
11
12
  gem 'database_cleaner'
@@ -20,4 +21,4 @@ platform :jruby do
20
21
  gem "jdbc-sqlite3"
21
22
  # See https://github.com/jruby/activerecord-jdbc-adapter/issues/253
22
23
  gem "activerecord-jdbcsqlite3-adapter", :github => 'jruby/activerecord-jdbc-adapter', :branch => 'v1.3.0.beta2'
23
- end
24
+ end
@@ -14,6 +14,7 @@ Gem::Specification.new do |gem|
14
14
  gem.name = "groupify"
15
15
  gem.require_paths = ["lib"]
16
16
  gem.version = Groupify::VERSION
17
+ gem.license = 'MIT'
17
18
 
18
19
  gem.add_development_dependency "mongoid", ">= 3.1"
19
20
  gem.add_development_dependency "activerecord", ">= 3.2"
@@ -61,17 +61,17 @@ module Groupify
61
61
  @member_klasses ||= Set.new
62
62
  has_many :group_memberships, :dependent => :destroy
63
63
  end
64
-
65
- def members
66
- self.class.default_member_class.joins(:group_memberships).where(:group_memberships => {:member_type => self.class.default_member_class.to_s}).uniq
67
- end
68
64
 
69
65
  def member_classes
70
66
  self.class.member_classes
71
67
  end
72
68
 
73
- def add(member)
74
- member.groups << self
69
+ def add(*members)
70
+ clear_association_cache
71
+
72
+ members.flatten.each do |member|
73
+ member.groups << self
74
+ end
75
75
  end
76
76
 
77
77
  # Merge a source group into this group.
@@ -86,7 +86,7 @@ module Groupify
86
86
  end
87
87
 
88
88
  def default_member_class
89
- @default_member_class ||= register(User)
89
+ @default_member_class ||= (User rescue false)
90
90
  end
91
91
 
92
92
  def default_member_class=(klass)
@@ -102,11 +102,6 @@ module Groupify
102
102
  def has_members(name)
103
103
  klass = name.to_s.classify.constantize
104
104
  register(klass)
105
-
106
- # Define specific members accessor, i.e. group.users
107
- define_method name.to_s.pluralize.underscore do
108
- klass.joins(:group_memberships).where(:group_memberships => {:group_id => self.id}).uniq
109
- end
110
105
  end
111
106
 
112
107
  # Merge two groups. The members of the source become members of the destination, and the source is destroyed.
@@ -129,8 +124,21 @@ module Groupify
129
124
 
130
125
  def register(member_klass)
131
126
  (@member_klasses ||= Set.new) << member_klass
127
+
128
+ associate_member_class(member_klass)
129
+
132
130
  member_klass
133
131
  end
132
+
133
+ def associate_member_class(member_klass)
134
+ association_name = member_klass.name.to_s.pluralize.underscore.to_sym
135
+ source_type = member_klass.base_class
136
+ has_many association_name, :through => :group_memberships, :source => :member, :source_type => source_type
137
+
138
+ if member_klass == default_member_class
139
+ has_many :members, :through => :group_memberships, :source => :member, :source_type => source_type
140
+ end
141
+ end
134
142
  end
135
143
  end
136
144
 
@@ -52,7 +52,7 @@ module Groupify
52
52
  @default_member_class = nil
53
53
  @member_klasses ||= Set.new
54
54
  end
55
-
55
+
56
56
  def members
57
57
  self.class.default_member_class.any_in(:group_ids => [self.id])
58
58
  end
@@ -61,8 +61,10 @@ module Groupify
61
61
  self.class.member_classes
62
62
  end
63
63
 
64
- def add(member)
65
- member.groups << self
64
+ def add(*members)
65
+ members.flatten.each do |member|
66
+ member.groups << self
67
+ end
66
68
  end
67
69
 
68
70
  # Merge a source group into this group.
@@ -110,9 +112,10 @@ module Groupify
110
112
  end
111
113
 
112
114
  source_group.member_classes.each do |klass|
113
- klass.any_in(:group_ids => [source_group.id]).update(:$set => {:"group_ids.$" => destination_group.id})
115
+ klass.any_in(:group_ids => [source_group.id]).update_all(:$set => {:"group_ids.$" => destination_group.id})
114
116
  end
115
- source_group.destroy
117
+
118
+ source_group.delete
116
119
  end
117
120
 
118
121
  protected
@@ -1,3 +1,3 @@
1
1
  module Groupify
2
- VERSION = "0.4.2"
2
+ VERSION = "0.5.0"
3
3
  end
@@ -1,6 +1,8 @@
1
1
  require 'spec_helper'
2
2
  require 'active_record'
3
3
 
4
+ puts "ActiveRecord version #{ActiveSupport::VERSION::STRING}"
5
+
4
6
  # Load database config
5
7
  if JRUBY
6
8
  require 'jdbc/sqlite3'
@@ -63,6 +65,9 @@ class User < ActiveRecord::Base
63
65
  acts_as_named_group_member
64
66
  end
65
67
 
68
+ class Manager < User
69
+ end
70
+
66
71
  class Widget < ActiveRecord::Base
67
72
  acts_as_group_member
68
73
  end
@@ -71,9 +76,6 @@ class Group < ActiveRecord::Base
71
76
  acts_as_group :members => [:users, :widgets], :default_members => :users
72
77
  end
73
78
 
74
- class Manager < User
75
- end
76
-
77
79
  class Organization < Group
78
80
  has_members :managers
79
81
  end
@@ -95,10 +97,20 @@ describe User do
95
97
  it { should respond_to :shares_any_group?}
96
98
  end
97
99
 
100
+ #ActiveRecord::Base.logger = Logger.new(STDOUT)
101
+
98
102
  describe Groupify::ActiveRecord do
99
103
  let(:user) { User.create! }
100
104
  let(:group) { Group.create! }
101
105
  let(:widget) { Widget.create! }
106
+
107
+ it "members and groups are empty when initialized" do
108
+ user.groups.should be_empty
109
+ User.new.groups.should be_empty
110
+
111
+ Group.new.members.should be_empty
112
+ group.members.should be_empty
113
+ end
102
114
 
103
115
  it "adds a group to a member" do
104
116
  user.groups << group
@@ -113,6 +125,16 @@ describe Groupify::ActiveRecord do
113
125
  group.members.should include(user)
114
126
  end
115
127
 
128
+ it "adds multiple members to a group" do
129
+ group.add(user, widget)
130
+ group.users.should include(user)
131
+ group.widgets.should include(widget)
132
+
133
+ users = [User.create!, User.create!]
134
+ group.add(users)
135
+ group.users.should include(*users)
136
+ end
137
+
116
138
  it 'lists which member classes can belong to this group' do
117
139
  group.class.member_classes.should include(User, Widget)
118
140
  group.member_classes.should include(User, Widget)
@@ -234,8 +256,13 @@ describe Groupify::ActiveRecord do
234
256
 
235
257
  it "members can check if groups are shared" do
236
258
  user.groups << group
259
+ widget.groups << group
237
260
  user2 = User.create!(:groups => [group])
238
261
 
262
+ user.shares_any_group?(widget).should be_true
263
+ Widget.shares_any_group(user).to_a.should include(widget)
264
+ User.shares_any_group(widget).to_a.should include(user, user2)
265
+
239
266
  user.shares_any_group?(user2).should be_true
240
267
  User.shares_any_group(user).to_a.should include(user2)
241
268
  end
@@ -17,11 +17,14 @@ RSpec.configure do |config|
17
17
  end
18
18
 
19
19
  require 'mongoid'
20
+ puts "Mongoid version #{Mongoid::VERSION}"
21
+
20
22
  require 'mongoid-rspec'
21
23
  include Mongoid::Matchers
22
24
 
23
25
  # Load mongoid config
24
26
  Mongoid.load!('./spec/groupify/mongoid.yml', :test)
27
+ #Moped.logger = Logger.new(STDOUT)
25
28
 
26
29
  require 'groupify'
27
30
  require 'groupify/adapter/mongoid'
@@ -33,6 +36,9 @@ class MongoidUser
33
36
  acts_as_named_group_member
34
37
  end
35
38
 
39
+ class MongoidManager < MongoidUser
40
+ end
41
+
36
42
  class MongoidTask
37
43
  include Mongoid::Document
38
44
 
@@ -55,7 +61,9 @@ end
55
61
 
56
62
  class MongoidProject < MongoidGroup
57
63
  has_members :mongoid_issues
64
+ has_members :mongoid_managers
58
65
  alias_method :issues, :mongoid_issues
66
+ alias_method :managers, :mongoid_managers
59
67
  end
60
68
 
61
69
  describe MongoidGroup do
@@ -79,6 +87,15 @@ describe Groupify::Mongoid do
79
87
  let!(:group) { MongoidGroup.create! }
80
88
  let(:task) { MongoidTask.create! }
81
89
  let(:issue) { MongoidIssue.create! }
90
+ let(:manager) { MongoidManager.create! }
91
+
92
+ it "members and groups are empty when initialized" do
93
+ user.groups.should be_empty
94
+ MongoidUser.new.groups.should be_empty
95
+
96
+ MongoidGroup.new.members.should be_empty
97
+ group.members.should be_empty
98
+ end
82
99
 
83
100
  it "adds a group to a member" do
84
101
  user.groups << group
@@ -93,6 +110,16 @@ describe Groupify::Mongoid do
93
110
  group.members.should include(user)
94
111
  end
95
112
 
113
+ it "adds multiple members to a group" do
114
+ group.add(user, task)
115
+ group.users.should include(user)
116
+ group.tasks.should include(task)
117
+
118
+ users = [MongoidUser.create!, MongoidUser.create!]
119
+ group.add(users)
120
+ group.users.should include(*users)
121
+ end
122
+
96
123
  it 'lists which member classes can belong to this group' do
97
124
  group.class.member_classes.should include(MongoidUser, MongoidTask)
98
125
  group.member_classes.should include(MongoidUser, MongoidTask)
@@ -137,12 +164,14 @@ describe Groupify::Mongoid do
137
164
  destination = MongoidProject.create!
138
165
 
139
166
  source.add(user)
167
+ source.add(manager)
140
168
  destination.add(task)
141
169
 
142
170
  destination.merge!(source)
143
171
  source.destroyed?.should be_true
144
172
 
145
173
  destination.users.to_a.should include(user)
174
+ destination.managers.to_a.should include(manager)
146
175
  destination.tasks.to_a.should include(task)
147
176
  end
148
177
 
@@ -3,8 +3,10 @@ require 'bundler/setup'
3
3
 
4
4
  Bundler.require(:default, :test, :development)
5
5
 
6
+ #require 'pry'
7
+
6
8
  JRUBY = defined?(JRUBY_VERSION)
7
9
 
8
10
  RSpec.configure do |config|
9
11
  config.order = "random"
10
- end
12
+ end
metadata CHANGED
@@ -1,111 +1,111 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: groupify
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.2
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - dwbutler
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-07-02 00:00:00.000000000 Z
11
+ date: 2014-03-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: mongoid
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - '>='
17
+ - - ">="
18
18
  - !ruby/object:Gem::Version
19
19
  version: '3.1'
20
20
  type: :development
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - '>='
24
+ - - ">="
25
25
  - !ruby/object:Gem::Version
26
26
  version: '3.1'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: activerecord
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
- - - '>='
31
+ - - ">="
32
32
  - !ruby/object:Gem::Version
33
33
  version: '3.2'
34
34
  type: :development
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
- - - '>='
38
+ - - ">="
39
39
  - !ruby/object:Gem::Version
40
40
  version: '3.2'
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: sqlite3
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
- - - '>='
45
+ - - ">="
46
46
  - !ruby/object:Gem::Version
47
47
  version: '0'
48
48
  type: :development
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
- - - '>='
52
+ - - ">="
53
53
  - !ruby/object:Gem::Version
54
54
  version: '0'
55
55
  - !ruby/object:Gem::Dependency
56
56
  name: rake
57
57
  requirement: !ruby/object:Gem::Requirement
58
58
  requirements:
59
- - - '>='
59
+ - - ">="
60
60
  - !ruby/object:Gem::Version
61
61
  version: '0'
62
62
  type: :development
63
63
  prerelease: false
64
64
  version_requirements: !ruby/object:Gem::Requirement
65
65
  requirements:
66
- - - '>='
66
+ - - ">="
67
67
  - !ruby/object:Gem::Version
68
68
  version: '0'
69
69
  - !ruby/object:Gem::Dependency
70
70
  name: rspec
71
71
  requirement: !ruby/object:Gem::Requirement
72
72
  requirements:
73
- - - '>='
73
+ - - ">="
74
74
  - !ruby/object:Gem::Version
75
75
  version: '0'
76
76
  type: :development
77
77
  prerelease: false
78
78
  version_requirements: !ruby/object:Gem::Requirement
79
79
  requirements:
80
- - - '>='
80
+ - - ">="
81
81
  - !ruby/object:Gem::Version
82
82
  version: '0'
83
83
  - !ruby/object:Gem::Dependency
84
84
  name: mongoid-rspec
85
85
  requirement: !ruby/object:Gem::Requirement
86
86
  requirements:
87
- - - '>='
87
+ - - ">="
88
88
  - !ruby/object:Gem::Version
89
89
  version: '0'
90
90
  type: :development
91
91
  prerelease: false
92
92
  version_requirements: !ruby/object:Gem::Requirement
93
93
  requirements:
94
- - - '>='
94
+ - - ">="
95
95
  - !ruby/object:Gem::Version
96
96
  version: '0'
97
97
  - !ruby/object:Gem::Dependency
98
98
  name: database_cleaner
99
99
  requirement: !ruby/object:Gem::Requirement
100
100
  requirements:
101
- - - '>='
101
+ - - ">="
102
102
  - !ruby/object:Gem::Version
103
103
  version: '0'
104
104
  type: :development
105
105
  prerelease: false
106
106
  version_requirements: !ruby/object:Gem::Requirement
107
107
  requirements:
108
- - - '>='
108
+ - - ">="
109
109
  - !ruby/object:Gem::Version
110
110
  version: '0'
111
111
  description: Adds group and membership functionality to Rails models
@@ -115,8 +115,8 @@ executables: []
115
115
  extensions: []
116
116
  extra_rdoc_files: []
117
117
  files:
118
- - .gitignore
119
- - .travis.yml
118
+ - ".gitignore"
119
+ - ".travis.yml"
120
120
  - Gemfile
121
121
  - LICENSE
122
122
  - README.md
@@ -134,7 +134,8 @@ files:
134
134
  - spec/groupify/mongoid_spec.rb
135
135
  - spec/spec_helper.rb
136
136
  homepage: https://github.com/dwbutler/groupify
137
- licenses: []
137
+ licenses:
138
+ - MIT
138
139
  metadata: {}
139
140
  post_install_message:
140
141
  rdoc_options: []
@@ -142,17 +143,17 @@ require_paths:
142
143
  - lib
143
144
  required_ruby_version: !ruby/object:Gem::Requirement
144
145
  requirements:
145
- - - '>='
146
+ - - ">="
146
147
  - !ruby/object:Gem::Version
147
148
  version: '0'
148
149
  required_rubygems_version: !ruby/object:Gem::Requirement
149
150
  requirements:
150
- - - '>='
151
+ - - ">="
151
152
  - !ruby/object:Gem::Version
152
153
  version: '0'
153
154
  requirements: []
154
155
  rubyforge_project:
155
- rubygems_version: 2.0.3
156
+ rubygems_version: 2.2.1
156
157
  signing_key:
157
158
  specification_version: 4
158
159
  summary: Group functionality for Rails