Commit fc40c834 by ivan Lan

Add Shotengai::Controller::Base & Add some controller

parent a8975486
require "shotengai/version" require "shotengai/version"
require 'rails' require 'rails'
# require 'active_record' require 'active_record'
module Shotengai module Shotengai
# Your code goes here...
autoload :Product, 'shotengai/product' autoload :Product, 'shotengai/product'
autoload :Series, 'shotengai/series' autoload :Series, 'shotengai/series'
autoload :Snapshot, 'shotengai/snapshot' autoload :Snapshot, 'shotengai/snapshot'
...@@ -16,6 +15,17 @@ module Shotengai ...@@ -16,6 +15,17 @@ module Shotengai
autoload :Engine, 'shotengai/engine' autoload :Engine, 'shotengai/engine'
module Controller module Controller
autoload :Base, 'shotengai/controllers/base'
module Merchant
autoload :ProductsController, 'shotengai/controllers/merchant/products_controller'
autoload :ProductSnapshotsController, 'shotengai/controllers/merchant/product_snapshots_controller'
autoload :ProductSeriesController, 'shotengai/controllers/merchant/product_series_controller'
autoload :OrdersController, 'shotengai/controllers/merchant/orders_controller'
end
module Customer
end
end end
end end
module Shotengai module Shotengai
class Controller < ApplicationController module Controller
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
...@@ -24,6 +25,19 @@ module Shotengai ...@@ -24,6 +25,19 @@ module Shotengai
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
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]
...@@ -76,4 +90,5 @@ module Shotengai ...@@ -76,4 +90,5 @@ module Shotengai
params.requrie(resource_key) params.requrie(resource_key)
end end
end end
end
end end
module Shotengai
module Controller
module Merchant
class OrdersController < Shotengai::Controller::Base
end
end
end
end
module Shotengai
module Controller
module Merchant
class ProductSeriesController < Shotengai::Controller::Base
self.resources = 'ProductSeries'
self.template_dir = 'shoutengai/merchant/product_series/'
private
def resource_params
# resource_key = self.resource.model_name.singular.to_sym
# # QUESTION: need these ?
# spec = params.fetch(:spec, nil).try(:permit!)
# datail = params.fetch(:datail, nil).try(:permit!)
# meta = params.fetch(:meta, nil).try(:permit!)
# params.permit(
# :price, :original_price,
# ).merge(
# { spec: spec, detail: detail, meta: meta }
# )
end
end
end
end
end
module Shotengai
module Controller
module Merchant
class SnapshotsController < Shotengai::Controller::Base
end
end
end
end
module Shotengai module Shotengai
module Controller module Controller
class ProductsMasterController module Merchant
cattr_accessor :resource { 'Product' } # 默认值 class ProductsController < Shotengai::Controller::Base
# respond_with @products, template: "#{self.template_dir}/index" self.resources = ::Product
cattr_accessor :template_dir { 'products/' } self.template_dir = 'shoutengai/merchant/products/'
class << self index_query do |resource|
def resource= resource_name # params[:catalogs] nil 返回所有
klass = Object.const_get resource_name resource.tagged_with(params[:catalogs])
raise ArgumentError.new(
'The resource of Product Controller should be a class inherited from Shotengai::Product'
) unless Shotengai::Product === klass
@@resource = klass
end
end
before_action :set_product, expect: [:index, :create]
respond_to :json
def index
@products = self.resource.all
respond_with @products, template: "#{self.template_dir}/index"
end
def show
@product = self.resource.find(params[:id])
respond_with @product, template: "#{self.template_dir}/show"
# need series list here
end end
def create def put_on_shelf
self.resource.create!(product_params) @resource.put_on_shelf!
head 201
end
def update
self.resource.update!(product_params)
head 200 head 200
end end
resource.aasm.state_machine.events.map(&:first).each do |event| def sold_out
define_method(event) do @resource.sold_out!
@product.send("#{event}!")
head 200 head 200
end end
end
private def destroy
def set_resource @resource.soft_delete!
@product = self.resource.find(params[:id]) head 204
end end
def product_params private
def resource_params
resource_key = self.resource.model_name.singular.to_sym 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!)
......
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