Module: ReactiveRecord::Setters

Included in:
Base
Defined in:
lib/reactive_record/active_record/reactive_record/setters.rb

Instance Method Summary collapse

Instance Method Details

#set_ar_aggregate(aggr, raw_value) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/reactive_record/active_record/reactive_record/setters.rb', line 7

def set_ar_aggregate(aggr, raw_value)
  set_common(aggr.attribute, raw_value) do |value, attr|
    @attributes[attr] ||= aggr.klass.new if new?
    abr = @attributes[attr].backing_record
    abr.virgin = false
    map = value.attributes if value
    aggr.mapped_attributes.each do |mapped_attr|
      abr.update_aggregate_attribute mapped_attr, map && map[mapped_attr]
    end
    return @attributes[attr]
  end
end

#set_attr_value(attr, raw_value) ⇒ Object



3
4
5
# File 'lib/reactive_record/active_record/reactive_record/setters.rb', line 3

def set_attr_value(attr, raw_value)
  set_common(attr, raw_value) { |value| update_simple_attribute(attr, value) }
end

#set_belongs_to(assoc, raw_value) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/reactive_record/active_record/reactive_record/setters.rb', line 44

def set_belongs_to(assoc, raw_value)
  set_common(assoc.attribute, raw_value) do |value, attr|
    if assoc.inverse.collection?
      update_has_many_through_associations assoc, value
      update_inverse_collections assoc, value
    else
      update_inverse_attribute assoc, value
    end
    # itself will just reactively read the value (a model instance)  by doing a .id
    update_belongs_to attr, value.itself
  end
end

#set_has_many(assoc, raw_value) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/reactive_record/active_record/reactive_record/setters.rb', line 31

def set_has_many(assoc, raw_value)
  set_common(assoc.attribute, raw_value) do |value, attr|
    # create a new collection to hold value, shove it in, and return the new collection
    # the replace method will take care of updating the inverse belongs_to links as
    # the collection is overwritten
    collection = Collection.new(assoc.klass, @ar_instance, assoc)
    collection.replace(value || [])
    @synced_attributes[attr] = value if data_loading?
    set_attribute_change_status_and_notify attr, value != @synced_attributes[attr], collection
    return collection
  end
end

#set_non_ar_aggregate(aggregation, raw_value) ⇒ Object



20
21
22
23
24
25
26
27
28
29
# File 'lib/reactive_record/active_record/reactive_record/setters.rb', line 20

def set_non_ar_aggregate(aggregation, raw_value)
  set_common(aggregation.attribute, raw_value) do |value, attr|
    if data_loading?
      @synced_attributes[attr] = aggregation.deserialize(aggregation.serialize(value))
    else
      changed = !@synced_attributes.key?(attr) || @synced_attributes[attr] != value
    end
    set_attribute_change_status_and_notify attr, changed, value
  end
end

#sync_has_many(attr) ⇒ Object



57
58
59
# File 'lib/reactive_record/active_record/reactive_record/setters.rb', line 57

def sync_has_many(attr)
  set_change_status_and_notify_only attr, @attributes[attr] != @synced_attributes[attr]
end

#update_simple_attribute(attr, value) ⇒ Object Also known as: update_belongs_to, update_aggregate_attribute



61
62
63
64
65
66
67
68
# File 'lib/reactive_record/active_record/reactive_record/setters.rb', line 61

def update_simple_attribute(attr, value)
  if data_loading?
    @synced_attributes[attr] = value
  else
    changed = !@synced_attributes.key?(attr) || @synced_attributes[attr] != value
  end
  set_attribute_change_status_and_notify attr, changed, value
end