Class: AdaptiveAlias::Patches::Base
- Inherits:
-
Object
- Object
- AdaptiveAlias::Patches::Base
- Defined in:
- lib/adaptive_alias/patches/base.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#check_matched ⇒ Object
readonly
Returns the value of attribute check_matched.
-
#removable ⇒ Object
readonly
Returns the value of attribute removable.
-
#remove_and_fix_association ⇒ Object
readonly
Returns the value of attribute remove_and_fix_association.
-
#removed ⇒ Object
readonly
Returns the value of attribute removed.
Instance Method Summary collapse
- #add_hooks!(current_column:, alias_column:, log_warning: false) ⇒ Object
- #do_remove! ⇒ Object
-
#initialize(klass, old_column, new_column) ⇒ Base
constructor
A new instance of Base.
- #log_warning ⇒ Object
- #mark_removable ⇒ Object
- #remove! ⇒ Object
Constructor Details
#initialize(klass, old_column, new_column) ⇒ Base
Returns a new instance of Base.
11 12 13 14 15 |
# File 'lib/adaptive_alias/patches/base.rb', line 11 def initialize(klass, old_column, new_column) @klass = klass @old_column = old_column @new_column = new_column end |
Instance Attribute Details
#check_matched ⇒ Object (readonly)
Returns the value of attribute check_matched.
6 7 8 |
# File 'lib/adaptive_alias/patches/base.rb', line 6 def check_matched @check_matched end |
#removable ⇒ Object (readonly)
Returns the value of attribute removable.
9 10 11 |
# File 'lib/adaptive_alias/patches/base.rb', line 9 def removable @removable end |
#remove_and_fix_association ⇒ Object (readonly)
Returns the value of attribute remove_and_fix_association.
7 8 9 |
# File 'lib/adaptive_alias/patches/base.rb', line 7 def remove_and_fix_association @remove_and_fix_association end |
#removed ⇒ Object (readonly)
Returns the value of attribute removed.
8 9 10 |
# File 'lib/adaptive_alias/patches/base.rb', line 8 def removed @removed end |
Instance Method Details
#add_hooks!(current_column:, alias_column:, log_warning: false) ⇒ Object
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/adaptive_alias/patches/base.rb', line 17 def add_hooks!(current_column:, alias_column:, log_warning: false) patch = self klass = @klass old_column = @old_column new_column = @new_column AdaptiveAlias.get_or_create_model_module(klass).instance_exec do remove_method(new_column) if method_defined?(new_column) define_method(new_column) do self[new_column] end remove_method("#{new_column}=") if method_defined?("#{new_column}=") define_method("#{new_column}=") do |*args| super(*args) end remove_method(old_column) if method_defined?(old_column) define_method(old_column) do patch.log_warning if log_warning self[old_column] end remove_method("#{old_column}=") if method_defined?("#{old_column}=") define_method("#{old_column}=") do |*args| patch.log_warning if log_warning super(*args) end end expected_association_err_msgs = [ "Mysql2::Error: Unknown column '#{klass.table_name}.#{current_column}' in 'where clause'".freeze, "Mysql2::Error: Unknown column '#{klass.table_name}.#{current_column}' in 'on clause'".freeze, "Mysql2::Error: Unknown column '#{klass.table_name}.#{current_column}' in 'field list'".freeze, ].freeze expected_ambiguous_association_err_msgs = [ "Mysql2::Error: Unknown column '#{current_column}' in 'field list'".freeze, ].freeze fix_arel_attributes = proc do |attr| next if not attr.is_a?(Arel::Attributes::Attribute) next if attr.name != current_column.to_s next if klass.table_name != attr.relation.name attr.name = alias_column.to_s end fix_arel_nodes = proc do |nodes| each_nodes(nodes) do |node| fix_arel_attributes.call(node.left) fix_arel_attributes.call(node.right) end end @check_matched = proc do |relation, reflection, model_klass, error| next false if not patch.removable # Error highlight behavior in Ruby 3.1 pollutes the error message error_msg = error.respond_to?(:original_message) ? error. : error. ambiguous = expected_ambiguous_association_err_msgs.include?(error_msg) if ambiguous next false if relation and klass.table_name != relation.klass.table_name next false if reflection and klass.table_name != reflection.klass.table_name next false if model_klass and klass.table_name != model_klass.table_name next false if !relation and !reflection and !model_klass end next false if not expected_association_err_msgs.include?(error_msg) and not ambiguous next true end @remove_and_fix_association = proc do |relation, reflection, &block| patch.remove! do if relation relation.reset # reset @arel joins = relation.arel.source.right # @ctx.source.right << create_join(relation, nil, klass) # adjust select fields index = relation.select_values.index(current_column) relation.select_values[index] = alias_column if index fix_arel_nodes.call(joins.map{|s| s.right.expr }) fix_arel_nodes.call(relation.where_clause.send(:predicates)) end reflection.clear_association_scope_cache if reflection block.call end end end |
#do_remove! ⇒ Object
130 131 132 133 134 135 136 137 138 |
# File 'lib/adaptive_alias/patches/base.rb', line 130 def do_remove! reset_caches(@klass) ActiveRecord::Base.descendants.each do |model_klass| reset_caches(model_klass) if model_klass.table_name == @klass.table_name end @check_matched = nil @remove_and_fix_association = nil end |
#log_warning ⇒ Object
112 113 114 115 116 117 |
# File 'lib/adaptive_alias/patches/base.rb', line 112 def log_warning if @prev_warning_time == nil || @prev_warning_time < Time.now - AdaptiveAlias.log_interval @prev_warning_time = Time.now AdaptiveAlias.unexpected_old_column_proc&.call end end |
#mark_removable ⇒ Object
140 141 142 |
# File 'lib/adaptive_alias/patches/base.rb', line 140 def mark_removable @removable = true end |
#remove! ⇒ Object
119 120 121 122 123 124 125 126 127 128 |
# File 'lib/adaptive_alias/patches/base.rb', line 119 def remove! if not @removed @removed = true new_patch = do_remove! end yield if block_given? ensure new_patch.mark_removable if new_patch end |