Commit 1db78141 by ivan Lan

Add Shotengai::Controller without test

parent 62ac46f9
module Shotengai
class Controller < ApplicationController
# ActiveRecord::Relation or ActiveRecord::Base
cattr_accessor :resources
# respond_with @products, template: "#{self.class.template_dir}/index"
cattr_accessor :template_dir #{ 'shoutengai/merchant/product_series/' }
class << self
# Add the index query to custom the @@index_resouces on the base of @@resources
# Foe example:
#
# index_query do |klass|
# klass.where(product: params[:product_id]).order('desc')
# end
#
def index_query &block
# 为了保证 self 为Controller instance variable ?? 才能有params 方法??
@@index_query = block
# self.index_resouces = block.call(self.resources)
end
def remove_methods *method_names
method_names.each { |name| self.remove_method name }
end
end
before_action :set_resource, except: [:index, :create]
def index
page = params[:page] || 1
per_page = params[:per_page] || 10
@resources = index_resources.paginate(page: page, per_page: per_page)
respond_with @resources, template: "#{@@template_dir}/index"
end
def show
respond_with @resource, template: "#{@@template_dir}/show"
end
def create
default_resources.create!(resource_params)
head 201
end
def update
@resource.update!(resource_params)
head 200
end
def destroy
@resourc.destroy!
head 204
end
private
def index_resources
@@index_query&.call(default_resources) || default_resources
end
def default_resources
self.class.resources
end
def set_resource
@resource = default_resources.find(params[:id])
end
def resource_params
end
end
end
\ No newline at end of file
......@@ -33,6 +33,10 @@ Gem::Specification.new do |spec|
spec.add_dependency 'acts-as-taggable-on'
spec.add_dependency 'rails'
# TODO: instead of gems below
spec.add_dependency 'jbuilder'
spec.add_dependency 'will_paginate', '~> 3.1.0'
spec.add_development_dependency 'bundler', '~> 1.14'
spec.add_development_dependency 'factory_girl_rails'
spec.add_development_dependency 'rake', '~> 10.0'
......
......@@ -3,8 +3,6 @@ require 'rails'
require 'active_record'
require 'shotengai'
require 'support/factory_girl'
# Dir['../support/**/*.rb'].each { |f| require f }
RSpec.configure do |config|
# Enable flags like --only-failures and --next-failure
......
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