utopia 2.17.0 → 2.18.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 5549c06603672129f49f5d8c7f00b9cc706786460ba6597c3459be007d508c5c
4
- data.tar.gz: 7ee6b130a7a671944ec64c7a98cc8b01e7f579e64e978e3733462c2f60ead135
3
+ metadata.gz: 1f996bae57d16bd9ab485b96680ad4896673ec871127c2d313e72f4466f46a9c
4
+ data.tar.gz: b5841b727a69814fa3fc0b58b649de6a23aa10109fa376c6ef55a66e499c480c
5
5
  SHA512:
6
- metadata.gz: ae1d066e702f89c544236458d8753bcc01ab07aade2eb9b7e89755d88d9ddce520e3f1453ee9c8535f5f2788ae1cd5f5d34d412b4fdd8ee0ef00008d476debfc
7
- data.tar.gz: bba30364281ce80ff9c25597c7181a903579fe6fe28cf128e53ab730c768a216a3632e999a3c1f7a4ee249debe43c3e4afcfe59e1b4fee2a3f1df6c9f04f3f0e
6
+ metadata.gz: 742e356fd94839d94068c67b6857f8ee8e8ee0f6912eb29bf08f6c4042aa2962ab3b6d3c2cdaca7d7820fb554938cda80f40ac37600c65f678799966c7fe66f1
7
+ data.tar.gz: 8b7612f9a4090f796c340901295e13beaa41b380354eb428d0321e5a25687b509b1e536dee03dede5c8776a3284f58627089abf2b79ef4fbfcf47428673792f8
@@ -52,7 +52,9 @@ module Utopia
52
52
 
53
53
  def href
54
54
  @href ||= @info.fetch(:uri) do
55
- (@path.dirname + @path.basename).to_s if @path
55
+ @info.fetch(:href) do
56
+ (@path.dirname + @path.basename).to_s if @path
57
+ end
56
58
  end
57
59
  end
58
60
 
@@ -142,7 +142,7 @@ module Utopia
142
142
  def load_metadata(path)
143
143
  yaml_path = File.join(path, LINKS_YAML)
144
144
 
145
- if File.exist?(yaml_path) && data = YAML::load_file(yaml_path)
145
+ if File.exist?(yaml_path) && data = YAML.load_file(yaml_path)
146
146
  return symbolize_keys(data)
147
147
  else
148
148
  return {}
@@ -234,7 +234,7 @@ module Utopia
234
234
  end
235
235
 
236
236
  def load_index(name, locale, info)
237
- info ||= {}
237
+ info ||= {}
238
238
 
239
239
  if locale and defaults = @metadata[name]
240
240
  info = defaults.merge(info)
@@ -245,17 +245,20 @@ module Utopia
245
245
  yield Link.new(:index, name, locale, path, info, path[-2])
246
246
  end
247
247
 
248
+ DEFAULT_INDEX_INFO = {href: nil}.freeze
249
+
250
+ # The default index for a directory which has no contents.
248
251
  def load_default_index(name = INDEX, info = {})
249
252
  path = @top + name
250
253
 
251
254
  if info
252
- info = {uri: nil}.merge(info)
255
+ info = DEFAULT_INDEX_INFO.merge(info)
253
256
  else
254
- info = {uri: nil}
257
+ info = DEFAULT_INDEX_INFO
255
258
  end
256
259
 
257
260
  # Specify a nil uri if no index could be found for the directory:
258
- yield Link.new(:index, name, nil, @top, info, path[-2])
261
+ yield Link.new(:index, name, nil, @top.to_directory, info, path[-2])
259
262
  end
260
263
 
261
264
  def load_file(name, locale, info)
@@ -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.
@@ -155,7 +155,7 @@ module Utopia
155
155
  end
156
156
 
157
157
  if @dump_environment
158
- mail.attachments['environment.yaml'] = YAML::dump(env)
158
+ mail.attachments['environment.yaml'] = YAML.dump(env)
159
159
  end
160
160
 
161
161
  return mail
@@ -140,7 +140,7 @@ module Utopia
140
140
  if directory?
141
141
  return self
142
142
  else
143
- return join([''])
143
+ return self.class.new(@components + [''])
144
144
  end
145
145
  end
146
146
 
@@ -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
- self.class.new(@components + other).simplify
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)
@@ -237,6 +243,7 @@ module Utopia
237
243
  return self.class.new(result)
238
244
  end
239
245
 
246
+ # Returns the first path component.
240
247
  def first
241
248
  if absolute?
242
249
  @components[1]
@@ -245,12 +252,19 @@ module Utopia
245
252
  end
246
253
  end
247
254
 
255
+ # Returns the last path component.
248
256
  def last
249
- @components.last
257
+ if @components != ['']
258
+ @components.last
259
+ end
250
260
  end
251
261
 
262
+ # Pops the last path component.
252
263
  def pop
253
- @components.pop
264
+ # We don't want to convert an absolute path to a relative path.
265
+ if @components != ['']
266
+ @components.pop
267
+ end
254
268
  end
255
269
 
256
270
  # @return [String] the last path component without any file extension.
@@ -21,5 +21,5 @@
21
21
  # THE SOFTWARE.
22
22
 
23
23
  module Utopia
24
- VERSION = "2.17.0"
24
+ VERSION = "2.18.3"
25
25
  end
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.17.0
4
+ version: 2.18.3
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-06-06 00:00:00.000000000 Z
11
+ date: 2020-09-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: concurrent-ruby
@@ -192,34 +192,6 @@ dependencies:
192
192
  - - ">="
193
193
  - !ruby/object:Gem::Version
194
194
  version: '0'
195
- - !ruby/object:Gem::Dependency
196
- name: bake-bundler
197
- requirement: !ruby/object:Gem::Requirement
198
- requirements:
199
- - - ">="
200
- - !ruby/object:Gem::Version
201
- version: '0'
202
- type: :development
203
- prerelease: false
204
- version_requirements: !ruby/object:Gem::Requirement
205
- requirements:
206
- - - ">="
207
- - !ruby/object:Gem::Version
208
- version: '0'
209
- - !ruby/object:Gem::Dependency
210
- name: bake-modernize
211
- requirement: !ruby/object:Gem::Requirement
212
- requirements:
213
- - - ">="
214
- - !ruby/object:Gem::Version
215
- version: '0'
216
- type: :development
217
- prerelease: false
218
- version_requirements: !ruby/object:Gem::Requirement
219
- requirements:
220
- - - ">="
221
- - !ruby/object:Gem::Version
222
- version: '0'
223
195
  - !ruby/object:Gem::Dependency
224
196
  name: bundler
225
197
  requirement: !ruby/object:Gem::Requirement
@@ -276,20 +248,6 @@ dependencies:
276
248
  - - "~>"
277
249
  - !ruby/object:Gem::Version
278
250
  version: '3.6'
279
- - !ruby/object:Gem::Dependency
280
- name: utopia-project
281
- requirement: !ruby/object:Gem::Requirement
282
- requirements:
283
- - - ">="
284
- - !ruby/object:Gem::Version
285
- version: '0'
286
- type: :development
287
- prerelease: false
288
- version_requirements: !ruby/object:Gem::Requirement
289
- requirements:
290
- - - ">="
291
- - !ruby/object:Gem::Version
292
- version: '0'
293
251
  description:
294
252
  email:
295
253
  executables:
@@ -319,9 +277,11 @@ files:
319
277
  - lib/utopia/content/tags.rb
320
278
  - lib/utopia/content_length.rb
321
279
  - lib/utopia/controller.rb
280
+ - lib/utopia/controller/actions.md
322
281
  - lib/utopia/controller/actions.rb
323
282
  - lib/utopia/controller/base.rb
324
283
  - lib/utopia/controller/respond.rb
284
+ - lib/utopia/controller/rewrite.md
325
285
  - lib/utopia/controller/rewrite.rb
326
286
  - lib/utopia/controller/variables.rb
327
287
  - lib/utopia/exceptions.rb
@@ -384,7 +344,7 @@ require_paths:
384
344
  - lib
385
345
  required_ruby_version: !ruby/object:Gem::Requirement
386
346
  requirements:
387
- - - "~>"
347
+ - - ">="
388
348
  - !ruby/object:Gem::Version
389
349
  version: '2.5'
390
350
  required_rubygems_version: !ruby/object:Gem::Requirement
@@ -393,7 +353,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
393
353
  - !ruby/object:Gem::Version
394
354
  version: '0'
395
355
  requirements: []
396
- rubygems_version: 3.1.2
356
+ rubygems_version: 3.0.3
397
357
  signing_key:
398
358
  specification_version: 4
399
359
  summary: Utopia is a framework for building dynamic content-driven websites.