class Redundancy::UpdateBase
Attributes
change_if[R]
context[R]
dest[R]
klass[R]
options[R]
source[R]
Public Class Methods
new(options)
click to toggle source
# File lib/redundancy/update_base.rb, line 8 def initialize options @options = options @klass = options[:klass] @source, @dest = options[:source], options[:dest] @change_if = options[:change_if] @update = options[:update] || false cleanup_context end
Public Instance Methods
after_save(record)
click to toggle source
# File lib/redundancy/update_base.rb, line 26 def after_save record end
before_save(record)
click to toggle source
ActiveRecord Hooks
# File lib/redundancy/update_base.rb, line 23 def before_save record end
force_update!(record)
click to toggle source
# File lib/redundancy/update_base.rb, line 18 def force_update! record set_context :force, true end
Protected Instance Methods
cleanup_context()
click to toggle source
# File lib/redundancy/update_base.rb, line 36 def cleanup_context @context = {} end
force()
click to toggle source
# File lib/redundancy/update_base.rb, line 44 def force @context[:force] end
foreign_key_changed?(record)
click to toggle source
# File lib/redundancy/update_base.rb, line 114 def foreign_key_changed? record record.send(:attribute_changed?, dest[:foreign_key]) end
get_target_from_association(record, key = :default)
click to toggle source
# File lib/redundancy/update_base.rb, line 56 def get_target_from_association record, key = :default set_context :"target_#{key}", dest[:association] ? record.send(dest[:association]) : record end
get_target_from_foreign_key(record, key = :default)
click to toggle source
# File lib/redundancy/update_base.rb, line 64 def get_target_from_foreign_key record, key = :default id = record.send(:attribute_was, dest[:foreign_key]) return unless id set_context :"target_#{key}", dest[:klass].where(id: id) end
get_target_from_prev_association(record, key = :default)
click to toggle source
# File lib/redundancy/update_base.rb, line 60 def get_target_from_prev_association record, key = :default set_context :"target_#{key}", record.send(:attribute_was, dest[:association]) end
get_target_from_relation_first_record(key = :default)
click to toggle source
# File lib/redundancy/update_base.rb, line 70 def get_target_from_relation_first_record key = :default target = context(:"target_#{key}") set_context :"target_#{key}", target.first if target.kind_of? ActiveRecord::Relation end
get_value_from_association(record, key = :default)
click to toggle source
# File lib/redundancy/update_base.rb, line 75 def get_value_from_association record, key = :default value = source[:association] ? record.send(source[:association]) : record value = value && source[:attribute] && value.send(source[:attribute]) value = nil if source[:nil_unless] && !record.send(source[:nil_unless]) set_context :"value_#{key}", value end
get_value_from_default(record, key = :default)
click to toggle source
# File lib/redundancy/update_base.rb, line 82 def get_value_from_default record, key = :default set_context :"value_#{key}", source[:default] end
get_value_from_target(record, key = :default)
click to toggle source
# File lib/redundancy/update_base.rb, line 86 def get_value_from_target record, key = :default target = context(:"target_#{key}") set_context :"value_#{key}", target && source[:attribute] && target.send(source[:attribute]) end
log(message)
click to toggle source
require ‘colorize’
# File lib/redundancy/update_base.rb, line 119 def log message # puts " Redundancy ".colorize(:green) + message end
need_update?(record)
click to toggle source
# File lib/redundancy/update_base.rb, line 110 def need_update? record force || !change_if || record.send(:attribute_changed?, change_if) end
raise_if_class_mismatch(record)
click to toggle source
# File lib/redundancy/update_base.rb, line 91 def raise_if_class_mismatch record raise ArgumentError, "record class mismatch, expected #{klass}, got #{record.class}" unless record.kind_of? klass end
set_context(key, value)
click to toggle source
Context
# File lib/redundancy/update_base.rb, line 32 def set_context key, value @context[key] = value end
update()
click to toggle source
# File lib/redundancy/update_base.rb, line 40 def update @context[:update] || @update end
update_method()
click to toggle source
# File lib/redundancy/update_base.rb, line 48 def update_method @context[:update_method] || (update ? :update_attribute : :write_attribute) end
update_target(record, key = :default)
click to toggle source
# File lib/redundancy/update_base.rb, line 95 def update_target record, key = :default target = context(:"target_#{key}") value = context(:"value_#{key}") case target when ActiveRecord::Base return if target.send(:read_attribute, dest[:attribute]) == value log "#{update_method} #{target.class}(#{target.id})##{dest[:attribute]} with #{value.inspect}" target.send(update_method, dest[:attribute], value) when ActiveRecord::Relation log "update_all #{target.class}##{dest[:attribute]} with #{value.inspect}" target.send(:update_all, dest[:attribute] => value) end end