synchronisable 1.1.2 → 1.1.3
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 +4 -4
- data/lib/synchronisable/context.rb +3 -8
- data/lib/synchronisable/error_handler.rb +18 -11
- data/lib/synchronisable/model/methods.rb +1 -1
- data/lib/synchronisable/source.rb +0 -3
- data/lib/synchronisable/version.rb +1 -1
- data/lib/synchronisable/worker/associations.rb +3 -2
- data/lib/synchronisable.rb +1 -1
- data/spec/dummy/app/synchronizers/stage_synchronizer.rb +4 -0
- data/spec/models/stage_spec.rb +20 -0
- metadata +4 -2
    
        checksums.yaml
    CHANGED
    
    | @@ -1,7 +1,7 @@ | |
| 1 1 | 
             
            ---
         | 
| 2 2 | 
             
            SHA1:
         | 
| 3 | 
            -
              metadata.gz:  | 
| 4 | 
            -
              data.tar.gz:  | 
| 3 | 
            +
              metadata.gz: 46c5332c053921eb44021c59a846a1451eede185
         | 
| 4 | 
            +
              data.tar.gz: d33c7cdb48f1266ebb3737a20c7a6c4b3688b12c
         | 
| 5 5 | 
             
            SHA512:
         | 
| 6 | 
            -
              metadata.gz:  | 
| 7 | 
            -
              data.tar.gz:  | 
| 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 | 
| 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 | 
            -
                #  | 
| 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 | 
            -
                   | 
| 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 | 
            -
                   | 
| 30 | 
            -
             | 
| 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 | 
            -
                 | 
| 37 | 
            -
             | 
| 38 | 
            -
             | 
| 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
         | 
| @@ -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 | 
| 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 |  | 
    
        data/lib/synchronisable.rb
    CHANGED
    
    | @@ -45,7 +45,7 @@ module Synchronisable | |
| 45 45 | 
             
                }
         | 
| 46 46 | 
             
              end
         | 
| 47 47 |  | 
| 48 | 
            -
              # Syncs models that  | 
| 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}.
         | 
| @@ -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. | 
| 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- | 
| 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
         |