Class: ActiveTools::ActiveRecord::AdaptiveBelongsTo::Adapter

Inherits:
Object
  • Object
show all
Defined in:
lib/active_tools/active_record/adaptive_belongs_to/adapter.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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, options = {})
  @owner = owner
  @assoc_name = assoc_name
  @options = 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_nameObject (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

#optionsObject (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
  @options
end

#ownerObject (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

#associationObject



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

#klassObject



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_attributesObject



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_attributesObject



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_commitObject



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_existedObject



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_destroyObject



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_backupObject



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_targetObject



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_nullifyObject



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_updateObject



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