effective_resources 1.9.2 → 1.9.6

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: 010a64ad908bb27807756924d3f05112c7910568615678bcc8c99b04c7c69f93
4
- data.tar.gz: cee9d5812ee210d09a8481c3261b625db6f7cf9b86d36fb261623d5e4fda2145
3
+ metadata.gz: 0e5b54802ada357459497c873ed7958745da7a62def14f6e0cea1b8b09b1ad3a
4
+ data.tar.gz: 391b11cbab23ea721aa01bb1ada824ab064b1182f16bc88083591746205424f2
5
5
  SHA512:
6
- metadata.gz: 72c9617fedc1420ec4b520328d66e92d765691d943c4ed0d0f6b642e876a74d9d2a11f3a9be16003a1fa8540dc20b43617b9f8dec740625010b7cacf37b1f63b
7
- data.tar.gz: 7a9e7735119fc14caf02d6cee1e0fe1e9558888fc6ca7df4d33c95f073d28b7422adbb0fd93092391067b8ccd06dcb8eb8708a2017dafac5e5375103aa055d92
6
+ metadata.gz: e33a16caedeaf449a75457ff5ee8ed7af961b3092e871ea3a404739054cb8fbf7fe0b9f388312e100ec6896c5f597591402a2efd0845a79bd4e44c1aa99fb16c
7
+ data.tar.gz: eb12d90c64e6e8467fd3dbc6733f7e7da0ee02a94231f33f464dc1896056e01f4b1a3e8998aa949815d741bb760fd732697c63682fe303cef2a132833ff801ee
@@ -9,7 +9,7 @@ module Effective
9
9
  @page_title ||= resource_plural_name.titleize
10
10
 
11
11
  self.resources ||= resource_scope.all if resource_scope.respond_to?(:all)
12
- @datatable = resource_datatable(:index)
12
+ @datatable = resource_datatable()
13
13
 
14
14
  run_callbacks(:resource_render)
15
15
  end
@@ -214,7 +214,7 @@ module Effective
214
214
  @page_title ||= "#{action.to_s.titleize} #{resource_plural_name.titleize}"
215
215
 
216
216
  if request.get?
217
- @datatable = resource_datatable(action)
217
+ @datatable = resource_datatable()
218
218
  run_callbacks(:resource_render)
219
219
 
220
220
  view = lookup_context.template_exists?(action, _prefixes) ? action : :index
@@ -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)
@@ -61,7 +61,20 @@ module Effective
61
61
  raise 'expected a label or block' unless (label || block_given?)
62
62
 
63
63
  instance_exec do
64
- before_action(opts) { @page_title ||= (block_given? ? instance_exec(&block) : label).to_s }
64
+ before_action(opts) do
65
+ @page_title ||= (block_given? ? instance_exec(&block) : label).to_s
66
+ end
67
+ end
68
+ end
69
+
70
+ # datatable -> { MyDatatable.new }, only: [:index]
71
+ def datatable(obj = nil, opts = {}, &block)
72
+ raise 'expected a proc or block' unless (obj.respond_to?(:call) || block_given?)
73
+
74
+ instance_exec do
75
+ before_action(opts) do
76
+ @datatable ||= (block_given? ? instance_exec(&block) : obj.call)
77
+ end
65
78
  end
66
79
  end
67
80
 
@@ -84,7 +97,6 @@ module Effective
84
97
  else
85
98
  define_method(:resource_scope_relation) { return obj }
86
99
  end
87
-
88
100
  end
89
101
 
90
102
  end
@@ -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
 
@@ -108,15 +108,16 @@ module Effective
108
108
  resource_scope.where_values_hash.symbolize_keys
109
109
  end
110
110
 
111
- def resource_datatable(action)
112
- datatable_klass = if action == :index
113
- effective_resource.datatable_klass
114
- else # Admin::ActionDatatable.new
115
- "#{[effective_resource.namespace.to_s.classify.presence, action.to_s.classify].compact.join('::')}Datatable".safe_constantize ||
116
- "#{[effective_resource.namespace.to_s.classify.presence, action.to_s.pluralize.classify].compact.join('::')}Datatable".safe_constantize ||
117
- "#{[effective_resource.namespace.to_s.classify.presence, action.to_s.singularize.classify].compact.join('::')}Datatable".safe_constantize
111
+ def resource_datatable
112
+ # This might have been done from a before action or dsl method
113
+ unless @datatable.nil?
114
+ raise('expected @datatable to be an Effective::Datatable') unless @datatable.kind_of?(Effective::Datatable)
115
+
116
+ @datatable.effective_resource = effective_resource
117
+ return @datatable
118
118
  end
119
119
 
120
+ datatable_klass = effective_resource.datatable_klass
120
121
  return unless datatable_klass.present?
121
122
 
122
123
  datatable = datatable_klass.new(resource_datatable_attributes)
@@ -76,7 +76,11 @@ module ActsAsWizard
76
76
  end
77
77
 
78
78
  def next_step
79
- required_steps.reverse.find { |step| can_visit_step?(step) } || required_steps.first
79
+ first_uncompleted_step ||
80
+ last_completed_step ||
81
+ required_steps.reverse.find { |step| can_visit_step?(step) } ||
82
+ required_steps.first ||
83
+ :start
80
84
  end
81
85
 
82
86
  def previous_step(step)
@@ -89,6 +93,13 @@ module ActsAsWizard
89
93
  previous.blank? || has_completed_step?(previous)
90
94
  end
91
95
 
96
+ def has_completed_all_previous_steps?(step)
97
+ index = required_steps.index(step).to_i
98
+ previous = required_steps[0...index]
99
+
100
+ previous.blank? || previous.all? { |step| has_completed_step?(step) }
101
+ end
102
+
92
103
  def has_completed_last_step?
93
104
  has_completed_step?(required_steps.last)
94
105
  end
@@ -97,12 +108,12 @@ module ActsAsWizard
97
108
 
98
109
  def can_revisit_completed_steps(step)
99
110
  return (step == required_steps.last) if has_completed_last_step?
100
- has_completed_previous_step?(step)
111
+ has_completed_all_previous_steps?(step)
101
112
  end
102
113
 
103
114
  def cannot_revisit_completed_steps(step)
104
115
  return (step == required_steps.last) if has_completed_last_step?
105
- has_completed_previous_step?(step) && !has_completed_step?(step)
116
+ has_completed_all_previous_steps?(step) && !has_completed_step?(step)
106
117
  end
107
118
 
108
119
  end
@@ -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)
@@ -14,7 +14,7 @@ module Effective
14
14
  include Effective::Resources::Paths
15
15
  include Effective::Resources::Relation
16
16
  include Effective::Resources::Sql
17
-
17
+ include Effective::Resources::Tenants
18
18
 
19
19
  # In practice, this is initialized two ways
20
20
  # With a klass and a namespace from effective_datatables
@@ -12,10 +12,17 @@ module Effective
12
12
  @controller_path ||= route_name #[namespace, plural_name].compact * '/')
13
13
  end
14
14
 
15
+ def engines
16
+ return ([Rails.application] + Rails::Engine.subclasses.reverse) unless tenant?
17
+
18
+ [Rails.application, Tenant.Engine] + Rails::Engine.subclasses.reverse.reject do |klass|
19
+ tenant_engines_blacklist.any? { |name| klass.name.start_with?(name) }
20
+ end
21
+ end
22
+
15
23
  def routes
16
24
  @routes ||= begin
17
25
  routes = nil
18
- engines = [Rails.application] + Rails::Engine.subclasses.reverse
19
26
 
20
27
  # Check from controller_path. This is generally correct.
21
28
  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
@@ -30,19 +30,19 @@ module Effective
30
30
 
31
31
  # Tenants
32
32
  def tenant_controller_path
33
- (Tenant.module_name.downcase + '/' + controller_path) if defined?(Tenant)
33
+ (Tenant.module_name.downcase + '/' + controller_path) if tenant?
34
34
  end
35
35
 
36
36
  def tenant_namespaced_class_name
37
- (Tenant.module_name + '::' + namespaced_class_name) if defined?(Tenant)
37
+ (Tenant.module_name + '::' + namespaced_class_name) if tenant?
38
38
  end
39
39
 
40
40
  def tenant_namespaced_module_name
41
- (Tenant.module_name + '::' + namespaced_module_name) if defined?(Tenant)
41
+ (Tenant.module_name + '::' + namespaced_module_name) if tenant?
42
42
  end
43
43
 
44
44
  def tenant_class_name
45
- (Tenant.module_name + '::' + class_name) if defined?(Tenant)
45
+ (Tenant.module_name + '::' + class_name) if tenant?
46
46
  end
47
47
 
48
48
  end
@@ -11,7 +11,7 @@ module Effective
11
11
 
12
12
  # Sets the class but also namespaces
13
13
  @model_klass = _klass_by_input(input)
14
- @model_klass = _klass_by_input(relation) if relation.present?
14
+ @model_klass = _klass_by_input(relation) unless relation.nil?
15
15
 
16
16
  # Consider controller_name
17
17
  if @model_klass && input.kind_of?(String) && namespace.blank?
@@ -40,7 +40,7 @@ module Effective
40
40
  end
41
41
 
42
42
  def find_tenant_datatable_klass
43
- return unless defined?(Tenant)
43
+ return unless tenant?
44
44
 
45
45
  "::#{tenant_controller_path.classify.pluralize}Datatable".safe_constantize ||
46
46
  "::#{tenant_controller_path.classify}Datatable".safe_constantize ||
@@ -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
  ]
@@ -65,16 +68,6 @@ module Effective
65
68
  def human_plural_name
66
69
  name.pluralize.gsub('::', ' ').underscore.gsub('_', ' ')
67
70
  end
68
-
69
- def tenant
70
- return nil unless defined?(Tenant)
71
- return nil unless klass.present?
72
- return nil unless class_name.include?('::')
73
-
74
- name = class_name.split('::').first.downcase.to_sym
75
- name if Rails.application.config.tenants[name].present?
76
- end
77
-
78
71
  end
79
72
  end
80
73
  end
@@ -5,8 +5,7 @@ module Effective
5
5
  module Paths
6
6
 
7
7
  def tenant_path
8
- return unless tenant.present?
9
- Tenant.engine_path(tenant).sub("#{Rails.root}/", '')
8
+ Tenant.engine_path(tenant).sub("#{Rails.root}/", '') if tenant?
10
9
  end
11
10
 
12
11
  def model_file
@@ -0,0 +1,24 @@
1
+ module Effective
2
+ module Resources
3
+ module Tenants
4
+
5
+ def tenant?
6
+ defined?(::Tenant)
7
+ end
8
+
9
+ def tenant
10
+ return unless tenant?
11
+ return nil unless klass.present?
12
+ return nil unless class_name.include?('::')
13
+
14
+ name = class_name.split('::').first.downcase.to_sym
15
+ name if Rails.application.config.tenants[name].present?
16
+ end
17
+
18
+ def tenant_engines_blacklist
19
+ return [] unless tenant?
20
+ Rails.application.config.tenants.map { |name, _| name.to_s.classify }
21
+ end
22
+ end
23
+ end
24
+ end
@@ -1,3 +1,3 @@
1
1
  module EffectiveResources
2
- VERSION = '1.9.2'.freeze
2
+ VERSION = '1.9.6'.freeze
3
3
  end
@@ -33,6 +33,35 @@ module EffectiveResources
33
33
 
34
34
  # Utilities
35
35
 
36
+ # This looks up the best class give the name
37
+ # If the Tenant is present, use those classes first.
38
+ def self.best(name)
39
+ klass = if defined?(Tenant)
40
+ ('::' + Tenant.module_name + '::' + name).safe_constantize ||
41
+ ('::' + Tenant.module_name + '::Effective::' + name).safe_constantize
42
+ end
43
+
44
+ klass ||= begin
45
+ ('::' + name).safe_constantize ||
46
+ ('::Effective::' + name).safe_constantize
47
+ end
48
+
49
+ raise("unable to find best #{name}") if klass.blank?
50
+
51
+ klass
52
+ end
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
+
36
65
  def self.truthy?(value)
37
66
  if defined?(::ActiveRecord::ConnectionAdapters::Column::TRUE_VALUES) # Rails <5
38
67
  ::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.2
4
+ version: 1.9.6
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-04 00:00:00.000000000 Z
11
+ date: 2021-11-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -184,6 +184,7 @@ files:
184
184
  - app/models/effective/resources/paths.rb
185
185
  - app/models/effective/resources/relation.rb
186
186
  - app/models/effective/resources/sql.rb
187
+ - app/models/effective/resources/tenants.rb
187
188
  - app/views/application/_flash.html.haml
188
189
  - app/views/application/create.js.erb
189
190
  - app/views/application/destroy.js.erb