adaptive_alias 0.2.4 → 1.1.0

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: f2c42a4fb70c22a74c09b64fba9c94dc0b1189a94c3e1ca954161ef20e074460
4
- data.tar.gz: 51ae34971dff012f5e691bd8fd79252db0706277e38dcdf43766b7eca1b163aa
3
+ metadata.gz: 2fe3d8361642cc73cba568a7a131efa94f9e17eeada3f1af45990ff876abcb6d
4
+ data.tar.gz: befec070dac8af388634e02f5479a78d61ca4e7ddd10e7a489aeae2e2e7180e8
5
5
  SHA512:
6
- metadata.gz: 1f30e10e5a9b035b255eeb129673563b56b10b5ee2fbdac22717fc31dd9a815e77bc1f5a5238c04dfe9cff27414e6db8e72f8bf5c1e360f3905dff29e1cd17d7
7
- data.tar.gz: 3b1048bc0d4a42ea9f06a4ecfd215a15ae4687f67d1d52b659a64a096151eb40efe987f7ce3b46273ab89013d0739e7ca925db4ced2f3ca4fe4fcc6a2fb7bb54
6
+ metadata.gz: 2c676366d3132428d290da9291e886f06620d07836a56b87bb59d82f4dc84b86e8ba73f317a26b126d01bb4011a86c04d5653476c277b49d2f81d3728be5e973
7
+ data.tar.gz: d26cc5ad6eb77b9cba338788eafcb9f3826b7dbe4cc4afbf1dfd8e22f32bf626be7294bfd87f007e60db429f7294127f994eb4d402c9699cde83017b0b38b540
data/.rubocop.yml CHANGED
@@ -222,9 +222,9 @@ Metrics/CyclomaticComplexity:
222
222
  Enabled: true
223
223
 
224
224
  Layout/LineLength:
225
- Description: 'Limit lines to 160 characters.'
225
+ Description: 'Limit lines to 180 characters.'
226
226
  StyleGuide: 'https://github.com/bbatsov/ruby-style-guide#80-character-limits'
227
- Max: 160
227
+ Max: 180
228
228
  Enabled: true
229
229
  Exclude:
230
230
  - 'config/locales/**'
data/CHANGELOG.md CHANGED
@@ -1,5 +1,13 @@
1
1
  ## Change Log
2
2
 
3
+ ### [v1.0.0](https://github.com/khiav223577/adaptive_alias/compare/v0.2.4...v1.0.0) 2022/09/22
4
+ - [#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)
5
+ - [#20](https://github.com/khiav223577/adaptive_alias/pull/20) [Fix] Problems with STI classes and models that uses same table name (@khiav223577)
6
+ - [#19](https://github.com/khiav223577/adaptive_alias/pull/19) [Fix] Rescue the error when doing direct access to target table's field (@khiav223577)
7
+
8
+ ### [v0.2.4](https://github.com/khiav223577/adaptive_alias/compare/v0.2.3...v0.2.4) 2022/08/18
9
+ - [#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)
10
+
3
11
  ### [v0.2.3](https://github.com/khiav223577/adaptive_alias/compare/v0.2.2...v0.2.3) 2022/08/17
4
12
  - [#17](https://github.com/khiav223577/adaptive_alias/pull/17) [Feature] Support joins (@khiav223577)
5
13
  - [#16](https://github.com/khiav223577/adaptive_alias/pull/16) [Enhance] No need to clone node (@khiav223577)
@@ -20,6 +20,7 @@ module AdaptiveAlias
20
20
  end
21
21
  end
22
22
 
23
+ # Nested module include is not supported until ruby 3.0
23
24
  class ActiveRecord::Base
24
25
  prepend AdaptiveAlias::ActiveModelPatches::WriteAttribute
25
26
  end
@@ -0,0 +1,18 @@
1
+ module AdaptiveAlias
2
+ module Hooks
3
+ module ActiveRecordCore
4
+ def find(*)
5
+ AdaptiveAlias.rescue_statement_invalid{ super }
6
+ end
7
+
8
+ def find_by(*)
9
+ AdaptiveAlias.rescue_statement_invalid{ super }
10
+ end
11
+ end
12
+ end
13
+ end
14
+
15
+ # Nested module include is not supported until ruby 3.0
16
+ class << ActiveRecord::Base
17
+ prepend AdaptiveAlias::Hooks::ActiveRecordCore
18
+ end
@@ -0,0 +1,14 @@
1
+ module AdaptiveAlias
2
+ module Hooks
3
+ module ActiveRecordPersistence
4
+ def _create_record(*)
5
+ AdaptiveAlias.rescue_statement_invalid(model: self){ super }
6
+ end
7
+ end
8
+ end
9
+ end
10
+
11
+ # Nested module include is not supported until ruby 3.0
12
+ class ActiveRecord::Base
13
+ prepend AdaptiveAlias::Hooks::ActiveRecordPersistence
14
+ 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
@@ -47,6 +47,7 @@ module AdaptiveAlias
47
47
  expected_association_err_msgs = [
48
48
  "Mysql2::Error: Unknown column '#{klass.table_name}.#{current_column}' in 'where clause'".freeze,
49
49
  "Mysql2::Error: Unknown column '#{klass.table_name}.#{current_column}' in 'on clause'".freeze,
50
+ "Mysql2::Error: Unknown column '#{klass.table_name}.#{current_column}' in 'field list'".freeze,
50
51
  ].freeze
51
52
 
52
53
  expected_ambiguous_association_err_msgs = [
@@ -61,7 +62,7 @@ module AdaptiveAlias
61
62
  @fix_missing_attribute = proc do |error_klass, error|
62
63
  next false if not patch.removable
63
64
  next false if patch.removed
64
- next false if klass != error_klass
65
+ next false if klass.table_name != error_klass.table_name
65
66
  next false if not expected_attribute_err_msgs.include?(error.message)
66
67
 
67
68
  patch.remove!
@@ -83,23 +84,39 @@ module AdaptiveAlias
83
84
  end
84
85
  end
85
86
 
86
- @fix_association = proc do |relation, reflection, error|
87
+ @fix_association = proc do |relation, reflection, model, error|
87
88
  next false if not patch.removable
88
89
  next false if patch.removed
89
90
 
90
91
  ambiguous = expected_ambiguous_association_err_msgs.include?(error.message)
91
92
 
92
93
  if ambiguous
93
- next false if relation and klass != relation.klass
94
- next false if reflection and klass != reflection.klass
94
+ next false if relation and klass.table_name != relation.klass.table_name
95
+ next false if reflection and klass.table_name != reflection.klass.table_name
96
+ next false if model and klass.table_name != model.class.table_name
97
+ next false if !relation and !reflection and !model
95
98
  end
96
99
 
97
100
  next false if not expected_association_err_msgs.include?(error.message) and not ambiguous
98
101
 
99
102
  patch.remove!
100
103
 
104
+ if model
105
+ attributes = model.instance_variable_get(:@attributes).instance_variable_get(:@attributes)
106
+ attributes.transform_keys! do |key|
107
+ key == current_column.to_s ? alias_column.to_s : key
108
+ end
109
+ end
110
+
101
111
  if relation
112
+ relation.reset # reset @arel
113
+
102
114
  joins = relation.arel.source.right # @ctx.source.right << create_join(relation, nil, klass)
115
+
116
+ # adjust select fields
117
+ index = relation.select_values.index(current_column)
118
+ relation.select_values[index] = alias_column if index
119
+
103
120
  fix_arel_nodes.call(joins.map{|s| s.right.expr })
104
121
  fix_arel_nodes.call(relation.where_clause.send(:predicates))
105
122
  end
@@ -119,8 +136,12 @@ module AdaptiveAlias
119
136
 
120
137
  def remove!
121
138
  @removed = true
122
- @klass.reset_column_information
123
- @klass.columns_hash
139
+
140
+ reset_caches(@klass)
141
+ ActiveRecord::Base.descendants.each do |model_klass|
142
+ reset_caches(model_klass) if model_klass.table_name == @klass.table_name
143
+ end
144
+
124
145
  @fix_association = nil
125
146
  @fix_missing_attribute = nil
126
147
  end
@@ -131,6 +152,15 @@ module AdaptiveAlias
131
152
 
132
153
  private
133
154
 
155
+ def reset_caches(klass)
156
+ # We need to call reload_schema_from_cache (which is called in reset_column_information),
157
+ # in order to reset klass.attributes_builder which are initialized with outdated defaults.
158
+ # If not, it will not raise missing attributes error when we try to access the column which has already been renamed,
159
+ # and we will have no way to know the column has been renamed since no error is raised for us to rescue.
160
+ klass.reset_column_information
161
+ klass.columns_hash
162
+ end
163
+
134
164
  def each_nodes(nodes, &block)
135
165
  nodes.each do |node|
136
166
  case node
@@ -1,3 +1,3 @@
1
1
  module AdaptiveAlias
2
- VERSION = '0.2.4'
2
+ VERSION = '1.1.0'
3
3
  end
@@ -13,6 +13,8 @@ require 'adaptive_alias/hooks/association'
13
13
  require 'adaptive_alias/hooks/association_scope'
14
14
  require 'adaptive_alias/hooks/singular_association'
15
15
  require 'adaptive_alias/hooks/relation'
16
+ require 'adaptive_alias/hooks/active_record_core'
17
+ require 'adaptive_alias/hooks/active_record_persistence'
16
18
 
17
19
  module AdaptiveAlias
18
20
  @log_interval = 10 * 60
@@ -41,12 +43,12 @@ module AdaptiveAlias
41
43
  end
42
44
  end
43
45
 
44
- def rescue_statement_invalid(relation, reflection, &block)
46
+ def rescue_statement_invalid(relation: nil, reflection: nil, model: nil, &block)
45
47
  yield
46
48
  rescue ActiveRecord::StatementInvalid => error
47
- 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) }
48
50
 
49
- result = rescue_statement_invalid(relation, reflection, &block)
51
+ result = rescue_statement_invalid(relation: relation, reflection: reflection, model: model, &block)
50
52
  AdaptiveAlias.current_patches.each_value(&:mark_removable)
51
53
  return result
52
54
  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: 0.2.4
4
+ version: 1.1.0
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-08-18 00:00:00.000000000 Z
11
+ date: 2022-09-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -128,6 +128,8 @@ files:
128
128
  - lib/adaptive_alias/active_model_patches/read_attribute.rb
129
129
  - lib/adaptive_alias/active_model_patches/remove_alias_attribute.rb
130
130
  - lib/adaptive_alias/active_model_patches/write_attribute.rb
131
+ - lib/adaptive_alias/hooks/active_record_core.rb
132
+ - lib/adaptive_alias/hooks/active_record_persistence.rb
131
133
  - lib/adaptive_alias/hooks/association.rb
132
134
  - lib/adaptive_alias/hooks/association_scope.rb
133
135
  - lib/adaptive_alias/hooks/relation.rb