Class: ActiveTools::ActiveRecord::AdaptiveBelongsTo::Adapter
- Inherits:
-
Object
- Object
- ActiveTools::ActiveRecord::AdaptiveBelongsTo::Adapter
- Defined in:
- lib/active_tools/active_record/adaptive_belongs_to/adapter.rb
Instance Attribute Summary collapse
-
#assoc_name ⇒ Object
readonly
Returns the value of attribute assoc_name.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#owner ⇒ Object
readonly
Returns the value of attribute owner.
Instance Method Summary collapse
- #association ⇒ Object
- #clear! ⇒ Object
-
#initialize(owner, assoc_name, options = {}) ⇒ Adapter
constructor
A new instance of Adapter.
- #klass ⇒ Object
- #read(name) ⇒ Object
- #target_attributes ⇒ Object
- #target_process_do ⇒ Object
- #template_attributes ⇒ Object
- #try_commit ⇒ Object
- #try_commit_existed ⇒ Object
- #try_destroy ⇒ Object
- #try_destroy_backup ⇒ Object
- #try_destroy_target ⇒ Object
- #try_nullify ⇒ Object
- #try_restore_refreshed_backup ⇒ Object
- #update_target_if_changed! ⇒ Object
- #write(name, value) ⇒ Object
Constructor Details
#initialize(owner, assoc_name, options = {}) ⇒ Adapter
Returns a new instance of Adapter.
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/active_tools/active_record/adaptive_belongs_to/adapter.rb', line 9 def initialize(owner, assoc_name, = {}) @owner = owner @assoc_name = assoc_name @options = .with_indifferent_access @foreign_key = reflection.foreign_key @attr_map = @options[:attr_map] @remote_attributes = @attr_map.keys @init_proc = @options[:init_proc] @nullify_if = @options[:nullify_if] @update_if = @options[:update_if] @destroy_if = @options[:destroy_if] @uniq_by = Array(@options[:uniq_by]).map(&:to_s) @target_process = @options[:target_process] @touch = @options[:touch] association.load_target end |
Instance Attribute Details
#assoc_name ⇒ Object (readonly)
Returns the value of attribute assoc_name.
5 6 7 |
# File 'lib/active_tools/active_record/adaptive_belongs_to/adapter.rb', line 5 def assoc_name @assoc_name end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
5 6 7 |
# File 'lib/active_tools/active_record/adaptive_belongs_to/adapter.rb', line 5 def @options end |
#owner ⇒ Object (readonly)
Returns the value of attribute owner.
5 6 7 |
# File 'lib/active_tools/active_record/adaptive_belongs_to/adapter.rb', line 5 def owner @owner end |
Instance Method Details
#association ⇒ Object
30 31 32 |
# File 'lib/active_tools/active_record/adaptive_belongs_to/adapter.rb', line 30 def association owner.association(assoc_name) end |
#clear! ⇒ Object
154 155 156 157 |
# File 'lib/active_tools/active_record/adaptive_belongs_to/adapter.rb', line 154 def clear! @template = nil @backup = nil end |
#klass ⇒ Object
26 27 28 |
# File 'lib/active_tools/active_record/adaptive_belongs_to/adapter.rb', line 26 def klass association.klass||reflection.class_name.constantize end |
#read(name) ⇒ Object
34 35 36 37 |
# File 'lib/active_tools/active_record/adaptive_belongs_to/adapter.rb', line 34 def read(name) valid_attribute?(name) association.loaded? ? target.try(name) : association.reload.target.try(name) end |
#target_attributes ⇒ Object
77 78 79 |
# File 'lib/active_tools/active_record/adaptive_belongs_to/adapter.rb', line 77 def target_attributes attributes(target, *@remote_attributes) end |
#target_process_do ⇒ Object
131 132 133 |
# File 'lib/active_tools/active_record/adaptive_belongs_to/adapter.rb', line 131 def target_process_do @target_process.try(:call, target, owner) end |
#template_attributes ⇒ Object
73 74 75 |
# File 'lib/active_tools/active_record/adaptive_belongs_to/adapter.rb', line 73 def template_attributes attributes(@template, *@remote_attributes) end |
#try_commit ⇒ Object
64 65 66 |
# File 'lib/active_tools/active_record/adaptive_belongs_to/adapter.rb', line 64 def try_commit try_commit_existed || try_restore_refreshed_backup end |
#try_commit_existed ⇒ Object
90 91 92 93 94 95 96 97 98 99 |
# File 'lib/active_tools/active_record/adaptive_belongs_to/adapter.rb', line 90 def try_commit_existed if @template.present? && @uniq_by.any? && (existed = detect_existed) && !(@backup.present? && same_records?(@backup, existed, :attributes => @uniq_by)) warn "Adaptive is fetching existed <#{existed.class.name}: #{existed.class.primary_key}: #{existed.send(existed.class.primary_key)}>" self.target = existed if updateable_backup? @backup.mark_for_destruction end true end end |
#try_destroy ⇒ Object
68 69 70 71 |
# File 'lib/active_tools/active_record/adaptive_belongs_to/adapter.rb', line 68 def try_destroy try_destroy_backup try_destroy_target end |
#try_destroy_backup ⇒ Object
101 102 103 104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/active_tools/active_record/adaptive_belongs_to/adapter.rb', line 101 def try_destroy_backup if destroyable_backup? warn "Adaptive is destroying backed up: <#{@backup.class.name}: #{@backup.class.primary_key}: #{@backup.send(@backup.class.primary_key)}>" begin @backup.destroy rescue ::ActiveRecord::StaleObjectError @backup.reload try_destroy_backup rescue ::ActiveRecord::StatementInvalid @backup.reload try_destroy_backup end end end |
#try_destroy_target ⇒ Object
116 117 118 119 120 121 122 123 124 125 126 127 128 129 |
# File 'lib/active_tools/active_record/adaptive_belongs_to/adapter.rb', line 116 def try_destroy_target if destroyable_target? warn "Adaptive is destroying target: <#{target.class.name}: #{target.class.primary_key}: #{target.send(target.class.primary_key)}>" begin target.destroy rescue ::ActiveRecord::StaleObjectError target.reload try_destroy_target rescue ::ActiveRecord::StatementInvalid target.reload try_destroy_target end end end |
#try_nullify ⇒ Object
56 57 58 59 60 61 62 |
# File 'lib/active_tools/active_record/adaptive_belongs_to/adapter.rb', line 56 def try_nullify if nullify_target? store_backup! self.target = nil true end end |
#try_restore_refreshed_backup ⇒ Object
81 82 83 84 85 86 87 88 |
# File 'lib/active_tools/active_record/adaptive_belongs_to/adapter.rb', line 81 def try_restore_refreshed_backup if updateable_backup? warn "Adaptive is going to update: <#{@backup.class.name}: #{@backup.class.primary_key}: #{@backup.send(@backup.class.primary_key)}>" @backup.attributes = template_attributes restore_backup! true end end |
#update_target_if_changed! ⇒ Object
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 |
# File 'lib/active_tools/active_record/adaptive_belongs_to/adapter.rb', line 135 def update_target_if_changed! if target && target.changes.any? warn "Adaptive is updating: <#{target.class.name}: #{target.class.primary_key}: #{target.send(target.class.primary_key)}>" if @touch begin owner.touch rescue end end begin target.save rescue ::ActiveRecord::StaleObjectError update_target_if_changed! rescue ::ActiveRecord::StatementInvalid update_target_if_changed! end end end |
#write(name, value) ⇒ Object
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/active_tools/active_record/adaptive_belongs_to/adapter.rb', line 39 def write(name, value) valid_attribute?(name) local_attribute = @attr_map[name] if value != read(name) owner.send(:attribute_will_change!, local_attribute) store_backup! create_template! target.send("#{name}=", value) @template.send("#{name}=", value) owner.send(:attributes_changed_by_setter).except!(local_attribute) if owner.changes[local_attribute].try(:last) == owner.changes[local_attribute].try(:first) if @backup.present? && same_records?(@backup, target, :attributes => @remote_attributes) restore_backup! end end value end |