Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion app/models/channel.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
class Channel < ApplicationRecord
class Channel < NationRecord
has_many :order_infos
has_many :product_options

Expand Down
8 changes: 7 additions & 1 deletion app/models/concerns/parse_country_code.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
module ParseCountryCode
attr_accessor :country_code
def country_code
@country_code ||= 'th'
end

def country_code=(code)
@country_code = (code || 'th').downcase
end
end
8 changes: 6 additions & 2 deletions app/models/country.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,17 @@ class Country < ApplicationRecord
has_many :products

validates_presence_of :name, :name_ko, :locale, :short_name
alias_attribute :code, :short_name

TH = find_by(short_name: :th)
VN = find_by(short_name: :vn)

def self.th
find_by(short_name: 'th')
TH
end

def self.vn
find_by(short_name: 'vn')
VN
end

def self.undef
Expand Down
13 changes: 11 additions & 2 deletions app/models/nation_record.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@ class NationRecord < ApplicationRecord

belongs_to :country, optional: true

default_scope -> { send(country_code.to_sym) }
default_scope -> { send(country_code) }
scope :th, -> { unscoped.includes(:country).where(country: Country.th) }
scope :vn, -> { unscoped.includes(:country).where(country: Country.vn) }
scope :undef, -> { unscoped.includes(:country).where(country: Country.undef) }
scope :at, ->(key) { unscoped.includes(:country).where(country: Country.at(key)) }

after_initialize :callback_set_default_country
before_save :callback_attaching_country

def self.migrate_original_record_to_thai_record
Expand All @@ -17,7 +18,15 @@ def self.migrate_original_record_to_thai_record

private

def callback_set_default_country
self.country_id ||= default_country.id
end

def callback_attaching_country
self.country ||= Country.send(ENV['APP_COUNTRY'])
callback_set_default_country
end

def default_country
@default_country ||= Country.send(self.class.country_code)
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ def self.call
Country.migrate_input_seed_data

national_models = [
Channel,
OrderInfo,
Brand,
Adjustment,
Expand Down
5 changes: 5 additions & 0 deletions db/migrate/channel/20200308091320_add_country_to_channel.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddCountryToChannel < ActiveRecord::Migration[6.0]
def change
add_reference :channels, :country, foreign_key: true
end
end