Commit bd678c51 by ivan Lan

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

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