before_actions 1.0.3 → 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (34) hide show
  1. checksums.yaml +4 -4
  2. data/lib/before_actions.rb +3 -8
  3. data/lib/before_actions/controller/controller.rb +18 -0
  4. data/lib/before_actions/controller/scope.rb +24 -0
  5. data/lib/before_actions/monkey_patching.rb +5 -0
  6. data/lib/before_actions/version.rb +1 -1
  7. data/spec/controllers/bunny_controller_spec.rb +29 -0
  8. data/spec/controllers/cat_controller_spec.rb +29 -0
  9. data/spec/controllers/dog_controller_spec.rb +29 -0
  10. data/spec/controllers/squirrel_controller_spec.rb +29 -0
  11. data/spec/dummy/app/controllers/admin_foos_controller.rb +5 -5
  12. data/spec/dummy/app/controllers/bunny_controller.rb +24 -0
  13. data/spec/dummy/app/controllers/cat_controller.rb +19 -0
  14. data/spec/dummy/app/controllers/dog_controller.rb +18 -0
  15. data/spec/dummy/app/controllers/foos_controller.rb +6 -6
  16. data/spec/dummy/app/controllers/squirrel_controller.rb +16 -0
  17. data/spec/dummy/app/views/bunny/one.html.erb +2 -0
  18. data/spec/dummy/app/views/bunny/three.html.erb +2 -0
  19. data/spec/dummy/app/views/bunny/two.html.erb +2 -0
  20. data/spec/dummy/app/views/cat/one.html.erb +2 -0
  21. data/spec/dummy/app/views/cat/three.html.erb +2 -0
  22. data/spec/dummy/app/views/cat/two.html.erb +2 -0
  23. data/spec/dummy/app/views/dog/one.html.erb +2 -0
  24. data/spec/dummy/app/views/dog/three.html.erb +2 -0
  25. data/spec/dummy/app/views/dog/two.html.erb +2 -0
  26. data/spec/dummy/app/views/squirrel/one.html.erb +2 -0
  27. data/spec/dummy/app/views/squirrel/three.html.erb +2 -0
  28. data/spec/dummy/app/views/squirrel/two.html.erb +2 -0
  29. data/spec/dummy/config/routes.rb +18 -2
  30. data/spec/dummy/lib/templates/rails/scaffold_controller/controller.rb +25 -21
  31. data/spec/dummy/log/test.log +2700 -0
  32. metadata +47 -6
  33. data/app/controllers/before_actions/foo_controller.rb +0 -8
  34. data/lib/before_actions/controller.rb +0 -47
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 2e4ee37335a868dce4d6989fa8bcf2aa51211305
4
- data.tar.gz: 04d348d389f0bf9b38a85f6a1c6280a4b28988dd
3
+ metadata.gz: 38d5782d5fc96b6f64457ca39615c5f14ebaffb6
4
+ data.tar.gz: 32e5ba5e92e3cdc20c4a189b3b18736c76b6ff80
5
5
  SHA512:
6
- metadata.gz: c34525ee42fbf171c2b3e390f05a4a7e39643b2e76980ce1fe1303c40ae33905fa2c9f33fc70546c12f036f4f301bf884370448ea9f87480d98e224d20a68d1e
7
- data.tar.gz: 7a5453eaddb502f232981e1c56022fab78c2b3ca0b591537b099a014af8b0eba8a61cdb64db95315eccec84bd45b542fa6e120225a2168e21617966a82831a9f
6
+ metadata.gz: 1232f85342de51775e3eae3e3253f0993ee5d76477aabb9a47c1623caf6a5335912060481248e6856136f65af48864953483e172fd44563e5667a7bd473f7631
7
+ data.tar.gz: ef712f8fb8f3cdced98e658639b83f04145758f83490f0261361d1214aa59accde3664abd441de9624315255666097915a2a113ecb23d71ccd9f3b532c93ab74
@@ -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,5 @@
1
+ if defined? ActionController::Base
2
+ ActionController::Base.class_eval do
3
+ include BeforeActions::Controller
4
+ end
5
+ end
@@ -1,3 +1,3 @@
1
1
  module BeforeActions
2
- VERSION = "1.0.3"
2
+ VERSION = "2.0.0"
3
3
  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
- actions(:new, :create, :edit, :update, :destroy) { render_not_authorized }
6
- actions(:index) { @admin_foos = AdminFoo.all }
7
- actions(:new) { raise_it }
8
- actions(:create) { raise_it }
9
- actions(:show, :edit, :update, :destroy) { @admin_foo = AdminFoo.find(params[:id]) }
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,19 @@
1
+ class CatController < ApplicationController
2
+
3
+ before_actions do
4
+ all { @cat = action_name }
5
+ end
6
+
7
+ after_actions do
8
+ only(:two) { @cat = "after two" }
9
+ end
10
+
11
+ def one
12
+ end
13
+
14
+ def two
15
+ end
16
+
17
+ def three
18
+ end
19
+ 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
- actions { } # load your nested resource's parent here if you need one
6
- actions(:index) { @foos = Foo.all }
7
- actions(:new) { @foo = Foo.new }
8
- actions(:create) { @foo = Foo.new(foo_params) }
9
- actions(:show, :edit, :update, :destroy) { @foo = Foo.find(params[:id]) }
10
- actions { } # run your authorization logic here if you need one
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
@@ -0,0 +1,16 @@
1
+ class SquirrelController < ApplicationController
2
+
3
+ before_actions do
4
+ all { @squirrel = action_name }
5
+ except(:one) { @squirrel = "except before one" }
6
+ end
7
+
8
+ def one
9
+ end
10
+
11
+ def two
12
+ end
13
+
14
+ def three
15
+ end
16
+ end
@@ -0,0 +1,2 @@
1
+ <h1>Bunny#one</h1>
2
+ <p>Find me in app/views/bunny/one.html.erb</p>
@@ -0,0 +1,2 @@
1
+ <h1>Bunny#three</h1>
2
+ <p>Find me in app/views/bunny/three.html.erb</p>
@@ -0,0 +1,2 @@
1
+ <h1>Bunny#two</h1>
2
+ <p>Find me in app/views/bunny/two.html.erb</p>
@@ -0,0 +1,2 @@
1
+ <h1>Cat#one</h1>
2
+ <p>Find me in app/views/cat/one.html.erb</p>
@@ -0,0 +1,2 @@
1
+ <h1>Cat#three</h1>
2
+ <p>Find me in app/views/cat/three.html.erb</p>
@@ -0,0 +1,2 @@
1
+ <h1>Cat#two</h1>
2
+ <p>Find me in app/views/cat/two.html.erb</p>
@@ -0,0 +1,2 @@
1
+ <h1>Dog#one</h1>
2
+ <p>Find me in app/views/dog/one.html.erb</p>
@@ -0,0 +1,2 @@
1
+ <h1>Dog#three</h1>
2
+ <p>Find me in app/views/dog/three.html.erb</p>
@@ -0,0 +1,2 @@
1
+ <h1>Dog#two</h1>
2
+ <p>Find me in app/views/dog/two.html.erb</p>
@@ -0,0 +1,2 @@
1
+ <h1>Squirrel#one</h1>
2
+ <p>Find me in app/views/squirrel/one.html.erb</p>
@@ -0,0 +1,2 @@
1
+ <h1>Squirrel#three</h1>
2
+ <p>Find me in app/views/squirrel/three.html.erb</p>
@@ -0,0 +1,2 @@
1
+ <h1>Squirrel#two</h1>
2
+ <p>Find me in app/views/squirrel/two.html.erb</p>
@@ -1,8 +1,24 @@
1
1
  Rails.application.routes.draw do
2
2
 
3
- resources :admin_foos
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
- actions { } # load your nested resource's parent here if you need one
11
- actions(:index) { @<%= plural_table_name %> = <%= orm_class.all(class_name) %> }
12
- actions(:new) { @<%= singular_table_name %> = <%= orm_class.build(class_name) %> }
13
- actions(:create) { @<%= singular_table_name %> = <%= orm_class.build(class_name, "#{singular_table_name}_params") %> }
14
- actions(:show, :edit, :update, :destroy) { @<%= singular_table_name %> = <%= orm_class.find(class_name, "params[:id]") %> }
15
- actions { } # run your authorization logic here if you need one
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