Skip to content

before-actions-gem/before_actions

Repository files navigation

Before Actions

Build Status Gem Version Dependency Status Code Climate License

Organize your controllers by grouping your before and after filters.

Why use this gem?

As developers, our teams are composed of a range of developers .... it's not always easy to put everybody in the same page

phases: authentication - your auth gem will do this one

pre-fetching-authorization fetching post-fetching-authorization

actual logic - action block

Ruby

Our tests cases run with 1.9.3, 2.0.0 and 2.1.0

We encourage you to work with any 1.9+ Ruby version

Rails

You can use this gem with Rails 3.1, 3.2 and 4+

Installation

Add this line to your application's Gemfile:

gem 'before_actions'

And then execute:

bundle

If you want your scaffold generated code to always look like this, just run:

rails g before_actions:template

Upgrading from v1.*

If you were already using this gem in your project and want to upgrade it, simply run:

bundle update before_actions
rails g before_actions:template

Then simply adjust your controllers to the new syntax

Demo

Restful

resource.png

Nested

nested.png

Sample Code

class ContactsController < ApplicationController

  # load and authorize resources
  before_actions do
    # listing actions
    only(:index) { @contacts = Contact.all }

    # building actions
    only(:new)    { @contact = Contact.new }
    only(:create) { @contact = Contact.new(contact_params) }

    # member actions, will raise a 404 if the model is not found
    only(:show, :edit, :update, :destroy) { @contact = Contact.find(params[:id]) }
  end

  after_actions do
    all { your_code_here }
    except(:index) { your_code_here }
  end


  around_actions do
    only(:create) do |controller, action|
      your_code
      action.call
      in_here
    end
  end

end

References

Contributing

  1. Fork it ( https://github.com/github.com/before-actions-gem/before_actions/fork )
  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. Create a new Pull Request

Versioning

Before Actions uses Semantic Versioning 2.0.0

About

Rails Gem to organize your controllers

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published