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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: e557ad6dfb235c393169c14d3475f37784eddb5e19e90bfa759083fb184cdc48
4
- data.tar.gz: f4b9c10641496b8383c4a6086394c168e2c8b28ef443e998c094c1ece6184738
3
+ metadata.gz: 9ef5eba619e3764fdc204b3e1b72ec0063b16c2319866688f4c8e7ff3808354d
4
+ data.tar.gz: '09e02f90c4db8a1f0f416ec6ece7471db4e9ab865a4bac701a4977c9c6f4e18a'
5
5
  SHA512:
6
- metadata.gz: 640902ab866c9302df97d98ddb6ad027c26ef295181cbbdb8d03b560c9a71870cf63e8576422d36b319c6782ff44919d41724ff9ffa48c26fdec1f3a8ea1c529
7
- data.tar.gz: a7a1c6aec9a59577060aac820aca413eb6d9d7f7ac97e0089ed56b3928c5ff67229c0e86d7632203431b17d02a84642f106ad996a8daf9686d3700833a4931ad
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
- namespace = controller_path.include?('admin/') ? 'admin' : 'application'
129
- defined?(Tenant) ? "#{Tenant.current}/#{namespace}" : namespace
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?(: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
+ # 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
@@ -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.19'.freeze
2
+ VERSION = '1.8.24'.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.19
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-04-30 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