Skip to content

Commit

Permalink
release 202007-1
Browse files Browse the repository at this point in the history
7월 첫번째 배포
릴리즈 노트 참고
  • Loading branch information
june20516 authored Jul 3, 2020
2 parents 326115e + 697bf30 commit 3e4ae22
Show file tree
Hide file tree
Showing 8 changed files with 127 additions and 6 deletions.
33 changes: 33 additions & 0 deletions app/models/application_record.rb
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,37 @@ def self.line_cover(indent: 3, line_length: 50)
puts "\n\n"
res
end

# 각 모델 클래스에서, 자신의 관계 모델에 대하여 좀비레코드 가능을 명시적으로 주입합니다.
def self.zombie(name)
target_association = self.associations[self.association_names.index(name)]
method_name = "zombie_#{name}"
variable = "#{method_name}"

class_eval <<-METHOD, __FILE__, __LINE__ + 1
def #{method_name}
return @#{variable} if @#{variable}
@#{variable} = self.#{name}
unless @#{variable}
last_version = PaperTrail::Version.where(item_id: #{name}_id, item_type: '#{target_association[:klass].name}').last
@#{variable} = last_version.reify if last_version && last_version.event == 'destroy'
end
@#{variable}
end
METHOD
end

# Zombie Query Method (삭제한 놈도 찾아드림)
def self.zombie_find(id)
find(id)
rescue ActiveRecord::RecordNotFound => e
record = nil
last_version = PaperTrail::Version.where(item_id: id, item_type: name).last
record = last_version.reify if last_version && last_version.event == 'destroy'

return record if record

raise e
end
end
34 changes: 29 additions & 5 deletions app/models/cart_item.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,25 +37,32 @@ class CartItem < ApplicationRecord
# @_barcode_count ||= barcodes.length
# end

zombie :product_option

delegate :page_title, to: :zombie_product_option
delegate :title, to: :zombie_product_option, prefix: :option # option_title
delegate :name, to: :zombie_product_option, prefix: :option # option_name
delegate :thumbnail, to: :product_page, prefix: :page

# 단위 정가 & 합계
delegate :base_price, to: :product_option
delegate :base_price, to: :zombie_product_option
def base_price_sum
option_count * product_option.base_price
option_count * (captured ? captured_base_price : base_price)
end

# 구성 품목 수량 & 합계
# A.K.A. EA (e.g. 10 EA)
# 몇 개의 ProductItem 재고단위로 구성되었는가.
delegate :unit_count, to: :product_option
delegate :unit_count, to: :zombie_product_option
def unit_count_sum
option_count * unit_count
end

# 단위 변동가 & 합계
# 일반적인 경우, 음수값을 가짐
delegate :price_change, to: :product_option
delegate :price_change, to: :zombie_product_option
def price_change_sum
option_count * price_change
option_count * (captured ? captured_price_change : price_change)
end

# 합산 가격
Expand Down Expand Up @@ -124,6 +131,23 @@ def self.item_count(product_item)
all.map { |cart_item| cart_item.item_count(product_item) }.sum
end

def capture_price_fields!
capture_price_fields
save!
end

def capture_price_fields
po = zombie_product_option
tap do |item|
item.captured_base_price = po.base_price
item.captured_discount_price = po.discount_price
item.captured_additional_price = po.additional_price
item.captured_price_change = po.price_change
item.captured_retail_price = po.retail_price
item.captured = true
end
end

# def _barcode_count
# @_barcode_count ||= barcodes.length
# end
Expand Down
1 change: 0 additions & 1 deletion app/models/order_info.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ class OrderInfo < NationRecord

delegate :order_status, to: :cart
# alias_attribute :status, :order_status
alias_attribute :ordered_at, :created_at
delegate :delivery_amount, to: :ship_info, allow_nil: true
delegate :amount, to: :payment, allow_nil: true
delegate :pay_method, to: :payment, allow_nil: true
Expand Down
11 changes: 11 additions & 0 deletions db/data/20200624093947_set_ordered_at_to_order_info.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class SetOrderedAtToOrderInfo < ActiveRecord::Migration[6.0]
def up
ApplicationRecord.country_context_with 'global' do
OrderInfo.update_all('ordered_at=created_at')
end
end

def down
raise ActiveRecord::IrreversibleMigration
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
class SetCapturedPricesToOrderedCartItems < ActiveRecord::Migration[6.0]
def up
ApplicationRecord.country_context_with 'global' do
OrderInfo.all.each do |order|
order.items.each do |cart_item|
timely_option = cart_item.product_option&.version_at(order.created_at) || ProductOption.zombie_find(cart_item.product_option_id)&.version_at(order.created_at)
cart_item.update(
captured_base_price: timely_option.base_price,
captured_discount_price: timely_option.discount_price,
captured_additional_price: timely_option.additional_price,
captured_retail_price: timely_option.retail_price,
captured_price_change: timely_option.price_change,
captured: true
) if timely_option
end
end
end
end

def down
raise ActiveRecord::IrreversibleMigration
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
class RenameCapturedPriceColumnsAndAddColumnCaptured < ActiveRecord::Migration[6.0]
def change
add_prefix_to_captured_price_columns

add_column :cart_items, :captured, :boolean, null: false, default: false
end

private

def add_prefix_to_captured_price_columns
rename_column :cart_items, :base_price, :captured_base_price
rename_column :cart_items, :discount_price, :captured_discount_price
rename_column :cart_items, :additional_price, :captured_additional_price
rename_column :cart_items, :retail_price, :captured_retail_price
rename_column :cart_items, :price_change, :captured_price_change
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class AddColumnsToCartItemsForCapturedPrices < ActiveRecord::Migration[6.0]
def change
add_column :cart_items, :base_price, :integer, null: false, default: 0
add_column :cart_items, :discount_price, :integer, null: false, default: 0
add_column :cart_items, :additional_price, :integer, null: false, default: 0
add_column :cart_items, :retail_price, :integer, null: false, default: 0
add_column :cart_items, :price_change, :integer, null: false, default: 0
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddColumnOrderedAtToOrderInfo < ActiveRecord::Migration[6.0]
def change
add_column :order_infos, :ordered_at, :datetime
end
end

0 comments on commit 3e4ae22

Please sign in to comment.