synchronisable 1.1.2 → 1.1.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 00ba5533ba3d705a1e3c245124ceb78afd57da1d
4
- data.tar.gz: abdedfecc1bf48eb0554cace0b9c323ccefe6fa6
3
+ metadata.gz: 46c5332c053921eb44021c59a846a1451eede185
4
+ data.tar.gz: d33c7cdb48f1266ebb3737a20c7a6c4b3688b12c
5
5
  SHA512:
6
- metadata.gz: 2cd022bab4b92cd95a3b738c0b49f935a5fdcb9abac8c38b2716ae506baa371e893881e0524f3495db65c44f10b9c032ba52d93fc5812a549fa97dfed934c401
7
- data.tar.gz: fd464896a1dce4ff99789b5e595186d5a6c043b0051759917a6a1479512484403fb31905bca4fae3f921bdfede5a102685b40d34d2f41540e21f12e34fbdcb70
6
+ metadata.gz: a42307f344f720d63a7519969481e94fffcc9a9010510cf39791ea43b93ad5e64304c80074aa1b5f5133d720fc16cbaf829650dfeb3da50d814efe3608eb72a4
7
+ data.tar.gz: 7b35e861d0ea8c002d4215b2965713c2c2ebb30f509f24e652f687f95e0237bb69be0dee671aa66fc944f4a714e417a9a4c85f59c1862f6d8f727879b0cff6f9
@@ -5,12 +5,12 @@ module Synchronisable
5
5
  :before, :after, :deleted
6
6
 
7
7
  def initialize(model, parent)
8
- @model, @parent = model, parent
8
+ @model, @parent = model, parent
9
9
  @errors = []
10
10
  @before, @after, @deleted = 0, 0, 0
11
11
  end
12
12
 
13
- # @return [String] summary synchronization info.
13
+ # @return [String] summary synchronization info
14
14
  def summary_message
15
15
  msg = I18n.t('messages.result',
16
16
  :model => model,
@@ -21,12 +21,7 @@ module Synchronisable
21
21
  :errors => errors.count
22
22
  )
23
23
 
24
- msg << I18n.t(
25
- 'messages.errors',
26
- :errors => errors.join('. ')
27
- ) if errors.present?
28
-
29
- msg
24
+ msg << I18n.t('messages.errors', errors: errors.join('. '))
30
25
  end
31
26
  end
32
27
  end
@@ -11,31 +11,38 @@ module Synchronisable
11
11
  @logger, @context = logger, context
12
12
  end
13
13
 
14
- # Wraps the given block in transaction.
15
- # Rescued exceptions are written to log and saved to errors array.
14
+ # Wraps the given block in transaction if it's source for a model
15
+ # on which #sync method was called and not a parent/child association.
16
+ # Rescued exceptions are written to log and saved to the errors array.
16
17
  #
17
18
  # @param source [Synchronisable::Source] synchronization source
18
19
  #
19
20
  # @return [Boolean] `true` if syncronization was completed
20
21
  # without errors, `false` otherwise
21
22
  def handle(source)
22
- block = ->() {
23
+ invoke(source) do
23
24
  yield
24
25
  return true
25
- }
26
-
27
- source.parent ? block.() : ActiveRecord::Base.transaction(&block)
26
+ end
28
27
  rescue Exception => e
29
- @context.errors << e
30
- log(e, source)
28
+ err_msg = error_message(e, source)
29
+
30
+ @context.errors << err_msg
31
+ @logger.error err_msg
32
+
31
33
  return false
32
34
  end
33
35
 
34
36
  private
35
37
 
36
- def log(e, source)
37
- msg = error_message(e, source)
38
- @logger.error msg
38
+ # Invokes a given block.
39
+ # Won't start a new transation if its not a "sync root".
40
+ def invoke(source, &block)
41
+ if source.parent
42
+ block.()
43
+ else
44
+ ActiveRecord::Base.transaction(&block)
45
+ end
39
46
  end
40
47
 
41
48
  def error_message(e, source)
@@ -42,7 +42,7 @@ module Synchronisable
42
42
  # :match_players => :player
43
43
  # })
44
44
  #
45
- # @example Here is all possible ways to call #sync:
45
+ # @example Here is all possible ways to call #sync
46
46
  # Model.sync
47
47
  # Model.sync({ :a1 => 1, :a2 => 2 })
48
48
  # Model.sync({ :includes => {...}, :parent => xxx })
@@ -22,9 +22,6 @@ module Synchronisable
22
22
  # Prepares synchronization source:
23
23
  # `remote_id`, `local_attributes`, `import_record` and `associations`.
24
24
  #
25
- # Sets foreign key if current model is specified as `has_one` or `has_many`
26
- # association of parent model.
27
- #
28
25
  # @api private
29
26
  def prepare(data, remote_attrs)
30
27
  @data = @parent
@@ -2,7 +2,7 @@ module Synchronisable
2
2
  module VERSION
3
3
  MAJOR = 1
4
4
  MINOR = 1
5
- PATCH = 2
5
+ PATCH = 3
6
6
  SUFFIX = nil
7
7
 
8
8
  STRING = [MAJOR, MINOR, PATCH, SUFFIX].compact.join('.')
@@ -34,12 +34,13 @@ module Synchronisable
34
34
 
35
35
  if import_record.nil? || association.force_sync
36
36
  data = @source.data.try(:merge, { id: id }) || id
37
- Controller.call(association.model, data, {})
37
+ Controller.call(association.model, data, { :parent => @source })
38
38
 
39
39
  import_record = find_import(id, association)
40
40
  end
41
41
 
42
- @source.local_attrs[association.key] = import_record.synchronisable.id
42
+ @source.local_attrs[association.key] = import_record
43
+ .try(:synchronisable).try(:id) # yep, it can happen
43
44
  end
44
45
  end
45
46
 
@@ -45,7 +45,7 @@ module Synchronisable
45
45
  }
46
46
  end
47
47
 
48
- # Syncs models that is defined in {Synchronisable#models}
48
+ # Syncs models that are defined in {Synchronisable#models}
49
49
  #
50
50
  # @param models [Array] array of models that should be synchronized.
51
51
  # This take a precedence over models defined in {Synchronisable#models}.
@@ -14,4 +14,8 @@ class StageSynchronizer < Synchronisable::Synchronizer
14
14
  except :ignored_1, :ignored_2
15
15
 
16
16
  gateway StageGateway
17
+
18
+ before_sync do |source|
19
+ source.local_attrs[:name] != 'ignored'
20
+ end
17
21
  end
@@ -0,0 +1,20 @@
1
+ require 'spec_helper'
2
+
3
+ describe Stage do
4
+ describe 'synchronization' do
5
+ let!(:remote_attrs) do
6
+ [
7
+ build(:remote_stage, tour_id: '1', eman: 'ignored'),
8
+ build(:remote_stage, tour_id: '2'),
9
+ build(:remote_stage, tour_id: '3')
10
+ ]
11
+ end
12
+
13
+ describe 'skipping with before_sync hook' do
14
+ subject { -> { Stage.sync(remote_attrs, :includes => {}) } }
15
+
16
+ it { is_expected.to change { Stage.count }.by(2) }
17
+ it { is_expected.to change { Synchronisable::Import.count }.by(2) }
18
+ end
19
+ end
20
+ 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.1.2
4
+ version: 1.1.3
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-08-05 00:00:00.000000000 Z
11
+ date: 2014-08-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -417,6 +417,7 @@ files:
417
417
  - spec/factories/stadium.rb
418
418
  - spec/factories/team.rb
419
419
  - spec/models/match_spec.rb
420
+ - spec/models/stage_spec.rb
420
421
  - spec/models/team_group_statistic_spec.rb
421
422
  - spec/models/team_spec.rb
422
423
  - spec/models/tournament_spec.rb
@@ -543,6 +544,7 @@ test_files:
543
544
  - spec/factories/stadium.rb
544
545
  - spec/factories/team.rb
545
546
  - spec/models/match_spec.rb
547
+ - spec/models/stage_spec.rb
546
548
  - spec/models/team_group_statistic_spec.rb
547
549
  - spec/models/team_spec.rb
548
550
  - spec/models/tournament_spec.rb