effective_resources 1.8.21 → 1.8.26

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: aedd54df5d6ebf911041fedaa519ab1f8fde409e6bed5fd9374e6b9460ef2506
4
- data.tar.gz: bd179f5ee32166042cd5e0f011b40e5738b7a7ec3abca222ce778c4ac69e20cc
3
+ metadata.gz: 594bd0ccce4fd20ed2b9a47b8212f4db0d092f6aaa7852dfa0b54d6eb8dac303
4
+ data.tar.gz: 066fa3be8c2cf7300517a7ca377496bed75516ec7e91dcc8e2b5bcd8a8185a8e
5
5
  SHA512:
6
- metadata.gz: 97a13796ce565f1b05891c1b92382eaaaa8677846d60add08dd23cdc2577b883bfc285b2b8f6b3d64fb39fff1fd3acd36143f476375d08bd1740f1d1003b62d3
7
- data.tar.gz: af4e9d7d6332731e7d5f6d7c6a0e17ec0c0d07aecff8d000e41ac9446b27b013c60fd218dcca846d06bf1f15bc3463b694f6e61c5b8784e10eb8c2d1efeab35d
6
+ metadata.gz: 9b92e61316487db82714da4ffffb7c7bc85f7de4027ba9ababf9b2f42df9d70391b4cbfb4884dd8dfb86f7a5a567ff24d815af2a73f7bc9b1a58ae3b296f1e4e
7
+ data.tar.gz: dfdcafafd72ffff2c629b47b61ac7d4e80c588962dcebc3c0d74dadc3131c9e121690bb73afcb60ae2fd309f2ba790faa44c832713a33505c428f87fa4c31398
@@ -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+
@@ -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
- :belongs_to
45
- elsif (column = column(name))
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
@@ -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.21'.freeze
2
+ VERSION = '1.8.26'.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.21
4
+ version: 1.8.26
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-19 00:00:00.000000000 Z
11
+ date: 2021-06-03 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