Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
S
shotengai
Project
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
open-source
shotengai
Commits
583e7f25
Commit
583e7f25
authored
Oct 05, 2017
by
ivan Lan
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Improve catalog about nested_level
parent
528c8c53
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
16 additions
and
7 deletions
+16
-7
2_create_shotengai_catalogs.rb
db/migrate/2_create_shotengai_catalogs.rb
+1
-0
catalog.rb
lib/shotengai/catalog.rb
+14
-6
product.rb
lib/shotengai/product.rb
+1
-1
No files found.
db/migrate/2_create_shotengai_catalogs.rb
View file @
583e7f25
...
...
@@ -4,6 +4,7 @@ class CreateShotengaiCatalogs < ActiveRecord::Migration[5.1]
t
.
string
:name
t
.
string
:level_type
t
.
string
:image
t
.
integer
:nested_level
t
.
json
:meta
# STI
t
.
string
:type
...
...
lib/shotengai/catalog.rb
View file @
583e7f25
...
...
@@ -7,11 +7,12 @@ module Shotengai
# name :string(255)
# level_type :string(255)
# image :string(255)
# meta :json
# type :string(255)
# super_catalog_id :integer
# created_at :datetime not null
# updated_at :datetime not null
# meta :json
# nested_level :integer
#
# Indexes
#
...
...
@@ -23,6 +24,8 @@ module Shotengai
self
.
table_name
=
'shotengai_catalogs'
validates_presence_of
:name
after_save
:set_nested_level
class
<<
self
def
inherited
subclass
subclass
.
has_many
:sub_catalogs
,
class_name:
subclass
.
name
,
foreign_key: :super_catalog_id
,
dependent: :destroy
...
...
@@ -53,7 +56,7 @@ module Shotengai
end
def
parse_tag
tag
tag
.
split
(
'-'
).
last
tag
end
# def input_from_file
# end
...
...
@@ -66,12 +69,12 @@ module Shotengai
end
def
tag
"
#{
self
.
class
.
name
}
-
#{
id
}
"
"
#{
id
}
"
end
def
nest_level
ancestors
.
count
end
#
def nest_level
#
ancestors.count
#
end
def
name_chain
ancestors
.
map
(
&
:name
)
...
...
@@ -93,5 +96,10 @@ module Shotengai
sub_catalogs:
sub_catalogs
.
map
(
&
:tree
)
}
end
private
def
set_nested_level
self
.
update_column
(
:nested_level
,
self
.
ancestors
.
count
)
end
end
end
lib/shotengai/product.rb
View file @
583e7f25
...
...
@@ -144,7 +144,7 @@ module Shotengai
}
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
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment