Class: Spree::PriceMigrator

Inherits:
Object
  • Object
show all
Defined in:
lib/spree/core/price_migrator.rb

Overview

This class performs a data migration. It's usually run from the `solidus:migrations:create_vat_prices` rake task.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(variant) ⇒ PriceMigrator

Returns a new instance of PriceMigrator



19
20
21
# File 'lib/spree/core/price_migrator.rb', line 19

def initialize(variant)
  @variant = variant
end

Instance Attribute Details

#variantObject (readonly)

Returns the value of attribute variant



17
18
19
# File 'lib/spree/core/price_migrator.rb', line 17

def variant
  @variant
end

Class Method Details

.migrate_default_vat_pricesObject

Migrate all variant's prices.



6
7
8
9
10
11
12
13
14
15
# File 'lib/spree/core/price_migrator.rb', line 6

def self.migrate_default_vat_prices
  # We need to tag the exisiting prices as "default", so that the VatPriceGenerator knows
  # that they include the default zone's VAT.
  Spree::Config.admin_vat_country_iso = Spree::Zone.default_tax.countries.first.iso
  Spree::Variant.find_each do |variant|
    new(variant).migrate_vat_prices
  end
  # This line stops all weird code paths that generate refunds from happening.
  Spree::Zone.default_tax.update(default_tax: false)
end

Instance Method Details

#migrate_vat_pricesObject



23
24
25
26
27
28
# File 'lib/spree/core/price_migrator.rb', line 23

def migrate_vat_prices
  # With a default tax zone, all prices include VAT by default. Let's tell them which one!
  variant.prices.update_all(country_iso: Spree::Config.admin_vat_country_iso)
  Spree::Variant::VatPriceGenerator.new(variant).run
  variant.save
end