adaptive_alias 1.0.0 → 1.1.1

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: f5938ea6d66ddde37c2f28e252297844a422d3431b37105c54b2246244880971
4
- data.tar.gz: ad5e44e57886b67db5eaacc898a910b5b916d194e8eca3ad821851f9b9c4a35e
3
+ metadata.gz: 2557ac5e97d02789b75b5468e51f3b67d677195cd90111416222bba40d47b79c
4
+ data.tar.gz: 54561a2436419e0dec856da7d678e3c01dfb437d6642ee3dc5a162492b848a54
5
5
  SHA512:
6
- metadata.gz: 2ed9da0a54fea620f1d31ebb8c6c15b11591eb5b34d96e21f699dd1c3c0acb64a9415ca224388688b8f6af1871f775b197a4876bf54e4abd24a59ec6ec154922
7
- data.tar.gz: bcc8815527922cfb226c7ea8c718f25ed9d2f8cd029b0666ec567c9a2fb2ce3414596cbfc39e146d817be7ee8b2f36dfa6a304267ad60cb903ac3dc54706321e
6
+ metadata.gz: 68b69b12d2f60509dc9c014d684727f688e57d87cd2baf612ba9cfa4e7efaa7b2e10ec05ecd60d1e83a9c547e4c6f896f816bc529b490ebb8f1a1e76888a5fd1
7
+ data.tar.gz: 90e873a39c890cacea169226ca33a78b404d347aa5a238a793e2a753e61d7b43a46507ea52f50b467ac45fbc923f8135e8b60cd3a4763f1061e20ab94c69aecf
@@ -24,7 +24,7 @@ jobs:
24
24
  ruby:
25
25
  - 2.6
26
26
  - 2.7
27
- - 3.0
27
+ - 3.0.4
28
28
  - 3.1
29
29
  gemfile:
30
30
  - 6.0.gemfile
data/.rubocop.yml CHANGED
@@ -812,7 +812,7 @@ Style/NumericLiterals:
812
812
  Add underscores to large numeric literals to improve their
813
813
  readability.
814
814
  StyleGuide: 'https://github.com/bbatsov/ruby-style-guide#underscores-in-numerics'
815
- Enabled: true
815
+ Enabled: false
816
816
 
817
817
  Style/OneLineConditional:
818
818
  Description: >-
data/CHANGELOG.md CHANGED
@@ -1,37 +1,45 @@
1
- ## Change Log
2
-
3
- ### [v0.2.4](https://github.com/khiav223577/adaptive_alias/compare/v0.2.3...v0.2.4) 2022/08/18
4
- - [#18](https://github.com/khiav223577/adaptive_alias/pull/18) [Fix] get model from a relation with join will not generate right query (after migration) (@khiav223577)
5
-
6
- ### [v0.2.3](https://github.com/khiav223577/adaptive_alias/compare/v0.2.2...v0.2.3) 2022/08/17
7
- - [#17](https://github.com/khiav223577/adaptive_alias/pull/17) [Feature] Support joins (@khiav223577)
8
- - [#16](https://github.com/khiav223577/adaptive_alias/pull/16) [Enhance] No need to clone node (@khiav223577)
9
- - [#15](https://github.com/khiav223577/adaptive_alias/pull/15) [Test] Add test cases to test calling pluck on same model (@khiav223577)
10
-
11
- ### [v0.2.2](https://github.com/khiav223577/adaptive_alias/compare/v0.2.1...v0.2.2) 2022/08/11
12
- - [#14](https://github.com/khiav223577/adaptive_alias/pull/14) [Fix] Fail to use backward-patch when target column has already been migrated (@khiav223577)
13
-
14
- ### [v0.2.1](https://github.com/khiav223577/adaptive_alias/compare/v0.2.0...v0.2.1) 2022/08/11
15
- - [#13](https://github.com/khiav223577/adaptive_alias/pull/13) [Fix] Nested module include is not supported until ruby 3.0 (@khiav223577)
16
- - [#12](https://github.com/khiav223577/adaptive_alias/pull/12) [Test] Add test cases to test column_names (@khiav223577)
17
-
18
- ### [v0.2.0](https://github.com/khiav223577/adaptive_alias/compare/v0.1.0...v0.2.0) 2022/08/05
19
- - [#11](https://github.com/khiav223577/adaptive_alias/pull/11) [Feature] Support polymorphic (@khiav223577)
20
- - [#10](https://github.com/khiav223577/adaptive_alias/pull/10) [Enhance] Prevent adding methods directly in class / module (@khiav223577)
21
- - [#9](https://github.com/khiav223577/adaptive_alias/pull/9) [Enhance] Prevent changing original order of where conditions (@khiav223577)
22
- - [#8](https://github.com/khiav223577/adaptive_alias/pull/8) [Test] Make sure we are reset to use original patch when some test cases fail (@khiav223577)
23
- - [#7](https://github.com/khiav223577/adaptive_alias/pull/7) [Test] Add test cases to test destroy (@khiav223577)
24
-
25
- ### [v0.1.0](https://github.com/khiav223577/adaptive_alias/compare/v0.0.3...v0.1.0) 2022/08/01
26
- - [#6](https://github.com/khiav223577/adaptive_alias/pull/6) [Feature] Deal with creating records (@khiav223577)
27
- - [#5](https://github.com/khiav223577/adaptive_alias/pull/5) [Enhance] Prevent infinite loop if something went wrong (@khiav223577)
28
- - [#4](https://github.com/khiav223577/adaptive_alias/pull/4) [Fix] Attributes writer method should be defined after schema changes (@khiav223577)
29
-
30
- ### [v0.0.3](https://github.com/khiav223577/adaptive_alias/compare/v0.0.2...v0.0.3) 2022/07/27
31
- - [#3](https://github.com/khiav223577/adaptive_alias/pull/3) [Fix] Prevent calling custom column names from raising missing attributes (@khiav223577)
32
-
33
- ### [v0.0.2](https://github.com/khiav223577/adaptive_alias/compare/v0.0.1...v0.0.2) 2022/07/26
34
- - [#2](https://github.com/khiav223577/adaptive_alias/pull/2) [Enhance] Doesn't need rails_compatibility in runtime (@khiav223577)
35
-
36
- ### v0.0.1 2022/07/22
37
- - [#1](https://github.com/khiav223577/adaptive_alias/pull/1) [Feature] Implement adaptive_alias features (@khiav223577)
1
+ ## Change Log
2
+
3
+ ### [v1.1.0](https://github.com/khiav223577/adaptive_alias/compare/v1.0.0...v1.1.0) 2022/09/27
4
+ - [#22](https://github.com/khiav223577/adaptive_alias/pull/22) [Fix] creation via autosave(belongs_to) association (@khiav223577)
5
+
6
+ ### [v1.0.0](https://github.com/khiav223577/adaptive_alias/compare/v0.2.4...v1.0.0) 2022/09/22
7
+ - [#21](https://github.com/khiav223577/adaptive_alias/pull/21) [Fix] Model with ignored_columns or calling find_by directly on model class with aliased column argument will fail (@khiav223577)
8
+ - [#20](https://github.com/khiav223577/adaptive_alias/pull/20) [Fix] Problems with STI classes and models that uses same table name (@khiav223577)
9
+ - [#19](https://github.com/khiav223577/adaptive_alias/pull/19) [Fix] Rescue the error when doing direct access to target table's field (@khiav223577)
10
+
11
+ ### [v0.2.4](https://github.com/khiav223577/adaptive_alias/compare/v0.2.3...v0.2.4) 2022/08/18
12
+ - [#18](https://github.com/khiav223577/adaptive_alias/pull/18) [Fix] Get model from a relation with join will not generate right query (after migration) (@khiav223577)
13
+
14
+ ### [v0.2.3](https://github.com/khiav223577/adaptive_alias/compare/v0.2.2...v0.2.3) 2022/08/17
15
+ - [#17](https://github.com/khiav223577/adaptive_alias/pull/17) [Feature] Support joins (@khiav223577)
16
+ - [#16](https://github.com/khiav223577/adaptive_alias/pull/16) [Enhance] No need to clone node (@khiav223577)
17
+ - [#15](https://github.com/khiav223577/adaptive_alias/pull/15) [Test] Add test cases to test calling pluck on same model (@khiav223577)
18
+
19
+ ### [v0.2.2](https://github.com/khiav223577/adaptive_alias/compare/v0.2.1...v0.2.2) 2022/08/11
20
+ - [#14](https://github.com/khiav223577/adaptive_alias/pull/14) [Fix] Fail to use backward-patch when target column has already been migrated (@khiav223577)
21
+
22
+ ### [v0.2.1](https://github.com/khiav223577/adaptive_alias/compare/v0.2.0...v0.2.1) 2022/08/11
23
+ - [#13](https://github.com/khiav223577/adaptive_alias/pull/13) [Fix] Nested module include is not supported until ruby 3.0 (@khiav223577)
24
+ - [#12](https://github.com/khiav223577/adaptive_alias/pull/12) [Test] Add test cases to test column_names (@khiav223577)
25
+
26
+ ### [v0.2.0](https://github.com/khiav223577/adaptive_alias/compare/v0.1.0...v0.2.0) 2022/08/05
27
+ - [#11](https://github.com/khiav223577/adaptive_alias/pull/11) [Feature] Support polymorphic (@khiav223577)
28
+ - [#10](https://github.com/khiav223577/adaptive_alias/pull/10) [Enhance] Prevent adding methods directly in class / module (@khiav223577)
29
+ - [#9](https://github.com/khiav223577/adaptive_alias/pull/9) [Enhance] Prevent changing original order of where conditions (@khiav223577)
30
+ - [#8](https://github.com/khiav223577/adaptive_alias/pull/8) [Test] Make sure we are reset to use original patch when some test cases fail (@khiav223577)
31
+ - [#7](https://github.com/khiav223577/adaptive_alias/pull/7) [Test] Add test cases to test destroy (@khiav223577)
32
+
33
+ ### [v0.1.0](https://github.com/khiav223577/adaptive_alias/compare/v0.0.3...v0.1.0) 2022/08/01
34
+ - [#6](https://github.com/khiav223577/adaptive_alias/pull/6) [Feature] Deal with creating records (@khiav223577)
35
+ - [#5](https://github.com/khiav223577/adaptive_alias/pull/5) [Enhance] Prevent infinite loop if something went wrong (@khiav223577)
36
+ - [#4](https://github.com/khiav223577/adaptive_alias/pull/4) [Fix] Attributes writer method should be defined after schema changes (@khiav223577)
37
+
38
+ ### [v0.0.3](https://github.com/khiav223577/adaptive_alias/compare/v0.0.2...v0.0.3) 2022/07/27
39
+ - [#3](https://github.com/khiav223577/adaptive_alias/pull/3) [Fix] Prevent calling custom column names from raising missing attributes (@khiav223577)
40
+
41
+ ### [v0.0.2](https://github.com/khiav223577/adaptive_alias/compare/v0.0.1...v0.0.2) 2022/07/26
42
+ - [#2](https://github.com/khiav223577/adaptive_alias/pull/2) [Enhance] Doesn't need rails_compatibility in runtime (@khiav223577)
43
+
44
+ ### v0.0.1 2022/07/22
45
+ - [#1](https://github.com/khiav223577/adaptive_alias/pull/1) [Feature] Implement adaptive_alias features (@khiav223577)
@@ -18,7 +18,6 @@ module AdaptiveAlias
18
18
  name = self.class.attribute_aliases[name] || name
19
19
 
20
20
  sync_with_transaction_state if @transaction_state&.finalized?
21
- return yield(name) if block_given? and AdaptiveAlias.missing_value?(@attributes, self.class, name)
22
21
  return @attributes.fetch_value(name, &block)
23
22
  end
24
23
  end
@@ -2,11 +2,11 @@ module AdaptiveAlias
2
2
  module Hooks
3
3
  module ActiveRecordCore
4
4
  def find(*)
5
- AdaptiveAlias.rescue_statement_invalid(nil, nil){ super }
5
+ AdaptiveAlias.rescue_statement_invalid{ super }
6
6
  end
7
7
 
8
8
  def find_by(*)
9
- AdaptiveAlias.rescue_statement_invalid(nil, nil){ super }
9
+ AdaptiveAlias.rescue_statement_invalid{ super }
10
10
  end
11
11
  end
12
12
  end
@@ -0,0 +1,52 @@
1
+ module AdaptiveAlias
2
+ module Hooks
3
+ module ActiveRecordPersistence
4
+ def _update_record
5
+ AdaptiveAlias.rescue_statement_invalid(model: self) do
6
+ attribute_set_fix!
7
+ super
8
+ end
9
+ end
10
+
11
+ def _create_record(*)
12
+ AdaptiveAlias.rescue_statement_invalid(model: self) do
13
+ attribute_set_fix!
14
+ super
15
+ end
16
+ end
17
+
18
+ private
19
+
20
+ def attribute_set_fix!
21
+ if attribute_names.any?{|name| self.class.attribute_aliases[name] }
22
+ attributes = @attributes.instance_variable_get(:@attributes)
23
+ attributes = attributes.instance_variable_get(:@delegate_hash) if attributes.is_a?(ActiveModel::LazyAttributeHash)
24
+
25
+ delete_duplicate_keys!(attributes)
26
+
27
+ attributes.transform_keys! do |key|
28
+ self.class.attribute_aliases[key] || key
29
+ end
30
+ end
31
+ end
32
+
33
+ # delete duplicate keys caused by instantiate after migration but before patch removal.
34
+ def delete_duplicate_keys!(attributes)
35
+ duplicate_keys = {}
36
+ delete_keys = []
37
+ attributes.each do |key, _|
38
+ aliased_key = self.class.attribute_aliases[key] || key
39
+ delete_keys << key if duplicate_keys[aliased_key] # delete old key and keep the new one
40
+ duplicate_keys[aliased_key] = true
41
+ end
42
+
43
+ delete_keys.each{|s| attributes.delete(s) }
44
+ end
45
+ end
46
+ end
47
+ end
48
+
49
+ # Nested module include is not supported until ruby 3.0
50
+ class ActiveRecord::Base
51
+ prepend AdaptiveAlias::Hooks::ActiveRecordPersistence
52
+ end
@@ -2,11 +2,7 @@ module AdaptiveAlias
2
2
  module Hooks
3
3
  module Association
4
4
  def find_target(*)
5
- AdaptiveAlias.rescue_statement_invalid(nil, reflection){ super }
6
- end
7
-
8
- def create!(attributes = {}, &block)
9
- AdaptiveAlias.rescue_statement_invalid(association_scope, reflection){ super }
5
+ AdaptiveAlias.rescue_statement_invalid(reflection: reflection){ super }
10
6
  end
11
7
  end
12
8
  end
@@ -2,15 +2,15 @@ module AdaptiveAlias
2
2
  module Hooks
3
3
  module Relation
4
4
  def pluck(*)
5
- AdaptiveAlias.rescue_statement_invalid(self, nil){ super }
5
+ AdaptiveAlias.rescue_statement_invalid(relation: self){ super }
6
6
  end
7
7
 
8
8
  def select_all(*)
9
- AdaptiveAlias.rescue_statement_invalid(self, nil){ super }
9
+ AdaptiveAlias.rescue_statement_invalid(relation: self){ super }
10
10
  end
11
11
 
12
12
  def exec_queries(*)
13
- AdaptiveAlias.rescue_statement_invalid(self, nil){ super }
13
+ AdaptiveAlias.rescue_statement_invalid(relation: self){ super }
14
14
  end
15
15
  end
16
16
  end
@@ -63,7 +63,10 @@ module AdaptiveAlias
63
63
  next false if not patch.removable
64
64
  next false if patch.removed
65
65
  next false if klass.table_name != error_klass.table_name
66
- next false if not expected_attribute_err_msgs.include?(error.message)
66
+
67
+ # Error highlight behavior in Ruby 3.1 pollutes the error message
68
+ error_msg = error.respond_to?(:original_message) ? error.original_message : error.message
69
+ next false if not expected_attribute_err_msgs.include?(error_msg)
67
70
 
68
71
  patch.remove!
69
72
  next true
@@ -84,18 +87,22 @@ module AdaptiveAlias
84
87
  end
85
88
  end
86
89
 
87
- @fix_association = proc do |relation, reflection, error|
90
+ @fix_association = proc do |relation, reflection, model, error|
88
91
  next false if not patch.removable
89
92
  next false if patch.removed
90
93
 
91
- ambiguous = expected_ambiguous_association_err_msgs.include?(error.message)
94
+ # Error highlight behavior in Ruby 3.1 pollutes the error message
95
+ error_msg = error.respond_to?(:original_message) ? error.original_message : error.message
96
+ ambiguous = expected_ambiguous_association_err_msgs.include?(error_msg)
92
97
 
93
98
  if ambiguous
94
99
  next false if relation and klass.table_name != relation.klass.table_name
95
100
  next false if reflection and klass.table_name != reflection.klass.table_name
101
+ next false if model and klass.table_name != model.class.table_name
102
+ next false if !relation and !reflection and !model
96
103
  end
97
104
 
98
- next false if not expected_association_err_msgs.include?(error.message) and not ambiguous
105
+ next false if not expected_association_err_msgs.include?(error_msg) and not ambiguous
99
106
 
100
107
  patch.remove!
101
108
 
@@ -1,3 +1,3 @@
1
1
  module AdaptiveAlias
2
- VERSION = '1.0.0'
2
+ VERSION = '1.1.1'
3
3
  end
@@ -14,6 +14,7 @@ require 'adaptive_alias/hooks/association_scope'
14
14
  require 'adaptive_alias/hooks/singular_association'
15
15
  require 'adaptive_alias/hooks/relation'
16
16
  require 'adaptive_alias/hooks/active_record_core'
17
+ require 'adaptive_alias/hooks/active_record_persistence'
17
18
 
18
19
  module AdaptiveAlias
19
20
  @log_interval = 10 * 60
@@ -42,12 +43,12 @@ module AdaptiveAlias
42
43
  end
43
44
  end
44
45
 
45
- def rescue_statement_invalid(relation, reflection, &block)
46
+ def rescue_statement_invalid(relation: nil, reflection: nil, model: nil, &block)
46
47
  yield
47
48
  rescue ActiveRecord::StatementInvalid => error
48
- raise error if AdaptiveAlias.current_patches.all?{|_key, patch| !patch.fix_association.call(relation, reflection, error) }
49
+ raise error if AdaptiveAlias.current_patches.all?{|_key, patch| !patch.fix_association.call(relation, reflection, model, error) }
49
50
 
50
- result = rescue_statement_invalid(relation, reflection, &block)
51
+ result = rescue_statement_invalid(relation: relation, reflection: reflection, model: model, &block)
51
52
  AdaptiveAlias.current_patches.each_value(&:mark_removable)
52
53
  return result
53
54
  end
@@ -69,13 +70,5 @@ module AdaptiveAlias
69
70
  klass.prepend(@model_modules[klass])
70
71
  return @model_modules[klass]
71
72
  end
72
-
73
- def missing_value?(attributes, klass, name)
74
- return false if attributes.key?(name)
75
-
76
- old_name = klass.attribute_aliases.key(name)
77
- return false if old_name == nil
78
- return !!AdaptiveAlias.current_patches[[klass, old_name.to_sym, name.to_sym]]
79
- end
80
73
  end
81
74
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: adaptive_alias
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - khiav reoy
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2022-09-22 00:00:00.000000000 Z
11
+ date: 2022-10-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -129,6 +129,7 @@ files:
129
129
  - lib/adaptive_alias/active_model_patches/remove_alias_attribute.rb
130
130
  - lib/adaptive_alias/active_model_patches/write_attribute.rb
131
131
  - lib/adaptive_alias/hooks/active_record_core.rb
132
+ - lib/adaptive_alias/hooks/active_record_persistence.rb
132
133
  - lib/adaptive_alias/hooks/association.rb
133
134
  - lib/adaptive_alias/hooks/association_scope.rb
134
135
  - lib/adaptive_alias/hooks/relation.rb