camaraderie 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,2 @@
1
+ source 'https://rubygems.org'
2
+ gemspec
data/LICENSE.md ADDED
@@ -0,0 +1,26 @@
1
+ Copyright (c) 2013, Mirego
2
+ All rights reserved.
3
+
4
+ Redistribution and use in source and binary forms, with or without
5
+ modification, are permitted provided that the following conditions are met:
6
+
7
+ - Redistributions of source code must retain the above copyright notice,
8
+ this list of conditions and the following disclaimer.
9
+ - Redistributions in binary form must reproduce the above copyright notice,
10
+ this list of conditions and the following disclaimer in the documentation
11
+ and/or other materials provided with the distribution.
12
+ - Neither the name of the Mirego nor the names of its contributors may
13
+ be used to endorse or promote products derived from this software without
14
+ specific prior written permission.
15
+
16
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17
+ AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18
+ IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19
+ ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20
+ LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21
+ CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22
+ SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23
+ INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24
+ CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25
+ ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26
+ POSSIBILITY OF SUCH DAMAGE.
data/README.md ADDED
@@ -0,0 +1,68 @@
1
+ # Camaraderie
2
+
3
+ Camaraderie takes away the pain of managing membership stuff between users and organizations.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'camaraderie'
11
+ ```
12
+
13
+ Then run the task to generate the migration:
14
+
15
+ ```bash
16
+ $ rails generate camaraderie:install
17
+ ```
18
+
19
+ ## Usage
20
+
21
+ First, you have to configure which type of memberships you want. Usually, you’d do this in `./config/initializers/camaraderie.rb`:
22
+
23
+ ```ruby
24
+ Camaraderie.configure do |config|
25
+ config.membership_types = %w(admin moderator member)
26
+ end
27
+ ```
28
+
29
+ Then, you can include the two provided modules into your `User`, `Organization` and `Membership` models:
30
+
31
+ ```ruby
32
+ class User < ActiveRecord::Base
33
+ acts_as_user
34
+ end
35
+
36
+ class Organization < ActiveRecord::Base
37
+ acts_as_organization
38
+ end
39
+
40
+ class Membership < ActiveRecord::Base
41
+ acts_as_membership
42
+ end
43
+ ```
44
+
45
+ You’re pretty much done after that. You’re now able to do this:
46
+
47
+ ```ruby
48
+ user = User.find(1)
49
+ organization = Organization.find(1)
50
+
51
+ # Add the user as an admin
52
+ organization.admins.create(user: user)
53
+
54
+ # Add the user as a regular member
55
+ organization.members.create(user: user)
56
+
57
+ # Check whether the user is an admin
58
+ user.admin_of?(organization)
59
+ # => true
60
+ ```
61
+
62
+ ## License
63
+
64
+ `Camaraderie` is © 2013 [Mirego](http://www.mirego.com) and may be freely distributed under the [New BSD license](http://opensource.org/licenses/BSD-3-Clause). See the [`LICENSE.md`](https://github.com/mirego/camaraderie/blob/master/LICENSE.md) file.
65
+
66
+ ## About Mirego
67
+
68
+ Mirego is a team of passionate people who believe that work is a place where you can innovate and have fun. We proudly built mobile applications for [iPhone](http://mirego.com/en/iphone-app-development/ "iPhone application development"), [iPad](http://mirego.com/en/ipad-app-development/ "iPad application development"), [Android](http://mirego.com/en/android-app-development/ "Android application development"), [Blackberry](http://mirego.com/en/blackberry-app-development/ "Blackberry application development"), [Windows Phone](http://mirego.com/en/windows-phone-app-development/ "Windows Phone application development") and [Windows 8](http://mirego.com/en/windows-8-app-development/ "Windows 8 application development").
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,26 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'camaraderie/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'camaraderie'
8
+ spec.version = Camaraderie::VERSION
9
+ spec.authors = ['Rémi Prévost']
10
+ spec.email = ['rprevost@mirego.com']
11
+ spec.description = 'Camaraderie takes away the pain of managing membership stuff between users and organizations.'
12
+ spec.summary = 'Camaraderie takes away the pain of managing membership stuff between users and organizations.'
13
+ spec.homepage = 'https://github.com/mirego/camaraderie'
14
+ spec.license = 'BSD 3-Clause'
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ['lib']
20
+
21
+ spec.add_development_dependency 'bundler', '~> 1.3'
22
+ spec.add_development_dependency 'rake'
23
+
24
+ spec.add_dependency 'activerecord', '>= 3.0.0'
25
+ spec.add_dependency 'activesupport', '>= 3.0.0'
26
+ end
@@ -0,0 +1,24 @@
1
+ module Camaraderie
2
+ module Membership
3
+ extend ActiveSupport::Concern
4
+ included do
5
+ # Associations
6
+ belongs_to :user, validate: true
7
+ belongs_to :organization
8
+
9
+ # Validations
10
+ validates :user, presence: true
11
+ validates :organization, presence: true
12
+ validates :membership_type, presence: true, inclusion: { in: Camaraderie.membership_types }, uniqueness: { scope: [:user_id, :organization_id] }
13
+
14
+ # Scopes
15
+ Camaraderie.membership_types.each do |type|
16
+ scope type.pluralize, lambda { where(membership_type: type) }
17
+ end
18
+
19
+ # Nested attributes
20
+ accepts_nested_attributes_for :user
21
+ accepts_nested_attributes_for :organization
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,24 @@
1
+ module Camaraderie
2
+ module Organization
3
+ extend ActiveSupport::Concern
4
+
5
+ included do
6
+ # Associations
7
+ has_many :memberships, dependent: :destroy
8
+ has_many :users, through: :memberships
9
+
10
+ # Define a method for each type of membership
11
+ #
12
+ # @example
13
+ # user = User.new(email: 'foo@example.com')
14
+ # Organization.first.admins.create(user: user)
15
+ Camaraderie.membership_types.each do |type|
16
+ class_eval <<-RUBY, __FILE__, __LINE__ + 1
17
+ def #{type.pluralize}
18
+ memberships.#{type.pluralize}
19
+ end
20
+ RUBY
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,10 @@
1
+ require 'camaraderie'
2
+ require 'rails'
3
+
4
+ module Camaraderie
5
+ class Railtie < Rails::Railtie
6
+ initializer 'camaraderie.active_record' do |app|
7
+ ActiveSupport.on_load :active_record, {}, &Camaraderie.inject_into_active_record
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,19 @@
1
+ module Camaraderie
2
+ module User
3
+ extend ActiveSupport::Concern
4
+
5
+ included do
6
+ # Associations
7
+ has_many :memberships, dependent: :destroy
8
+ has_many :organizations, through: :memberships
9
+
10
+ Camaraderie.membership_types.each do |type|
11
+ class_eval <<-RUBY, __FILE__, __LINE__ + 1
12
+ def #{type}_of?(organization)
13
+ !!memberships.admins.where(organization: organization).exists?
14
+ end
15
+ RUBY
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,3 @@
1
+ module Camaraderie
2
+ VERSION = '0.1.2'
3
+ end
@@ -0,0 +1,37 @@
1
+ require 'camaraderie/version'
2
+
3
+ require 'camaraderie/user'
4
+ require 'camaraderie/organization'
5
+ require 'camaraderie/membership'
6
+
7
+ module Camaraderie
8
+ # Yield a block to edit the @configuration variable
9
+ def self.configure
10
+ @configuration = OpenStruct.new
11
+ yield(@configuration)
12
+ end
13
+
14
+ # Return the allowed membership types
15
+ def self.membership_types
16
+ @configuration.membership_types
17
+ end
18
+
19
+ # Return a block that can be injected into a class
20
+ def self.inject_into_active_record
21
+ @inject_into_active_record ||= Proc.new do
22
+ def self.acts_as_user
23
+ self.send :include, Camaraderie::User
24
+ end
25
+
26
+ def self.acts_as_organization
27
+ self.send :include, Camaraderie::Organization
28
+ end
29
+
30
+ def self.acts_as_membership
31
+ self.send :include, Camaraderie::Membership
32
+ end
33
+ end
34
+ end
35
+ end
36
+
37
+ require 'camaraderie/railtie' if defined?(Rails) && Rails::VERSION::MAJOR >= 3
@@ -0,0 +1,4 @@
1
+ Description:
2
+ The camaraderie:install generator generates a migration that will
3
+ create the `memberships` table in your database. It will also
4
+ create a blank `Membership` model file.
@@ -0,0 +1,29 @@
1
+ require 'rails/generators'
2
+ require 'rails/generators/migration'
3
+
4
+ module Camaraderie
5
+ module Generators
6
+ class InstallGenerator < Rails::Generators::Base
7
+ include Rails::Generators::Migration
8
+ source_root File.expand_path('../templates', __FILE__)
9
+
10
+ # Implement the required interface for Rails::Generators::Migration.
11
+ # taken from http://github.com/rails/rails/blob/master/activerecord/lib/generators/active_record.rb
12
+ def self.next_migration_number(dirname)
13
+ if ActiveRecord::Base.timestamped_migrations
14
+ Time.now.utc.strftime('%Y%m%d%H%M%S')
15
+ else
16
+ '%.3d' % (current_migration_number(dirname) + 1)
17
+ end
18
+ end
19
+
20
+ def create_migration_file
21
+ migration_template 'migration.rb', 'db/migrate/add_camaraderie.rb'
22
+ end
23
+
24
+ def create_model_file
25
+ template "model.rb", "app/models/membership.rb"
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,20 @@
1
+ class AddCamaraderie < ActiveRecord::Migration
2
+ def up
3
+ create_table :memberships do |t|
4
+ t.references :user
5
+ t.references :organization
6
+ t.string :membership_type
7
+
8
+ t.timestamps
9
+ end
10
+
11
+ add_index "memberships", ["organization_id", "membership_type"], name: "index_memberships_on_organization_id_and_membership_type"
12
+ add_index "memberships", ["organization_id", "user_id", "membership_type"], name: "index_memberships_on_everything", unique: true
13
+ add_index "memberships", ["organization_id"], name: "index_memberships_on_organization_id"
14
+ add_index "memberships", ["user_id"], name: "index_memberships_on_user_id"
15
+ end
16
+
17
+ def down
18
+ drop_table :memberships
19
+ end
20
+ end
@@ -0,0 +1,3 @@
1
+ class Membership < ActiveRecord::Base
2
+ acts_as_membership
3
+ end
metadata ADDED
@@ -0,0 +1,134 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: camaraderie
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.2
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Rémi Prévost
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-07-12 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: bundler
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: '1.3'
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: '1.3'
30
+ - !ruby/object:Gem::Dependency
31
+ name: rake
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: activerecord
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: 3.0.0
54
+ type: :runtime
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.0
62
+ - !ruby/object:Gem::Dependency
63
+ name: activesupport
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: 3.0.0
70
+ type: :runtime
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: 3.0.0
78
+ description: Camaraderie takes away the pain of managing membership stuff between
79
+ users and organizations.
80
+ email:
81
+ - rprevost@mirego.com
82
+ executables: []
83
+ extensions: []
84
+ extra_rdoc_files: []
85
+ files:
86
+ - .gitignore
87
+ - Gemfile
88
+ - LICENSE.md
89
+ - README.md
90
+ - Rakefile
91
+ - camaraderie.gemspec
92
+ - lib/camaraderie.rb
93
+ - lib/camaraderie/membership.rb
94
+ - lib/camaraderie/organization.rb
95
+ - lib/camaraderie/railtie.rb
96
+ - lib/camaraderie/user.rb
97
+ - lib/camaraderie/version.rb
98
+ - lib/generators/camaraderie/USAGE
99
+ - lib/generators/camaraderie/install_generator.rb
100
+ - lib/generators/camaraderie/templates/migration.rb
101
+ - lib/generators/camaraderie/templates/model.rb
102
+ homepage: https://github.com/mirego/camaraderie
103
+ licenses:
104
+ - BSD 3-Clause
105
+ post_install_message:
106
+ rdoc_options: []
107
+ require_paths:
108
+ - lib
109
+ required_ruby_version: !ruby/object:Gem::Requirement
110
+ none: false
111
+ requirements:
112
+ - - ! '>='
113
+ - !ruby/object:Gem::Version
114
+ version: '0'
115
+ segments:
116
+ - 0
117
+ hash: -4019266767070009919
118
+ required_rubygems_version: !ruby/object:Gem::Requirement
119
+ none: false
120
+ requirements:
121
+ - - ! '>='
122
+ - !ruby/object:Gem::Version
123
+ version: '0'
124
+ segments:
125
+ - 0
126
+ hash: -4019266767070009919
127
+ requirements: []
128
+ rubyforge_project:
129
+ rubygems_version: 1.8.23
130
+ signing_key:
131
+ specification_version: 3
132
+ summary: Camaraderie takes away the pain of managing membership stuff between users
133
+ and organizations.
134
+ test_files: []