Commit 11ba283c by ivan Lan

Finish the spec for merchant product controller

parent a6c14536
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:2] | passed | 0.20555 seconds |
./spec/shotengai/models_spec.rb[1:1:2:1] | passed | 0.20124 seconds |
./spec/shotengai/models_spec.rb[1:1:2:2] | passed | 0.17955 seconds |
./spec/shotengai/models_spec.rb[1:1:2:3] | passed | 0.17157 seconds |
./spec/shotengai/models_spec.rb[1:1:3:1] | passed | 0.19801 seconds |
./spec/shotengai/models_spec.rb[1:1:3:2] | passed | 0.18172 seconds |
./spec/shotengai/models_spec.rb[1:1:3:3] | passed | 0.17998 seconds |
./spec/shotengai/models_spec.rb[1:1:4:1] | passed | 0.23986 seconds |
./spec/shotengai/models_spec.rb[1:1:5:1] | passed | 0.17938 seconds |
./spec/shotengai/models_spec.rb[1:1:5:2:1] | passed | 0.24027 seconds |
./spec/shotengai/models_spec.rb[1:1:5:2:2] | passed | 0.21941 seconds |
./spec/shotengai/models_spec.rb[1:2:1:1] | passed | 0.20203 seconds |
./spec/shotengai/models_spec.rb[1:2:1:2] | passed | 0.21369 seconds |
./spec/shotengai/models_spec.rb[1:2:2:1] | passed | 0.43436 seconds |
./spec/shotengai/models_spec.rb[1:2:2:2] | passed | 0.28966 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.3368 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.32811 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.32973 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.33828 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.33872 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.36069 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.3256 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.45201 seconds |
json.current_page @orders.current_page
json.total_pages @orders.total_pages
json.send(@orders.first.class.model_name.collection) @orders, partial: 'shotengai/share/order_simple', as: :order
\ No newline at end of file
json.set! @resources.klass.model_name.collection do
json.array! @resources, partial: 'shotengai/share/order_simple', as: :order
end
\ No newline at end of file
json.current_page @resources.current_page
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
end
json.current_page @resources.current_page
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
end
json.current_page @resources.current_page
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
end
json.extract! product, :id, :title, :status, :need_express, :need_time_attr,
: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
\ No newline at end of file
......@@ -35,6 +35,7 @@ module Shotengai
end
def validate_name_chain name_ary, order='desc'
return nil unless name_ary
ary = order.downcase.eql?('asc') ? name_ary.reverse : name_ary
where(name: ary.last).each do |bottom_catalog|
return name_ary if bottom_catalog.name_chain.eql?(ary)
......
module Shotengai
module Controller
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
class << self
# Add the index query to custom the @@index_resouces on the base of @@resources
# Foe example:
#
# index_query do |klass|
# index_query do |klass, params|
# klass.where(product: params[:product_id]).order('desc')
# end
#
def index_query &block
# 为了保证 self 为Controller instance variable ?? 才能有params 方法??
# could not get params here
@@index_query = block
# self.index_resouces = block.call(self.resources)
end
def remove_methods *method_names
......@@ -43,6 +46,11 @@ module Shotengai
before_action :set_resource, except: [:index, :create]
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
page = params[:page] || 1
per_page = params[:per_page] || 10
......@@ -55,8 +63,9 @@ module Shotengai
end
def create
default_resources.create!(resource_params)
head 201
@resource = default_resources.create!(resource_params)
# head 201
respond_with @resource, template: "#{@@template_dir}/show", status: 201
end
def update
......@@ -71,7 +80,7 @@ module Shotengai
private
def index_resources
@@index_query&.call(default_resources) || default_resources
@@index_query&.call(default_resources, params) || default_resources
end
def default_resources
......
......@@ -2,22 +2,23 @@ module Shotengai
module Controller
module Merchant
class ProductsController < Shotengai::Controller::Base
self.resources = ::Product
self.template_dir = 'shoutengai/merchant/products/'
self.resources = 'Product'
self.template_dir = 'shotengai/merchant/products/'
index_query do |resource|
# params[:catalogs] nil 返回所有
resource.tagged_with(params[:catalogs])
index_query do |resource, params|
params[:catalog_list] ?
resource.tagged_with(params[:catalog_list], on: :catalogs) :
resource
end
def put_on_shelf
@resource.put_on_shelf!
head 200
respond_with @resource, template: "#{@@template_dir}/show", status: 200
end
def sold_out
@resource.sold_out!
head 200
respond_with @resource, template: "#{@@template_dir}/show", status: 200
end
def destroy
......@@ -27,20 +28,26 @@ module Shotengai
private
def resource_params
resource_key = self.resource.model_name.singular.to_sym
# QUESTION: need these ?
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!)
params.requrie().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.require(resource_key).permit(
:title, :default_series_id,
:need_express, :need_time_attr, :cover_image,
:need_express, :need_time_attr, :cover_image, catalog_list: [],
banners: []
).merge(
{ spec: spec, detail: detail, meta: meta }
).merge(
other_resource_params
)
end
# rewrite this to add more custom column
def other_resource_params
params.require(resource_key).permit()
end
end
end
end
......
......@@ -73,9 +73,10 @@ module Shotengai
# tagger_with('xx', on: "#{tag_name}.to_sym): tag_name is plural
def join_catalog_system catalog_class_name, options={}
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
# 只有完整替换(只属于一个分类)的时候才进行验证,add remove 暂时未添加
# Just ctalogs_list = have a validation
class_eval do
define_method("#{tag_name}_list=") { |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