Commit 09d97099 by ivan Lan

Add module Shotengai::JoinCatalog

parent 31df0e3c
...@@ -12,6 +12,7 @@ module Shotengai ...@@ -12,6 +12,7 @@ module Shotengai
autoload :Catalog, 'shotengai/catalog' autoload :Catalog, 'shotengai/catalog'
autoload :AASM_DLC, 'shotengai/aasm_dlc' autoload :AASM_DLC, 'shotengai/aasm_dlc'
autoload :WebError, 'shotengai/web_error' autoload :WebError, 'shotengai/web_error'
autoload :JoinCatalog, 'shotengai/join_catalog'
autoload :JsonColumn, 'shotengai/json_column' autoload :JsonColumn, 'shotengai/json_column'
autoload :Harray, 'shotengai/harray' autoload :Harray, 'shotengai/harray'
autoload :Model, 'shotengai/model' autoload :Model, 'shotengai/model'
......
module Shotengai
module JoinCatalog
extend ActiveSupport::Concern
included do
require 'acts-as-taggable-on'
end
class_methods do
def join_catalog_system catalog_class_name, options={}
catalog_class = catalog_class_name.constantize
tag_name = options[:as] || catalog_class.model_name.singular
acts_as_taggable_on tag_name.to_sym
list_name = "#{tag_name}_list".to_sym
class_eval do
# define_method("#{tag_name}_list=") { |value|
# super catalog_class.validate_name_chain(value)
# }
scope "#{list_name}_filter".to_sym, ->(catalogs) {
tags = catalogs && catalogs.try(:tag) || catalogs.any? && catalogs&.map(&:tag)
tags ? tagged_with(tags, on: list_name) : all
}
define_method("#{tag_name}_ids=") { |ids|
send("#{list_name}=", catalog_class.ids_to_tags(ids))
}
define_method("#{tag_name}_ids") {
send(list_name).map(&:id)
}
define_method("#{tag_name}_names") {
send(list_name).map(&:name)
}
define_method(list_name) {
catalog_class.unscope(:order).where(id: super().map { |tag| Shotengai::Catalog.parse_tag(tag) }).order(:nested_level)
}
end
end
end
end
end
\ No newline at end of file
...@@ -28,7 +28,6 @@ module Shotengai ...@@ -28,7 +28,6 @@ module Shotengai
# #
class Product < Shotengai::Model class Product < Shotengai::Model
require 'acts-as-taggable-on'
self.table_name = 'shotengai_products' self.table_name = 'shotengai_products'
harray_accessor :spec_template, :remark_template harray_accessor :spec_template, :remark_template
...@@ -123,41 +122,11 @@ module Shotengai ...@@ -123,41 +122,11 @@ module Shotengai
subclass.has_many :snapshots, class_name: subclass.snapshot_class.name, through: :series, source: :snapshots subclass.has_many :snapshots, class_name: subclass.snapshot_class.name, through: :series, source: :snapshots
end end
# Will get methods: # Will get methods:
# "#{tag_name}_list" tag_name is singular # "#{tag_name}_list" tag_name is singular
# tagger_with('xx', on: "#{tag_name}.to_sym): tag_name is plural # tagger_with('xx', on: "#{tag_name}.to_sym): tag_name is plural
def join_catalog_system catalog_class_name, options={} include JoinCatalog
catalog_class = catalog_class_name.constantize
tag_name = options[:as] || catalog_class.model_name.singular
acts_as_taggable_on tag_name.to_sym
list_name = "#{tag_name}_list".to_sym
class_eval do
# define_method("#{tag_name}_list=") { |value|
# super catalog_class.validate_name_chain(value)
# }
scope "#{list_name}_filter".to_sym, ->(catalogs) {
tags = catalogs && catalogs.try(:tag) || catalogs.any? && catalogs&.map(&:tag)
tags ? tagged_with(tags, on: list_name) : all
}
define_method("#{tag_name}_ids=") { |ids|
send("#{list_name}=", catalog_class.ids_to_tags(ids))
}
define_method("#{tag_name}_ids") {
send(list_name).map(&:id)
}
define_method("#{tag_name}_names") {
send(list_name).map(&:name)
}
define_method(list_name) {
catalog_class.unscope(:order).where(id: super().map { |tag| Shotengai::Catalog.parse_tag(tag) }).order(:nested_level)
}
end
end
end end
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