Sha256: e39303cb8cbafa0d36d43f90eccd5043414cc8499c7ef1eccfd8bee2447a239d

Contents?: true

Size: 1.64 KB

Versions: 5

Compression:

Stored size: 1.64 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)
      if data.is_a?(ShopifyAPI::Base)
        data = ActiveSupport::JSON.decode(data.to_json)
      end
      data = data.with_indifferent_access

      return unless should_synchronise?(shop, data)

      begin
        instance = self.find_or_create_by!(self.synchronise_by(shop, data)) do |instance|
          instance.shop = shop
          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)

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

    def synchronise_all(shop, params = {})
      resource_count = shop.with_api_context { self::SHOPIFY_API_CLASS.count(params) }

      (1..(resource_count / SYNCHRONISES_PAGE_LIMIT.to_f).ceil).each do |page|
        DiscoApp::SynchroniseResourcesJob.perform_later(shop, self.name, params.merge(page: page, limit: SYNCHRONISES_PAGE_LIMIT))
      end
    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

5 entries across 5 versions & 1 rubygems

Version Path
disco_app-0.13.5 app/models/disco_app/concerns/synchronises.rb
disco_app-0.13.6 app/models/disco_app/concerns/synchronises.rb
disco_app-0.13.7 app/models/disco_app/concerns/synchronises.rb
disco_app-0.13.8 app/models/disco_app/concerns/synchronises.rb
disco_app-0.13.6.pre.puma.pre.3 app/models/disco_app/concerns/synchronises.rb