Class: SupplierSyncService
- Inherits:
-
Object
- Object
- SupplierSyncService
- Defined in:
- app/services/supplier_sync_service.rb
Overview
app/services/supplier_sync_service.rb
Instance Method Summary collapse
-
#initialize(supplier) ⇒ SupplierSyncService
constructor
A new instance of SupplierSyncService.
-
#persist(created, deleted, updated, enable_unit_migration: false) ⇒ Boolean
Persists changes to articles.
- #sync ⇒ Object
Constructor Details
#initialize(supplier) ⇒ SupplierSyncService
3 4 5 |
# File 'app/services/supplier_sync_service.rb', line 3 def initialize(supplier) @supplier = supplier end |
Instance Method Details
#persist(created, deleted, updated, enable_unit_migration: false) ⇒ Boolean
Persists changes to articles
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'app/services/supplier_sync_service.rb', line 23 def persist(created, deleted, updated, enable_unit_migration: false) has_error = false Article.transaction do # re-enable unit migration if requested @supplier.update_attribute(:unit_migration_completed, nil) if enable_unit_migration # delete articles begin has_error = !deleted.map(&:mark_as_deleted).all? unless deleted.empty? rescue StandardError # raises an exception when used in current order has_error = true end # Update articles if updated.first.is_a?(Array) # Handle [article, attributes] pairs updated.each do |article, attributes| article.latest_article_version.article_unit_ratios.clear article.latest_article_version.assign_attributes(attributes) article.save or (has_error = true) end else # Handle articles with attributes already set updated.each do |article| article.save or (has_error = true) end end # Add new articles created.each { |a| a.save or has_error = true } unless created.empty? raise ActiveRecord::Rollback if has_error end !has_error end |
#sync ⇒ Object
7 8 9 10 11 12 13 |
# File 'app/services/supplier_sync_service.rb', line 7 def sync updated, deleted, created = @supplier.sync_from_remote persist(created, deleted, updated) rescue StandardError => e Rails.logger.error("Error syncing supplier #{@supplier.id}: #{e.message}") false end |