Commit cc41041e by ivan Lan

Change product column :need_express to :express_way

parent 6421bb70
......@@ -11,7 +11,7 @@ class CreateShotengaiProductsAndOrders < ActiveRecord::Migration[5.1]
t.string :title
t.string :status
t.integer :default_series_id
t.boolean :need_express
t.integer :express_way
t.boolean :need_time_attr
t.string :cover_image
t.json :banners
......
......@@ -36,7 +36,7 @@ FactoryGirl.define do
}
}
# default_series_id ''
need_express true
express_way 'no_need'
# need_time_attr true
cover_image 'cover_image.image'
banners { [ 'image1', 'image2' ] }
......
json.extract! product, :id, :title, :status, :status_zh,
:need_express, :need_time_attr,
:express_way, :need_time_attr,
:cover_image, :banners, :detail, :meta
json.spec product.spec_output
# TODO: NOTE: catalog_list is only vaild in the template example
......
json.extract! product, :id, :title, :status, :status_zh,
:need_express, :cover_image
:express_way, :cover_image
json.catalog_list product.catalog_list if product.respond_to?(:catalog_list)
json.default_series product.default_series, partial: 'shotengai/share/series_simple', as: :series
......@@ -18,6 +18,9 @@ module Shotengai
# The view template dir
# respond_with @products, template: "#{self.class.template_dir}/index"
# Use method instead of instance variable in order to
# let the value could be delegated to superclass when you do not set the subclass value.
# 确保可继承 且 多个子类间不影响(类变量会出现)
def template_dir= template_dir
class_eval %Q{
def add_template_dir
......
......@@ -70,7 +70,7 @@ module Shotengai
# NOTE: :catalog_list is a default catalog list for template example, maybe should move it to the template controller, but it need add controller template for every controller
params.require(resource_key).permit(
:title, :default_series_id,
:need_express, :need_time_attr, :cover_image, catalog_ids: [],
:express_way, :need_time_attr, :cover_image, catalog_ids: [],
banners: [],
# spec_template: [:key, val: []],
# remark_template: [:key, :val],
......
......@@ -8,7 +8,6 @@ module Shotengai
# status :string(255)
# spec_template :json
# default_series_id :integer
# need_express :boolean
# need_time_attr :boolean
# cover_image :string(255)
# banners :json
......@@ -20,6 +19,7 @@ module Shotengai
# created_at :datetime not null
# updated_at :datetime not null
# remark_template :json
# express_way :integer
#
# Indexes
#
......@@ -70,6 +70,14 @@ module Shotengai
}[ status.to_sym ]
end
enum express_way: {
no_need: 0,
delivery: 1,
pick_up: 2,
both: 3,
custom: 4
}
def default_series
Shotengai::Series.alive.find_by_id(default_series_id) || series.alive.first
end
......
module Shotengai
module Taggable
extend ActiveSupport::Concern
included do
has_many :taggings, -> { order(order: :asc)}, as: :tagged
end
class_methods do
def join_catalog_system catalog_class_name, self_as: nil
catalog_class = catalog_class_name.constantize
raise ArgumentError.new('Wrong catalog class required.') unless Shotengai::Catalog === catalog_class
self_as ||= self.class_name.model_name.collection
# catalog_class.class_eval %Q{
# has_many :#{self_as}, class_name:
# }
catalog_class_collection = 'catalogs'
# 交集
self.scope :catalogs_intersection, -> (catalogs){
join(:taggings).where(taggings: { catalog_id: catalogs.map(&:id) })
}
# 并集
self.scope :catalogs_union, -> (catalogs){
join(:taggings).where(taggings: { catalog_id: catalogs.map(&:id) }).distant
}
end
def tagged_with class_name, as: nil
# Let target class to has_many :taggings
tag = class_name.constantize
ArgumentError.new("#{class_name} do not inherit from ActiveRecord::Base") unless ActiveRecord::Base === tag
tag.class_eval do
has_many :taggings, as: :tag
end
as ||= class_name.downcase.pluralize
# Query with tag
# Use tag_ids to avoid another querying
self.scope "have_#{as}".to_sym, ->(tag_ids) { joins(:taggings).where(taggings: { tag_type: tag.base_class.name, tag_id: tag_ids } ).distinct }
# Add update method
self.class_eval %Q{
def #{as}
#{tag}.joins(:taggings).where(taggings: { tagged: self }).distinct
end
def #{as}_tags
Tagging.where(tagged: self, tag_type: #{tag.base_class.name})
end
# Update by id array
def #{as}_ids= id_ary
ActiveRecord::Base.transaction do
#{as}_tags.destroy_all
Array(id_ary).each_with_index do |id, i|
#{as}_tags.create!(tag: #{tag}.find(id), order: i)
end
end
end
# Update by tags array
def #{as}= tags
ActiveRecord::Base.transaction do
#{as}_tags.destroy_all
Array(tags).each_with_index do |tag, i|
#{as}_tags.create!(tag: tag, order: i)
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