From ad844c345ff62acc11408fd70be3008952bf9a76 Mon Sep 17 00:00:00 2001 From: Bran <40688044+june20516@users.noreply.github.com> Date: Thu, 2 Jul 2020 18:32:50 +0900 Subject: [PATCH 1/2] =?UTF-8?q?=EA=B8=B0=EB=8A=A5=20:=20=ED=86=B5=EA=B3=84?= =?UTF-8?q?=20=EC=BD=98=EC=86=94=EC=9C=BC=EB=A1=9C=20quicksight=20?= =?UTF-8?q?=EC=82=AC=EC=9A=A9=20(#53)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * 기능: 카트아이템에 가격을 박제하기 위한 컬럼 추가 * 수정 : 카트아이템의 price 컬럼들에 prefix 추가, captured 컬럼 추가 https://github.com/gomicorp/GomiStore/issues/507 * 기능 : zombie 메서드 추가, captured price의 data migration 추가 https://github.com/gomicorp/GomiStore/issues/507 * 수정: 데이터마이그레이션과 컬럼마이그레이션의 실행 순서 수정 https://github.com/gomicorp/GomiStore/issues/507 * 기능: CartItem 에 좀비 옵션 기능 추가 및 가격 캡쳐 기능 스토어와 코드베이스 통일 https://github.com/gomicorp/GomiStore/issues/507 Co-authored-by: Yonghyun Kim (Fred) --- app/models/application_record.rb | 33 ++++++++++++++++++ app/models/cart_item.rb | 34 ++++++++++++++++--- ...t_captured_prices_to_ordered_cart_items.rb | 23 +++++++++++++ ...d_price_columns_and_add_column_captured.rb | 17 ++++++++++ ...lumns_to_cart_items_for_captured_prices.rb | 9 +++++ 5 files changed, 111 insertions(+), 5 deletions(-) create mode 100644 db/data/20200630093017_set_captured_prices_to_ordered_cart_items.rb create mode 100644 db/migrate/20200630092452_rename_captured_price_columns_and_add_column_captured.rb create mode 100644 db/migrate/cart_item/20200630080410_add_columns_to_cart_items_for_captured_prices.rb diff --git a/app/models/application_record.rb b/app/models/application_record.rb index bcb0af5a..eabf7ad9 100644 --- a/app/models/application_record.rb +++ b/app/models/application_record.rb @@ -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 diff --git a/app/models/cart_item.rb b/app/models/cart_item.rb index 9a729c61..6624018b 100644 --- a/app/models/cart_item.rb +++ b/app/models/cart_item.rb @@ -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 # 합산 가격 @@ -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 diff --git a/db/data/20200630093017_set_captured_prices_to_ordered_cart_items.rb b/db/data/20200630093017_set_captured_prices_to_ordered_cart_items.rb new file mode 100644 index 00000000..59764394 --- /dev/null +++ b/db/data/20200630093017_set_captured_prices_to_ordered_cart_items.rb @@ -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 diff --git a/db/migrate/20200630092452_rename_captured_price_columns_and_add_column_captured.rb b/db/migrate/20200630092452_rename_captured_price_columns_and_add_column_captured.rb new file mode 100644 index 00000000..915fa131 --- /dev/null +++ b/db/migrate/20200630092452_rename_captured_price_columns_and_add_column_captured.rb @@ -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 diff --git a/db/migrate/cart_item/20200630080410_add_columns_to_cart_items_for_captured_prices.rb b/db/migrate/cart_item/20200630080410_add_columns_to_cart_items_for_captured_prices.rb new file mode 100644 index 00000000..5a778eb2 --- /dev/null +++ b/db/migrate/cart_item/20200630080410_add_columns_to_cart_items_for_captured_prices.rb @@ -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 From 697bf30f8c81520e0c6eef55f0cf4f45bb996599 Mon Sep 17 00:00:00 2001 From: Bran <40688044+june20516@users.noreply.github.com> Date: Fri, 3 Jul 2020 13:36:38 +0900 Subject: [PATCH 2/2] =?UTF-8?q?=EA=B8=B0=EB=8A=A5=20:=20=EB=B2=A0=EC=98=A4?= =?UTF-8?q?=EB=9F=B0=20/=20Order=20Info=EC=97=90=20ordered=5Fat=EC=B6=94?= =?UTF-8?q?=EA=B0=80,=20=EA=B8=B0=EC=A1=B4=20alias=EC=A0=9C=EA=B1=B0=20(#5?= =?UTF-8?q?4)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/models/order_info.rb | 1 - .../20200624093947_set_ordered_at_to_order_info.rb | 11 +++++++++++ ...00624093424_add_column_ordered_at_to_order_info.rb | 5 +++++ 3 files changed, 16 insertions(+), 1 deletion(-) create mode 100644 db/data/20200624093947_set_ordered_at_to_order_info.rb create mode 100644 db/migrate/order_info/20200624093424_add_column_ordered_at_to_order_info.rb diff --git a/app/models/order_info.rb b/app/models/order_info.rb index cb9026c0..42cf8294 100644 --- a/app/models/order_info.rb +++ b/app/models/order_info.rb @@ -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 diff --git a/db/data/20200624093947_set_ordered_at_to_order_info.rb b/db/data/20200624093947_set_ordered_at_to_order_info.rb new file mode 100644 index 00000000..8a1b9018 --- /dev/null +++ b/db/data/20200624093947_set_ordered_at_to_order_info.rb @@ -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 diff --git a/db/migrate/order_info/20200624093424_add_column_ordered_at_to_order_info.rb b/db/migrate/order_info/20200624093424_add_column_ordered_at_to_order_info.rb new file mode 100644 index 00000000..024109a1 --- /dev/null +++ b/db/migrate/order_info/20200624093424_add_column_ordered_at_to_order_info.rb @@ -0,0 +1,5 @@ +class AddColumnOrderedAtToOrderInfo < ActiveRecord::Migration[6.0] + def change + add_column :order_infos, :ordered_at, :datetime + end +end