Class: Synchronisable::Source

Inherits:
Object
  • Object
show all
Defined in:
lib/synchronisable/source.rb

Overview

Synchronization source.

Constant Summary collapse

CHILD_ASSOCIATION_KEYS =
%i(has_one has_many)
PARENT_ASSOCIATION_KEYS =
%i(belongs_to)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(model, parent, includes) ⇒ Source

Returns a new instance of Source.



15
16
17
18
19
# File 'lib/synchronisable/source.rb', line 15

def initialize(model, parent, includes)
  @model, @parent, @synchronizer = model, parent, model.synchronizer
  @model_name = @model.to_s.demodulize.underscore.to_sym
  @includes = includes
end

Instance Attribute Details

#child_associationsObject (readonly)

Returns the value of attribute child_associations.



10
11
12
# File 'lib/synchronisable/source.rb', line 10

def child_associations
  @child_associations
end

#dataObject (readonly)

Returns the value of attribute data.



11
12
13
# File 'lib/synchronisable/source.rb', line 11

def data
  @data
end

#import_idsObject (readonly)

Returns the value of attribute import_ids.



11
12
13
# File 'lib/synchronisable/source.rb', line 11

def import_ids
  @import_ids
end

#import_recordObject

Returns the value of attribute import_record.



9
10
11
# File 'lib/synchronisable/source.rb', line 9

def import_record
  @import_record
end

#includesObject (readonly)

Returns the value of attribute includes.



11
12
13
# File 'lib/synchronisable/source.rb', line 11

def includes
  @includes
end

#local_attrsObject (readonly)

Returns the value of attribute local_attrs.



11
12
13
# File 'lib/synchronisable/source.rb', line 11

def local_attrs
  @local_attrs
end

#modelObject (readonly)

Returns the value of attribute model.



11
12
13
# File 'lib/synchronisable/source.rb', line 11

def model
  @model
end

#parentObject (readonly)

Returns the value of attribute parent.



11
12
13
# File 'lib/synchronisable/source.rb', line 11

def parent
  @parent
end

#parent_associationsObject (readonly)

Returns the value of attribute parent_associations.



10
11
12
# File 'lib/synchronisable/source.rb', line 10

def parent_associations
  @parent_associations
end

#remote_attrsObject (readonly)

Returns the value of attribute remote_attrs.



11
12
13
# File 'lib/synchronisable/source.rb', line 11

def remote_attrs
  @remote_attrs
end

#remote_idObject (readonly)

Returns the value of attribute remote_id.



11
12
13
# File 'lib/synchronisable/source.rb', line 11

def remote_id
  @remote_id
end

Instance Method Details

#dump_messageObject



62
63
64
65
66
67
68
# File 'lib/synchronisable/source.rb', line 62

def dump_message
  %Q(
    remote id: '#{remote_id}',
    remote attributes: #{remote_attrs},
    local attributes: #{local_attrs}
  )
end

#local_recordObject



58
59
60
# File 'lib/synchronisable/source.rb', line 58

def local_record
  @import_record.try(:synchronisable)
end

#prepare(data, remote_attrs) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Prepares synchronization source: ‘remote_id`, `local_attributes`, `import_record` and `associations`.

Sets foreign key if current model is specified as ‘has_one` or `has_many` association of parent model.



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/synchronisable/source.rb', line 28

def prepare(data, remote_attrs)
  @data = @parent
    .try(:source).try(:data)
    .try(:merge, data) || data

  @remote_attrs = remote_attrs.with_indifferent_access

  @remote_id    = @synchronizer.extract_remote_id(@remote_attrs)
  @local_attrs  = @synchronizer.map_attributes(@remote_attrs)
  @associations = @synchronizer.associations_for(@local_attrs)

  @parent_associations = filter_associations(PARENT_ASSOCIATION_KEYS)
  @child_associations  = filter_associations(CHILD_ASSOCIATION_KEYS)

  @import_record = Import.find_by(
    :remote_id => @remote_id.to_s,
    :synchronisable_type => @model
  )

  remove_association_keys_from_local_attrs

  # TODO: This should be in Synchronisable::RecordWorker
  set_belongs_to_parent_foreign_key
end

#updatable?Boolean

Returns:

  • (Boolean)


53
54
55
56
# File 'lib/synchronisable/source.rb', line 53

def updatable?
  import_record.present? &&
  local_record.present?
end