adaptive_alias 0.2.3 → 1.0.0

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: b0944e33c0d1dc0b83a734d921c7dadc3f37eaccd2356deebd9775168a98470c
4
- data.tar.gz: 3132d4d8a7de8e5325a8505a79f377daf227322f045e2bdc08ec41443d182348
3
+ metadata.gz: f5938ea6d66ddde37c2f28e252297844a422d3431b37105c54b2246244880971
4
+ data.tar.gz: ad5e44e57886b67db5eaacc898a910b5b916d194e8eca3ad821851f9b9c4a35e
5
5
  SHA512:
6
- metadata.gz: 4cf0abcab7059c77833a1eb3e04672bb26fa8f5d86c5d710d2fb603a0ef12fd07cad2e8acc04eb9e8ae2b8b1b39d09dd1c50382143cd4fcb55d6d5ff648ec6be
7
- data.tar.gz: ba28c61c515149710cffa4a97b23e4870e4b01f2da7e9114034e728e44cefe02a421c839ca1e8667c054660c274f78b0839bf9fd3eee5062b3ea03ff39554b33
6
+ metadata.gz: 2ed9da0a54fea620f1d31ebb8c6c15b11591eb5b34d96e21f699dd1c3c0acb64a9415ca224388688b8f6af1871f775b197a4876bf54e4abd24a59ec6ec154922
7
+ data.tar.gz: bcc8815527922cfb226c7ea8c718f25ed9d2f8cd029b0666ec567c9a2fb2ce3414596cbfc39e146d817be7ee8b2f36dfa6a304267ad60cb903ac3dc54706321e
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,6 +1,10 @@
1
1
  ## Change Log
2
2
 
3
- ### [upcoming](https://github.com/khiav223577/adaptive_alias/compare/v0.2.2...HEAD) 2022/08/15
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)
4
8
  - [#16](https://github.com/khiav223577/adaptive_alias/pull/16) [Enhance] No need to clone node (@khiav223577)
5
9
  - [#15](https://github.com/khiav223577/adaptive_alias/pull/15) [Test] Add test cases to test calling pluck on same model (@khiav223577)
6
10
 
@@ -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(nil, nil){ super }
6
+ end
7
+
8
+ def find_by(*)
9
+ AdaptiveAlias.rescue_statement_invalid(nil, nil){ 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
@@ -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,13 +62,28 @@ 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!
68
69
  next true
69
70
  end
70
71
 
72
+ fix_arel_attributes = proc do |attr|
73
+ next if not attr.is_a?(Arel::Attributes::Attribute)
74
+ next if attr.name != current_column.to_s
75
+ next if klass.table_name != attr.relation.name
76
+
77
+ attr.name = alias_column.to_s
78
+ end
79
+
80
+ fix_arel_nodes = proc do |nodes|
81
+ each_nodes(nodes) do |node|
82
+ fix_arel_attributes.call(node.left)
83
+ fix_arel_attributes.call(node.right)
84
+ end
85
+ end
86
+
71
87
  @fix_association = proc do |relation, reflection, error|
72
88
  next false if not patch.removable
73
89
  next false if patch.removed
@@ -75,8 +91,8 @@ module AdaptiveAlias
75
91
  ambiguous = expected_ambiguous_association_err_msgs.include?(error.message)
76
92
 
77
93
  if ambiguous
78
- next false if relation and klass != relation.klass
79
- 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
80
96
  end
81
97
 
82
98
  next false if not expected_association_err_msgs.include?(error.message) and not ambiguous
@@ -84,12 +100,16 @@ module AdaptiveAlias
84
100
  patch.remove!
85
101
 
86
102
  if relation
87
- relation.where_clause.send(:predicates).each do |node|
88
- next if node.left.name != current_column.to_s
89
- next if klass.table_name != node.left.relation.name
103
+ relation.reset # reset @arel
104
+
105
+ joins = relation.arel.source.right # @ctx.source.right << create_join(relation, nil, klass)
90
106
 
91
- node.left.name = alias_column.to_s
92
- end
107
+ # adjust select fields
108
+ index = relation.select_values.index(current_column)
109
+ relation.select_values[index] = alias_column if index
110
+
111
+ fix_arel_nodes.call(joins.map{|s| s.right.expr })
112
+ fix_arel_nodes.call(relation.where_clause.send(:predicates))
93
113
  end
94
114
 
95
115
  reflection.clear_association_scope_cache if reflection
@@ -107,8 +127,12 @@ module AdaptiveAlias
107
127
 
108
128
  def remove!
109
129
  @removed = true
110
- @klass.reset_column_information
111
- @klass.columns_hash
130
+
131
+ reset_caches(@klass)
132
+ ActiveRecord::Base.descendants.each do |model_klass|
133
+ reset_caches(model_klass) if model_klass.table_name == @klass.table_name
134
+ end
135
+
112
136
  @fix_association = nil
113
137
  @fix_missing_attribute = nil
114
138
  end
@@ -116,6 +140,28 @@ module AdaptiveAlias
116
140
  def mark_removable
117
141
  @removable = true
118
142
  end
143
+
144
+ private
145
+
146
+ def reset_caches(klass)
147
+ # We need to call reload_schema_from_cache (which is called in reset_column_information),
148
+ # in order to reset klass.attributes_builder which are initialized with outdated defaults.
149
+ # If not, it will not raise missing attributes error when we try to access the column which has already been renamed,
150
+ # and we will have no way to know the column has been renamed since no error is raised for us to rescue.
151
+ klass.reset_column_information
152
+ klass.columns_hash
153
+ end
154
+
155
+ def each_nodes(nodes, &block)
156
+ nodes.each do |node|
157
+ case node
158
+ when Arel::Nodes::Equality
159
+ yield(node)
160
+ when Arel::Nodes::And
161
+ each_nodes(node.children, &block)
162
+ end
163
+ end
164
+ end
119
165
  end
120
166
  end
121
167
  end
@@ -1,3 +1,3 @@
1
1
  module AdaptiveAlias
2
- VERSION = '0.2.3'
2
+ VERSION = '1.0.0'
3
3
  end
@@ -13,6 +13,7 @@ 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'
16
17
 
17
18
  module AdaptiveAlias
18
19
  @log_interval = 10 * 60
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.3
4
+ version: 1.0.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-17 00:00:00.000000000 Z
11
+ date: 2022-09-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -128,6 +128,7 @@ 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
131
132
  - lib/adaptive_alias/hooks/association.rb
132
133
  - lib/adaptive_alias/hooks/association_scope.rb
133
134
  - lib/adaptive_alias/hooks/relation.rb