utopia 2.18.0 → 2.18.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/utopia/controller/actions.md +76 -0
- data/lib/utopia/controller/rewrite.md +69 -0
- data/lib/utopia/path.rb +7 -1
- data/lib/utopia/version.rb +1 -1
- metadata +4 -16
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 2e27bab1354cbb8b2d4ab415c3c2570db443ece1628692bbd17ef1f6e7bd2329
|
4
|
+
data.tar.gz: 155b3b5ff07f33d836297db25be41d534c84e50ed9133b4d2ab69eec6730aeb8
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d73b02adae74b577cff9e5a9b47f2d226f5bf97b9e246baeb325c21728d83e3c46069c20b6b6e38309900951c74d72985800864e7c682e39b27681fc3bc58cf2
|
7
|
+
data.tar.gz: 8038fbd4bd0334ec21391e86e2c0e55cb3a0caeef59b7700f310c5772fdc50c71997e8fa4ab258a34ca1d36fe5f17f7c3363dc6645dcd1d25eed06e42795b2db
|
@@ -0,0 +1,76 @@
|
|
1
|
+
# Utopia::Controller::Actions
|
2
|
+
|
3
|
+
Actions let you match path patterns in your controller and execute code. In your `controller.rb` simply add:
|
4
|
+
|
5
|
+
```ruby
|
6
|
+
prepend Actions
|
7
|
+
```
|
8
|
+
|
9
|
+
If you are adding multiple things, like rewriting, they should come earlier in the chain, e.g:
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
prepend Rewrite, Actions
|
13
|
+
```
|
14
|
+
|
15
|
+
A simple CRUD controller might look like:
|
16
|
+
|
17
|
+
```ruby
|
18
|
+
prepend Actions
|
19
|
+
|
20
|
+
on 'index' do
|
21
|
+
@users = User.all
|
22
|
+
end
|
23
|
+
|
24
|
+
on 'new' do |request|
|
25
|
+
@user = User.new
|
26
|
+
|
27
|
+
if request.post?
|
28
|
+
@user.update_attributes(request.params['user'])
|
29
|
+
|
30
|
+
redirect! "index"
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
on 'edit' do |request|
|
35
|
+
@user = User.find(request.params['id'])
|
36
|
+
|
37
|
+
if request.post?
|
38
|
+
@user.update_attributes(request.params['user'])
|
39
|
+
|
40
|
+
redirect! "index"
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
on 'delete' do |request|
|
45
|
+
User.find(request.params['id']).destroy
|
46
|
+
|
47
|
+
redirect! "index"
|
48
|
+
end
|
49
|
+
```
|
50
|
+
|
51
|
+
## Path Matching
|
52
|
+
|
53
|
+
Path matching works from right to left, and `'**'` is a greedy operator. Controllers are invoked with a path relative to the controller's `URI_PATH`, so all lookups are relative to the controller.
|
54
|
+
|
55
|
+
<dl>
|
56
|
+
<dt><code class="language-ruby">"*"</code></dt>
|
57
|
+
<dd>Match a single path element</dd>
|
58
|
+
<dt><code class="language-ruby">"**"</code></dt>
|
59
|
+
<dd>Match all remaining path elements</dd>
|
60
|
+
<dt><code class="language-ruby">String</code></dt>
|
61
|
+
<dd>Match a named path component, e.g. <code class="language-ruby">"edit"</code>.</dd>
|
62
|
+
<dt><code class="language-ruby">Symbol</code></dt>
|
63
|
+
<dd>Equivalent to <code class="language-ruby">["**", symbol.to_s]</code>, e.g. <code class="language-ruby">:logout</code>.</dd>
|
64
|
+
</dl>
|
65
|
+
|
66
|
+
## Otherwise Matching
|
67
|
+
|
68
|
+
If no action was matched, it is sometimes useful to perform some specific behaviour. You can specify this by using the otherwise handler:
|
69
|
+
|
70
|
+
```ruby
|
71
|
+
otherwise do |request, path|
|
72
|
+
fail! :teapot
|
73
|
+
end
|
74
|
+
```
|
75
|
+
|
76
|
+
If you are doing this to perform some kind of rewriting, it may be preferable to use the [Rewrite](../rewrite/) controller layer.
|
@@ -0,0 +1,69 @@
|
|
1
|
+
# Utopia::Controller::Rewrite
|
2
|
+
|
3
|
+
This module can match and rewrite requests before they processed. This allows you to handle URLs like `/post/15/view` or `/blog/123-pictures-of-my-cat` easily. The basic rewrite operation is to extract some part of the path and optionally executes a block. That means that the path is modified before being passed on to the next layer in the controller, and controller instance variables may be set.
|
4
|
+
|
5
|
+
## Regular Expressions
|
6
|
+
|
7
|
+
In your `controller.rb`:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
prepend Rewrite, Actions
|
11
|
+
|
12
|
+
rewrite.extract_prefix permalink: /(?<id>\d+)-(?<title>.*)/ do |request, path, match|
|
13
|
+
# The rewrite matched, but there was no valid post, so we fail:
|
14
|
+
fail! unless @post = Post.find(@permalink[:id])
|
15
|
+
|
16
|
+
# If the path matched, but there was no suffix, we make it default to the post action:
|
17
|
+
if match.post_match.empty?
|
18
|
+
match.post_match.components << "post"
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
on 'post' do
|
23
|
+
# You can do further processing here.
|
24
|
+
fail! unless @post.published?
|
25
|
+
|
26
|
+
@comments = @post.comments.first(5)
|
27
|
+
end
|
28
|
+
|
29
|
+
on 'edit' do
|
30
|
+
# You can do further processing here.
|
31
|
+
fail! unless @current_user&.editor?
|
32
|
+
end
|
33
|
+
```
|
34
|
+
|
35
|
+
In your `post.xnode`, as an example:
|
36
|
+
|
37
|
+
```trenni
|
38
|
+
<content:page>
|
39
|
+
<content:heading>Post #{attributes[:permalink][:id]} about #{attributes[:permalink][:title]}</content:heading>
|
40
|
+
|
41
|
+
<p>#{attributes[:post].content}</p>
|
42
|
+
</content:page>
|
43
|
+
```
|
44
|
+
|
45
|
+
Keep in mind, that URLs like `/123-pictures-of-my-cat/edit` will work as expected, and hit the `edit` action of the controller.
|
46
|
+
|
47
|
+
## Restful Resources
|
48
|
+
|
49
|
+
Similar to the above, if we were solely interested in IDs, we could do the following:
|
50
|
+
|
51
|
+
```ruby
|
52
|
+
prepend Rewrite, Actions
|
53
|
+
|
54
|
+
rewrite.extract_prefix post_id: Integer do |request, path, match|
|
55
|
+
# The rewrite matched, but there was no valid post, so we fail:
|
56
|
+
fail! unless @post = Post.find(@post_id)
|
57
|
+
|
58
|
+
# If the path matched, but there was no suffix, we make it default to the post action:
|
59
|
+
if match.post_match.empty?
|
60
|
+
match.post_match.components << "post"
|
61
|
+
end
|
62
|
+
end
|
63
|
+
```
|
64
|
+
|
65
|
+
This will only match complete integers. Assuming this code is in `/blog/controller.rb`, it would match something like `/blog/123/view` and assign <code class="language-ruby">Integer("123")</code> to <code class="language-ruby">@post_id</code>.
|
66
|
+
|
67
|
+
### Matching.. other things
|
68
|
+
|
69
|
+
It's possible to match using <code class="language-ruby">Integer</code>, <code class="language-ruby">Float</code>, <code class="language-ruby">String</code>, and you can provide your own class which will be instantiated. If it doesn't match, raise an exception and the rewrite rule will fail.
|
data/lib/utopia/path.rb
CHANGED
@@ -178,8 +178,14 @@ module Utopia
|
|
178
178
|
@components
|
179
179
|
end
|
180
180
|
|
181
|
+
# @parameter other [Array(String)] The path components to append.
|
181
182
|
def join(other)
|
182
|
-
|
183
|
+
# Check whether other is an absolute path:
|
184
|
+
if other.first == ''
|
185
|
+
self.class.new(other)
|
186
|
+
else
|
187
|
+
self.class.new(@components + other).simplify
|
188
|
+
end
|
183
189
|
end
|
184
190
|
|
185
191
|
def expand(root)
|
data/lib/utopia/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: utopia
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.18.
|
4
|
+
version: 2.18.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Samuel Williams
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2020-
|
11
|
+
date: 2020-08-01 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: concurrent-ruby
|
@@ -248,20 +248,6 @@ dependencies:
|
|
248
248
|
- - "~>"
|
249
249
|
- !ruby/object:Gem::Version
|
250
250
|
version: '3.6'
|
251
|
-
- !ruby/object:Gem::Dependency
|
252
|
-
name: utopia-project
|
253
|
-
requirement: !ruby/object:Gem::Requirement
|
254
|
-
requirements:
|
255
|
-
- - ">="
|
256
|
-
- !ruby/object:Gem::Version
|
257
|
-
version: '0'
|
258
|
-
type: :development
|
259
|
-
prerelease: false
|
260
|
-
version_requirements: !ruby/object:Gem::Requirement
|
261
|
-
requirements:
|
262
|
-
- - ">="
|
263
|
-
- !ruby/object:Gem::Version
|
264
|
-
version: '0'
|
265
251
|
description:
|
266
252
|
email:
|
267
253
|
executables:
|
@@ -291,9 +277,11 @@ files:
|
|
291
277
|
- lib/utopia/content/tags.rb
|
292
278
|
- lib/utopia/content_length.rb
|
293
279
|
- lib/utopia/controller.rb
|
280
|
+
- lib/utopia/controller/actions.md
|
294
281
|
- lib/utopia/controller/actions.rb
|
295
282
|
- lib/utopia/controller/base.rb
|
296
283
|
- lib/utopia/controller/respond.rb
|
284
|
+
- lib/utopia/controller/rewrite.md
|
297
285
|
- lib/utopia/controller/rewrite.rb
|
298
286
|
- lib/utopia/controller/variables.rb
|
299
287
|
- lib/utopia/exceptions.rb
|