effective_resources 1.8.20 → 1.8.25

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: 2c710016d91d82e5d8705081872474e2ea12382b938b0d0c7b5836da9f1240a2
4
- data.tar.gz: c0fde3c5d9f60dd28a01690c60d4e352f94eb4f3d4e537c877290f06500debdb
3
+ metadata.gz: f3c3734459a525f8630d12e30d9e96b1e5c83e9d35cd94abecf6dcd791da2db1
4
+ data.tar.gz: 3c5d96f0b62dac483b987f16b97e7d9a003105ec28a1b09a6c556e77d379bae0
5
5
  SHA512:
6
- metadata.gz: 4b7c8f4a286e5a541b3a9ff3071e55b08c12b2499ae102723428c47bb563ab68716a63e05cd8c60f711f629e3b51396efd145a95f3e3f77b6b6bc3c13b0b06e7
7
- data.tar.gz: 00f0c760c16282fe326b09061376658f54efba2634cf270a50af3df559895ffaea3d83f38edb7e796b22803d9edc47503421403cfee6494c3bc47a7a0967d87e
6
+ metadata.gz: fcc4eb1793ace16a2f70c59fccba103f3398721eb6784dbb55d182e2320f131d11a2fae15a6a621e2ec7aa1fbe2f98074bdd66ff652fed943e835271a83cf611
7
+ data.tar.gz: 5eb1dded2b45631cd646bfda6a75d5d3cbcbf89e35f8d817d28aad950ba6e84274d30627b963e276392926bd337aec687f917a435aa11f9fc313d19395c6f6c2
@@ -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?(:message) ? error.message : resource.errors[attribute].to_sentence
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
+ # 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
@@ -11,6 +11,11 @@ module Effective
11
11
  @attributes = {}
12
12
  end
13
13
 
14
+ # For EffectiveLogging. This is a protected keyword I think.
15
+ def message(*args)
16
+ method_missing(:message, args)
17
+ end
18
+
14
19
  def read(&block)
15
20
  instance_exec(&block)
16
21
  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 - belong_tos.map { |reference| reference.foreign_key }
109
- names = names - [klass.primary_key, 'created_at', 'updated_at'] unless all
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+
@@ -32,6 +32,7 @@ module EffectiveResources
32
32
 
33
33
  ActiveRecord::Base.extend(EffectiveDeviseUser::Base)
34
34
  ActiveRecord::Base.extend(EffectiveResource::Base)
35
+ ActiveRecord::Base.include(EffectiveAfterCommit::Base)
35
36
  end
36
37
  end
37
38
 
@@ -1,3 +1,3 @@
1
1
  module EffectiveResources
2
- VERSION = '1.8.20'.freeze
2
+ VERSION = '1.8.25'.freeze
3
3
  end
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.20
4
+ version: 1.8.25
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-05-14 00:00:00.000000000 Z
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