Class: ActiveData::Model::Associations::EmbedsOne

Inherits:
Base
  • Object
show all
Defined in:
lib/active_data/model/associations/embeds_one.rb

Instance Attribute Summary

Attributes inherited from Base

#owner, #reflection

Instance Method Summary collapse

Methods inherited from Base

#apply_changes!, #evar_loaded?, #initialize, #inspect, #loaded!, #loaded?, #reload, #reset, #target, #transaction

Constructor Details

This class inherits a constructor from ActiveData::Model::Associations::Base

Instance Method Details

#apply_changesObject



21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/active_data/model/associations/embeds_one.rb', line 21

def apply_changes
  if target
    if target.destroyed? || target.marked_for_destruction?
      @destroyed = target
      clear
    else
      target.save
    end
  else
    true
  end
end

#build(attributes = {}) ⇒ Object



5
6
7
# File 'lib/active_data/model/associations/embeds_one.rb', line 5

def build attributes = {}
  self.target = reflection.klass.new(attributes)
end

#clearObject



62
63
64
65
# File 'lib/active_data/model/associations/embeds_one.rb', line 62

def clear
  target.try(:destroy)
  reload.nil?
end

#create(attributes = {}) ⇒ Object



9
10
11
# File 'lib/active_data/model/associations/embeds_one.rb', line 9

def create attributes = {}
  build(attributes).tap(&:save)
end

#create!(attributes = {}) ⇒ Object



13
14
15
# File 'lib/active_data/model/associations/embeds_one.rb', line 13

def create! attributes = {}
  build(attributes).tap(&:save!)
end

#defaultObject



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/active_data/model/associations/embeds_one.rb', line 45

def default
  unless evar_loaded?
    default = reflection.default(owner)
    if default
      object = if default.is_a?(reflection.klass)
        default
      else
        reflection.klass.with_sanitize(false) do
          reflection.klass.new(default)
        end
      end
      object.send(:clear_changes_information) if reflection.klass.dirty?
      object
    end
  end
end

#destroyedObject



17
18
19
# File 'lib/active_data/model/associations/embeds_one.rb', line 17

def destroyed
  @destroyed
end

#load_targetObject



40
41
42
43
# File 'lib/active_data/model/associations/embeds_one.rb', line 40

def load_target
  source = read_source
  source ? reflection.klass.instantiate(source) : default
end

#reader(force_reload = false) ⇒ Object



67
68
69
70
# File 'lib/active_data/model/associations/embeds_one.rb', line 67

def reader force_reload = false
  reload if force_reload
  target
end

#replace(object) ⇒ Object Also known as: writer



72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/active_data/model/associations/embeds_one.rb', line 72

def replace object
  if object
    raise AssociationTypeMismatch.new(reflection.klass, object.class) unless object.is_a?(reflection.klass)
    transaction do
      clear
      self.target = object
      apply_changes! if owner.persisted?
    end
  else
    clear
  end

  target
end

#target=(object) ⇒ Object



34
35
36
37
38
# File 'lib/active_data/model/associations/embeds_one.rb', line 34

def target= object
  setup_performers! object if object
  loaded!
  @target = object
end