/
controller.rdoctest
166 lines (80 loc) · 4.57 KB
/
controller.rdoctest
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
Hobo's Miscellaneous Controller Extensions
{.document-title}
This chapter of the Hobo Manual describes Hobo's controller extensions.
Contents
{.contents-heading}
- contents
{:toc}
doctest: prepare testapp environment
doctest_require: '../prepare_testapp'
{.hidden}
# Authentication support
## authorized?
This method checks if the user is authorized. It returns true by default. Override this method in your controllers if you want to restrict access to only a few actions or if you want to check if the user has the correct rights.
Example:
# only allow nonbobs
def authorized?
current_user.login != "bob"
end
## login_required
This is used as a filter to control authentication.
To require logins for all actions, use this in your controllers:
before_filter :login_required
To require logins for specific actions, use this in your controllers:
before_filter :login_required, :only => [ :edit, :update ]
To skip this in a subclassed controller:
skip_before_filter :login_required
## store_location
Store the URI of the current request in the session. We can return to this location by calling #redirect_back_or_default.
## redirect_back_or_default(default)
Redirect to the URI stored by the most recent store_location call or to the passed default.
## login_from_cookie and authenticated_user_from_cookie
When called with before_filter, `login_from_cookie` will call `authenticated_user_from_cookie` to check for
an `auth_token` cookie, and if the result is appropiate, it will log the user in.
See the [controller section in Users and Authentication](/manual/users_and_authentication#hobo_user_controller) for more information
## create_auth_cookie
Populates `cookies[:auth_token]` with `current_user` data.
See also the [Hobo User Controller section](/manual/users_and_authentication#hobo_user_controller) in the Users and Authentication manual.
# Cache
## expire_swept_caches_for(obj, attr=nil)
This method is to be used in the controller as a partner to the `<swept-cache>` tag.
Refer to the `<swept-cache>` taglib for more information.
# Model
## Introduction to the Hobo Model Controller Module
Adding `hobo_model_controller` to one of your controllers will include this module
This module uses `def included(base)` to run some code in the context of the current controller. Some interesting stuff:
* Defines @auto_actions as an empty hash if it's not already defined.
* Runs `extend ClassMethods` to add the ClassMethods module to the current controller.
* Adds a couple of helpers: `:model, :current_user`
* Rescues from some errors: `ActiveRecord::RecordNotFound, ActiveRecord::RecordNotFound, Hobo::Model::Lifecycles::LifecycleKeyError`
* Loads some subsite magic
## Class Methods
### model_name and model
`model_name` returns `model.name.underscore`
`model` returns `@model` or `controller_name.camelcase.singularize.constantize`
### autocomplete
This is the action called by the `<name-one>` tag to autocomplete results. See [Autocompleters](/manual/controllers#autocompleters) section in the controller manual.
### web_method
See [Web methods](/manual/controllers#web_methods) in the Controller manual.
### auto_actions
See [Selecting the automatic actions](/manual/controllers#selecting_the_automatic_actions) in the Controller manual.
### def_lifecycle_actions
See [Lifecycles](/manual/controllers#lifecycles) in the Controller manual.
## Instance Methods
### find_instance
If your model is Foo, this method is equivalent to
Foo.find(params[:id])
in the simple case. However, it also understands owned routes and sets `acting_user`.
@foo = find_instance
is quite frequently the first line a custom Hobo controller action.
### re_render_form
If you have errors on your page, you can call this helper to re-render the view that contains the source form, with errors highlighted.
### destination_after_submit
Contains the logic for [Automatic Redirects](/manual/controllers#automatic_redirects).
### parse_sort_param
Converts params[:sort] from the form used by `<table-plus>` into a form suitable to be passed to the [order_by](/manual/scopes#order_by) scope.
Simple usage is to pass a list of field names, however the last parameter can be a hash which maps from field name to database column name.
parse_sort_param(:title, :isbn, :author => "authors.last_name")
`parse_sort_param` accepts dotted field names, automatically pluralizing to determine the database column.
Story.include(:tasks).order_by(parse_sort_param("task.title"))
will sort on `task.title` as long as :tasks is included or joined to the scope