Class: ShopTransactionModelMigration

Inherits:
Migration
  • Object
show all
Defined in:
lib/migrations/03_shop_transaction_model.rb

Class Method Summary collapse

Class Method Details

.down(site) ⇒ Object



48
49
50
51
# File 'lib/migrations/03_shop_transaction_model.rb', line 48

def self.down(site)
  site.transactions.destroy
  site.transaction_pages.destroy
end

.up(site) ⇒ Object



2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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
# File 'lib/migrations/03_shop_transaction_model.rb', line 2

def self.up(site)
  site.records.create_model :transactions do |transactions|
    add_one   :cart
    add_field :product_total, :decimal, default: '0.0', validations: {required: {}}
    add_field :shipping_total, :decimal, default: '0.0', validations: {required: {}}
    add_field :tax_total, :decimal, default: '0.0', validations: {required: {}}
    add_field :total, :function, fn: 'sum(product_total, shipping_total, tax_total)'
    add_field :created_at, :time
    
    add_embed_one :shipping_address do
      add_field :name, :string
      add_field :address, :string
      add_field :city, :string
      add_field :state, :string
      add_field :country, :string
      add_field :postcode, :string
      add_field :phone, :string
      add_field :email, :string
    end
    
    add_embed_one :billing_address do
      add_field :name, :string
      add_field :address, :string
      add_field :city, :string
      add_field :state, :string
      add_field :country, :string
      add_field :postcode, :string
      add_field :phone, :string
      add_field :email, :string
    end
    
    add_field :payment_reference, :string
    
    # permissions
    users_group = site.groups['Users'].id
    transactions.view_group = users_group
    transactions.create_group = users_group
    transactions.update_group = users_group
    transactions.delete_group = users_group
  end
  
  site.pages.create_model :transaction_pages do |transaction_pages|
    transaction_pages.record_class_name = 'TransactionPage'
  end
end