adaptive_alias 0.2.4 → 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: f2c42a4fb70c22a74c09b64fba9c94dc0b1189a94c3e1ca954161ef20e074460
4
- data.tar.gz: 51ae34971dff012f5e691bd8fd79252db0706277e38dcdf43766b7eca1b163aa
3
+ metadata.gz: f5938ea6d66ddde37c2f28e252297844a422d3431b37105c54b2246244880971
4
+ data.tar.gz: ad5e44e57886b67db5eaacc898a910b5b916d194e8eca3ad821851f9b9c4a35e
5
5
  SHA512:
6
- metadata.gz: 1f30e10e5a9b035b255eeb129673563b56b10b5ee2fbdac22717fc31dd9a815e77bc1f5a5238c04dfe9cff27414e6db8e72f8bf5c1e360f3905dff29e1cd17d7
7
- data.tar.gz: 3b1048bc0d4a42ea9f06a4ecfd215a15ae4687f67d1d52b659a64a096151eb40efe987f7ce3b46273ab89013d0739e7ca925db4ced2f3ca4fe4fcc6a2fb7bb54
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,5 +1,8 @@
1
1
  ## Change Log
2
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
+
3
6
  ### [v0.2.3](https://github.com/khiav223577/adaptive_alias/compare/v0.2.2...v0.2.3) 2022/08/17
4
7
  - [#17](https://github.com/khiav223577/adaptive_alias/pull/17) [Feature] Support joins (@khiav223577)
5
8
  - [#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(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,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!
@@ -90,8 +91,8 @@ module AdaptiveAlias
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
95
96
  end
96
97
 
97
98
  next false if not expected_association_err_msgs.include?(error.message) and not ambiguous
@@ -99,7 +100,14 @@ module AdaptiveAlias
99
100
  patch.remove!
100
101
 
101
102
  if relation
103
+ relation.reset # reset @arel
104
+
102
105
  joins = relation.arel.source.right # @ctx.source.right << create_join(relation, nil, klass)
106
+
107
+ # adjust select fields
108
+ index = relation.select_values.index(current_column)
109
+ relation.select_values[index] = alias_column if index
110
+
103
111
  fix_arel_nodes.call(joins.map{|s| s.right.expr })
104
112
  fix_arel_nodes.call(relation.where_clause.send(:predicates))
105
113
  end
@@ -119,8 +127,12 @@ module AdaptiveAlias
119
127
 
120
128
  def remove!
121
129
  @removed = true
122
- @klass.reset_column_information
123
- @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
+
124
136
  @fix_association = nil
125
137
  @fix_missing_attribute = nil
126
138
  end
@@ -131,6 +143,15 @@ module AdaptiveAlias
131
143
 
132
144
  private
133
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
+
134
155
  def each_nodes(nodes, &block)
135
156
  nodes.each do |node|
136
157
  case node
@@ -1,3 +1,3 @@
1
1
  module AdaptiveAlias
2
- VERSION = '0.2.4'
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.4
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-18 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