syncify 0.1.5 → 0.1.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 1722af5f629f92ed4a34ede9157e902cb5d3523c
4
- data.tar.gz: b89894af010324fb43ec9dda4376ba4c9d2543b4
3
+ metadata.gz: bbbff47f10eb17cab6c833696be95392bb9da92b
4
+ data.tar.gz: 82dd0f3812f8ec948b2e3c056fcf4ab68b963e76
5
5
  SHA512:
6
- metadata.gz: dd8e95679bd3ab02725da8553bbdec166aa09745682a51fe62bacd43a65124baed7aed728d59c0806843fab90552fb3765fba081589374d73179c860776061bb
7
- data.tar.gz: 789e5dda07c8f5fb3263bf940cbf16e3dbcf83266afd76cb61fbabb8a521fc4ed500cd56d1b2508fd95bcee4db8299822b6c286c21412e741dfb383bc72f0e7e
6
+ metadata.gz: c520fc80891d255cb6fa2e15fbcc522a0c3d3625aca21829b45a705732f106bbd6442ae48af134b4e4a54dffa1f35e008f9c8203e57c29ba34514aba7e47ee97
7
+ data.tar.gz: cd2bd81aad8e1d4b8df61e1a58a9713bc7a6002777852585260d896a9214da508951da06e59ea514574ab9040e8e14cdcfc71675834caf3bde6dadadb49064cf
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- syncify (0.1.4)
4
+ syncify (0.1.6)
5
5
  active_interaction (~> 3.0)
6
6
  activerecord (~> 4.2)
7
7
  activerecord-import (~> 0.17)
@@ -11,12 +11,14 @@ module Syncify
11
11
  symbol :remote_database
12
12
 
13
13
  attr_accessor :identified_records
14
+ attr_accessor :has_and_belongs_to_many_associations
14
15
 
15
16
  validate :id_xor_where_present?
16
17
 
17
18
  def execute
18
19
  puts 'Identifying records to sync...'
19
20
  @identified_records = Set[]
21
+ @has_and_belongs_to_many_associations = {}
20
22
 
21
23
  remote do
22
24
  initial_query.each do |root_record|
@@ -94,11 +96,41 @@ module Syncify
94
96
 
95
97
  records.each do |record|
96
98
  associations.each do |association, nested_associations|
97
- traverse_associations(record.__send__(association), nested_associations)
99
+ if is_through_association?(record, association)
100
+ traverse_associations(
101
+ record.__send__(
102
+ record.class.reflect_on_association(association).through_reflection.name
103
+ ),
104
+ associations
105
+ )
106
+ else
107
+ associated_records = record.__send__(association)
108
+
109
+ if is_has_and_belongs_to_many_association?(record, association)
110
+ cache_has_and_belongs_to_many_association(record, association, associated_records)
111
+ end
112
+
113
+ traverse_associations(associated_records, nested_associations)
114
+ end
98
115
  end
99
116
  end
100
117
  end
101
118
 
119
+ def cache_has_and_belongs_to_many_association(record, association, associated_records)
120
+ has_and_belongs_to_many_associations[record] ||= {}
121
+ has_and_belongs_to_many_associations[record][association] = Array(associated_records)
122
+ end
123
+
124
+ def is_has_and_belongs_to_many_association?(record, association)
125
+ record.class.reflect_on_association(association).class ==
126
+ ActiveRecord::Reflection::HasAndBelongsToManyReflection
127
+ end
128
+
129
+ def is_through_association?(record, association)
130
+ record.class.reflect_on_association(association).class ==
131
+ ActiveRecord::Reflection::ThroughReflection
132
+ end
133
+
102
134
  def sync_records
103
135
  ActiveRecord::Base.connection.disable_referential_integrity do
104
136
  classify_identified_instances.each do |class_name, new_instances|
@@ -107,6 +139,17 @@ module Syncify
107
139
  clazz.where(id: new_instances.map(&:id)).delete_all
108
140
  clazz.import(new_instances, validate: false)
109
141
  end
142
+
143
+ has_and_belongs_to_many_associations.each do |record, associations|
144
+ associations.each do |association, associated_records|
145
+ local_record = record.class.find(record.id)
146
+ local_associated_records = associated_records.map do |associated_record|
147
+ associated_record.class.find(associated_record.id)
148
+ end
149
+ local_record.__send__(association) << local_associated_records
150
+ local_record.save
151
+ end
152
+ end
110
153
  end
111
154
  end
112
155
 
@@ -1,3 +1,3 @@
1
1
  module Syncify
2
- VERSION = "0.1.5"
2
+ VERSION = "0.1.6"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: syncify
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.5
4
+ version: 0.1.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Doug Hughes
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2019-08-19 00:00:00.000000000 Z
11
+ date: 2019-08-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler