Skip to content

Latest commit

 

History

History
91 lines (69 loc) · 3.34 KB

README.md

File metadata and controls

91 lines (69 loc) · 3.34 KB

SimpleController

Build Status Code Climate Test Coverage Dependency Status

This gem provides you the standard logic of a rails controller which needn't to be implemented by every controller in the same way. Therefore, this gem gives you the chance to avoid a lot of redundant code which furthermore improves the maintainability of your code. It requires Ruby >= 2.0.

Use it in your application

Installation is done by the following commands:

$ gem install simple_controller
$ bundle

Use it in your controllers:

class YourController < ActionController::Base (or another parent class extending this class)
  include SimpleController::IndexController (index)
  include SimpleController::CreateController (new + create)
  include SimpleController::ShowController (show)
  include SimpleController::UpdateController (edit + update)
  include SimpleController::DestroyController (destroy)

The generated instance variable usable for your views will be plural model name at index site, otherwise the singular model name. Assuming a model called pen will named @pens at index site, otherwise @pen.

Provided methods

Actually, SimpleController provides the CRUD methods of the included controllers. If you don't want to have permitted all model attributes, you need to override the model_params method. If you use a customized id for your model, you also need to override the set_object method.

That means that you just need to add a method like this to your class:

def model_params
    params.require(:user).permit(:name, :age)
end

or

def set_object
    User.find(params[:email])
end

If you want to change the default notices, you need to override the destroy_notice, create_notice or update_notice methods returning your opted message as string. Per default, the name of the model and a description of the action is displayed:

def create_notice
  # e.g. User has been created
  "#{model_name} #{I18n.t('successful_creation')}"
end

Moreover, the redirect paths after a creation, update and delete can be customized by overriding the create_redirect, update_redirect or delete_redirect method. The all expect the wanted path as string output:

def create_redirect
  # must be the path as string!
  'my_custom_path'
end

Required Adaption:

Add to your language files translations for the following symbols which are appended after the model's name:

your_language:
  successful_creation: has been created
  successful_update: has been updated
  successful_deletion: has been deleted
  unsuccessful_deletion: could not deleted

License

MIT

Contribute

Changes should be provided by a pull request. Additionally, rake must be executed outside the test folders to work correctly.