before_actions 1.0.3 → 2.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/before_actions.rb +3 -8
- data/lib/before_actions/controller/controller.rb +18 -0
- data/lib/before_actions/controller/scope.rb +24 -0
- data/lib/before_actions/monkey_patching.rb +5 -0
- data/lib/before_actions/version.rb +1 -1
- data/spec/controllers/bunny_controller_spec.rb +29 -0
- data/spec/controllers/cat_controller_spec.rb +29 -0
- data/spec/controllers/dog_controller_spec.rb +29 -0
- data/spec/controllers/squirrel_controller_spec.rb +29 -0
- data/spec/dummy/app/controllers/admin_foos_controller.rb +5 -5
- data/spec/dummy/app/controllers/bunny_controller.rb +24 -0
- data/spec/dummy/app/controllers/cat_controller.rb +19 -0
- data/spec/dummy/app/controllers/dog_controller.rb +18 -0
- data/spec/dummy/app/controllers/foos_controller.rb +6 -6
- data/spec/dummy/app/controllers/squirrel_controller.rb +16 -0
- data/spec/dummy/app/views/bunny/one.html.erb +2 -0
- data/spec/dummy/app/views/bunny/three.html.erb +2 -0
- data/spec/dummy/app/views/bunny/two.html.erb +2 -0
- data/spec/dummy/app/views/cat/one.html.erb +2 -0
- data/spec/dummy/app/views/cat/three.html.erb +2 -0
- data/spec/dummy/app/views/cat/two.html.erb +2 -0
- data/spec/dummy/app/views/dog/one.html.erb +2 -0
- data/spec/dummy/app/views/dog/three.html.erb +2 -0
- data/spec/dummy/app/views/dog/two.html.erb +2 -0
- data/spec/dummy/app/views/squirrel/one.html.erb +2 -0
- data/spec/dummy/app/views/squirrel/three.html.erb +2 -0
- data/spec/dummy/app/views/squirrel/two.html.erb +2 -0
- data/spec/dummy/config/routes.rb +18 -2
- data/spec/dummy/lib/templates/rails/scaffold_controller/controller.rb +25 -21
- data/spec/dummy/log/test.log +2700 -0
- metadata +47 -6
- data/app/controllers/before_actions/foo_controller.rb +0 -8
- data/lib/before_actions/controller.rb +0 -47
    
        checksums.yaml
    CHANGED
    
    | @@ -1,7 +1,7 @@ | |
| 1 1 | 
             
            ---
         | 
| 2 2 | 
             
            SHA1:
         | 
| 3 | 
            -
              metadata.gz:  | 
| 4 | 
            -
              data.tar.gz:  | 
| 3 | 
            +
              metadata.gz: 38d5782d5fc96b6f64457ca39615c5f14ebaffb6
         | 
| 4 | 
            +
              data.tar.gz: 32e5ba5e92e3cdc20c4a189b3b18736c76b6ff80
         | 
| 5 5 | 
             
            SHA512:
         | 
| 6 | 
            -
              metadata.gz:  | 
| 7 | 
            -
              data.tar.gz:  | 
| 6 | 
            +
              metadata.gz: 1232f85342de51775e3eae3e3253f0993ee5d76477aabb9a47c1623caf6a5335912060481248e6856136f65af48864953483e172fd44563e5667a7bd473f7631
         | 
| 7 | 
            +
              data.tar.gz: ef712f8fb8f3cdced98e658639b83f04145758f83490f0261361d1214aa59accde3664abd441de9624315255666097915a2a113ecb23d71ccd9f3b532c93ab74
         | 
    
        data/lib/before_actions.rb
    CHANGED
    
    | @@ -1,12 +1,7 @@ | |
| 1 1 | 
             
            require "before_actions/engine"
         | 
| 2 | 
            -
            require "before_actions/controller"
         | 
| 2 | 
            +
            require "before_actions/controller/scope"
         | 
| 3 | 
            +
            require "before_actions/controller/controller"
         | 
| 4 | 
            +
            require "before_actions/monkey_patching"
         | 
| 3 5 |  | 
| 4 6 | 
             
            module BeforeActions
         | 
| 5 7 | 
             
            end
         | 
| 6 | 
            -
             | 
| 7 | 
            -
             | 
| 8 | 
            -
            if defined? ActionController::Base
         | 
| 9 | 
            -
              ActionController::Base.class_eval do
         | 
| 10 | 
            -
                include BeforeActions::Controller
         | 
| 11 | 
            -
              end
         | 
| 12 | 
            -
            end
         | 
| @@ -0,0 +1,18 @@ | |
| 1 | 
            +
            module BeforeActions
         | 
| 2 | 
            +
              module Controller
         | 
| 3 | 
            +
                extend ActiveSupport::Concern
         | 
| 4 | 
            +
             | 
| 5 | 
            +
                module ClassMethods
         | 
| 6 | 
            +
                  def before_actions(&block)
         | 
| 7 | 
            +
                    Scope.new(self, 'before_action').instance_eval(&block)
         | 
| 8 | 
            +
                  end
         | 
| 9 | 
            +
                  def after_actions(&block)
         | 
| 10 | 
            +
                    Scope.new(self, 'after_action').instance_eval(&block)
         | 
| 11 | 
            +
                  end
         | 
| 12 | 
            +
                  def around_actions(&block)
         | 
| 13 | 
            +
                    Scope.new(self, 'around_action').instance_eval(&block)
         | 
| 14 | 
            +
                  end
         | 
| 15 | 
            +
                end
         | 
| 16 | 
            +
             | 
| 17 | 
            +
              end
         | 
| 18 | 
            +
            end
         | 
| @@ -0,0 +1,24 @@ | |
| 1 | 
            +
            module BeforeActions
         | 
| 2 | 
            +
              module Controller
         | 
| 3 | 
            +
                class Scope
         | 
| 4 | 
            +
             | 
| 5 | 
            +
                  def initialize(controller, the_method)
         | 
| 6 | 
            +
                    @controller = controller
         | 
| 7 | 
            +
                    @the_method = the_method
         | 
| 8 | 
            +
                  end
         | 
| 9 | 
            +
             | 
| 10 | 
            +
                  def all(&block)
         | 
| 11 | 
            +
                    @controller.send(@the_method, &block)
         | 
| 12 | 
            +
                  end
         | 
| 13 | 
            +
             | 
| 14 | 
            +
                  def only(*list, &block)
         | 
| 15 | 
            +
                    @controller.send(@the_method, {only: list}, &block)
         | 
| 16 | 
            +
                  end
         | 
| 17 | 
            +
             | 
| 18 | 
            +
                  def except(*list, &block)
         | 
| 19 | 
            +
                    @controller.send(@the_method, {only: list}, &block)
         | 
| 20 | 
            +
                  end
         | 
| 21 | 
            +
             | 
| 22 | 
            +
                end
         | 
| 23 | 
            +
              end
         | 
| 24 | 
            +
            end
         | 
| @@ -0,0 +1,29 @@ | |
| 1 | 
            +
            require 'rails_helper'
         | 
| 2 | 
            +
             | 
| 3 | 
            +
            RSpec.describe BunnyController, :type => :controller do
         | 
| 4 | 
            +
             | 
| 5 | 
            +
              describe "GET one" do
         | 
| 6 | 
            +
                it "returns http success" do
         | 
| 7 | 
            +
                  get :one
         | 
| 8 | 
            +
                  expect(response).to have_http_status(:success)
         | 
| 9 | 
            +
                  expect(assigns(:bunny)).to eq("one")
         | 
| 10 | 
            +
                end
         | 
| 11 | 
            +
              end
         | 
| 12 | 
            +
             | 
| 13 | 
            +
              describe "GET two" do
         | 
| 14 | 
            +
                it "returns http success" do
         | 
| 15 | 
            +
                  get :two
         | 
| 16 | 
            +
                  expect(response).to have_http_status(:success)
         | 
| 17 | 
            +
                  expect(assigns(:bunny)).to eq("two-around two- -single-responsibility- -around two-")
         | 
| 18 | 
            +
                end
         | 
| 19 | 
            +
              end
         | 
| 20 | 
            +
             | 
| 21 | 
            +
              describe "GET three" do
         | 
| 22 | 
            +
                it "returns http success" do
         | 
| 23 | 
            +
                  get :three
         | 
| 24 | 
            +
                  expect(response).to have_http_status(:success)
         | 
| 25 | 
            +
                  expect(assigns(:bunny)).to eq("three")
         | 
| 26 | 
            +
                end
         | 
| 27 | 
            +
              end
         | 
| 28 | 
            +
             | 
| 29 | 
            +
            end
         | 
| @@ -0,0 +1,29 @@ | |
| 1 | 
            +
            require 'rails_helper'
         | 
| 2 | 
            +
             | 
| 3 | 
            +
            RSpec.describe CatController, :type => :controller do
         | 
| 4 | 
            +
             | 
| 5 | 
            +
              describe "GET one" do
         | 
| 6 | 
            +
                it "returns http success" do
         | 
| 7 | 
            +
                  get :one
         | 
| 8 | 
            +
                  expect(response).to have_http_status(:success)
         | 
| 9 | 
            +
                  expect(assigns(:cat)).to eq("one")
         | 
| 10 | 
            +
                end
         | 
| 11 | 
            +
              end
         | 
| 12 | 
            +
             | 
| 13 | 
            +
              describe "GET two" do
         | 
| 14 | 
            +
                it "returns http success" do
         | 
| 15 | 
            +
                  get :two
         | 
| 16 | 
            +
                  expect(response).to have_http_status(:success)
         | 
| 17 | 
            +
                  expect(assigns(:cat)).to eq("after two")
         | 
| 18 | 
            +
                end
         | 
| 19 | 
            +
              end
         | 
| 20 | 
            +
             | 
| 21 | 
            +
              describe "GET three" do
         | 
| 22 | 
            +
                it "returns http success" do
         | 
| 23 | 
            +
                  get :three
         | 
| 24 | 
            +
                  expect(response).to have_http_status(:success)
         | 
| 25 | 
            +
                  expect(assigns(:cat)).to eq("three")
         | 
| 26 | 
            +
                end
         | 
| 27 | 
            +
              end
         | 
| 28 | 
            +
             | 
| 29 | 
            +
            end
         | 
| @@ -0,0 +1,29 @@ | |
| 1 | 
            +
            require 'rails_helper'
         | 
| 2 | 
            +
             | 
| 3 | 
            +
            RSpec.describe DogController, :type => :controller do
         | 
| 4 | 
            +
             | 
| 5 | 
            +
              describe "GET one" do
         | 
| 6 | 
            +
                it "returns http success" do
         | 
| 7 | 
            +
                  get :one
         | 
| 8 | 
            +
                  expect(response).to have_http_status(:success)
         | 
| 9 | 
            +
                  expect(assigns(:dog)).to eq("before one and two")
         | 
| 10 | 
            +
                end
         | 
| 11 | 
            +
              end
         | 
| 12 | 
            +
             | 
| 13 | 
            +
              describe "GET two" do
         | 
| 14 | 
            +
                it "returns http success" do
         | 
| 15 | 
            +
                  get :two
         | 
| 16 | 
            +
                  expect(response).to have_http_status(:success)
         | 
| 17 | 
            +
                  expect(assigns(:dog)).to eq("before one and two")
         | 
| 18 | 
            +
                end
         | 
| 19 | 
            +
              end
         | 
| 20 | 
            +
             | 
| 21 | 
            +
              describe "GET three" do
         | 
| 22 | 
            +
                it "returns http success" do
         | 
| 23 | 
            +
                  get :three
         | 
| 24 | 
            +
                  expect(response).to have_http_status(:success)
         | 
| 25 | 
            +
                  expect(assigns(:dog)).to eq("three")
         | 
| 26 | 
            +
                end
         | 
| 27 | 
            +
              end
         | 
| 28 | 
            +
             | 
| 29 | 
            +
            end
         | 
| @@ -0,0 +1,29 @@ | |
| 1 | 
            +
            require 'rails_helper'
         | 
| 2 | 
            +
             | 
| 3 | 
            +
            RSpec.describe SquirrelController, :type => :controller do
         | 
| 4 | 
            +
             | 
| 5 | 
            +
              describe "GET one" do
         | 
| 6 | 
            +
                it "returns http success" do
         | 
| 7 | 
            +
                  get :one
         | 
| 8 | 
            +
                  expect(response).to have_http_status(:success)
         | 
| 9 | 
            +
                  expect(assigns(:squirrel)).to eq("except before one")
         | 
| 10 | 
            +
                end
         | 
| 11 | 
            +
              end
         | 
| 12 | 
            +
             | 
| 13 | 
            +
              describe "GET two" do
         | 
| 14 | 
            +
                it "returns http success" do
         | 
| 15 | 
            +
                  get :two
         | 
| 16 | 
            +
                  expect(response).to have_http_status(:success)
         | 
| 17 | 
            +
                  expect(assigns(:squirrel)).to eq("two")
         | 
| 18 | 
            +
                end
         | 
| 19 | 
            +
              end
         | 
| 20 | 
            +
             | 
| 21 | 
            +
              describe "GET three" do
         | 
| 22 | 
            +
                it "returns http success" do
         | 
| 23 | 
            +
                  get :three
         | 
| 24 | 
            +
                  expect(response).to have_http_status(:success)
         | 
| 25 | 
            +
                  expect(assigns(:squirrel)).to eq("three")
         | 
| 26 | 
            +
                end
         | 
| 27 | 
            +
              end
         | 
| 28 | 
            +
             | 
| 29 | 
            +
            end
         | 
| @@ -2,11 +2,11 @@ class AdminFoosController < ApplicationController | |
| 2 2 |  | 
| 3 3 | 
             
              # Use callbacks to share common setup or constraints between actions.
         | 
| 4 4 | 
             
              before_actions do
         | 
| 5 | 
            -
                 | 
| 6 | 
            -
                 | 
| 7 | 
            -
                 | 
| 8 | 
            -
                 | 
| 9 | 
            -
                 | 
| 5 | 
            +
                only(:new, :create, :edit, :update, :destroy) { render_not_authorized }
         | 
| 6 | 
            +
                only(:index)                          { @admin_foos = AdminFoo.all               }
         | 
| 7 | 
            +
                only(:new)                            { raise_it      }
         | 
| 8 | 
            +
                only(:create)                         { raise_it      }
         | 
| 9 | 
            +
                only(:show, :edit, :update, :destroy) { @admin_foo  = AdminFoo.find(params[:id]) }
         | 
| 10 10 | 
             
              end
         | 
| 11 11 |  | 
| 12 12 | 
             
              def index
         | 
| @@ -0,0 +1,24 @@ | |
| 1 | 
            +
            class BunnyController < ApplicationController
         | 
| 2 | 
            +
             | 
| 3 | 
            +
              before_actions do
         | 
| 4 | 
            +
                all { @bunny = action_name }
         | 
| 5 | 
            +
              end
         | 
| 6 | 
            +
             | 
| 7 | 
            +
              around_actions do
         | 
| 8 | 
            +
                only(:two) do |controller, action|
         | 
| 9 | 
            +
                  @bunny += "-around two-"
         | 
| 10 | 
            +
                  action.call
         | 
| 11 | 
            +
                  @bunny += "-around two-"
         | 
| 12 | 
            +
                end
         | 
| 13 | 
            +
              end
         | 
| 14 | 
            +
             | 
| 15 | 
            +
              def one
         | 
| 16 | 
            +
              end
         | 
| 17 | 
            +
             | 
| 18 | 
            +
              def two
         | 
| 19 | 
            +
                @bunny += " -single-responsibility- "
         | 
| 20 | 
            +
              end
         | 
| 21 | 
            +
             | 
| 22 | 
            +
              def three
         | 
| 23 | 
            +
              end
         | 
| 24 | 
            +
            end
         | 
| @@ -0,0 +1,18 @@ | |
| 1 | 
            +
            class DogController < ApplicationController
         | 
| 2 | 
            +
             | 
| 3 | 
            +
              before_actions do
         | 
| 4 | 
            +
                all              { @dog = action_name }
         | 
| 5 | 
            +
                only(:one)       { @dog = "before one" }
         | 
| 6 | 
            +
                only(:one, :two) { @dog = "before one and two" }
         | 
| 7 | 
            +
              end
         | 
| 8 | 
            +
             | 
| 9 | 
            +
              def one
         | 
| 10 | 
            +
              end
         | 
| 11 | 
            +
             | 
| 12 | 
            +
              def two
         | 
| 13 | 
            +
              end
         | 
| 14 | 
            +
             | 
| 15 | 
            +
              def three
         | 
| 16 | 
            +
              end
         | 
| 17 | 
            +
             | 
| 18 | 
            +
            end
         | 
| @@ -2,12 +2,12 @@ class FoosController < ApplicationController | |
| 2 2 |  | 
| 3 3 | 
             
              # Use callbacks to share common setup or constraints between actions.
         | 
| 4 4 | 
             
              before_actions do
         | 
| 5 | 
            -
                 | 
| 6 | 
            -
                 | 
| 7 | 
            -
                 | 
| 8 | 
            -
                 | 
| 9 | 
            -
                 | 
| 10 | 
            -
                 | 
| 5 | 
            +
                # all                                   {  } # load your nested resource's parent here if you need one
         | 
| 6 | 
            +
                only(:index)                          { @foos = Foo.all                }
         | 
| 7 | 
            +
                only(:new)                            { @foo  = Foo.new                }
         | 
| 8 | 
            +
                only(:create)                         { @foo  = Foo.new(foo_params)    }
         | 
| 9 | 
            +
                only(:show, :edit, :update, :destroy) { @foo  = Foo.find(params[:id])  }
         | 
| 10 | 
            +
                # all                                   {  } # run your authorization logic here if you need one
         | 
| 11 11 | 
             
              end
         | 
| 12 12 |  | 
| 13 13 | 
             
              # GET /foos
         | 
    
        data/spec/dummy/config/routes.rb
    CHANGED
    
    | @@ -1,8 +1,24 @@ | |
| 1 1 | 
             
            Rails.application.routes.draw do
         | 
| 2 2 |  | 
| 3 | 
            -
               | 
| 3 | 
            +
              get 'squirrel/one'
         | 
| 4 | 
            +
             | 
| 5 | 
            +
              get 'squirrel/two'
         | 
| 6 | 
            +
             | 
| 7 | 
            +
              get 'squirrel/three'
         | 
| 8 | 
            +
             | 
| 9 | 
            +
              get 'bunny/one'
         | 
| 10 | 
            +
              get 'bunny/two'
         | 
| 11 | 
            +
              get 'bunny/three'
         | 
| 4 12 |  | 
| 13 | 
            +
              get 'cat/one'
         | 
| 14 | 
            +
              get 'cat/two'
         | 
| 15 | 
            +
              get 'cat/three'
         | 
| 16 | 
            +
             | 
| 17 | 
            +
              get 'dog/one'
         | 
| 18 | 
            +
              get 'dog/two'
         | 
| 19 | 
            +
              get 'dog/three'
         | 
| 20 | 
            +
             | 
| 21 | 
            +
              resources :admin_foos
         | 
| 5 22 | 
             
              resources :foos
         | 
| 6 23 |  | 
| 7 | 
            -
              mount BeforeActions::Engine => "/load_resource"
         | 
| 8 24 | 
             
            end
         | 
| @@ -7,12 +7,13 @@ class <%= controller_class_name %>Controller < ApplicationController | |
| 7 7 |  | 
| 8 8 | 
             
              # Use callbacks to share common setup or constraints between actions.
         | 
| 9 9 | 
             
              before_actions do
         | 
| 10 | 
            -
                 | 
| 11 | 
            -
                 | 
| 12 | 
            -
                 | 
| 13 | 
            -
                 | 
| 14 | 
            -
                 | 
| 15 | 
            -
                 | 
| 10 | 
            +
                # all                                   {  } # pre-fetching authorization step
         | 
| 11 | 
            +
                # all                                   {  } # load your nested resource's parent here
         | 
| 12 | 
            +
                only(:index)                          { @<%= plural_table_name %> = <%= orm_class.all(class_name) %>                }
         | 
| 13 | 
            +
                only(:new)                            { @<%= singular_table_name %>  = <%= orm_class.build(class_name) %>                }
         | 
| 14 | 
            +
                only(:create)                         { @<%= singular_table_name %>  = <%= orm_class.build(class_name, "#{singular_table_name}_params") %> }
         | 
| 15 | 
            +
                only(:show, :edit, :update, :destroy) { @<%= singular_table_name %>  = <%= orm_class.find(class_name, "params[:id]") %>  }
         | 
| 16 | 
            +
                # all                                   {  } # post-fetching authorization step
         | 
| 16 17 | 
             
              end
         | 
| 17 18 |  | 
| 18 19 | 
             
              # GET <%= route_url %>
         | 
| @@ -26,25 +27,11 @@ class <%= controller_class_name %>Controller < ApplicationController | |
| 26 27 |  | 
| 27 28 |  | 
| 28 29 |  | 
| 30 | 
            +
             | 
| 29 31 | 
             
              # GET <%= route_url %>/new
         | 
| 30 32 | 
             
              def new
         | 
| 31 33 | 
             
              end
         | 
| 32 34 |  | 
| 33 | 
            -
              # GET <%= route_url %>/1
         | 
| 34 | 
            -
              # GET <%= route_url %>/1.json
         | 
| 35 | 
            -
              def show
         | 
| 36 | 
            -
                respond_to do |format|
         | 
| 37 | 
            -
                  format.html # show.html.erb
         | 
| 38 | 
            -
                  format.json { render json: <%= "@#{singular_table_name}" %> }
         | 
| 39 | 
            -
                end
         | 
| 40 | 
            -
              end
         | 
| 41 | 
            -
             | 
| 42 | 
            -
              # GET <%= route_url %>/1/edit
         | 
| 43 | 
            -
              def edit
         | 
| 44 | 
            -
              end
         | 
| 45 | 
            -
             | 
| 46 | 
            -
             | 
| 47 | 
            -
             | 
| 48 35 | 
             
              # POST <%= route_url %>
         | 
| 49 36 | 
             
              # POST <%= route_url %>.json
         | 
| 50 37 | 
             
              def create
         | 
| @@ -59,6 +46,23 @@ class <%= controller_class_name %>Controller < ApplicationController | |
| 59 46 | 
             
                end
         | 
| 60 47 | 
             
              end
         | 
| 61 48 |  | 
| 49 | 
            +
             | 
| 50 | 
            +
             | 
| 51 | 
            +
             | 
| 52 | 
            +
             | 
| 53 | 
            +
              # GET <%= route_url %>/1
         | 
| 54 | 
            +
              # GET <%= route_url %>/1.json
         | 
| 55 | 
            +
              def show
         | 
| 56 | 
            +
                respond_to do |format|
         | 
| 57 | 
            +
                  format.html # show.html.erb
         | 
| 58 | 
            +
                  format.json { render json: <%= "@#{singular_table_name}" %> }
         | 
| 59 | 
            +
                end
         | 
| 60 | 
            +
              end
         | 
| 61 | 
            +
             | 
| 62 | 
            +
              # GET <%= route_url %>/1/edit
         | 
| 63 | 
            +
              def edit
         | 
| 64 | 
            +
              end
         | 
| 65 | 
            +
             | 
| 62 66 | 
             
              # PATCH/PUT <%= route_url %>/1
         | 
| 63 67 | 
             
              # PATCH/PUT <%= route_url %>/1.json
         | 
| 64 68 | 
             
              def update
         |