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

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

Instance Attribute Summary collapse

Attributes inherited from Base

#owner, #reflection

Instance Method Summary collapse

Methods inherited from Base

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

Constructor Details

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

Instance Attribute Details

#destroyedObject (readonly)

Returns the value of attribute destroyed.



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

def destroyed
  @destroyed
end

Instance Method Details

#apply_changesObject



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

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



7
8
9
# File 'lib/active_data/model/associations/embeds_one.rb', line 7

def build(attributes = {})
  self.target = build_object(attributes)
end

#clearObject



65
66
67
68
# File 'lib/active_data/model/associations/embeds_one.rb', line 65

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

#create(attributes = {}) ⇒ Object



11
12
13
# File 'lib/active_data/model/associations/embeds_one.rb', line 11

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

#create!(attributes = {}) ⇒ Object



15
16
17
# File 'lib/active_data/model/associations/embeds_one.rb', line 15

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

#defaultObject



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

def default
  return if evar_loaded?

  default = reflection.default(owner)

  return unless default

  object = if default.is_a?(reflection.klass)
    default
  else
    reflection.klass.with_sanitize(false) do
      build_object(default)
    end
  end
  object.send(:clear_changes_information) if reflection.klass.dirty?
  object
end

#load_targetObject



42
43
44
45
# File 'lib/active_data/model/associations/embeds_one.rb', line 42

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

#reader(force_reload = false) ⇒ Object



70
71
72
73
# File 'lib/active_data/model/associations/embeds_one.rb', line 70

def reader(force_reload = false)
  reload if force_reload
  target
end

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



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

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



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

def target=(object)
  if object
    callback(:before_add, object)
    setup_performers! object
  end
  loaded!
  @target = object
  callback(:after_add, object) if object
end