Commit a8975486 by ivan Lan

Add tempalte_controller

parent 1db78141
...@@ -12,7 +12,7 @@ module Shotengai ...@@ -12,7 +12,7 @@ module Shotengai
For example: For example:
rails generate shotengai:controllers merchant -n store --product MyProduct --order MyOrder rails generate shotengai:controllers merchant -n my_merchant --product MyProduct --order MyOrder
This will create serveral controller classes inherited from merchant product and order class This will create serveral controller classes inherited from merchant product and order class
For example: For example:
...@@ -40,13 +40,17 @@ module Shotengai ...@@ -40,13 +40,17 @@ module Shotengai
{ {
'products' => options[:product], 'products' => options[:product],
'orders' => options[:order], 'orders' => options[:order],
'product_series' => "#{options[:product]}Series" 'product_series' => "#{options[:product]}Series",
}.each do |key, klass_name| }.each do |key, klass_name|
@key, @klass_name = key, klass_name @key, @klass_name, @role = key, klass_name, role
template "#{role}/#{@key}_controller.rb", template "template_controller.rb",
"app/controllers/#{options[:namespace]}/#{klass_name.underscore.pluralize}_controller.rb" "app/controllers/#{options[:namespace]}/#{klass_name.underscore.pluralize}_controller.rb"
end end
end end
end end
end end
end end
\ No newline at end of file
class <%= @controller_prefix%><%= @klass_name.pluralize %>Controller < Shotengai::Customer::OrdersController
self.resource = '<%= @klass_name %>'
end
class <%= @controller_prefix%><%= @klass_name.pluralize %>Controller < Shotengai::Customer::ProductSeriesController
self.resource = '<%= @klass_name %>'
end
class <%= @controller_prefix%><%= @klass_name.pluralize %>Controller < Shotengai::Controller::Customer::ProductsController
self.resource = '<%= @klass_name %>'
end
class <%= @controller_prefix%><%= @klass_name.pluralize %>Controller < Shotengai::Merchant::OrdersController
self.resource = '<%= @klass_name %>'
end
class <%= @controller_prefix%><%= @klass_name.pluralize %>Controller < Shotengai::Merchant::ProductSeriesController
self.resource = '<%= @klass_name %>'
end
class <%= @controller_prefix%><%= @klass_name.pluralize %>Controller < Shotengai::Customer::ProductsController
self.resource = '<%= @klass_name %>'
end
class <%= @controller_prefix %><%= @klass_name.pluralize %>Controller < Shotengai::Controller::<%= "#{@role}/#{@key}".camelize %>Controller
self.resources = '<%= @klass_name %>'
end
module Shotengai module Shotengai
class Controller < ApplicationController class Controller < ApplicationController
# 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
# respond_with @products, template: "#{self.class.template_dir}/index" # respond_with @products, template: "#{self.class.template_dir}/index"
cattr_accessor :template_dir #{ 'shoutengai/merchant/product_series/' } 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
...@@ -25,6 +27,7 @@ module Shotengai ...@@ -25,6 +27,7 @@ module Shotengai
end end
before_action :set_resource, except: [:index, :create] before_action :set_resource, except: [:index, :create]
respond_to :json
def index def index
page = params[:page] || 1 page = params[:page] || 1
...@@ -48,7 +51,7 @@ module Shotengai ...@@ -48,7 +51,7 @@ module Shotengai
end end
def destroy def destroy
@resourc.destroy! @resource.destroy!
head 204 head 204
end end
...@@ -61,12 +64,16 @@ module Shotengai ...@@ -61,12 +64,16 @@ module Shotengai
self.class.resources self.class.resources
end end
def resource_key
(default_resources.try(:klass) || default_resources).model_name.singular.to_sym
end
def set_resource def set_resource
@resource = default_resources.find(params[:id]) @resource = default_resources.find(params[:id])
end end
def resource_params def resource_params
params.requrie(resource_key)
end end
end end
end end
\ No newline at end of file
module Shotengai
module ControllerHelper
module Product
included do
cattr_accessor :resource { 'Product' } # 默认值
# respond_with @products, template: "#{self.template_dir}/index"
cattr_accessor :template_dir { 'products/' }
end
class_methods do
def resource= resource_name
klass = Object.const_get resource_name
raise ArgumentError.new(
'The resource of Product Controller should be a class inherited from Shotengai::Product'
) unless Shotengai::Product === klass
@@resource = klass
end
# def template_dir dir
# self.template_dir = dir
# 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
def create
self.resource.create!(product_params)
head 201
end
def update
self.resource.update!(product_params)
head 200
end
resource.aasm.state_machine.events.map(&:first).each do |event|
define_method(event) do
@product.send("#{event}!")
head 200
end
end
private
def set_resource
@product = self.resource.find(params[:id])
end
def product_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!)
meta = params.require(resource_key).fetch(:meta, nil).try(:permit!)
params.requrie().permit(
:title, :default_series_id,
:need_express, :need_time_attr, :cover_image,
banners: []
).merge(
{ spec: spec, detail: detail, meta: meta }
)
end
end
end
end
\ No newline at end of file
module Shotengai
module ControllerHelper
module Product
included do
# cattr_accessor :resource
end
class_methods do
def product_as resource_name
self.cattr_accessor :resource
self.resource = Object.const_get resource_name
end
end
before_action :set_product, expect: [:index, :create]
respond_to :json
def index
@products = self.resource.all
respond_with @products
end
def show
@product = self.resource.find(params[:id])
respond_with @product
end
def create
self.resource.create!(product_params)
head 201
end
def update
self.resource.update!(product_params)
head 200
end
resource.aasm.state_machine.events.map(&:first).each do |event|
define_method(event) do
@product.send("#{event}!")
head 200
end
end
private
def set_resource
@product = self.resource.find()
end
def product_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!)
meta = params.require(resource_key).fetch(:meta, nil).try(:permit!)
params.requrie().permit(
:title, :default_series_id,
:need_express, :need_time_attr, :cover_image,
banners: []
).merge(
{ spec: spec, detail: detail, meta: meta }
)
end
end
end
end
\ No newline at end of file
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