Class: CatalogItem::PriceSync
- Inherits:
-
Object
- Object
- CatalogItem::PriceSync
- 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
-
#catalog_items ⇒ Object
readonly
Returns the value of attribute catalog_items.
-
#last_results ⇒ Object
readonly
Returns the value of attribute last_results.
-
#preloaded_parent_catalog_items ⇒ Object
readonly
Returns the value of attribute preloaded_parent_catalog_items.
Class Method Summary collapse
-
.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.
Instance Method Summary collapse
- #discount_to_apply(catalog_item) ⇒ Object
-
#initialize(catalog_items, options = {}) ⇒ PriceSync
constructor
catalog_items: a collection of catalog items to sync options: :do_not_preload, do not perform any filtering, joiing or preloading on catalog item, assumes it is passed with everything needed.
- #parent_catalog_item_amount(catalog_item) ⇒ Object
- #preload_parent_catalog_item ⇒ Object
- #record_discounted_price(catalog_item, price, discount_to_apply = 1.0) ⇒ Object
-
#sync_all_catalog_items(options = {}) ⇒ Object
Goes through each catalog item, sync price and saves.
-
#sync_price_from_parent(catalog_item) ⇒ Object
Sync price from the parent catalog item if any, pass in the target catalog_item.
Constructor Details
#initialize(catalog_items, options = {}) ⇒ PriceSync
catalog_items: a collection of catalog items to sync
options:
:do_not_preload, do not perform any filtering, joiing or preloading on catalog item, assumes it is passed with everything needed
18 19 20 21 22 23 24 25 26 27 |
# File 'app/services/catalog_item/price_sync.rb', line 18 def initialize(catalog_items, ={}) # Filter the list to catalog items whose catalog have a parent catalog @catalog_items = catalog_items unless [:do_not_preload] @catalog_items = @catalog_items. joins(catalog: :parent_catalog). includes(catalog: :parent_catalog) end @last_results = {} end |
Instance Attribute Details
#catalog_items ⇒ Object (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_results ⇒ Object (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_items ⇒ Object (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
11 12 13 |
# File 'app/services/catalog_item/price_sync.rb', line 11 def self.initialize_for_catalog(catalog, ={}) new(catalog.catalog_items,) end |
Instance Method Details
#discount_to_apply(catalog_item) ⇒ Object
49 50 51 |
# File 'app/services/catalog_item/price_sync.rb', line 49 def discount_to_apply(catalog_item) catalog_item.parent_catalog_discount || 0.0 end |
#parent_catalog_item_amount(catalog_item) ⇒ Object
57 58 59 60 61 |
# File 'app/services/catalog_item/price_sync.rb', line 57 def parent_catalog_item_amount(catalog_item) source_catalog_item = catalog_item.parent_catalog_item fail 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_item ⇒ Object
53 54 55 |
# File 'app/services/catalog_item/price_sync.rb', line 53 def preload_parent_catalog_item @preloaded_parent_catalog_items = {} end |
#record_discounted_price(catalog_item, price, discount_to_apply = 1.0) ⇒ Object
79 80 81 82 |
# File 'app/services/catalog_item/price_sync.rb', line 79 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 = {}) ⇒ Object
Goes through each catalog item, sync price and saves
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'app/services/catalog_item/price_sync.rb', line 30 def sync_all_catalog_items(={}) skip_save = [: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 else if 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. end 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
65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'app/services/catalog_item/price_sync.rb', line 65 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 |