Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

resourceable

Package Overview
Dependencies
Maintainers
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

resourceable

  • 0.1.2.1
  • Rubygems
  • Socket score

Version published
Maintainers
1
Created
Source

Resourceable

Replaces the need for scaffolded controllers while not restricting the developer.

Live Stream

I've been live streaming the creation of this gem over on Twitch. Videos are also posted to a YouTube Playlist

Dependencies

  • CanCanCan - loading and authorizing resource
  • Ransack - searching and sorting
  • Kaminari - pagination

Installation

Add this line to your application's Gemfile:

gem 'resourceable'

Usage

Run the installer. Resourceable will also run the installers for Kaminari, CanCan and Responders.

rails g resourceable:install

CRUD

Replaces the need for scaffolded controller code. You will still need too create the views (for now).

class UsersController < ApplicationController 
  crud permitted: [ # strong params (default to {})
    :email, :password 
    ], 
    cancan: { # cancan options (default is cancan default)
      class_name: User, 
      id_params: :user_id 
    },
    q: :search # ransack url variable (defaults to :q)
end

Strong params can be defined in an instance method named resource_params

class UsersController < ApplicationController 
  crud

  private 

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

Any of the regular CRUD actions can be done as one normally would. If you need some custom handling of resource creation, just define it. Resourceable will still handle any actions you haven't defined.

class UsersController < ApplicationController 
  crud permitted: [:email, :password]

  def create 
    if @user.valid?
      @user.save 
      UserMailer.new_user(@user).deliver_now
    else 
      render :new
    end
  end
end

Pagination

Pagination can be configured through the pagination option. Everything else is just plain Kaminari.

class UsersController < ApplicationController 
  crud pagination: {
      param: :user_page,  # default is :page, 
      per: 10,            # default is 20
    }
end

SimpleForm Inputs

has_many

Resourceable adds a new SimpleForm input of has_many to simplify has_many associations. Resourceable will use the provided partial to display the associated objects. Don't forget to update the permitted params in your controller for the nested attributes.

class Task < ApplicationRecord 
  has_many :details 
  accepts_nested_attributes_for :details
end

class Detail < ApplicationRecord 
  belongs_to :task
end

/ app/views/tasks/_form.slim
= simple_form_for @task do |f|
  = f.input :details, as: :has_many, partial: 'tasks/detail_fields'
/ app/views/tasks/_detail_fields.slim
= form_object.input :body

Contributing

Contribution directions go here.

License

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

FAQs

Package last updated on 24 Jul 2018

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