adaptive_alias 0.2.1 → 0.2.4

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: 0375ca9867e41c41fe18b38cb1f37ff2a0067367304f6481250cd573c5dc5be4
4
- data.tar.gz: bc0ebb4c4d63ea36e203b94f5c3ea0411c4fab408e93ea6c9dc0de5354c2db8c
3
+ metadata.gz: f2c42a4fb70c22a74c09b64fba9c94dc0b1189a94c3e1ca954161ef20e074460
4
+ data.tar.gz: 51ae34971dff012f5e691bd8fd79252db0706277e38dcdf43766b7eca1b163aa
5
5
  SHA512:
6
- metadata.gz: f0d5f519ab910bcf93c982f2ea6ca9c3e944ea0e90a07d6e938668b4575d450d6d019a60b62dd2020c373434e8ece4f7c1d9ea148ca269a4823718b80f8a8c07
7
- data.tar.gz: 9a699915af111f6e01aa6fac45a888583a629b1f6eb0d75c84a15188f329e3f34401a330a6ee33040453b4e9f2d6a018bca61626e0784944004832c3db6d6bc3
6
+ metadata.gz: 1f30e10e5a9b035b255eeb129673563b56b10b5ee2fbdac22717fc31dd9a815e77bc1f5a5238c04dfe9cff27414e6db8e72f8bf5c1e360f3905dff29e1cd17d7
7
+ data.tar.gz: 3b1048bc0d4a42ea9f06a4ecfd215a15ae4687f67d1d52b659a64a096151eb40efe987f7ce3b46273ab89013d0739e7ca925db4ced2f3ca4fe4fcc6a2fb7bb54
data/CHANGELOG.md CHANGED
@@ -1,5 +1,17 @@
1
1
  ## Change Log
2
2
 
3
+ ### [v0.2.3](https://github.com/khiav223577/adaptive_alias/compare/v0.2.2...v0.2.3) 2022/08/17
4
+ - [#17](https://github.com/khiav223577/adaptive_alias/pull/17) [Feature] Support joins (@khiav223577)
5
+ - [#16](https://github.com/khiav223577/adaptive_alias/pull/16) [Enhance] No need to clone node (@khiav223577)
6
+ - [#15](https://github.com/khiav223577/adaptive_alias/pull/15) [Test] Add test cases to test calling pluck on same model (@khiav223577)
7
+
8
+ ### [v0.2.2](https://github.com/khiav223577/adaptive_alias/compare/v0.2.1...v0.2.2) 2022/08/11
9
+ - [#14](https://github.com/khiav223577/adaptive_alias/pull/14) [Fix] Fail to use backward-patch when target column has already been migrated (@khiav223577)
10
+
11
+ ### [v0.2.1](https://github.com/khiav223577/adaptive_alias/compare/v0.2.0...v0.2.1) 2022/08/11
12
+ - [#13](https://github.com/khiav223577/adaptive_alias/pull/13) [Fix] Nested module include is not supported until ruby 3.0 (@khiav223577)
13
+ - [#12](https://github.com/khiav223577/adaptive_alias/pull/12) [Test] Add test cases to test column_names (@khiav223577)
14
+
3
15
  ### [v0.2.0](https://github.com/khiav223577/adaptive_alias/compare/v0.1.0...v0.2.0) 2022/08/05
4
16
  - [#11](https://github.com/khiav223577/adaptive_alias/pull/11) [Feature] Support polymorphic (@khiav223577)
5
17
  - [#10](https://github.com/khiav223577/adaptive_alias/pull/10) [Enhance] Prevent adding methods directly in class / module (@khiav223577)
@@ -4,7 +4,7 @@ module AdaptiveAlias
4
4
  module ActiveModelPatches
5
5
  module ApplyScope
6
6
  def apply_scope(scope, table, key, value)
7
- klass = table.instance_variable_get(:@klass) || table.send(:type_caster).send(:types)
7
+ klass = table.instance_variable_get(:@klass) || table.klass
8
8
  key = klass.attribute_aliases[key] || key
9
9
  super(scope, table, key, value)
10
10
  end
@@ -0,0 +1,26 @@
1
+ require 'active_record'
2
+
3
+ module AdaptiveAlias
4
+ module ActiveModelPatches
5
+ module ArelTable
6
+ def [](name)
7
+ name = name.to_s if name.is_a?(Symbol)
8
+ klass = self.klass
9
+ name = klass.attribute_aliases[name] || name if klass
10
+ super
11
+ end
12
+
13
+ def klass
14
+ return @type_caster.instance_variable_get(:@klass) if @type_caster.is_a?(ActiveRecord::TypeCaster::Connection)
15
+ return @type_caster.send(:types) if @type_caster.is_a?(ActiveRecord::TypeCaster::Map)
16
+ end
17
+ end
18
+ end
19
+ end
20
+
21
+ # https://github.com/rails/rails/commit/1ac40f16c5bc5246a4aaeab0558eb1c3078b3c6e
22
+ if Gem::Version.new(ActiveRecord::VERSION::STRING) < Gem::Version.new('6.1')
23
+ class Arel::Table
24
+ prepend AdaptiveAlias::ActiveModelPatches::ArelTable
25
+ end
26
+ end
@@ -46,6 +46,10 @@ module AdaptiveAlias
46
46
 
47
47
  expected_association_err_msgs = [
48
48
  "Mysql2::Error: Unknown column '#{klass.table_name}.#{current_column}' in 'where clause'".freeze,
49
+ "Mysql2::Error: Unknown column '#{klass.table_name}.#{current_column}' in 'on clause'".freeze,
50
+ ].freeze
51
+
52
+ expected_ambiguous_association_err_msgs = [
49
53
  "Mysql2::Error: Unknown column '#{current_column}' in 'field list'".freeze,
50
54
  ].freeze
51
55
 
@@ -64,21 +68,40 @@ module AdaptiveAlias
64
68
  next true
65
69
  end
66
70
 
71
+ fix_arel_attributes = proc do |attr|
72
+ next if not attr.is_a?(Arel::Attributes::Attribute)
73
+ next if attr.name != current_column.to_s
74
+ next if klass.table_name != attr.relation.name
75
+
76
+ attr.name = alias_column.to_s
77
+ end
78
+
79
+ fix_arel_nodes = proc do |nodes|
80
+ each_nodes(nodes) do |node|
81
+ fix_arel_attributes.call(node.left)
82
+ fix_arel_attributes.call(node.right)
83
+ end
84
+ end
85
+
67
86
  @fix_association = proc do |relation, reflection, error|
68
87
  next false if not patch.removable
69
88
  next false if patch.removed
70
- next false if not expected_association_err_msgs.include?(error.message)
89
+
90
+ ambiguous = expected_ambiguous_association_err_msgs.include?(error.message)
91
+
92
+ if ambiguous
93
+ next false if relation and klass != relation.klass
94
+ next false if reflection and klass != reflection.klass
95
+ end
96
+
97
+ next false if not expected_association_err_msgs.include?(error.message) and not ambiguous
71
98
 
72
99
  patch.remove!
73
100
 
74
101
  if relation
75
- relation.where_clause.send(:predicates).each do |node|
76
- next if node.left.name != current_column.to_s
77
- next if klass.table_name != node.left.relation.name
78
-
79
- node.left = node.left.clone
80
- node.left.name = alias_column.to_s
81
- end
102
+ joins = relation.arel.source.right # @ctx.source.right << create_join(relation, nil, klass)
103
+ fix_arel_nodes.call(joins.map{|s| s.right.expr })
104
+ fix_arel_nodes.call(relation.where_clause.send(:predicates))
82
105
  end
83
106
 
84
107
  reflection.clear_association_scope_cache if reflection
@@ -105,6 +128,19 @@ module AdaptiveAlias
105
128
  def mark_removable
106
129
  @removable = true
107
130
  end
131
+
132
+ private
133
+
134
+ def each_nodes(nodes, &block)
135
+ nodes.each do |node|
136
+ case node
137
+ when Arel::Nodes::Equality
138
+ yield(node)
139
+ when Arel::Nodes::And
140
+ each_nodes(node.children, &block)
141
+ end
142
+ end
143
+ end
108
144
  end
109
145
  end
110
146
  end
@@ -1,3 +1,3 @@
1
1
  module AdaptiveAlias
2
- VERSION = '0.2.1'
2
+ VERSION = '0.2.4'
3
3
  end
@@ -5,6 +5,7 @@ require 'adaptive_alias/active_model_patches/read_attribute'
5
5
  require 'adaptive_alias/active_model_patches/write_attribute'
6
6
  require 'adaptive_alias/active_model_patches/remove_alias_attribute'
7
7
  require 'adaptive_alias/active_model_patches/apply_scope'
8
+ require 'adaptive_alias/active_model_patches/arel_table'
8
9
  require 'adaptive_alias/patches/backward_patch'
9
10
  require 'adaptive_alias/patches/forward_patch'
10
11
 
@@ -33,7 +34,7 @@ module AdaptiveAlias
33
34
  extend ActiveSupport::Concern
34
35
 
35
36
  included do
36
- patch = (column_names.include?(new_column) ? Patches::BackwardPatch : Patches::ForwardPatch).new(self, old_column, new_column)
37
+ patch = (column_names.include?(new_column.to_s) ? Patches::BackwardPatch : Patches::ForwardPatch).new(self, old_column, new_column)
37
38
  patch.apply!
38
39
  patch.mark_removable
39
40
  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.1
4
+ version: 0.2.4
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-11 00:00:00.000000000 Z
11
+ date: 2022-08-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -124,6 +124,7 @@ files:
124
124
  - gemfiles/7.0.gemfile
125
125
  - lib/adaptive_alias.rb
126
126
  - lib/adaptive_alias/active_model_patches/apply_scope.rb
127
+ - lib/adaptive_alias/active_model_patches/arel_table.rb
127
128
  - lib/adaptive_alias/active_model_patches/read_attribute.rb
128
129
  - lib/adaptive_alias/active_model_patches/remove_alias_attribute.rb
129
130
  - lib/adaptive_alias/active_model_patches/write_attribute.rb