synchronisable 1.0.7 → 1.0.8

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: 2d260ffbc99b38098b73f2b2899406534c0057db
4
- data.tar.gz: 2092b4fb9b983b906fa8f6965e6ca776152b31b4
3
+ metadata.gz: 97bddcbfca63402c0394e535b25bef1c7d1f8a9e
4
+ data.tar.gz: cebba95741ae03eec51bfc4ba1c333fd5a8574cd
5
5
  SHA512:
6
- metadata.gz: e8fd530e99e19a584c54789c38a79417101fe28100086ef0e4120f83f1799aeda9f430db1b876fecf5da3455e05aeac4de251e1392f9490ea95bf6e6c4fece9c
7
- data.tar.gz: 3deb07640739440fabda1e25c8053045429097a7d8d6535ca27c01576aa644fdbcd1abc700633dc077f4308e42bbde061d55bd757a583eb27e51f85b5bb56a87
6
+ metadata.gz: ad86504ab1870a6f6f535ae0162093971dacac5c3d38d7208e5495ddd4d52acaacc6181e758246250b99b7c7535b2e9da7cd394c3a882101d84bf5db8b9e02ed
7
+ data.tar.gz: 7cfdc9b259f064693b096c79555aaa11124114c01bab4a2ffa1a37de9b24d0b97a3752772c0232e0bb138c36d797ea83d9a35f8461051c51225d741436bfeda9
@@ -5,17 +5,22 @@ class CreateImports < ActiveRecord::Migration
5
5
  t.integer :synchronisable_id, null: false
6
6
  t.text :attrs
7
7
  t.string :remote_id, null: false
8
+ t.string :unique_id
8
9
 
9
10
  t.timestamps
10
11
  end
11
12
 
12
13
  add_index :imports, :remote_id
13
14
  add_index :imports, [:synchronisable_type, :synchronisable_id]
15
+ add_index :imports, [:synchronisable_type, :remote_id]
16
+ add_index :imports, [:synchronisable_type, :unique_id]
14
17
  end
15
18
 
16
19
  def self.down
17
20
  remove_index :imports, :remote_id
18
21
  remove_index :imports, [:synchronisable_type, :synchronisable_id]
22
+ remove_index :imports, [:synchronisable_type, :remote_id]
23
+ remove_index :imports, [:synchronisable_type, :unique_id]
19
24
  drop_table :imports
20
25
  end
21
26
  end
@@ -10,10 +10,6 @@ module Synchronisable
10
10
  scope :with_remote_id, ->(id) { where(remote_id: id.to_s) }
11
11
  scope :with_remote_ids, ->(ids) { where(remote_id: ids.map(&:to_s)) }
12
12
 
13
- def self.find_by_remote_id(id)
14
- with_remote_id(id).first
15
- end
16
-
17
13
  def destroy_with_synchronisable
18
14
  ActiveRecord::Base.transaction do
19
15
  synchronisable.try :destroy
@@ -8,9 +8,8 @@ module Synchronisable
8
8
 
9
9
  attr_accessor :import_record
10
10
  attr_reader :parent_associations, :child_associations
11
- attr_reader :model, :remote_attrs, :remote_id,
12
- :local_attrs, :import_ids, :parent,
13
- :includes, :data
11
+ attr_reader :model, :remote_attrs, :remote_id, :unique_id,
12
+ :local_attrs, :import_ids, :parent, :includes, :data
14
13
 
15
14
  def initialize(model, parent, includes)
16
15
  @model, @parent, @synchronizer = model, parent, model.synchronizer
@@ -39,17 +38,32 @@ module Synchronisable
39
38
  @parent_associations = filter_associations(PARENT_ASSOCIATION_KEYS)
40
39
  @child_associations = filter_associations(CHILD_ASSOCIATION_KEYS)
41
40
 
42
- @import_record = Import.find_by(
43
- :remote_id => @remote_id.to_s,
44
- :synchronisable_type => @model
45
- )
41
+ @unique_id = @local_attrs[@synchronizer.unique_id]
42
+ @import_record = find_import
46
43
 
47
44
  remove_association_keys_from_local_attrs
48
-
49
- # TODO: This should be in Synchronisable::RecordWorker
50
45
  set_belongs_to_parent_foreign_key
51
46
  end
52
47
 
48
+ def find_import
49
+ (@unique_id.present? && find_import_by_unique_id) ||
50
+ find_import_by_remote_id
51
+ end
52
+
53
+ def find_import_by_unique_id
54
+ Import.find_by(
55
+ unique_id: @unique_id.to_s,
56
+ synchronisable_type: @model
57
+ )
58
+ end
59
+
60
+ def find_import_by_remote_id
61
+ Import.find_by(
62
+ remote_id: @remote_id.to_s,
63
+ synchronisable_type: @model
64
+ )
65
+ end
66
+
53
67
  def updatable?
54
68
  import_record.present? &&
55
69
  local_record.present?
@@ -62,6 +76,7 @@ module Synchronisable
62
76
  def dump_message
63
77
  %Q(
64
78
  remote id: '#{remote_id}',
79
+ unique_id: '#{unique_id}',
65
80
  remote attributes: #{remote_attrs},
66
81
  local attributes: #{local_attrs}
67
82
  )
@@ -18,6 +18,9 @@ module Synchronisable
18
18
  # The name of remote `id` attribute.
19
19
  attribute :remote_id, default: :id
20
20
 
21
+ # The unique id to prevent sync of duplicate records.
22
+ attribute :unique_id
23
+
21
24
  # Mapping configuration between local model attributes and
22
25
  # its remote counterpart (including id attribute).
23
26
  attribute :mappings, converter: ->(source) {
@@ -2,7 +2,7 @@ module Synchronisable
2
2
  module VERSION
3
3
  MAJOR = 1
4
4
  MINOR = 0
5
- PATCH = 7
5
+ PATCH = 8
6
6
  SUFFIX = nil
7
7
 
8
8
  STRING = [MAJOR, MINOR, PATCH, SUFFIX].compact.join('.')
@@ -2,6 +2,7 @@ class TournamentSynchronizer < Synchronisable::Synchronizer
2
2
  has_many :stages
3
3
 
4
4
  remote_id :tour_id
5
+ unique_id :name
5
6
 
6
7
  mappings(
7
8
  :eman => :name,
@@ -5,17 +5,23 @@ class CreateImports < ActiveRecord::Migration
5
5
  t.integer :synchronisable_id, null: false
6
6
  t.text :attrs
7
7
  t.string :remote_id, null: false
8
+ t.string :unique_id
8
9
 
9
10
  t.timestamps
10
11
  end
11
12
 
12
13
  add_index :imports, :remote_id
13
14
  add_index :imports, [:synchronisable_type, :synchronisable_id]
15
+ add_index :imports, [:synchronisable_type, :remote_id]
16
+ add_index :imports, [:synchronisable_type, :unique_id]
14
17
  end
15
18
 
16
19
  def self.down
17
20
  remove_index :imports, :remote_id
18
21
  remove_index :imports, [:synchronisable_type, :synchronisable_id]
22
+ remove_index :imports, [:synchronisable_type, :remote_id]
23
+ remove_index :imports, [:synchronisable_type, :unique_id]
19
24
  drop_table :imports
20
25
  end
21
26
  end
27
+
@@ -18,12 +18,15 @@ ActiveRecord::Schema.define(version: 20140609133855) do
18
18
  t.integer "synchronisable_id", null: false
19
19
  t.text "attrs"
20
20
  t.string "remote_id", null: false
21
+ t.string "unique_id"
21
22
  t.datetime "created_at"
22
23
  t.datetime "updated_at"
23
24
  end
24
25
 
25
26
  add_index "imports", ["remote_id"], name: "index_imports_on_remote_id"
27
+ add_index "imports", ["synchronisable_type", "remote_id"], name: "index_imports_on_synchronisable_type_and_remote_id"
26
28
  add_index "imports", ["synchronisable_type", "synchronisable_id"], name: "index_imports_on_synchronisable_type_and_synchronisable_id"
29
+ add_index "imports", ["synchronisable_type", "unique_id"], name: "index_imports_on_synchronisable_type_and_unique_id"
27
30
 
28
31
  create_table "match_players", force: true do |t|
29
32
  t.integer "match_id"
@@ -0,0 +1,18 @@
1
+ require 'spec_helper'
2
+
3
+ describe Tournament do
4
+ describe 'synchronization' do
5
+ let!(:remote_attrs) do
6
+ [
7
+ build(:remote_tournament, tour_id: '1', eman: 'tour-1'),
8
+ build(:remote_tournament, tour_id: '2', eman: 'tour-1'),
9
+ build(:remote_tournament, tour_id: '1', eman: 'tour-2')
10
+ ]
11
+ end
12
+
13
+ subject { -> { Tournament.sync(remote_attrs) } }
14
+
15
+ it { is_expected.to change { Tournament.count }.by(2) }
16
+ it { is_expected.to change { Synchronisable::Import.count }.by(2) }
17
+ end
18
+ end
@@ -12,12 +12,12 @@ describe Synchronisable do
12
12
  Synchronisable.models = %w(Match Team)
13
13
  end
14
14
 
15
- it { should change { Match.count }.by(1) }
16
- it { should change { Team.count }.by(2) }
17
- it { should change { Player.count }.by(4) }
18
- it { should change { MatchPlayer.count }.by(4) }
15
+ it { is_expected.to change { Match.count }.by(1) }
16
+ it { is_expected.to change { Team.count }.by(2) }
17
+ it { is_expected.to change { Player.count }.by(4) }
18
+ it { is_expected.to change { MatchPlayer.count }.by(4) }
19
19
 
20
- it { should change { Synchronisable::Import.count }.by(11) }
20
+ it { is_expected.to change { Synchronisable::Import.count }.by(11) }
21
21
  end
22
22
 
23
23
  context 'all' do
@@ -28,18 +28,16 @@ describe Synchronisable do
28
28
  )
29
29
  end
30
30
 
31
- it { should change { Tournament.count }.by(1) }
32
- it { should change { Stage.count }.by(2) }
33
- it { should change { Match.count }.by(1) }
34
- it { should change { Team.count }.by(2) }
35
- it { should change { MatchPlayer.count }.by(4) }
36
- it { should change { Player.count }.by(4) }
31
+ it { is_expected.to change { Tournament.count }.by(1) }
32
+ it { is_expected.to change { Stage.count }.by(2) }
33
+ it { is_expected.to change { Match.count }.by(1) }
34
+ it { is_expected.to change { Team.count }.by(2) }
35
+ it { is_expected.to change { MatchPlayer.count }.by(4) }
36
+ it { is_expected.to change { Player.count }.by(4) }
37
37
 
38
- it { should change { Synchronisable::Import.count }.by(14) }
38
+ it { is_expected.to change { Synchronisable::Import.count }.by(14) }
39
39
  end
40
40
 
41
- # TODO: Left here until :include option is implemented
42
- #
43
41
  # context 'when models setting is overriden in method call' do
44
42
  # before :all do
45
43
  # Synchronisable.models = %w(Team Match)
@@ -49,11 +47,11 @@ describe Synchronisable do
49
47
  # -> { Synchronisable.sync(Match, Player) }
50
48
  # end
51
49
 
52
- # it { should change { Match.count }.by(1) }
53
- # it { should change { Player.count }.by(22) }
54
- # it { should_not change { Team.count }.by(2) }
50
+ # it { is_expected.to change { Match.count }.by(1) }
51
+ # it { is_expected.to change { Player.count }.by(22) }
52
+ # it { is_expected.not_to change { Team.count }.by(2) }
55
53
 
56
- # it { should change { Synchronisable::Import.count }.by(5) }
54
+ # it { is_expected.to change { Synchronisable::Import.count }.by(5) }
57
55
  # end
58
56
  end
59
57
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: synchronisable
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.7
4
+ version: 1.0.8
5
5
  platform: ruby
6
6
  authors:
7
7
  - Vasiliy Yorkin
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-07-29 00:00:00.000000000 Z
11
+ date: 2014-07-30 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -419,6 +419,7 @@ files:
419
419
  - spec/models/match_spec.rb
420
420
  - spec/models/team_group_statistic_spec.rb
421
421
  - spec/models/team_spec.rb
422
+ - spec/models/tournament_spec.rb
422
423
  - spec/spec_helper.rb
423
424
  - spec/synchronisable/dsl/macro_spec.rb
424
425
  - spec/synchronisable/models/import_spec.rb
@@ -544,6 +545,7 @@ test_files:
544
545
  - spec/models/match_spec.rb
545
546
  - spec/models/team_group_statistic_spec.rb
546
547
  - spec/models/team_spec.rb
548
+ - spec/models/tournament_spec.rb
547
549
  - spec/spec_helper.rb
548
550
  - spec/synchronisable/dsl/macro_spec.rb
549
551
  - spec/synchronisable/models/import_spec.rb