effective_resources 1.9.5 → 1.9.9

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 83e99f559d60204f03679abaeacb94f0406596ca751221002c69adcb2cea52ee
4
- data.tar.gz: 7ff7732c4b04ffeff143812216af2e03a81008830fba3681614c44d59552959b
3
+ metadata.gz: 144152b4742a10b3d574bbe0458c8f80db4490dd178d0dbe323a180a05a87b46
4
+ data.tar.gz: 533c8ef7927b1782d85efaea9affc6c2a16d56452ae3c8d0df45f1ec8535f795
5
5
  SHA512:
6
- metadata.gz: 1884f93d5785dde2523add135e534d86a18880fe9cc1ded09d73a915ed1ce8c842907e63a0a9e1ac7aa829489b55d3f1eaf0de2be4add65cfaa806e0701fd422
7
- data.tar.gz: 7f88e77db260d68330f44868d79f8aebfd7458e19a3b4a4a205811071d2e3abc32f69777dd2ac2c6b964c0df4e1efde54e7b0233705ee7fd6d906e95facf9d1f
6
+ metadata.gz: cdd68a72ad36d7745ea3178d9f9b640799e1c406f0f1dec2d2e7c376dccadc02724c7298c10a96198dc552c9de21591457b36c601b74a91b60222675d8569386
7
+ data.tar.gz: 1d126ded3ea4f19d8d4d2040bf32037eb7a0e0c3f7adbdc2e9514d31fa35f54f301bdf6bafb27c3b1e7757ba680577440b2c6a7110f2103d94a07c98f702518b
@@ -228,7 +228,7 @@ module Effective
228
228
 
229
229
  # No attributes are assigned or saved. We purely call action! on the resource
230
230
 
231
- ActiveRecord::Base.transaction do
231
+ EffectiveResources.transaction do
232
232
  successes = resources.select do |resource|
233
233
  begin
234
234
  resource.public_send("#{action}!") if EffectiveResources.authorized?(self, action, resource)
@@ -30,7 +30,7 @@ module Effective
30
30
 
31
31
  success = false
32
32
 
33
- ActiveRecord::Base.transaction do
33
+ EffectiveResources.transaction(resource) do
34
34
  begin
35
35
  run_callbacks(:resource_before_save)
36
36
 
@@ -125,16 +125,16 @@ module ActsAsStatused
125
125
  define_method("#{sym}?") { status == sym.to_s }
126
126
  define_method("was_#{sym}?") { send("#{sym}_at").present? }
127
127
 
128
- unless has_attribute?("#{sym}_at")
128
+ #unless has_attribute?("#{sym}_at")
129
129
  define_method("#{sym}_at") { status_steps["#{sym}_at".to_sym] }
130
130
  define_method("#{sym}_at=") { |value| status_steps["#{sym}_at".to_sym] = value }
131
- end
131
+ #end
132
132
 
133
- unless has_attribute?("#{sym}_by_id")
133
+ #unless has_attribute?("#{sym}_by_id")
134
134
  define_method("#{sym}_by") { acts_as_statused_by_user(sym) }
135
135
  define_method("#{sym}_by_id") { status_steps["#{sym}_by".to_sym] }
136
136
  define_method("#{sym}_by_id=") { |value| status_steps["#{sym}_by".to_sym] = value }
137
- end
137
+ #end
138
138
 
139
139
  # approved!
140
140
  define_method("#{sym}!") do |atts = {}|
@@ -180,4 +180,3 @@ module ActsAsStatused
180
180
  end
181
181
 
182
182
  end
183
-
@@ -12,17 +12,17 @@ module EffectiveAfterCommit
12
12
 
13
13
  module Base
14
14
  def after_commit(connection: self.class.connection, &callback)
15
- Effective::AfterCommit.register_callback(connection: connection, name: __method__, callback: callback, no_tx_action: :execute)
15
+ Effective::AfterCommit.register_callback(connection: connection, name: __method__, callback: callback)
16
16
  end
17
17
 
18
18
  def before_commit(connection: self.class.connection, &callback)
19
19
  raise(NotImplementedError, "#{__method__} works only with Rails 5.0+") if ActiveRecord::VERSION::MAJOR < 5
20
- Effective::AfterCommit.register_callback(connection: connection, name: __method__, callback: callback, no_tx_action: :warn_and_execute)
20
+ Effective::AfterCommit.register_callback(connection: connection, name: __method__, callback: callback)
21
21
  end
22
22
 
23
23
  def after_rollback(connection: self.class.connection, &callback)
24
24
  raise('expected a block') unless block_given?
25
- Effective::AfterCommit.register_callback(connection: connection, name: __method__, callback: callback, no_tx_action: :exception)
25
+ Effective::AfterCommit.register_callback(connection: connection, name: __method__, callback: callback)
26
26
  end
27
27
  end
28
28
 
@@ -25,20 +25,11 @@ module Effective
25
25
  @handlers[:after_rollback]&.call
26
26
  end
27
27
 
28
- def self.register_callback(connection:, name:, no_tx_action:, callback:)
28
+ def self.register_callback(connection:, name:, callback:)
29
29
  raise ArgumentError, "#{name} expected a block" unless callback
30
30
 
31
- unless (connection.transaction_open? && connection.current_transaction.joinable?)
32
- case no_tx_action
33
- when :warn_and_execute
34
- warn "#{name}: No transaction open. Executing callback immediately."
35
- return callback.call
36
- when :execute
37
- return callback.call
38
- when :exception
39
- raise("#{name} is useless outside transaction")
40
- end
41
- end
31
+ raise("#{name} is useless outside transaction") unless connection.transaction_open?
32
+ raise("#{name} is useless outside transaction") unless connection.current_transaction.joinable?
42
33
 
43
34
  after_commit = Effective::AfterCommit.new("#{name}": callback)
44
35
  connection.add_transaction_record(after_commit)
@@ -12,10 +12,21 @@ module Effective
12
12
  @controller_path ||= route_name #[namespace, plural_name].compact * '/')
13
13
  end
14
14
 
15
+ def route_engines
16
+ if tenant? && Tenant.current.present?
17
+ [Rails.application, Tenant.Engine] + Rails::Engine.subclasses.reverse.reject do |klass|
18
+ tenant_engines_blacklist.any? { |name| klass.name.start_with?(name) }
19
+ end
20
+ else
21
+ [Rails.application] + Rails::Engine.subclasses.reverse
22
+ end
23
+ end
24
+
15
25
  def routes
16
26
  @routes ||= begin
17
27
  routes = nil
18
- engines = [Rails.application] + Rails::Engine.subclasses.reverse
28
+
29
+ engines = route_engines()
19
30
 
20
31
  # Check from controller_path. This is generally correct.
21
32
  engines.each do |engine|
@@ -150,7 +150,7 @@ module Effective
150
150
 
151
151
  is_scope = false
152
152
 
153
- ActiveRecord::Base.transaction do
153
+ EffectiveResources.transaction(klass) do
154
154
  begin
155
155
  relation = klass.public_send(name).kind_of?(ActiveRecord::Relation)
156
156
  rescue => e
@@ -32,11 +32,14 @@ module Effective
32
32
 
33
33
  def route_name_fallbacks
34
34
  mod = class_name.split('::').first.to_s.downcase
35
+ admin = ('admin' if namespace.present? && namespace.include?('/admin'))
35
36
 
36
37
  matches = [
37
38
  route_name.singularize,
38
39
  [*namespace, plural_name].join('/'),
40
+ [*admin, plural_name].join('/'),
39
41
  [*namespace, name].join('/'),
42
+ [*admin, name].join('/'),
40
43
  [*mod, *namespace, plural_name].join('/'),
41
44
  [*mod, *namespace, name].join('/')
42
45
  ]
@@ -15,6 +15,10 @@ module Effective
15
15
  name if Rails.application.config.tenants[name].present?
16
16
  end
17
17
 
18
+ def tenant_engines_blacklist
19
+ return [] unless tenant?
20
+ Rails.application.config.tenants.map { |name, _| name.to_s.classify }
21
+ end
18
22
  end
19
23
  end
20
24
  end
@@ -1,3 +1,3 @@
1
1
  module EffectiveResources
2
- VERSION = '1.9.5'.freeze
2
+ VERSION = '1.9.9'.freeze
3
3
  end
@@ -51,6 +51,32 @@ module EffectiveResources
51
51
  klass
52
52
  end
53
53
 
54
+ def self.transaction(resource = nil, &block)
55
+ connection = (resource if resource.respond_to?(:transaction))
56
+ connection ||= (resource.class if resource.class.respond_to?(:transaction))
57
+ connection ||= '::ApplicationRecord'.safe_constantize
58
+ connection ||= 'ActiveRecord::Base'.safe_constantize
59
+
60
+ raise('unable to determine transaction class') unless connection.present?
61
+
62
+ connection.transaction { yield }
63
+ end
64
+
65
+ # Used by streaming CSV export in datatables
66
+ def self.with_resource_enumerator(&block)
67
+ raise('expected a block') unless block_given?
68
+
69
+ tenant = Tenant.current if defined?(Tenant)
70
+
71
+ if tenant
72
+ Enumerator.new do |enumerator|
73
+ Tenant.as(tenant) { yield(enumerator) }
74
+ end
75
+ else
76
+ Enumerator.new { |enumerator| yield(enumerator) }
77
+ end
78
+ end
79
+
54
80
  def self.truthy?(value)
55
81
  if defined?(::ActiveRecord::ConnectionAdapters::Column::TRUE_VALUES) # Rails <5
56
82
  ::ActiveRecord::ConnectionAdapters::Column::TRUE_VALUES.include?(value)
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: effective_resources
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.9.5
4
+ version: 1.9.9
5
5
  platform: ruby
6
6
  authors:
7
7
  - Code and Effect
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-11-16 00:00:00.000000000 Z
11
+ date: 2021-12-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails