pg_rls 0.0.2.6.2 → 0.0.2.6.3

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: 87f7139b6e22ced3f4e32092df320ff675a995cfb5139b786da32de98087893e
4
- data.tar.gz: 4840e5dcab17c0ed382734fff92d3ddaf9a905b61aeba05a763fcb43fc68fc01
3
+ metadata.gz: 995d9d7741a7402f3a17cf8737bdbad60cdb1aae96460058ed1e02eba59af374
4
+ data.tar.gz: 013bc7af4c7ffabf0d9cde928882efa3a35af791673b51539c9957f4a54d6786
5
5
  SHA512:
6
- metadata.gz: 03d7a09af5bf048f6aa0b1473d5b3c6ccc0a83c0068efa2e84dd75a016a4e39c353fa6bdc3e5d213961129860f08c3b9a79f2824b05d33a90fafdfd4d26e3d0b
7
- data.tar.gz: 89e3ced934194385d2d851880c4ef675020d5a706135dd4cef4de8127106ddb158385586be559ac84b1648a23965e50e764d76fe5beace11c583912fdc7d30f8
6
+ metadata.gz: 3624e007d9826575c5713285b7c21c6410853c9970f0cf8f79b2ec71c5d99aa0f009fc32c9756de6131801e5cbc190b27b431714e036c2a139217c4f150f7a19
7
+ data.tar.gz: 8631b721048d4478da8d30f1cc294452cfd9a48f22e6eab8bf9748e345b3a5d26f886c92400035e3ff5c1ab993fc7759b26bbb1675b6f90b536dfcf271fe3b96
@@ -5,18 +5,23 @@ module PgRls
5
5
  module MultiTenancy
6
6
  def self.included(base)
7
7
  base.class_eval do
8
- before_action :switch_tenant!
8
+ around_action :switch_tenant!
9
+
10
+ def current_tenant
11
+ @current_tenant ||= request.subdomain
12
+ end
13
+ helper_method :current_tenant
9
14
  end
10
15
  end
11
16
 
12
17
  private
13
18
 
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])
19
+ def switch_tenant!(&block)
20
+ fetched_tenant = session[:_tenant] || current_tenant
21
+ Tenant.with_tenant(fetched_tenant) do |tenant|
22
+ session[:_tenant] = tenant
23
+ block.call(tenant)
24
+ end
20
25
  rescue NoMethodError
21
26
  session[:_tenant] = nil
22
27
  redirect_to '/'
data/lib/pg_rls/tenant.rb CHANGED
@@ -40,13 +40,17 @@ module PgRls
40
40
  end
41
41
 
42
42
  def fetch
43
+ fetch!
44
+ rescue ActiveRecord::StatementInvalid
45
+ 'no tenant is selected'
46
+ end
47
+
48
+ def fetch!
43
49
  @fetch ||= PgRls.main_model.find_by!(
44
50
  tenant_id: PgRls.connection_class.connection.execute(
45
51
  "SELECT current_setting('rls.tenant_id')"
46
52
  ).getvalue(0, 0)
47
53
  )
48
- rescue ActiveRecord::StatementInvalid
49
- 'no tenant is selected'
50
54
  end
51
55
 
52
56
  def find_main_model
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module PgRls
4
- VERSION = '0.0.2.6.2'
4
+ VERSION = '0.0.2.6.3'
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pg_rls
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2.6.2
4
+ version: 0.0.2.6.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Daniel Laloush