Class: ShopDiscountModelMigration

Inherits:
Migration
  • Object
show all
Defined in:
lib/migrations/11_shop_discount_model.rb

Class Method Summary collapse

Class Method Details

.down(site) ⇒ Object



13
14
15
16
17
18
# File 'lib/migrations/11_shop_discount_model.rb', line 13

def self.down(site)
  site.discounts.destroy
  site.carts.modify do |carts|
    remove_field :discounts
  end
end

.up(site) ⇒ Object



2
3
4
5
6
7
8
9
10
11
# File 'lib/migrations/11_shop_discount_model.rb', line 2

def self.up(site)
  site.records.create_model :discounts do |discounts|
    add_field :amount, :integer, validations: {required: {}}, default: '0.0'
  end
  
  site.carts.modify do |carts|
    add_many :discounts
    modify_field :total_cost, fn: 'subtract(product_holds.sum(total_cost), discounts.sum(amount))'
  end
end