New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

implicit_resource

Package Overview
Dependencies
Maintainers
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

implicit_resource

  • 0.2.0
  • Rubygems
  • Socket score

Version published
Maintainers
1
Created
Source

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_params
    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.

FAQs

Package last updated on 21 Apr 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc