Commit 11ba283c by ivan Lan

Finish the spec for merchant product controller

parent a6c14536
example_id | status | run_time | example_id | status | run_time |
------------------------------------------ | ------ | --------------- | ------------------------------------------ | ------ | --------------- |
./spec/shotengai/models_spec.rb[1:1:1:1] | passed | 0.44582 seconds | ./spec/shotengai/models_spec.rb[1:1:1:1] | passed | 0.58762 seconds |
./spec/shotengai/models_spec.rb[1:1:1:2] | passed | 0.20555 seconds | ./spec/shotengai/models_spec.rb[1:1:1:2] | passed | 0.3368 seconds |
./spec/shotengai/models_spec.rb[1:1:2:1] | passed | 0.20124 seconds | ./spec/shotengai/models_spec.rb[1:1:2:1] | passed | 0.32969 seconds |
./spec/shotengai/models_spec.rb[1:1:2:2] | passed | 0.17955 seconds | ./spec/shotengai/models_spec.rb[1:1:2:2] | passed | 0.32811 seconds |
./spec/shotengai/models_spec.rb[1:1:2:3] | passed | 0.17157 seconds | ./spec/shotengai/models_spec.rb[1:1:2:3] | passed | 0.34035 seconds |
./spec/shotengai/models_spec.rb[1:1:3:1] | passed | 0.19801 seconds | ./spec/shotengai/models_spec.rb[1:1:3:1] | passed | 0.32973 seconds |
./spec/shotengai/models_spec.rb[1:1:3:2] | passed | 0.18172 seconds | ./spec/shotengai/models_spec.rb[1:1:3:2] | passed | 0.33056 seconds |
./spec/shotengai/models_spec.rb[1:1:3:3] | passed | 0.17998 seconds | ./spec/shotengai/models_spec.rb[1:1:3:3] | passed | 0.33828 seconds |
./spec/shotengai/models_spec.rb[1:1:4:1] | passed | 0.23986 seconds | ./spec/shotengai/models_spec.rb[1:1:4:1] | passed | 0.35822 seconds |
./spec/shotengai/models_spec.rb[1:1:5:1] | passed | 0.17938 seconds | ./spec/shotengai/models_spec.rb[1:1:5:1] | passed | 0.33872 seconds |
./spec/shotengai/models_spec.rb[1:1:5:2:1] | passed | 0.24027 seconds | ./spec/shotengai/models_spec.rb[1:1:5:2:1] | passed | 0.36586 seconds |
./spec/shotengai/models_spec.rb[1:1:5:2:2] | passed | 0.21941 seconds | ./spec/shotengai/models_spec.rb[1:1:5:2:2] | passed | 0.36069 seconds |
./spec/shotengai/models_spec.rb[1:2:1:1] | passed | 0.20203 seconds | ./spec/shotengai/models_spec.rb[1:2:1:1] | passed | 0.34614 seconds |
./spec/shotengai/models_spec.rb[1:2:1:2] | passed | 0.21369 seconds | ./spec/shotengai/models_spec.rb[1:2:1:2] | passed | 0.3256 seconds |
./spec/shotengai/models_spec.rb[1:2:2:1] | passed | 0.43436 seconds | ./spec/shotengai/models_spec.rb[1:2:2:1] | passed | 0.62601 seconds |
./spec/shotengai/models_spec.rb[1:2:2:2] | passed | 0.28966 seconds | ./spec/shotengai/models_spec.rb[1:2:2:2] | passed | 0.45201 seconds |
json.current_page @orders.current_page json.current_page @orders.current_page
json.total_pages @orders.total_pages json.total_pages @orders.total_pages
json.send(@orders.first.class.model_name.collection) @orders, partial: 'shotengai/share/order_simple', as: :order json.set! @resources.klass.model_name.collection do
\ No newline at end of file json.array! @resources, partial: 'shotengai/share/order_simple', as: :order
end
\ No newline at end of file
json.current_page @resources.current_page json.current_page @resources.current_page
json.total_pages @resources.total_pages json.total_pages @resources.total_pages
json.set! @resources.first.class.model_name.collection do json.set! @resources.klass.model_name.collection do
json.array! @resources, partial: 'shotengai/share/product_simple', as: :product json.array! @resources, partial: 'shotengai/share/product_simple', as: :product
end end
json.current_page @resources.current_page json.current_page @resources.current_page
json.total_pages @resources.total_pages json.total_pages @resources.total_pages
json.set! @resources.first.class.model_name.collection do json.set! @resources.klass.model_name.collection do
json.array! @resources, partial: 'shotengai/share/product_simple', as: :products json.array! @resources, partial: 'shotengai/share/product_simple', as: :products
end end
json.current_page @resources.current_page json.current_page @resources.current_page
json.total_pages @resources.total_pages json.total_pages @resources.total_pages
json.set! @resources.first.class.model_name.collection do json.set! @resources.klass.model_name.collection do
json.array! @resources, partial: 'shotengai/share/snapshot_simple', as: :snapshot json.array! @resources, partial: 'shotengai/share/snapshot_simple', as: :snapshot
end end
json.extract! product, :id, :title, :status, :need_express, :need_time_attr, json.extract! product, :id, :title, :status, :need_express, :need_time_attr,
:cover_image, :banners, :spec, :detail, :meta :cover_image, :banners, :spec, :detail, :meta
# TODO: NOTE: catalog_list is only vaild in the template example
json.catalog_list product.catalog_list
json.default_series product.default_series, partial: 'shotengai/share/series_simple', as: :series json.default_series product.default_series, partial: 'shotengai/share/series_simple', as: :series
\ No newline at end of file
...@@ -35,6 +35,7 @@ module Shotengai ...@@ -35,6 +35,7 @@ module Shotengai
end end
def validate_name_chain name_ary, order='desc' def validate_name_chain name_ary, order='desc'
return nil unless name_ary
ary = order.downcase.eql?('asc') ? name_ary.reverse : name_ary ary = order.downcase.eql?('asc') ? name_ary.reverse : name_ary
where(name: ary.last).each do |bottom_catalog| where(name: ary.last).each do |bottom_catalog|
return name_ary if bottom_catalog.name_chain.eql?(ary) return name_ary if bottom_catalog.name_chain.eql?(ary)
......
module Shotengai module Shotengai
module Controller module Controller
class Base < ApplicationController class Base < ApplicationController
#
# The resources of this controller # The resources of this controller
# ActiveRecord::Relation or ActiveRecord::Base # ActiveRecord::Relation or ActiveRecord::Base
#
cattr_accessor :resources cattr_accessor :resources
#
# The view template dir # The view template dir
# respond_with @products, template: "#{self.class.template_dir}/index" # respond_with @products, template: "#{self.class.template_dir}/index"
#
cattr_accessor :template_dir cattr_accessor :template_dir
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:
# #
# index_query do |klass| # index_query do |klass, params|
# klass.where(product: params[:product_id]).order('desc') # klass.where(product: params[:product_id]).order('desc')
# end # end
# #
def index_query &block def index_query &block
# 为了保证 self 为Controller instance variable ?? 才能有params 方法?? # could not get params here
@@index_query = block @@index_query = block
# self.index_resouces = block.call(self.resources)
end end
def remove_methods *method_names def remove_methods *method_names
...@@ -42,6 +45,11 @@ module Shotengai ...@@ -42,6 +45,11 @@ module Shotengai
before_action :set_resource, except: [:index, :create] before_action :set_resource, except: [:index, :create]
respond_to :json respond_to :json
# TODO: could not catch the exception
# rescue_from Shotengai::WebError do |e|
# render json: { error: e.message }, status: e.status
# end
def index def index
page = params[:page] || 1 page = params[:page] || 1
...@@ -55,8 +63,9 @@ module Shotengai ...@@ -55,8 +63,9 @@ module Shotengai
end end
def create def create
default_resources.create!(resource_params) @resource = default_resources.create!(resource_params)
head 201 # head 201
respond_with @resource, template: "#{@@template_dir}/show", status: 201
end end
def update def update
...@@ -71,7 +80,7 @@ module Shotengai ...@@ -71,7 +80,7 @@ module Shotengai
private private
def index_resources def index_resources
@@index_query&.call(default_resources) || default_resources @@index_query&.call(default_resources, params) || default_resources
end end
def default_resources def default_resources
......
...@@ -2,22 +2,23 @@ module Shotengai ...@@ -2,22 +2,23 @@ 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 = 'shoutengai/merchant/products/' self.template_dir = 'shotengai/merchant/products/'
index_query do |resource| index_query do |resource, params|
# params[:catalogs] nil 返回所有 params[:catalog_list] ?
resource.tagged_with(params[:catalogs]) resource.tagged_with(params[:catalog_list], on: :catalogs) :
resource
end end
def put_on_shelf def put_on_shelf
@resource.put_on_shelf! @resource.put_on_shelf!
head 200 respond_with @resource, template: "#{@@template_dir}/show", status: 200
end end
def sold_out def sold_out
@resource.sold_out! @resource.sold_out!
head 200 respond_with @resource, template: "#{@@template_dir}/show", status: 200
end end
def destroy def destroy
...@@ -27,20 +28,26 @@ module Shotengai ...@@ -27,20 +28,26 @@ module Shotengai
private private
def resource_params def resource_params
resource_key = self.resource.model_name.singular.to_sym
# QUESTION: need these ? # QUESTION: need these ?
spec = params.require(resource_key).fetch(:spec, nil).try(:permit!) spec = params.require(resource_key).fetch(:spec, nil).try(:permit!)
datail = params.require(resource_key).fetch(:datail, nil).try(:permit!) detail = params.require(resource_key).fetch(:detail, nil).try(:permit!)
meta = params.require(resource_key).fetch(:meta, nil).try(:permit!) meta = params.require(resource_key).fetch(:meta, nil).try(:permit!)
# 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.requrie().permit( params.require(resource_key).permit(
:title, :default_series_id, :title, :default_series_id,
:need_express, :need_time_attr, :cover_image, :need_express, :need_time_attr, :cover_image, catalog_list: [],
banners: [] banners: []
).merge( ).merge(
{ spec: spec, detail: detail, meta: meta } { spec: spec, detail: detail, meta: meta }
).merge(
other_resource_params
) )
end end
# rewrite this to add more custom column
def other_resource_params
params.require(resource_key).permit()
end
end end
end end
end end
......
...@@ -73,9 +73,10 @@ module Shotengai ...@@ -73,9 +73,10 @@ module Shotengai
# tagger_with('xx', on: "#{tag_name}.to_sym): tag_name is plural # tagger_with('xx', on: "#{tag_name}.to_sym): tag_name is plural
def join_catalog_system catalog_class_name, options={} def join_catalog_system catalog_class_name, options={}
catalog_class = catalog_class_name.constantize catalog_class = catalog_class_name.constantize
tag_name = options[:as] || catalog_class.model_name.collection tag_name = options[:as] || catalog_class.model_name.singular
acts_as_taggable_on tag_name.to_sym acts_as_taggable_on tag_name.to_sym
# 只有完整替换(只属于一个分类)的时候才进行验证,add remove 暂时未添加 # 只有完整替换(只属于一个分类)的时候才进行验证,add remove 暂时未添加
# Just ctalogs_list = have a validation
class_eval do class_eval do
define_method("#{tag_name}_list=") { |value| define_method("#{tag_name}_list=") { |value|
super catalog_class.validate_name_chain(value) super catalog_class.validate_name_chain(value)
......
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