Class: CatalogItem::PriceSync

Inherits:
Object
  • Object
show all
Defined in:
app/services/catalog_item/price_sync.rb

Overview

Class responsible for synchronizing catalog items prices to a parent catalog

Defined Under Namespace

Classes: MissingParentCatalogItem

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(catalog_items, options = {}) ⇒ PriceSync

catalog_items: a collection of catalog items to sync

Parameters:

  • catalog_items (ActiveRecord::Relation)

    catalog items to sync

  • options (Hash) (defaults to: {})

    sync options

Options Hash (options):

  • do_not_preload (Boolean)

    do not perform any filtering, joining or preloading on catalog item, assumes it is passed with everything needed



22
23
24
25
26
27
28
29
30
31
# File 'app/services/catalog_item/price_sync.rb', line 22

def initialize(catalog_items, options = {})
  # Filter the list to catalog items whose catalog have a parent catalog
  @catalog_items = catalog_items
  unless options[:do_not_preload]
    @catalog_items = @catalog_items
                     .joins(catalog: :parent_catalog)
                     .includes(catalog: :parent_catalog)
  end
  @last_results = {}
end

Instance Attribute Details

#catalog_itemsObject (readonly)

Returns the value of attribute catalog_items.



4
5
6
# File 'app/services/catalog_item/price_sync.rb', line 4

def catalog_items
  @catalog_items
end

#last_resultsObject (readonly)

Returns the value of attribute last_results.



4
5
6
# File 'app/services/catalog_item/price_sync.rb', line 4

def last_results
  @last_results
end

#preloaded_parent_catalog_itemsObject (readonly)

Returns the value of attribute preloaded_parent_catalog_items.



4
5
6
# File 'app/services/catalog_item/price_sync.rb', line 4

def preloaded_parent_catalog_items
  @preloaded_parent_catalog_items
end

Class Method Details

.initialize_for_catalog(catalog, options = {}) ⇒ Object

Instantiate a price sync for a target catalog which contains catalog items
we want to sync price from their parent catalog

Parameters:

  • catalog (Catalog)

    the target catalog

  • options (Hash) (defaults to: {})

    sync options (see #initialize)

Options Hash (options):

  • do_not_preload (Boolean)

    skip filtering/joining/preloading, assumes catalog items come fully loaded



14
15
16
# File 'app/services/catalog_item/price_sync.rb', line 14

def self.initialize_for_catalog(catalog, options = {})
  new(catalog.catalog_items, options)
end

Instance Method Details

#discount_to_apply(catalog_item) ⇒ Object



54
55
56
# File 'app/services/catalog_item/price_sync.rb', line 54

def discount_to_apply(catalog_item)
  catalog_item.parent_catalog_discount || 0.0
end

#parent_catalog_item_amount(catalog_item) ⇒ Object



62
63
64
65
66
67
# File 'app/services/catalog_item/price_sync.rb', line 62

def parent_catalog_item_amount(catalog_item)
  source_catalog_item = catalog_item.parent_catalog_item
  raise MissingParentCatalogItem, "Catalog Item #{catalog_item.id} does not have a parent source catalog item" unless source_catalog_item

  source_catalog_item.amount
end

#preload_parent_catalog_itemObject



58
59
60
# File 'app/services/catalog_item/price_sync.rb', line 58

def preload_parent_catalog_item
  @preloaded_parent_catalog_items = {}
end

#record_discounted_price(catalog_item, price, discount_to_apply = 1.0) ⇒ Object



85
86
87
88
# File 'app/services/catalog_item/price_sync.rb', line 85

def record_discounted_price(catalog_item, price, discount_to_apply = 1.0)
  target_amount = price * (1.0 - discount_to_apply)
  catalog_item.amount = target_amount
end

#sync_all_catalog_items(options = {}) ⇒ Hash

Goes through each catalog item, sync price and saves

Parameters:

  • options (Hash) (defaults to: {})

    sync options

Options Hash (options):

  • skip_save (Boolean)

    sync prices without saving the catalog items

Returns:

  • (Hash)

    last results keyed by catalog item id



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'app/services/catalog_item/price_sync.rb', line 37

def sync_all_catalog_items(options = {})
  skip_save = options[:skip_save]
  @last_results = {}
  @catalog_items.each do |catalog_item|
    sync_price_from_parent(catalog_item)
    if skip_save
      @last_results[catalog_item.id][:save] = :skip
    elsif catalog_item.save
      @last_results[catalog_item.id][:save] = :success
    else
      @last_results[catalog_item.id][:save] = :error
      @last_results[catalog_item.id][:errors] = catalog_item.errors.full_messages
    end
  end
  last_results
end

#sync_price_from_parent(catalog_item) ⇒ Object

Sync price from the parent catalog item if any, pass in the target
catalog_item



71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'app/services/catalog_item/price_sync.rb', line 71

def sync_price_from_parent(catalog_item)
  parent_amount = parent_catalog_item_amount(catalog_item)
  discount = discount_to_apply(catalog_item)
  record_discounted_price(catalog_item, parent_amount, discount)
  @last_results[catalog_item.id] = { name: catalog_item.to_s,
                                     catalog: catalog_item.catalog.to_s,
                                     was: catalog_item.amount_was.to_f.round(2),
                                     is: catalog_item.amount.to_f.round(2),
                                     changed: catalog_item.amount_changed?,
                                     discount: discount.to_f.round(2) }

  catalog_item
end