Commit 4e007d9e by ivan Lan

Refactor Shotengai::Controller::Base

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