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
- #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_update ⇒ 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 |
# 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 @remote_attributes = @options[:remote_attributes] @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) 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
27 28 29 |
# File 'lib/active_tools/active_record/adaptive_belongs_to/adapter.rb', line 27 def association owner.association(assoc_name) end |
#clear! ⇒ Object
125 126 127 128 |
# File 'lib/active_tools/active_record/adaptive_belongs_to/adapter.rb', line 125 def clear! @template = nil @backup = nil end |
#klass ⇒ Object
23 24 25 |
# File 'lib/active_tools/active_record/adaptive_belongs_to/adapter.rb', line 23 def klass association.klass||reflection.class_name.constantize end |
#read(name) ⇒ Object
31 32 33 34 |
# File 'lib/active_tools/active_record/adaptive_belongs_to/adapter.rb', line 31 def read(name) valid_attribute?(name) target.send(name) if target end |
#target_attributes ⇒ Object
68 69 70 |
# File 'lib/active_tools/active_record/adaptive_belongs_to/adapter.rb', line 68 def target_attributes attributes(target, *@remote_attributes) end |
#template_attributes ⇒ Object
64 65 66 |
# File 'lib/active_tools/active_record/adaptive_belongs_to/adapter.rb', line 64 def template_attributes attributes(@template, *@remote_attributes) end |
#try_commit ⇒ Object
55 56 57 |
# File 'lib/active_tools/active_record/adaptive_belongs_to/adapter.rb', line 55 def try_commit try_commit_existed || try_update end |
#try_commit_existed ⇒ Object
87 88 89 90 91 92 93 94 95 |
# File 'lib/active_tools/active_record/adaptive_belongs_to/adapter.rb', line 87 def try_commit_existed if @template.present? && @uniq_by.any? && (existed = detect_existed) self.target = existed if updateable_backup? @backup.mark_for_destruction end true end end |
#try_destroy ⇒ Object
59 60 61 62 |
# File 'lib/active_tools/active_record/adaptive_belongs_to/adapter.rb', line 59 def try_destroy try_destroy_backup try_destroy_target end |
#try_destroy_backup ⇒ Object
97 98 99 100 101 102 103 104 105 106 107 108 109 |
# File 'lib/active_tools/active_record/adaptive_belongs_to/adapter.rb', line 97 def try_destroy_backup if destroyable_backup? 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
111 112 113 114 115 116 117 118 119 120 121 122 123 |
# File 'lib/active_tools/active_record/adaptive_belongs_to/adapter.rb', line 111 def try_destroy_target if destroyable_target? 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
48 49 50 51 52 53 |
# File 'lib/active_tools/active_record/adaptive_belongs_to/adapter.rb', line 48 def try_nullify if nullify? store_backup! self.target = nil end end |
#try_update ⇒ Object
72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/active_tools/active_record/adaptive_belongs_to/adapter.rb', line 72 def try_update if updateable_backup? begin @backup.update(template_attributes) rescue ::ActiveRecord::StaleObjectError @backup.reload try_update rescue ::ActiveRecord::StatementInvalid @backup.reload try_update end self.target = @backup end end |
#write(name, value) ⇒ Object
36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/active_tools/active_record/adaptive_belongs_to/adapter.rb', line 36 def write(name, value) valid_attribute?(name) if value != read(name) store_backup! create_template! target.send("#{name}=", value) if same_as_backup? restore_backup! end end end |