gutentag-multitenancy 0.1.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.
- checksums.yaml +7 -0
- data/.gitignore +9 -0
- data/Gemfile +3 -0
- data/LICENSE.txt +21 -0
- data/README.md +68 -0
- data/Rakefile +10 -0
- data/gutentag-multitenancy.gemspec +28 -0
- data/lib/gutentag/multitenancy/engine.rb +29 -0
- data/lib/gutentag/multitenancy/taggable.rb +32 -0
- data/lib/gutentag/multitenancy/tenant_tagger.rb +23 -0
- data/lib/gutentag/multitenancy/version.rb +5 -0
- data/lib/gutentag/multitenancy.rb +11 -0
- metadata +111 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 189f617a953d1d1ceaacd4bc7de82b3e8d54cd71
|
4
|
+
data.tar.gz: b51023d31ed942fdc6c611e35be263ce5fcb50e0
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: e0f8260ef48289d684a9411f20efd0e9d80a82cdd50264e745de80f4b481f390f0736768d93b41baf64f732a82537614b441ff9ad83c893583b7e701464b306a
|
7
|
+
data.tar.gz: 84fa5812352a81daf6b877e7c0c760682eebb845907349d5f9629200e1bc70f9348fe5708c87900b23667c364f930f2461df7f2c4fa0fd9f4082a028a7d6263a
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2015 Andy Stewart
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,68 @@
|
|
1
|
+
# Gutentag::Multitenancy
|
2
|
+
|
3
|
+
This gem extends [Gutentag](https://github.com/pat/gutentag) with multitenancy support.
|
4
|
+
|
5
|
+
Gutentag's tags are available to every user of your application; this gem scopes the tags by tenant.
|
6
|
+
|
7
|
+
|
8
|
+
## Installation
|
9
|
+
|
10
|
+
If you haven't installed Gutentag yet, [install](https://github.com/pat/gutentag#installation) it now. You'll need to modify your Gemfile to install the latest version from HEAD:
|
11
|
+
|
12
|
+
```ruby
|
13
|
+
gem 'gutentag', github: 'pat/gutentag', ref: '51bd8f2f37f21dc4ef84a87889db4f28aa3573e2'
|
14
|
+
```
|
15
|
+
|
16
|
+
Now run the following migration (TODO: make a generator for this!):
|
17
|
+
|
18
|
+
```ruby
|
19
|
+
add_column :gutentag_tags, :tenant_id, :integer, null: false
|
20
|
+
|
21
|
+
remove_index :gutentag_tags, :name
|
22
|
+
add_index :gutentag_tags, [:name, :tenant_id], unique: true
|
23
|
+
```
|
24
|
+
|
25
|
+
Add this line to your application's Gemfile:
|
26
|
+
|
27
|
+
```ruby
|
28
|
+
gem 'gutentag-multitenancy'
|
29
|
+
```
|
30
|
+
|
31
|
+
And then execute:
|
32
|
+
|
33
|
+
$ bundle
|
34
|
+
|
35
|
+
Or install it yourself as:
|
36
|
+
|
37
|
+
$ gem install gutentag-multitenancy
|
38
|
+
|
39
|
+
|
40
|
+
## Usage
|
41
|
+
|
42
|
+
Define a `tenant_id` method in each model which can have tags. In the following example each author has their own set of tags for articles:
|
43
|
+
|
44
|
+
```ruby
|
45
|
+
class Article < ActiveRecord::Base
|
46
|
+
belongs_to :author
|
47
|
+
|
48
|
+
has_many_tags # from Gutentag
|
49
|
+
|
50
|
+
def tenant_id
|
51
|
+
author.id
|
52
|
+
end
|
53
|
+
end
|
54
|
+
```
|
55
|
+
|
56
|
+
To retrieve all the tags for a tenant, use the `by_tenant_id` scope:
|
57
|
+
|
58
|
+
```ruby
|
59
|
+
Gutentag::Tag.by_tenant_id 42
|
60
|
+
```
|
61
|
+
|
62
|
+
|
63
|
+
## License
|
64
|
+
|
65
|
+
Copyright 2015 Andrew Stewart.
|
66
|
+
|
67
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
68
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'gutentag/multitenancy/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "gutentag-multitenancy"
|
8
|
+
spec.version = Gutentag::Multitenancy::VERSION
|
9
|
+
spec.authors = ["Andy Stewart"]
|
10
|
+
spec.email = ["boss@airbladesoftware.com"]
|
11
|
+
|
12
|
+
spec.summary = %q{Extends Gutentag with support for multitenancy.}
|
13
|
+
spec.description = %q{Extends Gutentag with support for multitenancy.}
|
14
|
+
spec.homepage = "https://github.com/airblade/gutentag-multitenancy"
|
15
|
+
spec.license = "MIT"
|
16
|
+
|
17
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
18
|
+
spec.bindir = "exe"
|
19
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
20
|
+
spec.require_paths = ["lib"]
|
21
|
+
|
22
|
+
spec.add_development_dependency "bundler", "~> 1.10"
|
23
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
24
|
+
spec.add_development_dependency "minitest"
|
25
|
+
|
26
|
+
# NOTE: requires gutentag @ 51bd8f2 or later (not yet released).
|
27
|
+
spec.add_dependency 'gutentag', '>= 0.7.0'
|
28
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
require "#{Gutentag::Engine.root}/app/models/gutentag/tag.rb"
|
2
|
+
|
3
|
+
module Gutentag
|
4
|
+
module Multitenancy
|
5
|
+
|
6
|
+
class Engine < ::Rails::Engine
|
7
|
+
engine_name :gutentag_multitenancy
|
8
|
+
|
9
|
+
ActiveSupport.on_load :active_record do
|
10
|
+
Gutentag::Tag.class_eval do
|
11
|
+
attr_accessible :tenant_id if ::ActiveRecord::VERSION::MAJOR == 3
|
12
|
+
|
13
|
+
scope :by_tenant_id, ->(tenant_id) { where tenant_id: tenant_id }
|
14
|
+
|
15
|
+
def self.find_by_name_and_tenant_id(name, tenant_id)
|
16
|
+
where(tenant_id: tenant_id).find_by_name(name)
|
17
|
+
end
|
18
|
+
|
19
|
+
def self.find_or_create_by_name_and_tenant_id(name, tenant_id)
|
20
|
+
find_by_name_and_tenant_id(name, tenant_id) || create(name: name, tenant_id: tenant_id)
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
include Taggable
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
end
|
29
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
module Gutentag
|
2
|
+
module Multitenancy
|
3
|
+
|
4
|
+
module Taggable
|
5
|
+
def self.included(base)
|
6
|
+
base.extend ClassMethods
|
7
|
+
end
|
8
|
+
|
9
|
+
module ClassMethods
|
10
|
+
def has_many_tags
|
11
|
+
super
|
12
|
+
include InstanceMethodsOnActivation
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
module InstanceMethodsOnActivation
|
17
|
+
def tenant_id
|
18
|
+
raise NotImplementedError, 'taggable must implement'
|
19
|
+
end
|
20
|
+
|
21
|
+
private
|
22
|
+
|
23
|
+
def persist_tags
|
24
|
+
persister = Gutentag::Persistence.new(self)
|
25
|
+
persister.tagger = TenantTagger.new(tenant_id)
|
26
|
+
persister.persist
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
end
|
32
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
module Gutentag
|
2
|
+
module Multitenancy
|
3
|
+
|
4
|
+
class TenantTagger
|
5
|
+
def initialize(tenant_id)
|
6
|
+
@tenant_id = tenant_id
|
7
|
+
end
|
8
|
+
|
9
|
+
def find_by_name(name)
|
10
|
+
Gutentag::Tag.find_by_name_and_tenant_id(name, tenant_id)
|
11
|
+
end
|
12
|
+
|
13
|
+
def find_or_create(name)
|
14
|
+
Gutentag::Tag.find_or_create_by_name_and_tenant_id(name, tenant_id)
|
15
|
+
end
|
16
|
+
|
17
|
+
private
|
18
|
+
|
19
|
+
attr_reader :tenant_id
|
20
|
+
end
|
21
|
+
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1,11 @@
|
|
1
|
+
Gutentag.tag_validations = lambda { |klass|
|
2
|
+
klass.validates :name,
|
3
|
+
presence: true,
|
4
|
+
uniqueness: {case_sensitive: false, scope: :tenant_id}
|
5
|
+
}
|
6
|
+
|
7
|
+
require "gutentag/multitenancy/version"
|
8
|
+
require "gutentag/multitenancy/tenant_tagger"
|
9
|
+
require "gutentag/multitenancy/taggable"
|
10
|
+
require "gutentag/multitenancy/engine"
|
11
|
+
|
metadata
ADDED
@@ -0,0 +1,111 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: gutentag-multitenancy
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Andy Stewart
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-12-09 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.10'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.10'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: minitest
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: gutentag
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: 0.7.0
|
62
|
+
type: :runtime
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: 0.7.0
|
69
|
+
description: Extends Gutentag with support for multitenancy.
|
70
|
+
email:
|
71
|
+
- boss@airbladesoftware.com
|
72
|
+
executables: []
|
73
|
+
extensions: []
|
74
|
+
extra_rdoc_files: []
|
75
|
+
files:
|
76
|
+
- ".gitignore"
|
77
|
+
- Gemfile
|
78
|
+
- LICENSE.txt
|
79
|
+
- README.md
|
80
|
+
- Rakefile
|
81
|
+
- gutentag-multitenancy.gemspec
|
82
|
+
- lib/gutentag/multitenancy.rb
|
83
|
+
- lib/gutentag/multitenancy/engine.rb
|
84
|
+
- lib/gutentag/multitenancy/taggable.rb
|
85
|
+
- lib/gutentag/multitenancy/tenant_tagger.rb
|
86
|
+
- lib/gutentag/multitenancy/version.rb
|
87
|
+
homepage: https://github.com/airblade/gutentag-multitenancy
|
88
|
+
licenses:
|
89
|
+
- MIT
|
90
|
+
metadata: {}
|
91
|
+
post_install_message:
|
92
|
+
rdoc_options: []
|
93
|
+
require_paths:
|
94
|
+
- lib
|
95
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
96
|
+
requirements:
|
97
|
+
- - ">="
|
98
|
+
- !ruby/object:Gem::Version
|
99
|
+
version: '0'
|
100
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
101
|
+
requirements:
|
102
|
+
- - ">="
|
103
|
+
- !ruby/object:Gem::Version
|
104
|
+
version: '0'
|
105
|
+
requirements: []
|
106
|
+
rubyforge_project:
|
107
|
+
rubygems_version: 2.2.2
|
108
|
+
signing_key:
|
109
|
+
specification_version: 4
|
110
|
+
summary: Extends Gutentag with support for multitenancy.
|
111
|
+
test_files: []
|