Commit 4e007d9e by ivan Lan

Refactor Shotengai::Controller::Base

parent 0b76f7be
class <%= @controller_prefix %><%= @key.classify.pluralize %>Controller < Shotengai::Controller::<%= "#{@role}/#{@key}".camelize %>Controller class <%= @controller_prefix %><%= @key.classify.pluralize %>Controller < Shotengai::Controller::<%= "#{@role}/#{@key}".camelize %>Controller
self.resources = <%= @klass_name %> self.base_resources = <%= @klass_name %>
end end
module Shotengai module Shotengai
module Controller module Controller
class Base < ApplicationController class Base < ApplicationController
#
# The resources of this controller
# ActiveRecord::Relation or ActiveRecord::Base
#
cattr_accessor :resources
#
# The view template dir
# respond_with @products, template: "#{self.class.template_dir}/index"
#
cattr_accessor :template_dir
@@index_query = nil
@@default_query = nil
class << self class << self
# Add the index query to custom the @@index_resouces on the base of @@resources
# Foe example:
#
# index_query do |klass, params|
# klass.where(product: params[:product_id]).order('desc')
# end
# #
def index_query &block # The base_resources of this controller
# could not get params here # ActiveRecord::Relation or ActiveRecord::Base
@@index_query = block
def base_resources= resources
class_eval %Q{
def add_base_resources
@base_resources = ::#{resources}
end
}
end end
# @@default_query would be useful for create & set_resource #
# Just like: # The view template dir
# one_product.series.create! => default_query where(product_id: params[:product_id]) # respond_with @products, template: "#{self.class.template_dir}/index"
#
def default_query &block def template_dir= template_dir
@@default_query = block class_eval %Q{
def add_template_dir
@template_dir = "#{template_dir}"
end
}
end end
# Refuse to search methods in superclasses # Refuse to search methods in superclasses
def remove_actions *actions def remove_actions *actions
actions.each { |name| remove_possible_method name } actions.each { |name| remove_possible_method name }
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 end
before_action :set_resource, except: [:index, :create] before_action :set_resource, except: [:index, :create]
...@@ -78,17 +55,17 @@ module Shotengai ...@@ -78,17 +55,17 @@ module Shotengai
page = params[:page] || 1 page = params[:page] || 1
per_page = params[:per_page] || 10 per_page = params[:per_page] || 10
@resources = index_resources.paginate(page: page, per_page: per_page) @resources = index_resources.paginate(page: page, per_page: per_page)
respond_with @resources, template: "#{@@template_dir}/index" respond_with @resources, template: "#{@template_dir}/index"
end end
def show def show
respond_with @resource, template: "#{@@template_dir}/show" respond_with @resource, template: "#{@template_dir}/show"
end end
def create def create
@resource = default_resources.create!(resource_params) @resource = default_resources.create!(resource_params)
# head 201 # head 201
respond_with @resource, template: "#{@@template_dir}/show", status: 201 respond_with @resource, template: "#{@template_dir}/show", status: 201
end end
def update def update
...@@ -101,13 +78,44 @@ module Shotengai ...@@ -101,13 +78,44 @@ module Shotengai
head 204 head 204
end end
def initialize
super
try(:add_base_resources)
try(:add_template_dir)
end
private private
# Instance method: default_query
# example:
# def default_query resources
# resources.where(product_id: params[:product_id])
# 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 resources
# end
# Add the index query to custom the @@index_resouces on the base of @@resources
# Foe example:
#
# def index_query resources
# resources.where(product: params[:product_id]).order('desc')
# end
#
# def index_query resources
# end
def index_resources def index_resources
@@index_query&.call(default_resources, params, request) || default_resources try(:index_query, default_resources) || default_resources
end end
def default_resources def default_resources
@@default_query&.call(self.class.resources, params, request) || self.class.resources try(:default_query, @base_resources) || @base_resources
end end
def resource_key def resource_key
...@@ -122,6 +130,7 @@ module Shotengai ...@@ -122,6 +130,7 @@ module Shotengai
# def resource_params # def resource_params
# super&.merge params.require(:some_key) # super&.merge params.require(:some_key)
# end # end
def resource_params def resource_params
params.require(resource_key).permit! params.require(resource_key).permit!
end end
......
...@@ -2,7 +2,7 @@ module Shotengai ...@@ -2,7 +2,7 @@ module Shotengai
module Controller module Controller
module Customer module Customer
class CartsController < Shotengai::Controller::Base class CartsController < Shotengai::Controller::Base
self.resources = Cart self.base_resources = Cart
self.template_dir = 'shotengai/customer/cart' self.template_dir = 'shotengai/customer/cart'
before_action :buyer_auth before_action :buyer_auth
...@@ -11,12 +11,6 @@ module Shotengai ...@@ -11,12 +11,6 @@ module Shotengai
remove_actions :index, :create, :destroy remove_actions :index, :create, :destroy
default_query do |resource, params, request|
end
index_query do |resource, params, request|
end
private private
def buyer_auth def buyer_auth
@buyer = params[:buyer_type].constantize.find(params[:buyer_id]) @buyer = params[:buyer_type].constantize.find(params[:buyer_id])
......
...@@ -2,7 +2,7 @@ module Shotengai ...@@ -2,7 +2,7 @@ module Shotengai
module Controller module Controller
module Customer module Customer
class OrdersController < Shotengai::Controller::Base class OrdersController < Shotengai::Controller::Base
self.resources = Order self.base_resources = Order
self.template_dir = 'shotengai/customer/orders/' self.template_dir = 'shotengai/customer/orders/'
before_action :buyer_auth before_action :buyer_auth
...@@ -10,21 +10,22 @@ module Shotengai ...@@ -10,21 +10,22 @@ module Shotengai
remove_actions :destroy remove_actions :destroy
default_query do |resource, params, request| def default_query resources
resources.where(buyer: @buyer)
end end
index_query do |resource, params, request| def index_query resources
resource.status_is(params[:status]) resources.status_is(params[:status])
end end
def create # Use :series_id & :count def create # Use :series_id & :count
@resource = @buyer.buy_it_immediately(snapshot_params, resource_params) @resource = @buyer.buy_it_immediately(snapshot_params, resource_params)
respond_with @resource, template: "#{@@template_dir}/show", status: 201 respond_with @resource, template: "#{@template_dir}/show", status: 201
end end
def pay def pay
@resource.pay! @resource.pay!
respond_with @resource, template: "#{@@template_dir}/show" respond_with @resource, template: "#{@template_dir}/show"
end end
def destroy def destroy
...@@ -34,7 +35,7 @@ module Shotengai ...@@ -34,7 +35,7 @@ module Shotengai
def confirm def confirm
@resource.confirm! @resource.confirm!
respond_with @resource, template: "#{@@template_dir}/show" respond_with @resource, template: "#{@template_dir}/show"
end end
private private
......
...@@ -2,18 +2,16 @@ module Shotengai ...@@ -2,18 +2,16 @@ module Shotengai
module Controller module Controller
module Customer module Customer
class ProductSeriesController < Shotengai::Controller::Base class ProductSeriesController < Shotengai::Controller::Base
self.resources = ProductSeries self.base_resources = ProductSeries
self.template_dir = 'shotengai/customer/series/' self.template_dir = 'shotengai/customer/series/'
remove_actions :create, :update, :destroy remove_actions :create, :update, :destroy
default_query do |resource, params, request| def default_query resources
resource.where( resources.where(
params[:product_id] && { shotengai_product_id: params[:product_id] } params[:product_id] && { shotengai_product_id: params[:product_id] }
) )
end end
index_query do |resource, params, request|
end
end end
end end
end end
......
...@@ -2,34 +2,31 @@ module Shotengai ...@@ -2,34 +2,31 @@ module Shotengai
module Controller module Controller
module Customer module Customer
class ProductSnapshotsController < Shotengai::Controller::Base class ProductSnapshotsController < Shotengai::Controller::Base
self.resources = ProductSnapshot self.base_resources = ProductSnapshot
self.template_dir = 'shotengai/customer/snapshots/' self.template_dir = 'shotengai/customer/snapshots/'
before_action :buyer_auth before_action :buyer_auth
before_action :edit_only_unpaid, except: [:index, :show, :create] before_action :edit_only_unpaid, except: [:index, :show, :create]
self.default_query do |resource, params, request|
def default_query resources
# /orders/:order_id/snapshots # /orders/:order_id/snapshots
# /series/:series_id/snapshots # /series/:series_id/snapshots
buyer = params[:buyer_type].constantize.find(params[:buyer_id]) order_id = request.path_info.include?('cart') ? @buyer.order_cart.id : params[:order_id]
order_id = request.path_info.include?('cart') ? buyer.order_cart.id : params[:order_id] resources.where(
resource.where(
order_id && { shotengai_order_id: order_id } order_id && { shotengai_order_id: order_id }
).count ).count
resource.where( resources.where(
order_id && { shotengai_order_id: order_id } order_id && { shotengai_order_id: order_id }
).where( ).where(
params[:series_id] && { shotengai_series_id: params[:series_id] } params[:series_id] && { shotengai_series_id: params[:series_id] }
) )
end end
index_query do |resource, params, request|
end
# 不指定 order 时,默认创建在 cart 中 # 不指定 order 时,默认创建在 cart 中
# TODO: WARNING: snapshots # TODO: WARNING: snapshots
def create def create
@resource = default_resources.create!(resource_params) @resource = default_resources.create!(resource_params)
respond_with @resource, template: "#{@@template_dir}/show", status: 201 respond_with @resource, template: "#{@template_dir}/show", status: 201
end end
private private
......
...@@ -2,18 +2,14 @@ module Shotengai ...@@ -2,18 +2,14 @@ module Shotengai
module Controller module Controller
module Customer module Customer
class ProductsController < Shotengai::Controller::Base class ProductsController < Shotengai::Controller::Base
self.resources = Product self.base_resources = Product
self.template_dir = 'shotengai/customer/products/' self.template_dir = 'shotengai/customer/products/'
remove_actions :create, :update, :destroy remove_actions :create, :update, :destroy
default_query do |resource, params, request|
end
index_query do |resource, params, request| def index_query resources
params[:catalog_list] ? params[:catalog_list] ?
resource.tagged_with(params[:catalog_list], on: :catalogs) : resources.tagged_with(params[:catalog_list], on: :catalogs) :
resource resources
end end
end end
end end
......
...@@ -2,22 +2,18 @@ module Shotengai ...@@ -2,22 +2,18 @@ module Shotengai
module Controller module Controller
module Merchant module Merchant
class OrdersController < Shotengai::Controller::Base class OrdersController < Shotengai::Controller::Base
self.resources = Order self.base_resources = ::Order
self.template_dir = 'shotengai/merchant/orders/' self.template_dir = 'shotengai/merchant/orders/'
remove_actions :create, :destroy remove_actions :create, :destroy
default_query do |resource, params, request| def index_query resources
resources.status_is(params[:status])
end
index_query do |resource, params, request|
resource.status_is(params[:status])
end end
def send_out def send_out
@resource.send_out! @resource.send_out!
respond_with @resource, template: "#{@@template_dir}/show" respond_with @resource, template: "#{@template_dir}/show"
end end
private private
......
...@@ -2,19 +2,15 @@ module Shotengai ...@@ -2,19 +2,15 @@ module Shotengai
module Controller module Controller
module Merchant module Merchant
class ProductSeriesController < Shotengai::Controller::Base class ProductSeriesController < Shotengai::Controller::Base
self.resources = ProductSeries self.base_resources = ProductSeries
self.template_dir = 'shotengai/merchant/series/' self.template_dir = 'shotengai/merchant/series/'
default_query do |resource, params, request| def default_query resources
resource.where( resources.where(
params[:product_id] && { shotengai_product_id: params[:product_id] } params[:product_id] && { shotengai_product_id: params[:product_id] }
) )
end end
index_query do |resource, params, request|
end
private private
def resource_params def resource_params
spec = params.require(resource_key).fetch(:spec, nil).try(:permit!) spec = params.require(resource_key).fetch(:spec, nil).try(:permit!)
......
...@@ -2,18 +2,18 @@ module Shotengai ...@@ -2,18 +2,18 @@ module Shotengai
module Controller module Controller
module Merchant module Merchant
class ProductSnapshotsController < Shotengai::Controller::Base class ProductSnapshotsController < Shotengai::Controller::Base
self.resources = ProductSnapshot self.base_resources = ProductSnapshot
self.template_dir = 'shotengai/merchant/snapshots/' self.template_dir = 'shotengai/merchant/snapshots/'
remove_actions :create, :destroy remove_actions :create, :destroy
before_action :edit_only_unpaid, only: :update before_action :edit_only_unpaid, only: :update
default_query do |resource, params, request| def default_query resources
resource.in_order resources.in_order
end end
index_query do |resource, params, request| def index_query resources
resource.where( resources.where(
params[:order_id] && { shotengai_order_id: params[:order_id] } params[:order_id] && { shotengai_order_id: params[:order_id] }
).where( ).where(
params[:product_series_id] && { shotengai_series_id: params[:product_series_id] } params[:product_series_id] && { shotengai_series_id: params[:product_series_id] }
......
...@@ -2,29 +2,29 @@ module Shotengai ...@@ -2,29 +2,29 @@ module Shotengai
module Controller module Controller
module Merchant module Merchant
class ProductsController < Shotengai::Controller::Base class ProductsController < Shotengai::Controller::Base
self.resources = Product self.base_resources = Product
self.template_dir = 'shotengai/merchant/products/' self.template_dir = 'shotengai/merchant/products/'
before_action :manager_auth before_action :manager_auth
default_query do |resource, params, request| def default_query resources
resource.where(manager: @manager) resources.where(@manager && { manager: @manager })
end end
index_query do |resource, params, request| def index_query resources
params[:catalog_list] ? params[:catalog_list] ?
resource.tagged_with(params[:catalog_list], on: :catalogs) : resources.tagged_with(params[:catalog_list], on: :catalogs) :
resource resources
end end
def put_on_shelf def put_on_shelf
@resource.put_on_shelf! @resource.put_on_shelf!
respond_with @resource, template: "#{@@template_dir}/show", status: 200 respond_with @resource, template: "#{@template_dir}/show", status: 200
end end
def sold_out def sold_out
@resource.sold_out! @resource.sold_out!
respond_with @resource, template: "#{@@template_dir}/show", status: 200 respond_with @resource, template: "#{@template_dir}/show", status: 200
end end
def destroy def destroy
......
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