Project

rescue-dog

0.0
No commit activity in last 3 years
No release in over 3 years
Declare simple CRUD and respond error methods.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

Runtime

>= 4.0.0
 Project Readme

Rescue Dog

Gem Version Coverage Status Build Status Dependency Status

The Rescue-Dog responds HTTP status (the code and message) when raises the exception for Rails.

Installation

Add "rescue-dog" gem to your Gemfile

gem 'rescue-dog'

And run bundle install command.

Declare CRUD Actions

  1. Include Rescue::Controller (Rescue::Controller::StaticorRescue::Controller::Dynamic`).
  2. Call rescue_controller method.
  3. Declare xxx_params method. (cf. Rails 4 / Strong Parameters)

Simple CRUD Actions

class UsersController < ApplicationController
  rescue_controller User, :new, :edit, :show,
    create: { render: lambda { redirect_to edit_user_path(@user) } ,rescue: lambda { render :new  } },
    update: { render: lambda { redirect_to edit_user_path(@user) } ,rescue: lambda { render :edit } },
    destroy: { render: lambda { redirect_to root_path }             ,rescue: lambda { render :edit } }

  ...

  private
  def create_params
    params.require(:user).permit(
      :name, :email, :password
    )
  end

  def update_params
    params.require(:user).permit(
      :email, :password
    )
  end

  # Destroyed condition variable object
  def destroy_params
    { id: params[:id] }
  end
end

Customized Actions

class UsersController < ApplicationController
  rescue_controller User, :new, :edit, :show

  def create
    rescue_respond(:customized_create_call, create_params,
      render: lambda { redirect_to edit_user_path(@user) },
      rescue: lambda { render :new  } }
    )
  end

  private
  def create_params
    params.require(:user).permit(
      :name, :email, :password
    )
  end

  def customized_create_call params
    if User.exists?(params)
      raise UserDuplicationError, "your email is duplicated!"
    else
      @user = User.new(params)
      @user.save!
    end
  end

end

Render Errors

  1. Include Rescue::Controller::Static or Rescue::Controller::Dynamic.
  2. Call rescue_associate method. And then, the exception class is defined and added to rescue_handlers.
  3. Raise the exception or Call response_status method.

Render Static Files

Render /public/400(.:format) if you raise BadRequest exception.

class ApplicationController
   
  include Rescue::Controller::Static
  rescue_associate :BadRequest   ,with: 400
  rescue_associate :Unauthorized ,with: 401
  rescue_associate :NotFound     ,with: 404
  rescue_associate :ServerError  ,with: 500

Render Template

Render app/views/errors/404(.:format) if you raise NotFound exception.

class ApplicationController
   
  include Rescue::Controller::Dynamic
  rescue_associate :BadRequest   ,with: 400
  rescue_associate :Unauthorized ,with: 401
  rescue_associate :NotFound     ,with: 404
  rescue_associate :ServerError  ,with: 500

Associated with the exceptions

Call the response method when raise an exception.

for ActiveRecord

rescue_associate ActiveRecord::RecordNotFound ,with: 404

for Mongoid

rescue_associate Mongoid::Errors::DocumentNotFound, BSON::InvalidObjectId, with: 404

Learn more usage, check spec

Respond Application Error Codes

  1. Include Rescue::Controller::Dynamic (NOT Rescue::Controller::Static).
  2. Include Rescue::RespondError
  3. Raise the exception (cf. Rescue::ApplicationError::STATUS_CODES)
class ApplicationController
  include Rescue::Controller::Dynamic
  include Rescue::RespondError

Suppress HTTP Response Codes

All responses will be returned with a 200 OK status code, even if the error occurs.

Rescue.configure do |config|
  config.suppress_response_codes = true
end

Contributing

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request

LICENSE

(The MIT License)

Copyright © 2013 yulii. See LICENSE.txt for further details.