adaptive_alias 0.1.0 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +5 -0
- data/lib/adaptive_alias/active_model_patches/apply_scope.rb +17 -0
- data/lib/adaptive_alias/active_model_patches/read_attribute.rb +23 -15
- data/lib/adaptive_alias/active_model_patches/remove_alias_attribute.rb +16 -10
- data/lib/adaptive_alias/active_model_patches/write_attribute.rb +19 -16
- data/lib/adaptive_alias/hooks/association.rb +2 -2
- data/lib/adaptive_alias/hooks/relation.rb +3 -3
- data/lib/adaptive_alias/patches/backward_patch.rb +3 -1
- data/lib/adaptive_alias/patches/base.rb +11 -7
- data/lib/adaptive_alias/patches/forward_patch.rb +3 -1
- data/lib/adaptive_alias/version.rb +1 -1
- data/lib/adaptive_alias.rb +4 -3
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 26cc1e7589ccce6ae23b91968e01b0c419efbd482ce217c5c2efceaef33cb35d
|
4
|
+
data.tar.gz: ef91b943cb0aeb07a1f9ac8370195d5f75fe48ce3c3908cc7f546ead6719fe7a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 9969274e1e866cc6ff99349a0da832044c3171fc654e9603afd4151846f9ac82cd31c76513ff865e4669d0dc96eb7abedd62225331eb46a769bbcb4bed681fec
|
7
|
+
data.tar.gz: 9fad3f81d3bd1e4d768d43c94e476719a0e0a6b0af0fd7bf82fe89fae0195dcb877f8b73fb91c9d09fd0650ebfd82c12277a61616799fb710d94f9b80b008b97
|
data/CHANGELOG.md
CHANGED
@@ -1,5 +1,10 @@
|
|
1
1
|
## Change Log
|
2
2
|
|
3
|
+
### [v0.1.0](https://github.com/khiav223577/adaptive_alias/compare/v0.0.3...v0.1.0) 2022/08/01
|
4
|
+
- [#6](https://github.com/khiav223577/adaptive_alias/pull/6) Deal with creating records (@khiav223577)
|
5
|
+
- [#5](https://github.com/khiav223577/adaptive_alias/pull/5) Prevent infinite loop if something went wrong (@khiav223577)
|
6
|
+
- [#4](https://github.com/khiav223577/adaptive_alias/pull/4) Fix: attributes writer method should be defined after schema changes (@khiav223577)
|
7
|
+
|
3
8
|
### [v0.0.3](https://github.com/khiav223577/adaptive_alias/compare/v0.0.2...v0.0.3) 2022/07/27
|
4
9
|
- [#3](https://github.com/khiav223577/adaptive_alias/pull/3) Fix: prevent calling custom column names from raising missing attributes (@khiav223577)
|
5
10
|
|
@@ -0,0 +1,17 @@
|
|
1
|
+
require 'active_record'
|
2
|
+
|
3
|
+
module AdaptiveAlias
|
4
|
+
module ActiveModelPatches
|
5
|
+
module ApplyScope
|
6
|
+
def apply_scope(scope, table, key, value)
|
7
|
+
klass = table.instance_variable_get(:@klass) || table.send(:type_caster).send(:types)
|
8
|
+
key = klass.attribute_aliases[key] || key
|
9
|
+
super(scope, table, key, value)
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
class ActiveRecord::Associations::AssociationScope
|
16
|
+
prepend AdaptiveAlias::ActiveModelPatches::ApplyScope
|
17
|
+
end
|
@@ -1,22 +1,30 @@
|
|
1
1
|
require 'active_record'
|
2
2
|
|
3
|
-
module
|
4
|
-
|
5
|
-
|
6
|
-
|
3
|
+
module AdaptiveAlias
|
4
|
+
module ActiveModelPatches
|
5
|
+
module ReadAttribute
|
6
|
+
def read_attribute(attr_name, &block) # :nodoc:
|
7
|
+
name = attr_name.to_s
|
8
|
+
name = self.class.attribute_aliases[name] || name
|
7
9
|
|
8
|
-
|
9
|
-
|
10
|
-
|
10
|
+
name = @primary_key if name == 'id' && @primary_key
|
11
|
+
_read_attribute(name, &block)
|
12
|
+
end
|
11
13
|
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
14
|
+
# This method exists to avoid the expensive primary_key check internally, without
|
15
|
+
# breaking compatibility with the write_attribute API
|
16
|
+
def _read_attribute(attr_name, &block) # :nodoc:
|
17
|
+
name = attr_name.to_s
|
18
|
+
name = self.class.attribute_aliases[name] || name
|
17
19
|
|
18
|
-
|
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
|
+
return @attributes.fetch_value(name, &block)
|
23
|
+
end
|
24
|
+
end
|
21
25
|
end
|
22
26
|
end
|
27
|
+
|
28
|
+
module ActiveRecord::AttributeMethods::Read
|
29
|
+
prepend AdaptiveAlias::ActiveModelPatches::ReadAttribute
|
30
|
+
end
|
@@ -1,18 +1,24 @@
|
|
1
1
|
require 'active_model'
|
2
2
|
|
3
|
-
module
|
4
|
-
module
|
5
|
-
|
6
|
-
mod
|
7
|
-
|
3
|
+
module AdaptiveAlias
|
4
|
+
module ActiveModelPatches
|
5
|
+
module RemoveAliasAttribute
|
6
|
+
def remove_proxy_call(mod, name)
|
7
|
+
mod.module_eval "remove_method(:'#{name}')", __FILE__, __LINE__ + 1
|
8
|
+
end
|
8
9
|
|
9
|
-
|
10
|
-
|
10
|
+
def remove_alias_attribute(new_name)
|
11
|
+
self.attribute_aliases = attribute_aliases.except(new_name.to_s)
|
11
12
|
|
12
|
-
|
13
|
-
|
14
|
-
|
13
|
+
attribute_method_matchers.each do |matcher|
|
14
|
+
matcher_new = matcher.method_name(new_name).to_s
|
15
|
+
remove_proxy_call self, matcher_new
|
16
|
+
end
|
15
17
|
end
|
16
18
|
end
|
17
19
|
end
|
18
20
|
end
|
21
|
+
|
22
|
+
module ActiveModel::AttributeMethods::ClassMethods
|
23
|
+
include AdaptiveAlias::ActiveModelPatches::RemoveAliasAttribute
|
24
|
+
end
|
@@ -1,22 +1,25 @@
|
|
1
1
|
require 'active_record'
|
2
2
|
|
3
|
-
module
|
4
|
-
|
5
|
-
|
6
|
-
|
3
|
+
module AdaptiveAlias
|
4
|
+
module ActiveModelPatches
|
5
|
+
module WriteAttribute
|
6
|
+
def write_attribute(attr_name, value)
|
7
|
+
name = attr_name.to_s
|
8
|
+
name = self.class.attribute_aliases[name] || name
|
9
|
+
super(name, value)
|
10
|
+
end
|
7
11
|
|
8
|
-
|
9
|
-
|
12
|
+
# This method exists to avoid the expensive primary_key check internally, without
|
13
|
+
# breaking compatibility with the write_attribute API
|
14
|
+
def _write_attribute(attr_name, value) # :nodoc:
|
15
|
+
name = attr_name.to_s
|
16
|
+
name = self.class.attribute_aliases[name] || name
|
17
|
+
super(name, value)
|
18
|
+
end
|
19
|
+
end
|
10
20
|
end
|
21
|
+
end
|
11
22
|
|
12
|
-
|
13
|
-
|
14
|
-
def _write_attribute(attr_name, value) # :nodoc:
|
15
|
-
name = attr_name.to_s
|
16
|
-
name = self.class.attribute_aliases[name] || name
|
17
|
-
|
18
|
-
sync_with_transaction_state if @transaction_state&.finalized?
|
19
|
-
@attributes.write_from_user(name, value)
|
20
|
-
value
|
21
|
-
end
|
23
|
+
module ActiveRecord::AttributeMethods::Write
|
24
|
+
prepend AdaptiveAlias::ActiveModelPatches::WriteAttribute
|
22
25
|
end
|
@@ -2,11 +2,11 @@ module AdaptiveAlias
|
|
2
2
|
module Hooks
|
3
3
|
module Association
|
4
4
|
def find_target(*)
|
5
|
-
AdaptiveAlias.rescue_statement_invalid(nil){ super }
|
5
|
+
AdaptiveAlias.rescue_statement_invalid(nil, reflection){ super }
|
6
6
|
end
|
7
7
|
|
8
8
|
def create!(attributes = {}, &block)
|
9
|
-
AdaptiveAlias.rescue_statement_invalid(association_scope){ super }
|
9
|
+
AdaptiveAlias.rescue_statement_invalid(association_scope, reflection){ super }
|
10
10
|
end
|
11
11
|
end
|
12
12
|
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){ super }
|
5
|
+
AdaptiveAlias.rescue_statement_invalid(self, nil){ super }
|
6
6
|
end
|
7
7
|
|
8
8
|
def select_all(*)
|
9
|
-
AdaptiveAlias.rescue_statement_invalid(self){ super }
|
9
|
+
AdaptiveAlias.rescue_statement_invalid(self, nil){ super }
|
10
10
|
end
|
11
11
|
|
12
12
|
def exec_queries(*)
|
13
|
-
AdaptiveAlias.rescue_statement_invalid(self){ super }
|
13
|
+
AdaptiveAlias.rescue_statement_invalid(self, nil){ super }
|
14
14
|
end
|
15
15
|
end
|
16
16
|
end
|
@@ -15,7 +15,9 @@ module AdaptiveAlias
|
|
15
15
|
super
|
16
16
|
@klass.remove_alias_attribute(@old_column)
|
17
17
|
@klass.define_attribute_method(@old_column)
|
18
|
-
ForwardPatch.new(@klass, @old_column, @new_column)
|
18
|
+
new_patch = ForwardPatch.new(@klass, @old_column, @new_column)
|
19
|
+
new_patch.apply!
|
20
|
+
return new_patch
|
19
21
|
end
|
20
22
|
end
|
21
23
|
end
|
@@ -64,21 +64,25 @@ module AdaptiveAlias
|
|
64
64
|
next true
|
65
65
|
end
|
66
66
|
|
67
|
-
@fix_association = proc do |
|
67
|
+
@fix_association = proc do |relation, reflection, error|
|
68
68
|
next false if not patch.removable
|
69
69
|
next false if patch.removed
|
70
70
|
next false if not expected_association_err_msgs.include?(error.message)
|
71
71
|
|
72
72
|
patch.remove!
|
73
73
|
|
74
|
-
if
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
74
|
+
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
|
80
82
|
end
|
81
83
|
|
84
|
+
reflection.clear_association_scope_cache if reflection
|
85
|
+
|
82
86
|
next true
|
83
87
|
end
|
84
88
|
end
|
@@ -15,7 +15,9 @@ module AdaptiveAlias
|
|
15
15
|
super
|
16
16
|
@klass.remove_alias_attribute(@new_column)
|
17
17
|
@klass.define_attribute_method(@new_column)
|
18
|
-
BackwardPatch.new(@klass, @old_column, @new_column)
|
18
|
+
new_patch = BackwardPatch.new(@klass, @old_column, @new_column)
|
19
|
+
new_patch.apply!
|
20
|
+
return new_patch
|
19
21
|
end
|
20
22
|
end
|
21
23
|
end
|
data/lib/adaptive_alias.rb
CHANGED
@@ -4,6 +4,7 @@ require 'adaptive_alias/version'
|
|
4
4
|
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
|
+
require 'adaptive_alias/active_model_patches/apply_scope'
|
7
8
|
require 'adaptive_alias/patches/backward_patch'
|
8
9
|
require 'adaptive_alias/patches/forward_patch'
|
9
10
|
|
@@ -39,12 +40,12 @@ module AdaptiveAlias
|
|
39
40
|
end
|
40
41
|
end
|
41
42
|
|
42
|
-
def rescue_statement_invalid(relation, &block)
|
43
|
+
def rescue_statement_invalid(relation, reflection, &block)
|
43
44
|
yield
|
44
45
|
rescue ActiveRecord::StatementInvalid => error
|
45
|
-
raise error if AdaptiveAlias.current_patches.all?{|_key, patch| !patch.fix_association.call(relation, error) }
|
46
|
+
raise error if AdaptiveAlias.current_patches.all?{|_key, patch| !patch.fix_association.call(relation, reflection, error) }
|
46
47
|
|
47
|
-
result = rescue_statement_invalid(relation, &block)
|
48
|
+
result = rescue_statement_invalid(relation, reflection, &block)
|
48
49
|
AdaptiveAlias.current_patches.each_value(&:mark_removable)
|
49
50
|
return result
|
50
51
|
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.
|
4
|
+
version: 0.2.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-
|
11
|
+
date: 2022-08-05 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -123,6 +123,7 @@ files:
|
|
123
123
|
- gemfiles/6.1.gemfile
|
124
124
|
- gemfiles/7.0.gemfile
|
125
125
|
- lib/adaptive_alias.rb
|
126
|
+
- lib/adaptive_alias/active_model_patches/apply_scope.rb
|
126
127
|
- lib/adaptive_alias/active_model_patches/read_attribute.rb
|
127
128
|
- lib/adaptive_alias/active_model_patches/remove_alias_attribute.rb
|
128
129
|
- lib/adaptive_alias/active_model_patches/write_attribute.rb
|