pg_rls 0.0.2.4 → 0.0.2.6

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
  SHA256:
3
- metadata.gz: 97b8d67f3ffcd5b65cc10db46a9a0b981545881ad1e6ce40152c73feadf18b3f
4
- data.tar.gz: fd084b44bc7a6301e196aafeb670071426e8f85bc861a83c1dc7e025386a6ecd
3
+ metadata.gz: b8738d2e841c3b624b073fce3a496def262acf19cda3edbf138d0f7840a692b3
4
+ data.tar.gz: fe073b693f792122f84cef65d786ed5d6b727d2d351c146fae6371c45819d8ad
5
5
  SHA512:
6
- metadata.gz: 461960383dd168e92d23a7d046c9baf521a03dca494f60918cf4712c878ae3d37abd5d3b357c09e59eb4d8cfbbb50b00d18c6110217f2add95f230f767bb58b5
7
- data.tar.gz: 0febfe88828a1875c8156a5cd010e5ee25f3b10e7e714e3bbe07233f70e09abb7201ea357f1e22dd3733c48854ef4aa7cfc214775b42365c77ec04a3463113b8
6
+ metadata.gz: 247e45f3fbe9553746f55bd3f35a62c67087b2628119a6eb019aaaa29350a66ff51de2ab8d194cf6796f3f389fd0a28bfe979e443b8792842ce059149ed9d14e
7
+ data.tar.gz: d9466802ab4410695da1729b7d6864b57f5b214fa02fc079cea72741b752ec61b00ac4896074c2c0232206ada8df6292ac977d9431520ca06660e2e9f2da47bd
@@ -5,15 +5,26 @@ module PgRls
5
5
  module MultiTenancy
6
6
  def self.included(base)
7
7
  base.class_eval do
8
- before_action :switch_tenant
8
+ before_action :switch_tenant!
9
9
  end
10
10
  end
11
11
 
12
12
  private
13
13
 
14
- def switch_tenant
15
- Tenant.switch!(request.subdomain)
16
- session[:_tenant] = request.subdomain
14
+ def switch_tenant!
15
+ tenant = request.subdomain
16
+ Tenant.switch!(tenant)
17
+ session[:_tenant] = tenant
18
+ rescue PgRls::Errors::TenantNotFound, ActiveRecord::RecordNotFound
19
+ Tenant.switch(session[:_tenant])
20
+ rescue NoMethodError
21
+ session[:_tenant] = nil
22
+ redirect_to '/'
23
+ end
24
+
25
+ def switch_tenant_by_resource!(resource = nil)
26
+ Tenant.switch!(resource)
27
+ session[:_tenant] = resource
17
28
  rescue PgRls::Errors::TenantNotFound, ActiveRecord::RecordNotFound
18
29
  Tenant.switch(session[:_tenant])
19
30
  rescue NoMethodError
@@ -17,7 +17,7 @@ module PgRls
17
17
 
18
18
  def self.included(base)
19
19
  establish_secure_connection
20
- base.ignored_columns = %w(tenant_id)
20
+ base.ignored_columns = %w[tenant_id]
21
21
  end
22
22
  end
23
23
  end
data/lib/pg_rls/tenant.rb CHANGED
@@ -37,6 +37,15 @@ module PgRls
37
37
  'no tenant is selected'
38
38
  end
39
39
 
40
+ def find_main_model
41
+ PgRls.main_model.ignored_columns = []
42
+ PgRls.main_model.find_by!(
43
+ tenant_id: PgRls.connection_class.connection.execute(
44
+ "SELECT current_setting('rls.tenant_id')"
45
+ ).getvalue(0, 0)
46
+ )
47
+ end
48
+
40
49
  def reset_rls!
41
50
  @fetch = nil
42
51
  @tenant = nil
@@ -46,6 +55,8 @@ module PgRls
46
55
  private
47
56
 
48
57
  def switch_tenant!(resource)
58
+ PgRls.main_model.ignored_columns = []
59
+
49
60
  connection_adapter = PgRls.connection_class
50
61
  find_tenant(resource)
51
62
 
@@ -70,7 +81,7 @@ module PgRls
70
81
  end
71
82
 
72
83
  def find_tenant_by_method(resource, method)
73
- PgRls.main_model.send("find_by_#{method}!", resource)
84
+ PgRls.main_model.send("find_by_#{method}!", resource.send(method))
74
85
  rescue ActiveRecord::RecordNotFound
75
86
  nil
76
87
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module PgRls
4
- VERSION = '0.0.2.4'
4
+ VERSION = '0.0.2.6'
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pg_rls
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2.4
4
+ version: 0.0.2.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Daniel Laloush
8
- autorequire:
8
+ autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2022-09-30 00:00:00.000000000 Z
11
+ date: 2023-02-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -80,7 +80,7 @@ licenses:
80
80
  - MIT
81
81
  metadata:
82
82
  rubygems_mfa_required: 'true'
83
- post_install_message:
83
+ post_install_message:
84
84
  rdoc_options: []
85
85
  require_paths:
86
86
  - lib
@@ -95,8 +95,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
95
95
  - !ruby/object:Gem::Version
96
96
  version: '0'
97
97
  requirements: []
98
- rubygems_version: 3.3.15
99
- signing_key:
98
+ rubygems_version: 3.3.26
99
+ signing_key:
100
100
  specification_version: 4
101
101
  summary: Write a short summary, because RubyGems requires one.
102
102
  test_files: []