focused_controller 0.2.0 → 1.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.
data/README.md
CHANGED
@@ -17,19 +17,25 @@ for discussion.
|
|
17
17
|
|
18
18
|
``` ruby
|
19
19
|
class ApplicationController
|
20
|
-
|
20
|
+
class Action < ApplicationController
|
21
|
+
include FocusedController::Mixin
|
22
|
+
end
|
21
23
|
end
|
22
24
|
|
23
25
|
module PostsController
|
24
|
-
class
|
26
|
+
class Action < ApplicationController::Action
|
27
|
+
before_filter :authenticate
|
28
|
+
end
|
29
|
+
|
30
|
+
class Index < Action
|
25
31
|
expose(:posts) { Post.recent.limit(5) }
|
26
32
|
end
|
27
33
|
|
28
|
-
class New <
|
34
|
+
class New < Action
|
29
35
|
expose(:post) { Post.new }
|
30
36
|
end
|
31
37
|
|
32
|
-
class Singular <
|
38
|
+
class Singular < Action
|
33
39
|
expose(:post) { Post.find params[:id] }
|
34
40
|
before_filter { redirect_to root_path unless post.accessible_to?(current_user) }
|
35
41
|
end
|
@@ -49,22 +55,19 @@ module PostsController
|
|
49
55
|
end
|
50
56
|
```
|
51
57
|
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
action gives us.
|
66
|
-
* The `#call` method is what gets invoked when the action runs, so put
|
67
|
-
the 'active ingredients' in here.
|
58
|
+
You can include `FocusedController::Mixin` anywhere, so you don't have
|
59
|
+
to use Focused Controller in every single controller if you don't want
|
60
|
+
to. I find it useful to define `ApplicationController::Action` and
|
61
|
+
inherit from that where needed.
|
62
|
+
|
63
|
+
The `#call` method is what gets invoked when the request is served.
|
64
|
+
|
65
|
+
The `#expose` declaration defines a method which runs the block and
|
66
|
+
memoizes the result. It also makes `post` a helper method so you can
|
67
|
+
call it from the view template.
|
68
|
+
|
69
|
+
The `before_filter` in Singular is inherited by precisely the actions
|
70
|
+
that need it, so we don't need to specify `:only` or `:except`.
|
68
71
|
|
69
72
|
## Routing ##
|
70
73
|
|
@@ -224,7 +227,7 @@ describe UsersController do
|
|
224
227
|
include FocusedController::RSpecHelper
|
225
228
|
|
226
229
|
describe UsersController::Create do
|
227
|
-
|
230
|
+
it "creates a user" do
|
228
231
|
subject.params = { user: { name: 'Jon' } }
|
229
232
|
expect { subject.call }.to change(User, :count).by(1)
|
230
233
|
response.should redirect_to(user_path(subject.user))
|
@@ -13,8 +13,13 @@ module FocusedController
|
|
13
13
|
options = @options.dup
|
14
14
|
|
15
15
|
if to = to_option
|
16
|
-
options[:to]
|
17
|
-
|
16
|
+
options[:to] = FocusedController::Route.new(to)
|
17
|
+
|
18
|
+
# This causes rails to spit out a bit of extra useful information in the case
|
19
|
+
# of a routing error. The :action option is also necessary for the routing to
|
20
|
+
# work on Rails <= 3.1.
|
21
|
+
options[:action] = FocusedController.action_name
|
22
|
+
options[:controller] = to.underscore
|
18
23
|
end
|
19
24
|
|
20
25
|
options
|
@@ -1,19 +1,13 @@
|
|
1
1
|
module PostsController
|
2
|
-
class
|
3
|
-
end
|
4
|
-
|
5
|
-
class Index < Action
|
2
|
+
class Index < ApplicationController
|
6
3
|
expose(:posts) { Post.all }
|
7
4
|
end
|
8
5
|
|
9
|
-
class
|
6
|
+
class New < ApplicationController
|
10
7
|
expose(:post) { Post.new params[:post] }
|
11
8
|
end
|
12
9
|
|
13
|
-
class
|
14
|
-
end
|
15
|
-
|
16
|
-
class Create < Initializer
|
10
|
+
class Create < New
|
17
11
|
def call
|
18
12
|
if post.save
|
19
13
|
redirect_to post, :notice => 'Post was successfully created.'
|
@@ -23,17 +17,14 @@ module PostsController
|
|
23
17
|
end
|
24
18
|
end
|
25
19
|
|
26
|
-
class
|
20
|
+
class Show < ApplicationController
|
27
21
|
expose(:post) { Post.find params[:id] }
|
28
22
|
end
|
29
23
|
|
30
|
-
class
|
31
|
-
end
|
32
|
-
|
33
|
-
class Edit < Finder
|
24
|
+
class Edit < Show
|
34
25
|
end
|
35
26
|
|
36
|
-
class Update <
|
27
|
+
class Update < Edit
|
37
28
|
def call
|
38
29
|
if post.update_attributes(params[:post])
|
39
30
|
redirect_to post, :notice => 'Post was successfully updated.'
|
@@ -43,7 +34,7 @@ module PostsController
|
|
43
34
|
end
|
44
35
|
end
|
45
36
|
|
46
|
-
class Destroy <
|
37
|
+
class Destroy < Edit
|
47
38
|
def call
|
48
39
|
post.destroy
|
49
40
|
redirect_to posts_url
|
@@ -56,7 +56,8 @@ module FocusedController
|
|
56
56
|
mappings.each do |(method, path), controller|
|
57
57
|
route = recognize(path, :method => method)
|
58
58
|
route.app.name.must_equal controller
|
59
|
-
route.defaults[:action].must_equal
|
59
|
+
route.defaults[:action].must_equal FocusedController.action_name
|
60
|
+
route.defaults[:controller].must_equal controller.underscore
|
60
61
|
end
|
61
62
|
end
|
62
63
|
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: focused_controller
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 1.0.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-
|
12
|
+
date: 2012-09-20 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: actionpack
|