Effective Resources

Make your controller an effective resource controller.

Implements the 7 RESTful actions as a one-liner on any controller.

Getting Started

gem 'effective_resources'

Run the bundle command to install it:

bundle install

Install the configuration file:

rails generate effective_resources:install

The generator will install an initializer which describes all configuration options.

Usage

Add to your contoller:

class PostsController < ApplicationController
  include Effective::CrudController

  member_action :something

  protected

  # If it's a Hash of attributes, the controller will call .where(attributes)
  # and the attributes will be used to initialize the datatable on index
  #
  # If it's an ActiveRecord scope, or symbol, we initialize a datatable with {resource_scope: true}
  # and leave it as a TODO for the datatable to do the right thin .
  def post_scope
    {client_id: current_user.client_id}
    # Post.where(client_id: current_user.client_id)
    # :approved
  end

  def post_params
    params.require(:post).permit(:id, :title, :body)
  end

end

What it does

Implements the 7 RESTful actions: index, new, create, show, edit, update, destroy.

  • Loads an appropriate @post type instance
  • Sets a @page_title (effective_pages).
  • Calls authorize as per the configured EffectiveResources.authorization_method (flow through to CanCan or Pundit)
  • Does the create/update save
  • Sets a flash[:success] and redirects on success, or sets a flash.now[:danger] and renders on error.

Helpers

simple_form_submit

Call simple_form_submit(f) like follows:

= simple_form_for(post) do |f|
  = f.input :title
  = f.input :body

  = simple_form_submit(f)

to render 3 submit buttons: Save, Save and Continue, and Save and Add New.

simple_form_save

Call simple_form_save(f) like follows:

= simple_form_for(post) do |f|
  ...
  = simple_form_save(f)

to render just the Save button, with appropriate data-disable, title, etc.

License

MIT License. Copyright Code and Effect Inc.

Contributing

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Bonus points for test coverage
  6. Create new Pull Request