Code Monkey home page Code Monkey logo

implicit_resource's Introduction

ImplicitResource

Gem Version

In most cases you will have the same old code in your controllers, this gem allows some very basic assumptions while giving you the option to override the behavior. This allows your controllers to be limited to just the required code.

Assumptions this code makes:

  1. The controller is named for the model. e.g. ArticlesController will assume Article model.
  2. Standard restful actions: index, show, new, create, edit, update, destroy.
  3. A method named permitted_attributes exists for permitted_params.

Here's an example of the bare minimum code required to get started:

ArticlesController < ApplicationController
  include ImplicitResource # Opt in to implicit behavior, or include it in ApplicationController.

  private

  def permitted_attributes(resource)
    params.require(:article).permit(:title, :body)
  end
end

Installationc

Add this line to your application's Gemfile:

gem "implicit_resource"

Add this line to your controller

include ImplicitResource

Customization

ImplicitResource only targets basic controller functionality. Everything that implicit_resource does can be overriden in the controller.

Overriding resource methods

You can override the collection and resource methods to achieve more flexibility in how things are queried.

ArticlesController < ApplicationController
  include ImplicitResource

  private

  def collection # Collection is a placeholder for any collection of objects to be returned
    model_klass.includes(:comments).search(title: params[:title_search_terms])
  end

  def resource # Resource is a placeholder for any single object to be returned
    collection.find_by(token_id: params[:token_id])
  end
end

Overriding the model class

You can override the model_klass method to allow for controller/model naming mismatch.

ArticlesController < ApplicationController
  include ImplicitResource

  private

  def model_klass
    Story
  end
end

License

The gem is available as open source under the terms of the MIT License.

implicit_resource's People

Contributors

codezomb avatar

Watchers

 avatar

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.