Skip to content

Commit

Permalink
release 202006-1 | Merge pull request #52 from gomicorp/develop
Browse files Browse the repository at this point in the history
* 기능: Brand 와 OrderInfo 가 너무 멀어서 매개테이블 추가
* 기능: Brand 와 ProductOption 이 너무 멀어서 매개테이블 추가
  • Loading branch information
yhk1038 authored Jun 18, 2020
2 parents 5e7755a + 6e7182a commit 326115e
Show file tree
Hide file tree
Showing 17 changed files with 153 additions and 7 deletions.
8 changes: 8 additions & 0 deletions app/models/brand.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,14 @@ class Brand < NationRecord
has_many :items, through: :product_item_groups, class_name: :ProductItem
has_many :managers, through: :company

# ===============================================
has_many :product_option_brands, class_name: 'ProductOptionBrand', dependent: :delete_all
has_many :product_options, through: :product_option_brands
# ===============================================
has_many :order_info_brands, class_name: 'OrderInfoBrand', dependent: :delete_all
has_many :order_infos, through: :order_info_brands
# ===============================================

def official_site_url
'#'
end
Expand Down
12 changes: 9 additions & 3 deletions app/models/country.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ class Country < ApplicationRecord
validates_presence_of :name, :name_ko, :locale, :short_name
alias_attribute :code, :short_name

TH = find_or_create_by(name: 'thailand', name_ko: '태국', locale: 'th', short_name: :th)
VN = find_or_create_by(name: 'vietnam', name_ko: '베트남', locale: 'vi', short_name: :vn)
TH = find_or_create_by(name: 'thailand', name_ko: '태국', locale: 'th', short_name: 'th', iso_code: 'THB')
VN = find_or_create_by(name: 'vietnam', name_ko: '베트남', locale: 'vi', short_name: 'vn', iso_code: 'VND')
KO = find_or_create_by(name: 'korea', name_ko: '한국', locale: 'ko', short_name: 'ko', iso_code: 'KRW')

def self.th
TH
Expand All @@ -17,6 +18,10 @@ def self.vn
VN
end

def self.ko
KO
end

def self.undef
where(short_name: nil)
end
Expand All @@ -42,7 +47,8 @@ def self.migrate_input_seed_data
def self.seed_data
[
{ name: 'vietnam', name_ko: '베트남', locale: 'vi', short_name: 'vn' },
{ name: 'thailand', name_ko: '태국', locale: 'th', short_name: 'th' }
{ name: 'thailand', name_ko: '태국', locale: 'th', short_name: 'th' },
{ name: 'korea', name_ko: '한국', locale: 'ko', short_name: 'ko' }
]
end
end
8 changes: 7 additions & 1 deletion app/models/order_info.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,15 @@ class OrderInfo < NationRecord
has_one :ship_info, dependent: :destroy
has_one :payment, dependent: :destroy
has_one :user, through: :cart
has_many :cart_items, through: :cart
has_many :items, through: :cart
has_many :product_options, through: :items, source: :product_option
has_many :adjustments, class_name: 'Adjustment'

# ===============================================
has_many :order_info_brands, class_name: 'OrderInfoBrand', dependent: :delete_all
has_many :brands, through: :order_info_brands
# ===============================================

validates_presence_of :cart_id, :enc_id
validates_uniqueness_of :cart_id, :enc_id

Expand Down
6 changes: 6 additions & 0 deletions app/models/order_info_brand.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class OrderInfoBrand < ApplicationRecord
belongs_to :order_info, class_name: 'OrderInfo'
belongs_to :brand, class_name: 'Brand'

validates_uniqueness_of :order_info_id, scope: :brand_id
end
5 changes: 5 additions & 0 deletions app/models/product_option.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ class ProductOption < ApplicationRecord
has_one :product, foreign_key: :default_option_id, dependent: :nullify
has_one :product_page, class_name: 'Product', through: :option_group, source: :product

# ===============================================
has_many :product_option_brands, class_name: 'ProductOptionBrand', dependent: :delete_all
has_many :brands, through: :product_option_brands
# ===============================================

enum discount_type: %i[no const ratio]

scope :active, -> { where(is_active: true) }
Expand Down
6 changes: 6 additions & 0 deletions app/models/product_option_brand.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class ProductOptionBrand < ApplicationRecord
belongs_to :product_option, class_name: 'ProductOption'
belongs_to :brand, class_name: 'Brand'

validates_uniqueness_of :product_option_id, scope: :brand_id
end
8 changes: 8 additions & 0 deletions app/models/product_option_bridge.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,14 @@ def items
end
end

def brands
case connectable_type
when 'ProductItem'
[] << connectable.brand
when 'ProductCollection'
connectable.brands.uniq
end
end

## ===== before calculator =====

Expand Down
3 changes: 0 additions & 3 deletions db/data/20200519052212_set_countries_data_to_cart.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,11 @@ class SetCountriesDataToCart < ActiveRecord::Migration[6.0]
def up
ApplicationRecord.country_context_with 'global' do
default_country = Country.th

Cart.unscoped.all.each do |cart|
country_source = cart.order_info
cart_item = cart.items.first

country_source ||= cart_item&.product_option&.channel
country_source ||= cart_item&.product_item_barcodes&.first&.product_item

if country_source
cart.update!(country: country_source.country)
else
Expand Down
17 changes: 17 additions & 0 deletions db/data/20200604021223_set_product_option_brands.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
class SetProductOptionBrands < ActiveRecord::Migration[6.0]
def up
ApplicationRecord.country_context_with 'global' do
ProductOption.all.each do |option|
option.bridges.each do |bridge|
bridge.brands.each do |brand|
ProductOptionBrand.create(product_option: option, brand: brand)
end
end
end
end
end

def down
raise ActiveRecord::IrreversibleMigration if Rails.production?
end
end
26 changes: 26 additions & 0 deletions db/data/20200605013634_set_order_info_brands.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
class SetOrderInfoBrands < ActiveRecord::Migration[6.0]
def up
ApplicationRecord.country_context_with 'global' do
OrderInfo.all.each do |order_info|
order_info.product_options.each do |option|
option.bridges.each do |bridge|
bridge.brands.each do |brand|
OrderInfoBrand.create(order_info: order_info, brand: brand)
end
end
end

# 정상적인 로직으로 데이터를 채워줄 수 없는 레거시 데이터에 대한 대응
order_info.items.each do |item|
item.barcodes.each do |barcode|
OrderInfoBrand.create(order_info: order_info, brand: barcode.product.brand)
end
end
end
end
end

def down
# raise ActiveRecord::IrreversibleMigration if Rails.production?
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddUniqueToCartIdForOrderInfo < ActiveRecord::Migration[6.0]
def change
add_index :order_infos, :cart_id, unique: true
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
class CreateOrderInfoBrands < ActiveRecord::Migration[6.0]
def change
create_table :order_info_brands do |t|
t.references :order_info, null: false, foreign_key: true
t.references :brand, null: false, foreign_key: true

t.timestamps
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
class CreateProductOptionBrands < ActiveRecord::Migration[6.0]
def change
create_table :product_option_brands do |t|
t.references :product_option, null: false, foreign_key: true
t.references :brand, null: false, foreign_key: true

t.timestamps
end
end
end
11 changes: 11 additions & 0 deletions test/fixtures/order_info_brands.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Read about fixtures at https://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html

# This model initially had no columns defined. If you add columns to the
# model remove the '{}' from the fixture names and add the columns immediately
# below each fixture, per the syntax in the comments below
#
one: {}
# column: value
#
two: {}
# column: value
11 changes: 11 additions & 0 deletions test/fixtures/product_option_brands.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Read about fixtures at https://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html

# This model initially had no columns defined. If you add columns to the
# model remove the '{}' from the fixture names and add the columns immediately
# below each fixture, per the syntax in the comments below
#
one: {}
# column: value
#
two: {}
# column: value
7 changes: 7 additions & 0 deletions test/models/order_info_brand_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
require 'test_helper'

class OrderInfoBrandTest < ActiveSupport::TestCase
# test "the truth" do
# assert true
# end
end
7 changes: 7 additions & 0 deletions test/models/product_option_brand_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
require 'test_helper'

class ProductOptionBrandTest < ActiveSupport::TestCase
# test "the truth" do
# assert true
# end
end

0 comments on commit 326115e

Please sign in to comment.