Class: CatalogItem::Remapper
- Inherits:
-
Object
- Object
- CatalogItem::Remapper
- Defined in:
- app/services/catalog_item/remapper.rb
Overview
Remaps line items in an itemizable from one catalog to another
Defined Under Namespace
Classes: CatalogItemMissing
Instance Attribute Summary collapse
-
#itemizable ⇒ Object
readonly
Returns the value of attribute itemizable.
-
#logger ⇒ Object
readonly
Returns the value of attribute logger.
-
#results ⇒ Object
readonly
Returns the value of attribute results.
-
#strict_mode ⇒ Object
readonly
Returns the value of attribute strict_mode.
Instance Method Summary collapse
- #fix_all ⇒ Object
- #fix_associated_records ⇒ Object
- #fix_catalog ⇒ Object
-
#initialize(itemizable, options = {}) ⇒ Remapper
constructor
A new instance of Remapper.
- #invalid_line_items(itemizable) ⇒ Object
Constructor Details
#initialize(itemizable, options = {}) ⇒ Remapper
Returns a new instance of Remapper.
8 9 10 11 12 13 14 15 16 |
# File 'app/services/catalog_item/remapper.rb', line 8 def initialize(itemizable, ={}) @itemizable = itemizable @options = @logger = [:logger] || Rails.logger raise "Customer or catalog nil." if @itemizable.customer.nil? or @itemizable.customer.catalog_id.nil? @valid_catalog = [:target_catalog] || @itemizable.customer.catalog @results = [] @strict_mode = [:strict_mode] # Blow up if there's an error end |
Instance Attribute Details
#itemizable ⇒ Object (readonly)
Returns the value of attribute itemizable.
6 7 8 |
# File 'app/services/catalog_item/remapper.rb', line 6 def itemizable @itemizable end |
#logger ⇒ Object (readonly)
Returns the value of attribute logger.
6 7 8 |
# File 'app/services/catalog_item/remapper.rb', line 6 def logger @logger end |
#results ⇒ Object (readonly)
Returns the value of attribute results.
6 7 8 |
# File 'app/services/catalog_item/remapper.rb', line 6 def results @results end |
#strict_mode ⇒ Object (readonly)
Returns the value of attribute strict_mode.
6 7 8 |
# File 'app/services/catalog_item/remapper.rb', line 6 def strict_mode @strict_mode end |
Instance Method Details
#fix_all ⇒ Object
60 61 62 |
# File 'app/services/catalog_item/remapper.rb', line 60 def fix_all fix_catalog + fix_associated_records end |
#fix_associated_records ⇒ Object
56 57 58 |
# File 'app/services/catalog_item/remapper.rb', line 56 def fix_associated_records (@itemizable.try(:room_configurations) || []).map{|rc| self.class.new(rc).fix_catalog }.flatten end |
#fix_catalog ⇒ Object
22 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 |
# File 'app/services/catalog_item/remapper.rb', line 22 def fix_catalog @results = [] logger.info "Fix_catalog: called on #{@itemizable.class.name} #{@itemizable.id}" line_items_to_fix = invalid_line_items(@itemizable).to_a return @results unless line_items_to_fix.present? || @itemizable.currency != @itemizable.customer.store.currency @itemizable.transaction do @itemizable.currency = @itemizable.customer.store.currency line_items_to_fix.each do |li| logger.info "Fix_catalog: line item id #{li.id} sku #{li.sku} has invalid catalog: #{li.catalog_item.catalog_id} but should be #{@valid_catalog.id}" remapped_catalog_item = @valid_catalog.catalog_items.active.by_skus(li.sku).first if remapped_catalog_item old_catalog_item_id = li.catalog_item_id li.catalog_item = remapped_catalog_item li.price = remapped_catalog_item.amount li.save! unless @options[:skip_save] @results << "LineItem:#{li.id} was updated from catalog_item_id: #{old_catalog_item_id} to #{remapped_catalog_item.id}" else msg = "LineItem: #{li.id}, item: #{li.sku} is not available in new catalog #{@valid_catalog.id}" if strict_mode raise CatalogItemMissing.new(msg) end @results << "#{msg} and was removed" li.destroy #will still save in audit trail if need be #raise " Missing catalog item for sku #{li.sku} in catalog id #{@valid_catalog.id}" end end @itemizable.try(:set_for_recalc) @itemizable.save! unless @options[:skip_save] end @itemizable.reload unless @options[:skip_save] @results end |
#invalid_line_items(itemizable) ⇒ Object
18 19 20 |
# File 'app/services/catalog_item/remapper.rb', line 18 def invalid_line_items(itemizable) itemizable.line_items.non_shipping.joins(:catalog_item).where(CatalogItem[:catalog_id].not_eq(@valid_catalog.id)) end |