groupify 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +17 -0
- data/.travis.yml +9 -0
- data/Gemfile +4 -0
- data/LICENSE +22 -0
- data/README.md +54 -0
- data/Rakefile +11 -0
- data/groupify.gemspec +28 -0
- data/lib/groupify.rb +100 -0
- data/lib/groupify/version.rb +3 -0
- data/spec/groupify/mongoid_spec.rb +39 -0
- data/spec/spec_helper.rb +9 -0
- metadata +123 -0
data/.gitignore
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 David Butler
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,54 @@
|
|
1
|
+
# Groupify [![Build Status](https://secure.travis-ci.org/dwbutler/groupify.png)](http://travis-ci.org/dwbutler/groupify)
|
2
|
+
Adds group and membership functionality to Rails models.
|
3
|
+
|
4
|
+
Currently only Mongoid is supported. Tested in Ruby 1.8.7 and 1.9.3.
|
5
|
+
|
6
|
+
## Installation
|
7
|
+
|
8
|
+
Add this line to your application's Gemfile:
|
9
|
+
|
10
|
+
gem 'groupify'
|
11
|
+
|
12
|
+
And then execute:
|
13
|
+
|
14
|
+
$ bundle
|
15
|
+
|
16
|
+
Or install it yourself as:
|
17
|
+
|
18
|
+
$ gem install groupify
|
19
|
+
|
20
|
+
## Getting Started
|
21
|
+
In your group model:
|
22
|
+
|
23
|
+
class Group
|
24
|
+
include Mongoid::Document
|
25
|
+
|
26
|
+
acts_as_group
|
27
|
+
end
|
28
|
+
|
29
|
+
In your user model:
|
30
|
+
|
31
|
+
class User
|
32
|
+
include Mongoid::Document
|
33
|
+
|
34
|
+
acts_as_group_member
|
35
|
+
end
|
36
|
+
|
37
|
+
Create groups and add members:
|
38
|
+
|
39
|
+
group = Group.new
|
40
|
+
user = User.new
|
41
|
+
|
42
|
+
user.groups << group
|
43
|
+
or
|
44
|
+
group.add user
|
45
|
+
|
46
|
+
Check the specs for more details.
|
47
|
+
|
48
|
+
## Contributing
|
49
|
+
|
50
|
+
1. Fork it
|
51
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
52
|
+
3. Commit your changes (`git commit -am 'Added some feature'`)
|
53
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
54
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
data/groupify.gemspec
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require File.expand_path('../lib/groupify/version', __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |gem|
|
5
|
+
gem.authors = ["dwbutler"]
|
6
|
+
gem.email = ["dwbutler@ucla.edu"]
|
7
|
+
gem.description = %q{Adds group and membership functionality to Rails models}
|
8
|
+
gem.summary = %q{Group functionality for Rails}
|
9
|
+
gem.homepage = "https://github.com/dwbutler/groupify"
|
10
|
+
|
11
|
+
gem.files = `git ls-files`.split($\)
|
12
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
13
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
14
|
+
gem.name = "groupify"
|
15
|
+
gem.require_paths = ["lib"]
|
16
|
+
gem.version = Groupify::VERSION
|
17
|
+
|
18
|
+
gem.add_development_dependency "rails", "~> 3.2"
|
19
|
+
gem.add_development_dependency "rspec"
|
20
|
+
|
21
|
+
if RUBY_VERSION < '1.9'
|
22
|
+
gem.add_development_dependency "mongoid", "~> 2.0"
|
23
|
+
gem.add_development_dependency 'mongoid-rspec', '1.4.5'
|
24
|
+
else
|
25
|
+
gem.add_development_dependency "mongoid", "~> 3.0"
|
26
|
+
gem.add_development_dependency 'mongoid-rspec', '~> 1.5.1'
|
27
|
+
end
|
28
|
+
end
|
data/lib/groupify.rb
ADDED
@@ -0,0 +1,100 @@
|
|
1
|
+
require "groupify/version"
|
2
|
+
require 'active_support'
|
3
|
+
require 'mongoid'
|
4
|
+
|
5
|
+
# Groups and members
|
6
|
+
module Groupify
|
7
|
+
extend ActiveSupport::Concern
|
8
|
+
|
9
|
+
module ClassMethods
|
10
|
+
def acts_as_group(opts = {})
|
11
|
+
include Groupify::Group
|
12
|
+
|
13
|
+
if (member_klass = opts.delete :default_members)
|
14
|
+
default_member_class = member_klass.to_s.classify.constantize
|
15
|
+
end
|
16
|
+
|
17
|
+
if (member_klasses = opts.delete :members)
|
18
|
+
member_klasses.each do |member_klass|
|
19
|
+
has_members(member_klass)
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
def acts_as_group_member(opts = {})
|
25
|
+
class_eval { @group_class_name = opts[:class_name] || 'Group' }
|
26
|
+
include Groupify::GroupMember
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
# Functionality related to groups.
|
31
|
+
# Usage:
|
32
|
+
# class Group
|
33
|
+
# acts_as_group, :members => [:users]
|
34
|
+
# ...
|
35
|
+
# end
|
36
|
+
#
|
37
|
+
module Group
|
38
|
+
extend ActiveSupport::Concern
|
39
|
+
|
40
|
+
included do
|
41
|
+
@default_member_class = nil
|
42
|
+
end
|
43
|
+
|
44
|
+
def members
|
45
|
+
self.class.default_member_class.any_in(:group_ids => [self.id])
|
46
|
+
end
|
47
|
+
|
48
|
+
def add(member)
|
49
|
+
member.groups << self
|
50
|
+
end
|
51
|
+
|
52
|
+
module ClassMethods
|
53
|
+
def default_member_class; @default_member_class || User; end
|
54
|
+
def default_member_class=(klass); @default_member_class = klass; end
|
55
|
+
|
56
|
+
def has_members(name)
|
57
|
+
klass = name.to_s.classify.constantize
|
58
|
+
define_method name.to_s.pluralize.underscore do
|
59
|
+
klass.any_in(:group_ids => [self.id])
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
# Functionality related to group members.
|
66
|
+
# Usage:
|
67
|
+
# class User
|
68
|
+
# acts_as_group_member
|
69
|
+
# ...
|
70
|
+
# end
|
71
|
+
#
|
72
|
+
module GroupMember
|
73
|
+
extend ActiveSupport::Concern
|
74
|
+
|
75
|
+
included do
|
76
|
+
group_class_name='Group' unless defined?(@group_class_name)
|
77
|
+
has_and_belongs_to_many :groups, :autosave => true, :inverse_of => nil, :class_name => @group_class_name
|
78
|
+
end
|
79
|
+
|
80
|
+
def in_group?(group); self.groups.include? group; end
|
81
|
+
def in_any_group?(*groups); (self.groups & groups.flatten).present?; end
|
82
|
+
def in_all_groups?(*groups); self.groups == groups.flatten; end
|
83
|
+
def shares_any_group?(other)
|
84
|
+
in_any_group?(other.groups)
|
85
|
+
end
|
86
|
+
|
87
|
+
module ClassMethods
|
88
|
+
def group_class_name; @group_class_name || 'Group'; end
|
89
|
+
def group_class_name=(klass); @group_class_name = klass; end
|
90
|
+
|
91
|
+
def none; where(:id => nil); end
|
92
|
+
def in_group(group); group.present? ? where(:group_ids.in => [group.id]) : none; end
|
93
|
+
def in_any_group(*groups); groups.present? ? where(:group_ids.in => groups.flatten.map{|g|g.id}) : none; end
|
94
|
+
def in_all_groups(*groups); groups.present? ? where(:group_ids => groups.flatten.map{|g|g.id}) : none; end
|
95
|
+
def shares_any_group(other); in_any_group(other.groups); end
|
96
|
+
end
|
97
|
+
end
|
98
|
+
end
|
99
|
+
|
100
|
+
Mongoid::Document.send :include, Groupify
|
@@ -0,0 +1,39 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'mongoid'
|
3
|
+
require 'mongoid-rspec'
|
4
|
+
include Mongoid::Matchers
|
5
|
+
|
6
|
+
class MongoidUser
|
7
|
+
include Mongoid::Document
|
8
|
+
|
9
|
+
acts_as_group_member :class_name => 'MongoidGroup'
|
10
|
+
end
|
11
|
+
|
12
|
+
class MongoidTask
|
13
|
+
include Mongoid::Document
|
14
|
+
|
15
|
+
acts_as_group_member :class_name => 'MongoidGroup'
|
16
|
+
end
|
17
|
+
|
18
|
+
class MongoidGroup
|
19
|
+
include Mongoid::Document
|
20
|
+
|
21
|
+
acts_as_group :members => [:mongoid_users, :mongoid_tasks], :default_members => :mongoid_users
|
22
|
+
|
23
|
+
end
|
24
|
+
|
25
|
+
describe MongoidGroup do
|
26
|
+
it { should respond_to :members}
|
27
|
+
it { should respond_to :add }
|
28
|
+
end
|
29
|
+
|
30
|
+
describe MongoidUser do
|
31
|
+
it { should respond_to :groups}
|
32
|
+
it { should respond_to :in_group?}
|
33
|
+
it { should respond_to :in_any_group?}
|
34
|
+
it { should respond_to :in_all_groups?}
|
35
|
+
it { should respond_to :shares_any_group?}
|
36
|
+
|
37
|
+
# Mongoid specific
|
38
|
+
it { should have_and_belong_to_many(:groups).of_type(MongoidGroup) }
|
39
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,123 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: groupify
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- dwbutler
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-08-25 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
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
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ~>
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '3.2'
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: rspec
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ! '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
type: :development
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: mongoid
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ~>
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '3.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: '3.0'
|
62
|
+
- !ruby/object:Gem::Dependency
|
63
|
+
name: mongoid-rspec
|
64
|
+
requirement: !ruby/object:Gem::Requirement
|
65
|
+
none: false
|
66
|
+
requirements:
|
67
|
+
- - ~>
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: 1.5.1
|
70
|
+
type: :development
|
71
|
+
prerelease: false
|
72
|
+
version_requirements: !ruby/object:Gem::Requirement
|
73
|
+
none: false
|
74
|
+
requirements:
|
75
|
+
- - ~>
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: 1.5.1
|
78
|
+
description: Adds group and membership functionality to Rails models
|
79
|
+
email:
|
80
|
+
- dwbutler@ucla.edu
|
81
|
+
executables: []
|
82
|
+
extensions: []
|
83
|
+
extra_rdoc_files: []
|
84
|
+
files:
|
85
|
+
- .gitignore
|
86
|
+
- .travis.yml
|
87
|
+
- Gemfile
|
88
|
+
- LICENSE
|
89
|
+
- README.md
|
90
|
+
- Rakefile
|
91
|
+
- groupify.gemspec
|
92
|
+
- lib/groupify.rb
|
93
|
+
- lib/groupify/version.rb
|
94
|
+
- spec/groupify/mongoid_spec.rb
|
95
|
+
- spec/spec_helper.rb
|
96
|
+
homepage: https://github.com/dwbutler/groupify
|
97
|
+
licenses: []
|
98
|
+
post_install_message:
|
99
|
+
rdoc_options: []
|
100
|
+
require_paths:
|
101
|
+
- lib
|
102
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
103
|
+
none: false
|
104
|
+
requirements:
|
105
|
+
- - ! '>='
|
106
|
+
- !ruby/object:Gem::Version
|
107
|
+
version: '0'
|
108
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
109
|
+
none: false
|
110
|
+
requirements:
|
111
|
+
- - ! '>='
|
112
|
+
- !ruby/object:Gem::Version
|
113
|
+
version: '0'
|
114
|
+
requirements: []
|
115
|
+
rubyforge_project:
|
116
|
+
rubygems_version: 1.8.24
|
117
|
+
signing_key:
|
118
|
+
specification_version: 3
|
119
|
+
summary: Group functionality for Rails
|
120
|
+
test_files:
|
121
|
+
- spec/groupify/mongoid_spec.rb
|
122
|
+
- spec/spec_helper.rb
|
123
|
+
has_rdoc:
|