acts_as_tenant 0.4.0 → 0.4.1

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: b818ac81194035979865164439e777511f68c06c
4
- data.tar.gz: f395680cfb279dd618df7e88ca6373adb2f6596a
3
+ metadata.gz: 215e3c4bffac36cc0e111df4d91ce5d48cd9180b
4
+ data.tar.gz: 4cc15333e4bb02df2708e6e1b65d8b1d62fe0732
5
5
  SHA512:
6
- metadata.gz: 61cffd95ae9374d1f09a6353785468d8b13db7a3fbb2e9936585d9b0f8092a34a6d683bb20032715465e7cb4e72afacd67fc80dd5cf6c6b1b01178658b67bc36
7
- data.tar.gz: 9f2f90d90b7ec7a25b443da85dc89cb75912bf348e600175e5a83af9ed14699ab1a2313e463eda90bfbd3db87674e148bacdc11e0d275a5aa5c2928590fc4387
6
+ metadata.gz: 13089890ed6915b0cac19a818ca3d45d30e3e24c4534aa238a3f039674f1d375508534271b919c31aa142ed3871afde6c1796ea5123e3acf1db6c50dc67eb567
7
+ data.tar.gz: 444740576efaca0fd486d3a8ab8d72b39f33122dbbec9cf18f40a7f16089cb97c2107624e1d131b6df27190f7c89ab006b83e1cd58dcec216380698b4bfdcc0a
@@ -1,6 +1,4 @@
1
1
  language: ruby
2
- services:
3
- - mongodb
4
2
  before_install:
5
3
  - gem update bundler
6
- script: bundle exec rake spec:all
4
+ script: bundle exec rake spec
@@ -1,3 +1,8 @@
1
+ 0.4.1
2
+ ------
3
+ * Removed (stale, no longer working) MongoDB support; moved code to separate branch
4
+ * Added without_tenant option (see readme, thx duboff)
5
+
1
6
  0.4.0
2
7
  ------
3
8
  * (Sub)domain lookup is no longer case insensitive
data/Rakefile CHANGED
@@ -3,14 +3,3 @@ require 'bundler/gem_tasks'
3
3
  require 'rspec/core/rake_task'
4
4
  RSpec::Core::RakeTask.new(:spec)
5
5
  task :default => :spec
6
-
7
- desc 'Run the test suite for all supported ORMs.'
8
- namespace :spec do
9
- task :all do
10
- %w[active_record mongoid].each do |orm|
11
- ENV["ORM"] = orm
12
- Rake::Task["spec"].reenable
13
- Rake::Task["spec"].invoke
14
- end
15
- end
16
- end
@@ -18,7 +18,6 @@ Gem::Specification.new do |s|
18
18
  s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
19
19
  s.require_paths = ["lib"]
20
20
 
21
- #add_runtime_dependency("rails")
22
21
  s.add_runtime_dependency('request_store', '>= 1.0.5')
23
22
  s.add_dependency('rails','>= 3.1')
24
23
  #s.add_dependency('request_store', '>= 1.0.5')
@@ -27,7 +26,7 @@ Gem::Specification.new do |s|
27
26
  s.add_development_dependency('rspec-rails')
28
27
  s.add_development_dependency('database_cleaner', '~> 1.3.0')
29
28
  s.add_development_dependency('sqlite3')
30
- s.add_development_dependency('mongoid', '~> 4.0')
29
+ #s.add_development_dependency('mongoid', '~> 4.0')
31
30
 
32
31
  s.add_development_dependency('sidekiq', '3.2.1')
33
32
  end
@@ -1,5 +1,6 @@
1
1
  module ActsAsTenant
2
2
  @@tenant_klass = nil
3
+ @@models_with_global_records = []
3
4
 
4
5
  def self.set_tenant_klass(klass)
5
6
  @@tenant_klass = klass
@@ -9,6 +10,14 @@ module ActsAsTenant
9
10
  @@tenant_klass
10
11
  end
11
12
 
13
+ def self.models_with_global_records
14
+ @@models_with_global_records
15
+ end
16
+
17
+ def self.add_global_record_model model
18
+ @@models_with_global_records.push(model)
19
+ end
20
+
12
21
  def self.fkey
13
22
  "#{@@tenant_klass.to_s}_id"
14
23
  end
@@ -78,6 +87,8 @@ module ActsAsTenant
78
87
  def acts_as_tenant(tenant = :account, options = {})
79
88
  ActsAsTenant.set_tenant_klass(tenant)
80
89
 
90
+ ActsAsTenant.add_global_record_model(self) if options[:has_global_records]
91
+
81
92
  # Create the association
82
93
  valid_options = options.slice(:foreign_key, :class_name, :inverse_of)
83
94
  fkey = valid_options[:foreign_key] || ActsAsTenant.fkey
@@ -88,7 +99,9 @@ module ActsAsTenant
88
99
  raise ActsAsTenant::Errors::NoTenantSet
89
100
  end
90
101
  if ActsAsTenant.current_tenant
91
- where(fkey.to_sym => ActsAsTenant.current_tenant.id)
102
+ keys = [ActsAsTenant.current_tenant.id]
103
+ keys.push(nil) if options[:has_global_records]
104
+ where(fkey.to_sym => keys)
92
105
  else
93
106
  Rails::VERSION::MAJOR < 4 ? scoped : all
94
107
  end
@@ -167,6 +180,26 @@ module ActsAsTenant
167
180
  end
168
181
 
169
182
  validates_uniqueness_of(fields, args)
183
+
184
+ if ActsAsTenant.models_with_global_records.include?(self)
185
+ validate do |instance|
186
+ Array(fields).each do |field|
187
+ if instance.new_record?
188
+ unless self.class.where(fkey.to_sym => [nil, instance[fkey]],
189
+ field.to_sym => instance[field]).empty?
190
+ errors.add(field, 'has already been taken')
191
+ end
192
+ else
193
+ unless self.class.where(fkey.to_sym => [nil, instance[fkey]],
194
+ field.to_sym => instance[field])
195
+ .where.not(:id => instance.id).empty?
196
+ errors.add(field, 'has already been taken')
197
+ end
198
+
199
+ end
200
+ end
201
+ end
202
+ end
170
203
  end
171
204
  end
172
205
  end
@@ -1,3 +1,3 @@
1
1
  module ActsAsTenant
2
- VERSION = "0.4.0"
2
+ VERSION = "0.4.1"
3
3
  end
@@ -107,3 +107,10 @@ class Comment < ActiveRecord::Base
107
107
  belongs_to :task, -> { where(comments: { commentable_type: 'Task' }) }, foreign_key: 'commentable_id'
108
108
  acts_as_tenant :account
109
109
  end
110
+
111
+ class GlobalProject < ActiveRecord::Base
112
+ self.table_name = 'projects'
113
+
114
+ acts_as_tenant :account, has_global_records: true
115
+ validates_uniqueness_to_tenant :name
116
+ end
@@ -1,5 +1,5 @@
1
1
  require 'spec_helper'
2
- require "#{$orm}_models"
2
+ require 'active_record_models'
3
3
 
4
4
  describe ActsAsTenant do
5
5
  after { ActsAsTenant.current_tenant = nil }
@@ -123,6 +123,32 @@ describe ActsAsTenant do
123
123
  it { @project.account }
124
124
  end
125
125
 
126
+ describe 'A tenant model with global records' do
127
+ before do
128
+ @account = Account.create!(:name => 'foo')
129
+ @project1 = GlobalProject.create!(:name => 'foobar global')
130
+ @project2 = GlobalProject.create!(:name => 'unaccessible project', :account => Account.create!)
131
+ ActsAsTenant.current_tenant = @account
132
+ @project3 = GlobalProject.create!(:name => 'foobar')
133
+ end
134
+
135
+ it 'should return two projects' do
136
+ expect(GlobalProject.all.count).to eq(2)
137
+ end
138
+
139
+ it 'should validate the project name against the global records too' do
140
+ expect(GlobalProject.new(:name => 'foobar').valid?).to be(false)
141
+ expect(GlobalProject.new(:name => 'foobar new').valid?).to be(true)
142
+ expect(GlobalProject.new(:name => 'foobar global').valid?).to be(false)
143
+ expect(@project1.valid?).to be(true)
144
+ end
145
+
146
+ it 'should add the model to ActsAsTenant.models_with_global_records' do
147
+ expect(ActsAsTenant.models_with_global_records.include?(GlobalProject)).to be(true)
148
+ expect(ActsAsTenant.models_with_global_records.include?(Project)).to be(false)
149
+ end
150
+ end
151
+
126
152
  # Associations
127
153
  describe 'Associations should be correctly scoped by current tenant' do
128
154
  before do
@@ -1,8 +1,7 @@
1
1
  $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
2
2
  $LOAD_PATH.unshift(File.dirname(__FILE__))
3
3
 
4
- $orm = ENV["ORM"] || "active_record"
5
- require "#{$orm}_helper"
4
+ require "active_record_helper"
6
5
 
7
6
  require 'rspec/rails'
8
7
  require 'acts_as_tenant'
@@ -11,7 +10,7 @@ RSpec.configure do |config|
11
10
  config.after(:each) do
12
11
  ActsAsTenant.current_tenant = nil
13
12
  end
14
-
13
+
15
14
  config.infer_base_class_for_anonymous_controllers = true
16
15
  end
17
16
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: acts_as_tenant
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 0.4.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Erwin Matthijssen
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-08-14 00:00:00.000000000 Z
11
+ date: 2017-03-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: request_store
@@ -94,20 +94,6 @@ dependencies:
94
94
  - - ">="
95
95
  - !ruby/object:Gem::Version
96
96
  version: '0'
97
- - !ruby/object:Gem::Dependency
98
- name: mongoid
99
- requirement: !ruby/object:Gem::Requirement
100
- requirements:
101
- - - "~>"
102
- - !ruby/object:Gem::Version
103
- version: '4.0'
104
- type: :development
105
- prerelease: false
106
- version_requirements: !ruby/object:Gem::Requirement
107
- requirements:
108
- - - "~>"
109
- - !ruby/object:Gem::Version
110
- version: '4.0'
111
97
  - !ruby/object:Gem::Dependency
112
98
  name: sidekiq
113
99
  requirement: !ruby/object:Gem::Requirement
@@ -155,9 +141,6 @@ files:
155
141
  - spec/acts_as_tenant/tenant_by_subdomain_or_domain.rb
156
142
  - spec/acts_as_tenant/tenant_by_subdomain_spec.rb
157
143
  - spec/database.yml
158
- - spec/mongoid.yml
159
- - spec/mongoid_helper.rb
160
- - spec/mongoid_models.rb
161
144
  - spec/spec_helper.rb
162
145
  homepage: http://www.rollcallapp.com/blog
163
146
  licenses: []
@@ -192,7 +175,4 @@ test_files:
192
175
  - spec/acts_as_tenant/tenant_by_subdomain_or_domain.rb
193
176
  - spec/acts_as_tenant/tenant_by_subdomain_spec.rb
194
177
  - spec/database.yml
195
- - spec/mongoid.yml
196
- - spec/mongoid_helper.rb
197
- - spec/mongoid_models.rb
198
178
  - spec/spec_helper.rb
@@ -1,6 +0,0 @@
1
- test:
2
- sessions:
3
- default:
4
- database: acts_as_tenant_test
5
- hosts:
6
- - localhost:27017
@@ -1,23 +0,0 @@
1
- require "action_controller/railtie"
2
- require "action_mailer/railtie"
3
- require 'mongoid'
4
- require 'database_cleaner'
5
-
6
- Mongoid.logger = Moped.logger = Logger.new(File.join(File.dirname(__FILE__), "debug.log"))
7
- Mongoid.logger.level = Moped.logger.level = Logger::DEBUG
8
- Mongoid.load!(File.join(File.dirname(__FILE__), "/mongoid.yml"), :test)
9
-
10
- RSpec.configure do |config|
11
- config.before(:suite) do
12
- DatabaseCleaner[:mongoid].strategy = :truncation
13
- DatabaseCleaner[:mongoid].clean_with(:truncation)
14
- end
15
-
16
- config.before(:each) do
17
- DatabaseCleaner[:mongoid].start
18
- end
19
-
20
- config.after(:each) do
21
- DatabaseCleaner[:mongoid].clean
22
- end
23
- end
@@ -1,81 +0,0 @@
1
- class Account
2
- include Mongoid::Document
3
- include ActsAsTenant::ModelExtensions
4
- field :name, type: String
5
- field :subdomain, type: String
6
- field :domain, type: String
7
- has_many :projects
8
- end
9
-
10
- class Project
11
- include Mongoid::Document
12
- include ActsAsTenant::ModelExtensions
13
- field :name, type: String
14
- has_one :manager
15
- has_many :tasks
16
- acts_as_tenant :account
17
-
18
- validates_uniqueness_to_tenant :name
19
- end
20
-
21
- class Manager
22
- include Mongoid::Document
23
- include ActsAsTenant::ModelExtensions
24
- field :name, type: String
25
- belongs_to :project
26
- acts_as_tenant :account
27
- end
28
-
29
- class Task
30
- include Mongoid::Document
31
- include ActsAsTenant::ModelExtensions
32
- field :name, type: String
33
- field :completed, type: Boolean
34
- belongs_to :project
35
- default_scope -> { where(:completed => nil).order("name" => :asc) }
36
-
37
- acts_as_tenant :account
38
- validates_uniqueness_of :name
39
- end
40
-
41
- class UnscopedModel
42
- include Mongoid::Document
43
- include ActsAsTenant::ModelExtensions
44
- field :name, type: String
45
- validates_uniqueness_of :name
46
- end
47
-
48
- class AliasedTask
49
- include Mongoid::Document
50
- include ActsAsTenant::ModelExtensions
51
- field :name, type: String
52
- acts_as_tenant(:account)
53
- belongs_to :project_alias, :class_name => "Project"
54
- end
55
-
56
- class UniqueTask
57
- include Mongoid::Document
58
- include ActsAsTenant::ModelExtensions
59
- field :name, type: String
60
- field :user_defined_scope, type: String
61
- acts_as_tenant(:account)
62
- belongs_to :project
63
- validates_uniqueness_to_tenant :name, scope: :user_defined_scope
64
- end
65
-
66
- class CustomForeignKeyTask
67
- include Mongoid::Document
68
- include ActsAsTenant::ModelExtensions
69
- field :name, type: String
70
- field :accountID, type: Integer
71
- acts_as_tenant(:account, :foreign_key => "accountID")
72
- validates_uniqueness_to_tenant :name
73
- end
74
-
75
- class Comment
76
- include Mongoid::Document
77
- include ActsAsTenant::ModelExtensions
78
- belongs_to :commentable, polymorphic: true
79
- belongs_to :task, foreign_key: 'commentable_id'
80
- acts_as_tenant :account
81
- end