Sha256: 88177797cac72c1b775d2f8d0bfae2d6aa8a753e5c800f6d4f9762e8d78f3d30

Contents?: true

Size: 1.38 KB

Versions: 3

Compression:

Stored size: 1.38 KB

Contents

module DiscoApp::Concerns::Synchronises

  extend ActiveSupport::Concern

  class_methods do
    # Define the number of resources per page to fetch.
    SYNCHRONISES_PAGE_LIMIT = 250

    def should_synchronise?(_shop, _data)
      true
    end

    def synchronise_by(_shop, data)
      { id: data[:id] }
    end

    def synchronise(shop, data)
      data = JSON.parse(data.to_json) if data.is_a?(ShopifyAPI::Base)
      data = data.with_indifferent_access

      return unless should_synchronise?(shop, data)

      begin
        instance = find_or_create_by!(synchronise_by(shop, data)) do |new_instance|
          new_instance.shop = shop
          new_instance.data = data
        end
      rescue ActiveRecord::RecordNotUnique, PG::UniqueViolation
        retry
      end

      instance.update(data: data)

      instance
    end

    def should_synchronise_deletion?(_shop, _data)
      true
    end

    def synchronise_deletion(shop, data)
      data = data.with_indifferent_access

      return unless should_synchronise_deletion?(shop, data)

      where(shop: shop, id: data[:id]).destroy_all
    end

    def synchronise_all(shop, params = {})
      DiscoApp::SynchroniseResourcesJob.perform_later(shop, name, params)
    end
  end

  included do
    # Override the "read" data attribute to allow indifferent access.
    def data
      read_attribute(:data).with_indifferent_access
    end
  end

end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
disco_app-0.18.3 app/models/disco_app/concerns/synchronises.rb
disco_app-0.18.6 app/models/disco_app/concerns/synchronises.rb
disco_app-0.18.4 app/models/disco_app/concerns/synchronises.rb