utopia 1.9.1 → 1.9.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/documentation/pages/wiki/_navigation.xnode +15 -0
- data/documentation/pages/wiki/content.md +3 -2
- data/documentation/pages/wiki/controller.rb +1 -0
- data/documentation/pages/wiki/controller/actions/content.md +68 -0
- data/documentation/pages/wiki/controller/links.yaml +4 -0
- data/documentation/pages/wiki/controller/rewrite/content.md +64 -0
- data/documentation/pages/wiki/links.yaml +2 -0
- data/documentation/pages/wiki/show.xnode +3 -3
- data/lib/utopia/content/markup.rb +5 -5
- data/lib/utopia/path/matcher.rb +4 -2
- data/lib/utopia/version.rb +1 -1
- data/setup/site/public/_static/site.css +7 -0
- metadata +7 -3
- data/documentation/pages/wiki/rewriting-urls/content.md +0 -41
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 7f08603b8fc77e5ffe85cf2f402be73c09502643
|
4
|
+
data.tar.gz: 81540559da3ee6823a0a3f1754c7446b28228b5a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: caa96b4c7e561170b1bcfc4a1d783561d3e46b4e65f8c3ecadce76a3b43846f6d4fa2d26765d644aca197a77206b775ed30bed79f8e1c58bdc97459e90f815ce
|
7
|
+
data.tar.gz: 8e000468cebb8b29e356269e83e3dd6f79cfd7d64f41ad4155ca79943f1e74a28f8b2e3fb3bd1df0da9c7e64da8595f8795c7e1b38bd0c398406716311a67a91
|
@@ -0,0 +1,15 @@
|
|
1
|
+
<?r prev = nil ?>
|
2
|
+
<nav>
|
3
|
+
<?r
|
4
|
+
Utopia::Path[attributes[:path]].descend do |path|
|
5
|
+
next unless path.last
|
6
|
+
|
7
|
+
link = links(path.dirname, :name => path.last, :locale => localization.current_locale, :display => nil).first
|
8
|
+
|
9
|
+
if link and File.exist?(current.node.local_path(link.path + "content.md"))
|
10
|
+
?> › <a href="#{link.path}">#{link.title}</a><?r
|
11
|
+
elsif link
|
12
|
+
?> › <span>#{link.title}</span><?r
|
13
|
+
end
|
14
|
+
end ?>
|
15
|
+
</nav>
|
@@ -1,4 +1,4 @@
|
|
1
|
-
#
|
1
|
+
# Utopia Documentation Wiki
|
2
2
|
|
3
3
|
This wiki includes documentation and examples showing how to use Utopia.
|
4
4
|
|
@@ -8,4 +8,5 @@ This wiki includes documentation and examples showing how to use Utopia.
|
|
8
8
|
|
9
9
|
## Advanced Topics
|
10
10
|
|
11
|
-
- [
|
11
|
+
- [Controller Actions](controller/actions/)
|
12
|
+
- [Controller Rewrite](controller/rewrite/)
|
@@ -0,0 +1,68 @@
|
|
1
|
+
# Controller::Actions
|
2
|
+
|
3
|
+
Actions let you match path patterns in your controller and execute code. In your `controller.rb` simply add:
|
4
|
+
|
5
|
+
prepend Actions
|
6
|
+
|
7
|
+
If you are adding multiple things, like rewriting, they should come earlier in the chain, e.g:
|
8
|
+
|
9
|
+
prepend Rewrite, Actions
|
10
|
+
|
11
|
+
A simple CRUD controller might look like:
|
12
|
+
|
13
|
+
prepend Actions
|
14
|
+
|
15
|
+
on 'index' do
|
16
|
+
@users = User.all
|
17
|
+
end
|
18
|
+
|
19
|
+
on 'new' do |request|
|
20
|
+
@user = User.new
|
21
|
+
|
22
|
+
if request.post?
|
23
|
+
@user.update_attributes(request.params['user'])
|
24
|
+
|
25
|
+
redirect! "index"
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
on 'edit' do |request|
|
30
|
+
@user = User.find(request.params['id'])
|
31
|
+
|
32
|
+
if request.post?
|
33
|
+
@user.update_attributes(request.params['user'])
|
34
|
+
|
35
|
+
redirect! "index"
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
on 'delete' do |request|
|
40
|
+
User.find(request.params['id']).destroy
|
41
|
+
|
42
|
+
redirect! "index"
|
43
|
+
end
|
44
|
+
|
45
|
+
## Path Matching
|
46
|
+
|
47
|
+
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.
|
48
|
+
|
49
|
+
<dl>
|
50
|
+
<dt>"*"</dt>
|
51
|
+
<dd>Match a single path element</dd>
|
52
|
+
<dt>"**"<dt>
|
53
|
+
<dd>Match all remaining path elements</dd>
|
54
|
+
<dt>String</dt>
|
55
|
+
<dd>Match a named path component</dd>
|
56
|
+
<dt>Symbol</dt>
|
57
|
+
<dd>Equivalent to ["**", symbol.to_s]</dd>
|
58
|
+
</dl>
|
59
|
+
|
60
|
+
## Otherwise Matching
|
61
|
+
|
62
|
+
If no action was matched, it is sometimes useful to perform some specific behaviour. You can specify this by using the otherwise handler:
|
63
|
+
|
64
|
+
otherwise do |request, path|
|
65
|
+
fail! :teapot
|
66
|
+
end
|
67
|
+
|
68
|
+
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,64 @@
|
|
1
|
+
# Controller::Rewrite
|
2
|
+
|
3
|
+
The `Controller::Rewrite` layer 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
|
+
prepend Actions, Rewrite
|
10
|
+
|
11
|
+
rewrite.extract_prefix permalink: /(?<id>\d+)-(?<title>.*)/ do |request, path, match|
|
12
|
+
# The rewrite matched, but there was no valid post, so we fail:
|
13
|
+
fail! unless @post = Post.find(@permalink[:id])
|
14
|
+
|
15
|
+
# If the path matched, but there was no suffix, we make it default to the post action:
|
16
|
+
if match.post_match.empty?
|
17
|
+
match.post_match.components << "post"
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
on 'post' do
|
22
|
+
# You can do further processing here.
|
23
|
+
fail! unless @post.published?
|
24
|
+
|
25
|
+
@comments = @post.comments.first(5)
|
26
|
+
end
|
27
|
+
|
28
|
+
on 'edit' do
|
29
|
+
# You can do further processing here.
|
30
|
+
fail! unless @current_user&.editor?
|
31
|
+
end
|
32
|
+
|
33
|
+
In your `post.xnode`, as an example:
|
34
|
+
|
35
|
+
<page>
|
36
|
+
<heading>Post #{attributes[:permalink][:id]} about #{attributes[:permalink][:title]}</heading>
|
37
|
+
|
38
|
+
<p>#{attributes[:post].content}</p>
|
39
|
+
</page>
|
40
|
+
|
41
|
+
Keep in mind, that URLs like `/123-pictures-of-my-cat/edit` will work as expected, and hit the `edit` action of the controller.
|
42
|
+
|
43
|
+
## Restful Resources
|
44
|
+
|
45
|
+
Similar to the above, if we were solely interested in IDs, we could do the following:
|
46
|
+
|
47
|
+
prepend Actions, Rewrite
|
48
|
+
|
49
|
+
rewrite.extract_prefix post_id: Integer do |request, path, match|
|
50
|
+
# The rewrite matched, but there was no valid post, so we fail:
|
51
|
+
fail! unless @post = Post.find(@post_id)
|
52
|
+
|
53
|
+
# If the path matched, but there was no suffix, we make it default to the post action:
|
54
|
+
if match.post_match.empty?
|
55
|
+
match.post_match.components << "post"
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
This will only match complete integers. Assuming this code is in `/blog/controller.rb`, it would match something like `/blog/123/view` and assign Integer("123") to `@post_id`.
|
60
|
+
|
61
|
+
### Matching.. other things
|
62
|
+
|
63
|
+
It's possible to match using `Integer`, `Float`, `String`, 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.
|
64
|
+
|
@@ -60,23 +60,23 @@ module Utopia
|
|
60
60
|
attr :offset
|
61
61
|
|
62
62
|
def to_s
|
63
|
-
@tag.
|
63
|
+
"<#{@tag.name}#{@tag.attributes.empty? ? '' : ' ...'}>"
|
64
64
|
end
|
65
65
|
end
|
66
66
|
|
67
67
|
class UnbalancedTagError < StandardError
|
68
68
|
def initialize(buffer, opening_tag, closing_tag = nil)
|
69
69
|
@buffer = buffer
|
70
|
-
@opening_tag =
|
70
|
+
@opening_tag = opening_tag
|
71
71
|
@closing_tag = closing_tag
|
72
72
|
end
|
73
73
|
|
74
74
|
attr :buffer
|
75
|
-
attr :
|
75
|
+
attr :opening_tag
|
76
76
|
attr :closing_tag
|
77
77
|
|
78
78
|
def start_location
|
79
|
-
Trenni::Location.new(@buffer.read,
|
79
|
+
Trenni::Location.new(@buffer.read, opening_tag.offset)
|
80
80
|
end
|
81
81
|
|
82
82
|
def end_location
|
@@ -144,7 +144,7 @@ module Utopia
|
|
144
144
|
tag = @current.tag
|
145
145
|
|
146
146
|
if tag.name != name
|
147
|
-
raise UnbalancedTagError.new(@buffer,
|
147
|
+
raise UnbalancedTagError.new(@buffer, @current, ParsedTag.new(name, offset))
|
148
148
|
end
|
149
149
|
|
150
150
|
@delegate.tag_end(tag)
|
data/lib/utopia/path/matcher.rb
CHANGED
@@ -52,14 +52,16 @@ module Utopia
|
|
52
52
|
|
53
53
|
def coerce(klass, value)
|
54
54
|
if klass == Integer
|
55
|
-
Integer(value)
|
55
|
+
Integer(value)
|
56
56
|
elsif klass == Float
|
57
|
-
Float(value)
|
57
|
+
Float(value)
|
58
58
|
elsif klass == String
|
59
59
|
value.to_s
|
60
60
|
else
|
61
61
|
klass.new(value)
|
62
62
|
end
|
63
|
+
rescue
|
64
|
+
return nil
|
63
65
|
end
|
64
66
|
|
65
67
|
# This is a path prefix matching algorithm. The pattern is an array of String, Symbol, Regexp, or nil. The components is an array of String.
|
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: 1.9.
|
4
|
+
version: 1.9.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Samuel Williams
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-
|
11
|
+
date: 2016-12-01 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: trenni
|
@@ -200,11 +200,15 @@ files:
|
|
200
200
|
- documentation/pages/errors/file-not-found.xnode
|
201
201
|
- documentation/pages/links.yaml
|
202
202
|
- documentation/pages/welcome/index.xnode
|
203
|
+
- documentation/pages/wiki/_navigation.xnode
|
203
204
|
- documentation/pages/wiki/content.md
|
204
205
|
- documentation/pages/wiki/controller.rb
|
206
|
+
- documentation/pages/wiki/controller/actions/content.md
|
207
|
+
- documentation/pages/wiki/controller/links.yaml
|
208
|
+
- documentation/pages/wiki/controller/rewrite/content.md
|
205
209
|
- documentation/pages/wiki/development-environment-setup/content.md
|
206
210
|
- documentation/pages/wiki/edit.xnode
|
207
|
-
- documentation/pages/wiki/
|
211
|
+
- documentation/pages/wiki/links.yaml
|
208
212
|
- documentation/pages/wiki/server-setup/content.md
|
209
213
|
- documentation/pages/wiki/show.xnode
|
210
214
|
- documentation/pages/wiki/your-first-page/content.md
|
@@ -1,41 +0,0 @@
|
|
1
|
-
# Rewriting URLs
|
2
|
-
|
3
|
-
The `Controller::Rewrite` layer can match and rewrite requests before they processed. This allows you to have URLs like `/post/15/view` or `/blog/123-pictures-of-my-cat`. The basic rewrite operation is to extract some part of the path prefix. That means that the path is modified before being passed on to the next layer in the controller.
|
4
|
-
|
5
|
-
## Permalink Example
|
6
|
-
|
7
|
-
In your `controller.rb`:
|
8
|
-
|
9
|
-
prepend Actions, Rewrite
|
10
|
-
|
11
|
-
rewrite.extract_prefix permalink: /(?<id>\d+)-(?<title>.*)/ do |request, path, match|
|
12
|
-
# The rewrite matched, but there was no valid post, so we fail:
|
13
|
-
fail! unless @post = Post.find(@permalink[:id])
|
14
|
-
|
15
|
-
# If the path matched, but there was no suffix, we make it default to the post action:
|
16
|
-
if match.post_match.empty?
|
17
|
-
match.post_match.components << "post"
|
18
|
-
end
|
19
|
-
end
|
20
|
-
|
21
|
-
on 'post' do
|
22
|
-
# You can do further processing here.
|
23
|
-
fail! unless @post.published?
|
24
|
-
|
25
|
-
@comments = @post.comments.first(5)
|
26
|
-
end
|
27
|
-
|
28
|
-
on 'edit' do
|
29
|
-
# You can do further processing here.
|
30
|
-
fail! unless @current_user&.editor?
|
31
|
-
end
|
32
|
-
|
33
|
-
In your `post.xnode`, as an example:
|
34
|
-
|
35
|
-
<page>
|
36
|
-
<heading>Post #{attributes[:permalink][:id]} about #{attributes[:permalink][:title]}</heading>
|
37
|
-
|
38
|
-
<p>#{attributes[:post].content}</p>
|
39
|
-
</page>
|
40
|
-
|
41
|
-
Keep in mind, that URLs like `/123-pictures-of-my-cat/edit` will work as expected, and hit the `edit` action of the controller.
|