Skip to content

Instantly share code, notes, and snippets.

@carlqt
Last active August 5, 2021 12:37
Show Gist options
  • Select an option

  • Save carlqt/c7790b15658b3cb8e0dc3c7c91d3d158 to your computer and use it in GitHub Desktop.

Select an option

Save carlqt/c7790b15658b3cb8e0dc3c7c91d3d158 to your computer and use it in GitHub Desktop.

Revisions

  1. carlqt revised this gist Aug 5, 2021. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions self_collect_station.rb
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,6 @@
    class SelfCollectStation
    def self.update_or_create(params)
    station = detect_or_initialize_by(extension_id: attributes[:extension_id])
    def self.update_or_create(params)
    station = detect_or_initialize_by(extension_id: attributes[:extension_id])

    if station.persisted?
    station.update(attributes)
  2. carlqt revised this gist Aug 5, 2021. 1 changed file with 4 additions and 3 deletions.
    7 changes: 4 additions & 3 deletions update_or_create.rb
    Original file line number Diff line number Diff line change
    @@ -17,9 +17,10 @@ def initialize(data, country:, attribute_mapper_class:, provider_name:)

    def call
    self.prepared_data = prepare_data_for_sync

    prepared_data.map do |attributes|
    SelectCollectStataion.update_or_create(attributes)
    self_collect_stations = SelfCollectStation.where(external_id: external_ids, provider: provider_name)

    prepared_data.map do |attributes|
    self_collect_stations.update_or_create(attributes)
    end
    end

  3. carlqt revised this gist Aug 5, 2021. 1 changed file with 10 additions and 2 deletions.
    12 changes: 10 additions & 2 deletions self_collect_station.rb
    Original file line number Diff line number Diff line change
    @@ -1,11 +1,19 @@
    class SelfCollectStation
    def self.update_or_create(attributes)
    station = find_or_initialize_by(attributes)
    def self.update_or_create(params)
    station = detect_or_initialize_by(extension_id: attributes[:extension_id])

    if station.persisted?
    station.update(attributes)
    else
    station.save
    end
    end

    def self.detect_or_initialize_by(params)
    if loaded?
    detect { |record| record.extension_id == params[:extension_id] } || new(params)
    else
    find_or_initialize_by(params)
    end
    end
    end
  4. carlqt revised this gist Aug 4, 2021. No changes.
  5. carlqt revised this gist Aug 4, 2021. No changes.
  6. carlqt created this gist Aug 4, 2021.
    11 changes: 11 additions & 0 deletions self_collect_station.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,11 @@
    class SelfCollectStation
    def self.update_or_create(attributes)
    station = find_or_initialize_by(attributes)

    if station.persisted?
    station.update(attributes)
    else
    station.save
    end
    end
    end
    39 changes: 39 additions & 0 deletions update_or_create.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,39 @@
    module SelfCollectStations
    module Synchronizers
    class Base
    attr_reader :data,
    :country,
    :attribute_mapper_class,
    :provider_name

    attr_accessor :prepared_data

    def initialize(data, country:, attribute_mapper_class:, provider_name:)
    @data = data
    @country = country
    @attribute_mapper_class = attribute_mapper_class
    @provider_name = provider_name
    end

    def call
    self.prepared_data = prepare_data_for_sync

    prepared_data.map do |attributes|
    SelectCollectStataion.update_or_create(attributes)
    end
    end

    private

    def resources
    JSON.parse(data)
    end

    def prepare_data_for_sync
    resources.each_with_object({}) do |api_record, memo|
    record = attribute_mapper_class.new(record: api_record, country: country, provider_name: provider_name)
    memo[record.external_id] = record
    end
    end
    end
    end