Commit bd678c51 by ivan Lan

Add default_query to Shotengai::Controller::Base & Fix foreign_key

parent e97b8d05
......@@ -46,7 +46,7 @@ module Shotengai
self.model_name.singular.to_sym,
class_name: self.name,
optional: true,
foreign_key: :shotengai_orders_id
foreign_key: :shotengai_order_id
)
end
end
......
......@@ -12,6 +12,9 @@ module Shotengai
#
cattr_accessor :template_dir
@@index_query ||= nil
@@default_scope ||= nil
class << self
# Add the index query to custom the @@index_resouces on the base of @@resources
# Foe example:
......@@ -25,22 +28,30 @@ module Shotengai
@@index_query = block
end
# @@default_query would be useful for create & set_resource
# Just like:
# one_product.series.create! => default_query where(product_id: params[:product_id])
#
def default_query &block
@@default_query = block
end
def remove_methods *method_names
method_names.each { |name| self.remove_method name }
end
def resources= klass_name
retries ||= 1
@@resources = klass_name.constantize
rescue NameError
# If Product havent been load, ProductSeries or ProductSnapshot would not exists
unless (retries =- 1) < 0
klass_name.remove('Series', 'Snapshot').constantize
retry
else
raise
end
end
# def resources= klass_name
# retries ||= 1
# @@resources = klass_name.constantize
# rescue NameError
# # If Product havent been load, ProductSeries or ProductSnapshot would not exists
# unless (retries =- 1) < 0
# klass_name.remove('Series', 'Snapshot').constantize
# retry
# else
# raise
# end
# end
end
before_action :set_resource, except: [:index, :create]
......@@ -84,7 +95,7 @@ module Shotengai
end
def default_resources
self.class.resources
@@default_query&.call(self.class.resources, params) || self.class.resources
end
def resource_key
......
......@@ -2,23 +2,24 @@ module Shotengai
module Controller
module Merchant
class ProductSeriesController < Shotengai::Controller::Base
self.resources = 'ProductSeries'
self.template_dir = 'shoutengai/merchant/product_series/'
self.resources = ProductSeries
self.template_dir = 'shotengai/merchant/series/'
private
default_query do |klass, params|
klass.where(shotengai_product_id: params)
end
private
def resource_params
# resource_key = self.resource.model_name.singular.to_sym
# # QUESTION: need these ?
# spec = params.fetch(:spec, nil).try(:permit!)
# datail = params.fetch(:datail, nil).try(:permit!)
# meta = params.fetch(:meta, nil).try(:permit!)
# params.permit(
# :price, :original_price,
# ).merge(
# { spec: spec, detail: detail, meta: meta }
# )
spec = params.require(resource_key).fetch(:spec, nil).try(:permit!)
meta = params.require(resource_key).fetch(:meta, nil).try(:permit!)
params.require(resource_key).permit(
:original_price, :price, :stock
).merge(
{ spec: spec, meta: meta }
).merge(
other_resource_params
)
end
end
end
......
......@@ -2,7 +2,7 @@ module Shotengai
module Controller
module Merchant
class ProductsController < Shotengai::Controller::Base
self.resources = 'Product'
self.resources = Product
self.template_dir = 'shotengai/merchant/products/'
index_query do |resource, params|
......
......@@ -95,20 +95,20 @@ module Shotengai
has_many :snapshots, -> {
where(type: good_classes.map { |good_class| "#{good_class.name}Snapshot" })
}, class_name: 'Shotengai::Snapshot',
foreign_key: :shotengai_orders_id
foreign_key: :shotengai_order_id
good_classes.each do |klass|
has_many(
klass.snapshot_class.model_name.collection.to_sym,
class_name: klass.snapshot_class.name,
foreign_key: :shotengai_orders_id
foreign_key: :shotengai_order_id
)
# optional: true 允许父对象不存在
klass.snapshot_class.belongs_to(
self.model_name.singular.to_sym,
class_name: self.name,
optional: true,
foreign_key: :shotengai_orders_id
foreign_key: :shotengai_order_id
)
end
......
......@@ -64,7 +64,7 @@ module Shotengai
end
def add_associations subclass
subclass.has_many :series, class_name: subclass.series_class.name, foreign_key: 'shotengai_products_id'
subclass.has_many :series, class_name: subclass.series_class.name, foreign_key: 'shotengai_product_id'
subclass.has_many :snapshots, class_name: subclass.snapshot_class.name, through: :series, source: :snapshots
end
......
......@@ -16,14 +16,14 @@ module Shotengai
#
# Indexes
#
# index_shotengai_series_on_shotengai_products_id (shotengai_products_id)
# index_shotengai_series_on_shotengai_product_id (shotengai_product_id)
# index_shotengai_series_on_type (type)
#
class Series < ActiveRecord::Base
self.table_name = 'shotengai_series'
validate :check_spec, if: :spec
validates_uniqueness_of :spec, scope: :shotengai_products_id
validates_uniqueness_of :spec, scope: :shotengai_product_id
delegate :detail, :banners, :cover_image, to: :product
......@@ -37,8 +37,8 @@ module Shotengai
def add_associations
# belongs to Product
@subclass.belongs_to :product, foreign_key: :shotengai_products_id, class_name: @product_name
@subclass.belongs_to @product_name.underscore.to_sym, foreign_key: :shotengai_products_id, class_name: @product_name
@subclass.belongs_to :product, foreign_key: :shotengai_product_id, class_name: @product_name
@subclass.belongs_to @product_name.underscore.to_sym, foreign_key: :shotengai_product_id, class_name: @product_name
# has many snapshot
@subclass.has_many :snapshots, class_name: "#{@product_name}Snapshot", foreign_key: :shotengai_series_id
end
......
......@@ -20,7 +20,7 @@ module Shotengai
#
# Indexes
#
# index_shotengai_snapshots_on_shotengai_orders_id (shotengai_orders_id)
# index_shotengai_snapshots_on_shotengai_order_id (shotengai_order_id)
# index_shotengai_snapshots_on_shotengai_series_id (shotengai_series_id)
# index_shotengai_snapshots_on_type (type)
#
......
......@@ -9,7 +9,7 @@
# spec :json
# type :string(255)
# meta :json
# shotengai_products_id :integer
# shotengai_product_id :integer
# created_at :datetime not null
# updated_at :datetime not null
#
......
......@@ -13,7 +13,7 @@
# type :string(255)
# meta :json
# shotengai_series_id :integer
# shotengai_orders_id :integer
# shotengai_order_id :integer
# created_at :datetime not null
# updated_at :datetime not null
#
......
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