Skip to content

Instantly share code, notes, and snippets.

@thinkclay
Last active August 29, 2015 14:07
Show Gist options
  • Save thinkclay/c89ae4bcdeb5c99a1ae4 to your computer and use it in GitHub Desktop.
Save thinkclay/c89ae4bcdeb5c99a1ae4 to your computer and use it in GitHub Desktop.
class DepositsController < BaseController
include Manageable
end
module Manageable
extend ActiveSupport::Concern
included do
before_action :set_record, only: [:show, :edit, :update, :destroy, :approve]
def index
@records = params[:name].constantize.order(params[:sort]).page(params[:page])
end
def all
@records = params[:name].constantize.order(params[:sort])
render 'index'
end
def show
end
def new
@record = params[:name].constantize.new
end
def create
@record = params[:name].constantize.new(record_params)
if @record.save
redirect_to polymorphic_path([:office, @record]), notice: t("#{params[:name].constantize.name.downcase}.created")
else
render action: 'new'
end
end
def edit
end
def update
respond_to do |format|
if @record.update(record_params)
current_administrator.activities.create! trackable: @record, action: 'updated'
format.html { redirect_to polymorphic_path([:office, @record]), notice: t("#{params[:name].constantize.name.downcase}.updated") }
format.json { render json: @record, status: 200 }
else
format.html { render action: 'edit' }
format.json { render json: @record.errors, status: :unprocessable_entity }
end
end
end
def destroy
@record.destroy
current_administrator.activities.create! trackable: @record, action: 'deleted'
redirect_to polymorphic_path([:office, params[:name].constantize]), notice: t("#{params[:name].constantize.name.downcase}.destroyed")
end
def approve
@record.verified = true
if @record.save(validate: false)
redirect_to polymorphic_path([:office, params[:name].constantize]), notice: t("#{params[:name].constantize.name.downcase}.verified")
else
render action: 'edit'
end
end
end
def set_record
@record = params[:name].constantize.find(params[:id])
end
def record_params
params.require(params[:name].constantize.name.downcase.to_sym).permit!
end
end
Application.routes.draw do
concern :paginatable do
get '(page/:page)', action: :index, on: :collection, as: ''
collection do
get 'all'
end
end
concern :transactionable do
member do
get 'approve'
end
end
resources :deposits, concerns: [:paginatable, :transactionable], defaults: { name: 'Deposit' }
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment