Commit 583e7f25 by ivan Lan

Improve catalog about nested_level

parent 528c8c53
...@@ -4,6 +4,7 @@ class CreateShotengaiCatalogs < ActiveRecord::Migration[5.1] ...@@ -4,6 +4,7 @@ class CreateShotengaiCatalogs < ActiveRecord::Migration[5.1]
t.string :name t.string :name
t.string :level_type t.string :level_type
t.string :image t.string :image
t.integer :nested_level
t.json :meta t.json :meta
# STI # STI
t.string :type t.string :type
......
...@@ -7,11 +7,12 @@ module Shotengai ...@@ -7,11 +7,12 @@ module Shotengai
# name :string(255) # name :string(255)
# level_type :string(255) # level_type :string(255)
# image :string(255) # image :string(255)
# meta :json
# type :string(255) # type :string(255)
# super_catalog_id :integer # super_catalog_id :integer
# created_at :datetime not null # created_at :datetime not null
# updated_at :datetime not null # updated_at :datetime not null
# meta :json
# nested_level :integer
# #
# Indexes # Indexes
# #
...@@ -23,6 +24,8 @@ module Shotengai ...@@ -23,6 +24,8 @@ module Shotengai
self.table_name = 'shotengai_catalogs' self.table_name = 'shotengai_catalogs'
validates_presence_of :name validates_presence_of :name
after_save :set_nested_level
class << self class << self
def inherited subclass def inherited subclass
subclass.has_many :sub_catalogs, class_name: subclass.name, foreign_key: :super_catalog_id, dependent: :destroy subclass.has_many :sub_catalogs, class_name: subclass.name, foreign_key: :super_catalog_id, dependent: :destroy
...@@ -53,7 +56,7 @@ module Shotengai ...@@ -53,7 +56,7 @@ module Shotengai
end end
def parse_tag tag def parse_tag tag
tag.split('-').last tag
end end
# def input_from_file # def input_from_file
# end # end
...@@ -66,12 +69,12 @@ module Shotengai ...@@ -66,12 +69,12 @@ module Shotengai
end end
def tag def tag
"#{self.class.name}-#{id}" "#{id}"
end end
def nest_level # def nest_level
ancestors.count # ancestors.count
end # end
def name_chain def name_chain
ancestors.map(&:name) ancestors.map(&:name)
...@@ -93,5 +96,10 @@ module Shotengai ...@@ -93,5 +96,10 @@ module Shotengai
sub_catalogs: sub_catalogs.map(&:tree) sub_catalogs: sub_catalogs.map(&:tree)
} }
end end
private
def set_nested_level
self.update_column(:nested_level, self.ancestors.count)
end
end end
end end
...@@ -144,7 +144,7 @@ module Shotengai ...@@ -144,7 +144,7 @@ module Shotengai
} }
define_method(list_name) { define_method(list_name) {
catalog_class.where(id: super().map { |tag| Shotengai::Catalog.parse_tag(tag) }) catalog_class.where(id: super().map { |tag| Shotengai::Catalog.parse_tag(tag) }).order(:nested_level)
} }
end end
end end
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment