14
14
# ```
15
15
# redirect to: Users::Index, status: 301
16
16
#
17
- # # or use the built in enum value
18
- # redirect to: Users::Index, status: :moved_permanently
17
+ # # or use the built- in enum value
18
+ # redirect to: Users::Index, status: HTTP::Status::MOVED_PERMANENTLY
19
19
# ```
20
20
#
21
- # You can find a list of all of the possible statuses [here](https://crystal-lang.org/api/latest/HTTP/Status.html).
21
+ # Alternatively, the status code can also be configured globally through the `redirect_status` setting:
22
+ #
23
+ # ```
24
+ # Lucky::Redirectable.configure do |config|
25
+ # config.redirect_status = 303
26
+ #
27
+ # # or using a built-in enum value
28
+ # config.redirect_status = HTTP::Status::SEE_OTHER.value
29
+ # end
30
+ # ```
31
+ #
32
+ # You can find a list of all possible statuses [here](https://crystal-lang.org/api/latest/HTTP/Status.html).
22
33
#
23
34
# Internally, all the different methods in this module eventually use the
24
35
# method that takes a `String`. However, it's recommended you pass a
25
36
# `Lucky::Action` class if possible because it guarantees runtime safety.
26
37
module Lucky::Redirectable
38
+ Habitat .create do
39
+ setting redirect_status : Int32 = HTTP ::Status ::FOUND .value
40
+ end
41
+
27
42
# Redirect back with a `Lucky::Action` fallback
28
43
#
29
44
# ```
30
45
# redirect_back fallback: Users::Index
31
46
# ```
32
- def redirect_back (* , fallback : Lucky ::Action .class, status = 302 , allow_external = false ) : Lucky ::TextResponse
47
+ def redirect_back (
48
+ * ,
49
+ fallback : Lucky ::Action .class,
50
+ status = Lucky ::Redirectable .settings.redirect_status,
51
+ allow_external = false
52
+ ) : Lucky ::TextResponse
33
53
redirect_back fallback: fallback.route, status: status, allow_external: allow_external
34
54
end
35
55
@@ -38,7 +58,12 @@ module Lucky::Redirectable
38
58
# ```
39
59
# redirect_back fallback: Users::Show.with(user.id)
40
60
# ```
41
- def redirect_back (* , fallback : Lucky ::RouteHelper , status = 302 , allow_external = false ) : Lucky ::TextResponse
61
+ def redirect_back (
62
+ * ,
63
+ fallback : Lucky ::RouteHelper ,
64
+ status = Lucky ::Redirectable .settings.redirect_status,
65
+ allow_external = false
66
+ ) : Lucky ::TextResponse
42
67
redirect_back fallback: fallback.path, status: status, allow_external: allow_external
43
68
end
44
69
@@ -47,7 +72,12 @@ module Lucky::Redirectable
47
72
# ```
48
73
# redirect_back fallback: "/users", status: HTTP::Status::MOVED_PERMANENTLY
49
74
# ```
50
- def redirect_back (* , fallback : String , status : HTTP ::Status , allow_external = false ) : Lucky ::TextResponse
75
+ def redirect_back (
76
+ * ,
77
+ fallback : String ,
78
+ status : HTTP ::Status ,
79
+ allow_external = false
80
+ ) : Lucky ::TextResponse
51
81
redirect_back fallback: fallback, status: status.value, allow_external: allow_external
52
82
end
53
83
@@ -74,7 +104,12 @@ module Lucky::Redirectable
74
104
# They can be explicitly allowed if necessary
75
105
#
76
106
# redirect_back fallback: "/home", allow_external: true
77
- def redirect_back (* , fallback : String , status : Int32 = 302 , allow_external : Bool = false ) : Lucky ::TextResponse
107
+ def redirect_back (
108
+ * ,
109
+ fallback : String ,
110
+ status : Int32 = Lucky ::Redirectable .settings.redirect_status,
111
+ allow_external : Bool = false
112
+ ) : Lucky ::TextResponse
78
113
referer = request.headers[" Referer" ]?
79
114
80
115
if referer && (allow_external || allowed_host?(referer))
@@ -89,7 +124,10 @@ module Lucky::Redirectable
89
124
# ```
90
125
# redirect to: Users::Show.with(user.id), status: 301
91
126
# ```
92
- def redirect (to route : Lucky ::RouteHelper , status = 302 ) : Lucky ::TextResponse
127
+ def redirect (
128
+ to route : Lucky ::RouteHelper ,
129
+ status = Lucky ::Redirectable .settings.redirect_status
130
+ ) : Lucky ::TextResponse
93
131
redirect to: route.path, status: status
94
132
end
95
133
@@ -98,14 +136,17 @@ module Lucky::Redirectable
98
136
# ```
99
137
# redirect to: Users::Index
100
138
# ```
101
- def redirect (to action : Lucky ::Action .class, status = 302 ) : Lucky ::TextResponse
139
+ def redirect (
140
+ to action : Lucky ::Action .class,
141
+ status = Lucky ::Redirectable .settings.redirect_status
142
+ ) : Lucky ::TextResponse
102
143
redirect to: action.route, status: status
103
144
end
104
145
105
146
# Redirect to the given path, with a human friendly status
106
147
#
107
148
# ```
108
- # redirect to: "/users", status: :moved_permanently
149
+ # redirect to: "/users", status: HTTP::Status::MOVED_PERMANENTLY
109
150
# ```
110
151
def redirect (to path : String , status : HTTP ::Status ) : Lucky ::TextResponse
111
152
redirect(path, status.value)
@@ -118,7 +159,10 @@ module Lucky::Redirectable
118
159
# redirect to: "/users/1", status: 301
119
160
# ```
120
161
# Note: It's recommended to use the method above that accepts a human friendly version of the status
121
- def redirect (to path : String , status : Int32 = 302 ) : Lucky ::TextResponse
162
+ def redirect (
163
+ to path : String ,
164
+ status : Int32 = Lucky ::Redirectable .settings.redirect_status
165
+ ) : Lucky ::TextResponse
122
166
# flash messages are not consumed here, so keep them for the next action
123
167
flash.keep
124
168
context.response.headers.add " Location" , path
0 commit comments