- Introduced `TranslatableName` module to allow for localized names for `Country` and `Region` models. - Updated views to display `localized_name` instead of `name` for improved internationalization. - Refactored JSON serialization for `translations` attribute. - Enhanced localization support by adding new languages: Japanese and Korean, with updated locale files. - Removed outdated English and Chinese locales for countries and regions to clean up the codebase.
34 lines
777 B
Ruby
34 lines
777 B
Ruby
class Region < ApplicationRecord
|
|
include TranslatableName
|
|
|
|
extend FriendlyId
|
|
friendly_id :name, use: :slugged
|
|
|
|
has_many :countries, dependent: :restrict_with_error
|
|
has_many :cities, through: :countries
|
|
has_many :subregions
|
|
|
|
validates :name, presence: true, uniqueness: true
|
|
validates :code, presence: true, uniqueness: true
|
|
|
|
serialize :translations, coder: JSON
|
|
|
|
def to_s
|
|
name
|
|
end
|
|
|
|
# def localized_name
|
|
# I18n.t("regions.#{code}")
|
|
# end
|
|
|
|
# 模型中允许被搜索的关联
|
|
def self.ransackable_associations(auth_object = nil)
|
|
[ "countries", "cities" ]
|
|
end
|
|
|
|
# 允许被搜索的属性列表
|
|
def self.ransackable_attributes(auth_object = nil)
|
|
[ "code", "created_at", "id", "id_value", "name", "slug", "updated_at" ]
|
|
end
|
|
end
|