Created
May 30, 2014 18:45
-
-
Save dipak1112/ce25511aa176d23fcf2c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Admin::DriversController < AdminController | |
before_action :set_admin_driver, only: [:show, :edit, :update, :destroy] | |
helper_method :sort_column, :sort_direction | |
add_breadcrumb 'Drivers', :admin_drivers_path | |
add_breadcrumb 'Create a new Driver', '', :only => [:new, :create] | |
# GET /admin/drivers | |
# GET /admin/drivers.json | |
def index | |
if params[:name].present? | |
name = params[:name].empty? ? '' : params[:name] | |
if current_admin_user.is_super_admin? | |
@admin_drivers = Driver.search_drivers(name).page(params[:page]).per(25) | |
else | |
@admin_drivers = current_admin_user.drivers.search_drivers(name).page(params[:page]).per(25) | |
end | |
else | |
if current_admin_user.is_super_admin? | |
@admin_drivers = Driver.order(sort_order).page(params[:page]).per(25) | |
else | |
@admin_drivers = current_admin_user.drivers.order(sort_order).page(params[:page]).per(25) | |
end | |
end | |
end | |
# GET /admin/drivers/1 | |
# GET /admin/drivers/1.json | |
def show | |
add_breadcrumb @admin_driver.name,'' | |
end | |
# GET /admin/drivers/new | |
def new | |
@admin_driver = Driver.new | |
end | |
# GET /admin/drivers/1/edit | |
def edit | |
add_breadcrumb 'Edit a Driver - ' + @admin_driver.name | |
end | |
# POST /admin/drivers | |
# POST /admin/drivers.json | |
def create | |
@admin_driver = Driver.new(admin_driver_params) | |
respond_to do |format| | |
if @admin_driver.save | |
format.html { redirect_to admin_driver_path(@admin_driver), notice: 'Driver was successfully created.' } | |
format.json { render action: 'show', status: :created, location: @admin_driver } | |
else | |
format.html { render 'new' } | |
format.json { render json: @admin_driver.errors, status: :unprocessable_entity } | |
end | |
end | |
end | |
# PATCH/PUT /admin/drivers/1 | |
# PATCH/PUT /admin/drivers/1.json | |
def update | |
respond_to do |format| | |
if @admin_driver.update(admin_driver_params) | |
format.html { redirect_to admin_driver_path, notice: 'Driver was successfully updated.' } | |
format.json { head :no_content } | |
else | |
format.html { render 'edit' } | |
format.json { render json: @admin_driver.errors, status: :unprocessable_entity } | |
end | |
end | |
end | |
# DELETE /admin/drivers/1 | |
# DELETE /admin/drivers/1.json | |
def destroy | |
@admin_driver.destroy | |
respond_to do |format| | |
format.html { redirect_to admin_drivers_url } | |
format.json { head :no_content } | |
end | |
end | |
private | |
# Use callbacks to share common setup or constraints between actions. | |
def set_admin_driver | |
@admin_driver = Driver.find(params[:id]) | |
end | |
# Never trust parameters from the scary internet, only allow the white list through. | |
def admin_driver_params | |
params.require(:driver).permit(:name,:email,:password,:phone_no,:avatar,:car_id, :company_user_id) | |
end | |
def sort_column | |
%w[email].include?(params[:sort]) ? params[:sort] : "email" | |
end | |
def sort_direction | |
%w[asc desc].include?(params[:direction]) ? params[:direction] : "asc" | |
end | |
def sort_order | |
sort_column + " "+ sort_direction | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment