effective_resources 1.8.22 → 1.8.27
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/app/models/concerns/effective_after_commit.rb +29 -0
- data/app/models/effective/after_commit.rb +48 -0
- data/app/models/effective/http.rb +111 -0
- data/app/models/effective/resources/attributes.rb +4 -2
- data/app/models/effective/resources/instance.rb +1 -1
- data/app/models/effective/resources/sql.rb +6 -4
- 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: 49b0e10cdea6482347d89604f85458147fe74f06ecbf0085a3ed9b8a2b68ecdd
|
4
|
+
data.tar.gz: dcfc9bd5423907bf6a5cd9b4a1a1ac191ea49bd774033a51d209064de38db017
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b5795d6c0d9565e2df3777b51ca4a074cbb1aa9eeef0f68f7a16914319dcf4860456c576548be7496af848272d3f4c579fe03057a5c126db2b89f0a621670f03
|
7
|
+
data.tar.gz: fb53b1dc820211022a2ba9c4730613c94ae40aa5c7dfdcdf216e5a92930001a7ae032fc77ae3b81e39f718f995f8cdadc72f7a4a31b9da380ceaa2aaf64a42b6
|
@@ -0,0 +1,29 @@
|
|
1
|
+
# EffectiveAfterCommit
|
2
|
+
#
|
3
|
+
# Execute code after the ActiveRecord transaction has committed
|
4
|
+
# Inspired by https://github.com/Envek/after_commit_everywhere
|
5
|
+
#
|
6
|
+
# This is automatically included into ActiveRecord::Base
|
7
|
+
#
|
8
|
+
# after_commit { MyMailer.welcome.deliver_later }
|
9
|
+
|
10
|
+
module EffectiveAfterCommit
|
11
|
+
extend ActiveSupport::Concern
|
12
|
+
|
13
|
+
module Base
|
14
|
+
def after_commit(connection: self.class.connection, &callback)
|
15
|
+
Effective::AfterCommit.register_callback(connection: connection, name: __method__, callback: callback, no_tx_action: :execute)
|
16
|
+
end
|
17
|
+
|
18
|
+
def before_commit(connection: self.class.connection, &callback)
|
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)
|
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
|
@@ -0,0 +1,48 @@
|
|
1
|
+
module Effective
|
2
|
+
class AfterCommit
|
3
|
+
|
4
|
+
def initialize(**handlers)
|
5
|
+
@handlers = handlers
|
6
|
+
end
|
7
|
+
|
8
|
+
def has_transactional_callbacks?
|
9
|
+
true
|
10
|
+
end
|
11
|
+
|
12
|
+
def trigger_transactional_callbacks?
|
13
|
+
true
|
14
|
+
end
|
15
|
+
|
16
|
+
def before_committed!(*)
|
17
|
+
@handlers[:before_commit]&.call
|
18
|
+
end
|
19
|
+
|
20
|
+
def committed!(args)
|
21
|
+
@handlers[:after_commit]&.call
|
22
|
+
end
|
23
|
+
|
24
|
+
def rolledback!(*)
|
25
|
+
@handlers[:after_rollback]&.call
|
26
|
+
end
|
27
|
+
|
28
|
+
def self.register_callback(connection:, name:, no_tx_action:, callback:)
|
29
|
+
raise ArgumentError, "#{name} expected a block" unless callback
|
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
|
42
|
+
|
43
|
+
after_commit = Effective::AfterCommit.new("#{name}": callback)
|
44
|
+
connection.add_transaction_record(after_commit)
|
45
|
+
end
|
46
|
+
|
47
|
+
end
|
48
|
+
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+
|
@@ -8,7 +8,7 @@ module Effective
|
|
8
8
|
end
|
9
9
|
|
10
10
|
def columns
|
11
|
-
klass.columns
|
11
|
+
klass.respond_to?(:columns) ? klass.columns : []
|
12
12
|
end
|
13
13
|
|
14
14
|
def column_names
|
@@ -40,9 +40,11 @@ module Effective
|
|
40
40
|
def sql_type(name)
|
41
41
|
name = name.to_s.split('.').first
|
42
42
|
|
43
|
-
if belongs_to(name)
|
44
|
-
|
45
|
-
|
43
|
+
return :belongs_to if belongs_to(name)
|
44
|
+
|
45
|
+
column = column(name)
|
46
|
+
|
47
|
+
if column.present?
|
46
48
|
column.type
|
47
49
|
elsif has_many(name)
|
48
50
|
:has_many
|
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.27
|
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-21 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
|