Last active
August 5, 2021 12:37
-
-
Save carlqt/c7790b15658b3cb8e0dc3c7c91d3d158 to your computer and use it in GitHub Desktop.
Revisions
-
carlqt revised this gist
Aug 5, 2021 . 1 changed file with 2 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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]) if station.persisted? station.update(attributes) -
carlqt revised this gist
Aug 5, 2021 . 1 changed file with 4 additions and 3 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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 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 -
carlqt revised this gist
Aug 5, 2021 . 1 changed file with 10 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,11 +1,19 @@ class SelfCollectStation 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 -
carlqt revised this gist
Aug 4, 2021 . No changes.There are no files selected for viewing
-
carlqt revised this gist
Aug 4, 2021 . No changes.There are no files selected for viewing
-
carlqt created this gist
Aug 4, 2021 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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 This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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