effective_resources 1.8.19 → 1.8.24
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/app/controllers/concerns/effective/crud_controller.rb +4 -2
- data/app/controllers/concerns/effective/flash_messages.rb +1 -1
- data/app/models/concerns/effective_after_commit.rb +29 -0
- data/app/models/concerns/effective_devise_user.rb +2 -0
- data/app/models/effective/after_commit.rb +53 -0
- data/app/models/effective/http.rb +111 -0
- data/app/models/effective/model_reader.rb +5 -0
- data/app/models/effective/resources/attributes.rb +4 -2
- data/lib/effective_resources/engine.rb +1 -0
- data/lib/effective_resources/version.rb +1 -1
- metadata +5 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 9ef5eba619e3764fdc204b3e1b72ec0063b16c2319866688f4c8e7ff3808354d
|
4
|
+
data.tar.gz: '09e02f90c4db8a1f0f416ec6ece7471db4e9ab865a4bac701a4977c9c6f4e18a'
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 65b2ec2e37dd19c24375b02533b86519733d83652b24fc131f267d625de17bdd0210efea61ca84e7186e2b3b0d5821a94e7faf1805665bf752c0b2636456eb8f
|
7
|
+
data.tar.gz: 8bf7c81fd895011224ffa0ed49942bf3c8517ca7e7a2c8141820247c859014de9fa1eed17517b25ed5951bff7d904c091b165d4ab1f2710ad8e674c34d9a8d06
|
@@ -125,8 +125,10 @@ module Effective
|
|
125
125
|
end
|
126
126
|
|
127
127
|
def resource_layout
|
128
|
-
|
129
|
-
|
128
|
+
if defined?(Tenant)
|
129
|
+
namespace = controller_path.include?('admin/') ? 'admin' : 'application'
|
130
|
+
"#{Tenant.current}/#{namespace}"
|
131
|
+
end
|
130
132
|
end
|
131
133
|
|
132
134
|
def resource_params_method_name
|
@@ -41,7 +41,7 @@ module Effective
|
|
41
41
|
|
42
42
|
messages = resource.errors.map do |error|
|
43
43
|
attribute = error.respond_to?(:attribute) ? error.attribute : error
|
44
|
-
message = error.respond_to?(:
|
44
|
+
message = error.respond_to?(:attribute) ? error.message : resource.errors[attribute].to_sentence
|
45
45
|
|
46
46
|
if message[0] == message[0].upcase # If the error begins with a capital letter
|
47
47
|
message
|
@@ -0,0 +1,29 @@
|
|
1
|
+
# EffectiveAfterCommit
|
2
|
+
#
|
3
|
+
# Inspired by https://github.com/Envek/after_commit_everywhere
|
4
|
+
#
|
5
|
+
# This is automatically included into ActiveRecord::Base
|
6
|
+
#
|
7
|
+
# after_commit { MyMailer.welcome.deliver_later }
|
8
|
+
|
9
|
+
module EffectiveAfterCommit
|
10
|
+
extend ActiveSupport::Concern
|
11
|
+
|
12
|
+
module Base
|
13
|
+
def after_commit(connection: self.class.connection, &callback)
|
14
|
+
Effective::AfterCommit.register_callback(connection: connection, name: __method__, callback: callback, no_tx_action: :execute)
|
15
|
+
end
|
16
|
+
|
17
|
+
def before_commit(connection: self.class.connection, &callback)
|
18
|
+
raise(NotImplementedError, "#{__method__} works only with Rails 5.0+") if ActiveRecord::VERSION::MAJOR < 5
|
19
|
+
|
20
|
+
Effective::AfterCommit.register_callback(connection: connection, name: __method__, callback: callback, no_tx_action: :warn_and_execute)
|
21
|
+
end
|
22
|
+
|
23
|
+
def after_rollback(connection: self.class.connection, &callback)
|
24
|
+
raise('expected a block') unless block_given?
|
25
|
+
Effective::AfterCommit.register_callback(connection: connection, name: __method__, callback: callback, no_tx_action: :exception)
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
end
|
@@ -65,6 +65,8 @@ module EffectiveDeviseUser
|
|
65
65
|
end
|
66
66
|
|
67
67
|
module ClassMethods
|
68
|
+
def effective_devise_user?; true; end
|
69
|
+
|
68
70
|
def permitted_sign_up_params # Should contain all fields as per views/users/_sign_up_fields
|
69
71
|
raise('please define a self.permitted_sign_up_params')
|
70
72
|
[:email, :password, :password_confirmation, :first_name, :last_name, :name, :login]
|
@@ -0,0 +1,53 @@
|
|
1
|
+
module Effective
|
2
|
+
class AfterCommit
|
3
|
+
|
4
|
+
def initialize(connection:, **handlers)
|
5
|
+
@connection = connection
|
6
|
+
@handlers = handlers
|
7
|
+
end
|
8
|
+
|
9
|
+
def has_transactional_callbacks?
|
10
|
+
true
|
11
|
+
end
|
12
|
+
|
13
|
+
def trigger_transactional_callbacks?
|
14
|
+
true
|
15
|
+
end
|
16
|
+
|
17
|
+
def before_committed!(*)
|
18
|
+
@handlers[:before_commit]&.call
|
19
|
+
end
|
20
|
+
|
21
|
+
def committed!(args)
|
22
|
+
@handlers[:after_commit]&.call
|
23
|
+
end
|
24
|
+
|
25
|
+
def rolledback!(*)
|
26
|
+
@handlers[:after_rollback]&.call
|
27
|
+
end
|
28
|
+
|
29
|
+
def add_to_transaction(*)
|
30
|
+
@connection.add_transaction_record(self)
|
31
|
+
end
|
32
|
+
|
33
|
+
def self.register_callback(connection:, name:, no_tx_action:, callback:)
|
34
|
+
raise ArgumentError, "#{name} expected a block" unless callback
|
35
|
+
|
36
|
+
unless (connection.transaction_open? && connection.current_transaction.joinable?)
|
37
|
+
case no_tx_action
|
38
|
+
when :warn_and_execute
|
39
|
+
warn "#{name}: No transaction open. Executing callback immediately."
|
40
|
+
return callback.call
|
41
|
+
when :execute
|
42
|
+
return callback.call
|
43
|
+
when :exception
|
44
|
+
raise("#{name} is useless outside transaction")
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
after_commit = Effective::AfterCommit.new(connection: connection, "#{name}": callback)
|
49
|
+
connection.add_transaction_record(after_commit)
|
50
|
+
end
|
51
|
+
|
52
|
+
end
|
53
|
+
end
|
@@ -0,0 +1,111 @@
|
|
1
|
+
module Effective
|
2
|
+
class Http
|
3
|
+
|
4
|
+
def self.get(endpoint, params: nil, headers: nil)
|
5
|
+
headers = { 'Content-Type': 'application/json' }.merge(headers || {})
|
6
|
+
query = ('?' + params.compact.map { |k, v| "$#{k}=#{v}" }.join('&')) if params.present?
|
7
|
+
|
8
|
+
uri = URI.parse(endpoint + query.to_s)
|
9
|
+
|
10
|
+
http = Net::HTTP.new(uri.host, uri.port)
|
11
|
+
http.read_timeout = 10
|
12
|
+
http.use_ssl = true if endpoint.start_with?('https')
|
13
|
+
|
14
|
+
response = with_retries do
|
15
|
+
puts "[GET] #{uri}" if Rails.env.development?
|
16
|
+
http.get(uri, headers)
|
17
|
+
end
|
18
|
+
|
19
|
+
unless ['200', '204'].include?(response.code.to_s)
|
20
|
+
puts("Response code: #{response.code} #{response.body}")
|
21
|
+
return false
|
22
|
+
end
|
23
|
+
|
24
|
+
JSON.parse(response.body)
|
25
|
+
end
|
26
|
+
|
27
|
+
def self.post(endpoint, params:, headers: nil)
|
28
|
+
headers = { 'Content-Type': 'application/json' }.merge(headers || {})
|
29
|
+
|
30
|
+
uri = URI.parse(endpoint)
|
31
|
+
|
32
|
+
http = Net::HTTP.new(uri.host, uri.port)
|
33
|
+
http.read_timeout = 10
|
34
|
+
http.use_ssl = true if endpoint.start_with?('https')
|
35
|
+
|
36
|
+
response = with_retries do
|
37
|
+
puts "[POST] #{uri} #{params}" if Rails.env.development?
|
38
|
+
http.post(uri.path, params.to_json, headers)
|
39
|
+
end
|
40
|
+
|
41
|
+
unless ['200', '204'].include?(response.code.to_s)
|
42
|
+
puts("Response code: #{response.code} #{response.body}")
|
43
|
+
return false
|
44
|
+
end
|
45
|
+
|
46
|
+
JSON.parse(response.body)
|
47
|
+
end
|
48
|
+
|
49
|
+
def self.patch(endpoint, params:, headers: nil)
|
50
|
+
headers = { 'Content-Type': 'application/json' }.merge(headers || {})
|
51
|
+
|
52
|
+
uri = URI.parse(endpoint)
|
53
|
+
|
54
|
+
http = Net::HTTP.new(uri.host, uri.port)
|
55
|
+
http.read_timeout = 10
|
56
|
+
http.use_ssl = true if endpoint.start_with?('https')
|
57
|
+
|
58
|
+
response = with_retries do
|
59
|
+
puts "[PATCH] #{uri} #{params}" if Rails.env.development?
|
60
|
+
http.post(uri.path, params.to_json, headers)
|
61
|
+
end
|
62
|
+
|
63
|
+
unless ['200', '204'].include?(response.code.to_s)
|
64
|
+
puts("Response code: #{response.code} #{response.body}")
|
65
|
+
return false
|
66
|
+
end
|
67
|
+
|
68
|
+
JSON.parse(response.body)
|
69
|
+
end
|
70
|
+
|
71
|
+
def self.delete(endpoint, params: nil, headers: nil)
|
72
|
+
headers = { 'Content-Type': 'application/json' }.merge(headers || {})
|
73
|
+
query = ('?' + params.compact.map { |k, v| "$#{k}=#{v}" }.join('&')) if params.present?
|
74
|
+
|
75
|
+
uri = URI.parse(endpoint + query.to_s)
|
76
|
+
|
77
|
+
http = Net::HTTP.new(uri.host, uri.port)
|
78
|
+
http.read_timeout = 10
|
79
|
+
http.use_ssl = true if endpoint.start_with?('https')
|
80
|
+
|
81
|
+
response = with_retries do
|
82
|
+
puts "[DELETE] #{uri}" if Rails.env.development?
|
83
|
+
http.delete(uri, headers)
|
84
|
+
end
|
85
|
+
|
86
|
+
unless ['200', '204'].include?(response.code.to_s)
|
87
|
+
puts("Response code: #{response.code} #{response.body}")
|
88
|
+
return false
|
89
|
+
end
|
90
|
+
|
91
|
+
JSON.parse(response.body)
|
92
|
+
end
|
93
|
+
|
94
|
+
private
|
95
|
+
|
96
|
+
def self.with_retries(retries: 3, wait: 2, &block)
|
97
|
+
raise('expected a block') unless block_given?
|
98
|
+
|
99
|
+
begin
|
100
|
+
return yield
|
101
|
+
rescue Exception => e
|
102
|
+
if (retries -= 1) > 0
|
103
|
+
sleep(wait); retry
|
104
|
+
else
|
105
|
+
raise
|
106
|
+
end
|
107
|
+
end
|
108
|
+
end
|
109
|
+
|
110
|
+
end
|
111
|
+
end
|
@@ -105,8 +105,10 @@ module Effective
|
|
105
105
|
attributes = (klass.new().attributes rescue nil)
|
106
106
|
return [] unless attributes
|
107
107
|
|
108
|
-
names = attributes.keys
|
109
|
-
names
|
108
|
+
names = attributes.keys
|
109
|
+
names -= belong_tos.map { |reference| reference.foreign_key }
|
110
|
+
names -= belong_tos.map { |reference| reference.foreign_type if reference.options[:polymorphic] }
|
111
|
+
names -= [klass.primary_key, 'created_at', 'updated_at'] unless all
|
110
112
|
|
111
113
|
attributes = names.inject({}) do |h, name|
|
112
114
|
if klass.respond_to?(:column_for_attribute) # Rails 4+
|
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.8.
|
4
|
+
version: 1.8.24
|
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
|
+
date: 2021-06-02 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|
@@ -157,13 +157,16 @@ files:
|
|
157
157
|
- app/models/concerns/acts_as_statused.rb
|
158
158
|
- app/models/concerns/acts_as_tokened.rb
|
159
159
|
- app/models/concerns/acts_as_wizard.rb
|
160
|
+
- app/models/concerns/effective_after_commit.rb
|
160
161
|
- app/models/concerns/effective_devise_user.rb
|
161
162
|
- app/models/concerns/effective_resource.rb
|
162
163
|
- app/models/concerns/has_many_rich_texts.rb
|
163
164
|
- app/models/effective/access_denied.rb
|
164
165
|
- app/models/effective/action_failed.rb
|
166
|
+
- app/models/effective/after_commit.rb
|
165
167
|
- app/models/effective/attribute.rb
|
166
168
|
- app/models/effective/code_reader.rb
|
169
|
+
- app/models/effective/http.rb
|
167
170
|
- app/models/effective/model_reader.rb
|
168
171
|
- app/models/effective/resource.rb
|
169
172
|
- app/models/effective/resource_exec.rb
|