Coder Social home page Coder Social logo

simplecontroller's Introduction

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.

simplecontroller's People

Contributors

philippneugebauer avatar

Watchers

James Cloos avatar  avatar

simplecontroller's Issues

Setup Test Environment automatically

Do this automatically:
Changes should be provided by a pull request. To set up the simplecontroller environment, the execution of rake db:migrate RAILS_ENV=test in test\dummy folder is required.

Rails really dependency?

Although the intention is to use the gem for rails, it is really required to use it with rails?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.