Commit a8975486 by ivan Lan

Add tempalte_controller

parent 1db78141
......@@ -12,7 +12,7 @@ module Shotengai
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
For example:
......@@ -40,13 +40,17 @@ module Shotengai
{
'products' => options[:product],
'orders' => options[:order],
'product_series' => "#{options[:product]}Series"
'product_series' => "#{options[:product]}Series",
}.each do |key, klass_name|
@key, @klass_name = key, klass_name
template "#{role}/#{@key}_controller.rb",
@key, @klass_name, @role = key, klass_name, role
template "template_controller.rb",
"app/controllers/#{options[:namespace]}/#{klass_name.underscore.pluralize}_controller.rb"
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
class Controller < 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 #{ 'shoutengai/merchant/product_series/' }
cattr_accessor :template_dir
class << self
# Add the index query to custom the @@index_resouces on the base of @@resources
......@@ -25,6 +27,7 @@ module Shotengai
end
before_action :set_resource, except: [:index, :create]
respond_to :json
def index
page = params[:page] || 1
......@@ -48,7 +51,7 @@ module Shotengai
end
def destroy
@resourc.destroy!
@resource.destroy!
head 204
end
......@@ -61,12 +64,16 @@ module Shotengai
self.class.resources
end
def resource_key
(default_resources.try(:klass) || default_resources).model_name.singular.to_sym
end
def set_resource
@resource = default_resources.find(params[:id])
end
def resource_params
params.requrie(resource_key)
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