utopia 1.9.0 → 1.9.1

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
  SHA1:
3
- metadata.gz: 92b8e6466b0a5eea4cb1e6009734e9cea5c76160
4
- data.tar.gz: 9854cb98bf4f4ac5794fe19370c3af2e28aec456
3
+ metadata.gz: 1e43b609f07c8331def9621c56b40b2f9298d35b
4
+ data.tar.gz: d137536094afd723da06e757ab0112b85dd31c50
5
5
  SHA512:
6
- metadata.gz: fdc050063c8b3b7ece0d94ee388fec013f52adc0cc2f63f92a7e4bb94eb849a8ecbfb41664a183047475adc88f5959b019077edc5cdbb9061eb8b8a8057a2a60
7
- data.tar.gz: 781fe07c015abd3d777f2b4cd0340d43deee4846ca05ec2171c882569bd56add14604cc54ae5276b414dc778be77d2be7c86e90835a4793abb1df52b462b4117
6
+ metadata.gz: a6513105d8c673492b3f5c4269d6b25d28aab62e95f875c94a78fa48b727784c17c5a53b667c449ca60fdd6db88f04d5556b2f1e8d4f6320321dec3bb3956557
7
+ data.tar.gz: 79fc5393a3babeea2f1cb03328d551c11dc8166f0693e862855fee02a3e215114282a77e3fd4c353d547efafb86f9a639d80f7a4ad098e93abadc52dd94b34ec
data/.travis.yml CHANGED
@@ -6,7 +6,7 @@ before_install:
6
6
  - git config --global user.name "Samuel Williams"
7
7
  rvm:
8
8
  - 2.2.4
9
- - 2.3.0
9
+ - 2.3.2
10
10
  - ruby-head
11
11
  - rbx-3.65
12
12
  env: COVERAGE=true BENCHMARK=true
data/README.md CHANGED
@@ -205,10 +205,6 @@ The controller layer can do more complex operations by prepending modules into i
205
205
  @user = User.find_by_id(@id)
206
206
  end
207
207
 
208
- before do |request, path|
209
- # Always executed, before any controller specific actions are performed.
210
- end
211
-
212
208
  on 'edit' do |request, path|
213
209
  if request.post?
214
210
  @user.update_attributes(request[:user])
@@ -217,10 +213,7 @@ The controller layer can do more complex operations by prepending modules into i
217
213
 
218
214
  otherwise do |request, path|
219
215
  # Executed if no specific named actions were executed.
220
- end
221
-
222
- after do |request, path|
223
- # Always executed, after all other controller actions are performed.
216
+ succeed!
224
217
  end
225
218
 
226
219
  ### Content
@@ -4,4 +4,8 @@ This wiki includes documentation and examples showing how to use Utopia.
4
4
 
5
5
  - [Development Environment Setup](development-environment-setup/)
6
6
  - [Server Setup](server-setup/)
7
- - [Your first page](your-first-page/)
7
+ - [Your first page](your-first-page/)
8
+
9
+ ## Advanced Topics
10
+
11
+ - [Rewriting URLs](rewriting-urls/)
@@ -23,8 +23,6 @@ def read_contents
23
23
  end
24
24
 
25
25
  on '**/edit' do |request, path|
26
- puts "Editing..."
27
-
28
26
  if request.post?
29
27
  FileUtils.mkdir_p File.dirname(@page_file)
30
28
  File.write(@page_file, request.params['content'])
@@ -0,0 +1,41 @@
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.
@@ -61,6 +61,7 @@ module Utopia
61
61
  def apply(path, index = -1, &block)
62
62
  # ** is greedy, it always matches if possible and matches all remaining input.
63
63
  if match_all = self[WILDCARD_GREEDY] and match_all.callback?
64
+ # It's possible in this callback that path is modified.
64
65
  matched = true; yield(match_all)
65
66
  end
66
67
 
@@ -112,6 +113,13 @@ module Utopia
112
113
  end
113
114
 
114
115
  module ClassMethods
116
+ def self.extended(klass)
117
+ klass.instance_eval do
118
+ @actions = nil
119
+ @otherwise = nil
120
+ end
121
+ end
122
+
115
123
  def actions
116
124
  @actions ||= Action.new
117
125
  end
@@ -124,19 +132,23 @@ module Utopia
124
132
  actions.define(Path.split(first) + path, options, &block)
125
133
  end
126
134
 
135
+ def otherwise(&block)
136
+ @otherwise = block
137
+ end
138
+
127
139
  def dispatch(controller, request, path)
128
140
  if @actions
129
- @actions.apply(path.components) do |action|
141
+ matched = @actions.apply(path.components) do |action|
130
142
  controller.instance_exec(request, path, &action.callback)
131
- end || controller.otherwise(request, path)
143
+ end
144
+ end
145
+
146
+ if @otherwise and !matched
147
+ controller.instance_exec(request, path, &@otherwise)
132
148
  end
133
149
  end
134
150
  end
135
151
 
136
- # TODO: Test this functionality and confirm working. Document.
137
- def otherwise(request, path)
138
- end
139
-
140
152
  # Given a request, call associated actions if at least one exists.
141
153
  def process!(request, path)
142
154
  # puts "Actions\#process!(..., #{path.inspect})"
@@ -35,23 +35,6 @@ module Utopia
35
35
  end
36
36
 
37
37
  class Rule
38
- def initialize(arguments, block)
39
- @arguments = arguments
40
- @block = block
41
-
42
- self.freeze
43
- end
44
-
45
- def freeze
46
- @arguments.freeze
47
- @block.freeze
48
-
49
- super
50
- end
51
-
52
- attr :arguments
53
- attr :block
54
-
55
38
  def apply_match_to_context(match_data, context)
56
39
  match_data.names.each do |name|
57
40
  context.instance_variable_set("@#{name}", match_data[name])
@@ -60,14 +43,14 @@ module Utopia
60
43
  end
61
44
 
62
45
  class ExtractPrefixRule < Rule
63
- def initialize(arguments, block)
64
- @matcher = Path::Matcher.new(arguments)
65
-
66
- super
46
+ def initialize(patterns, block)
47
+ @matcher = Path::Matcher.new(patterns)
48
+ @block = block
67
49
  end
68
50
 
69
51
  def freeze
70
52
  @matcher.freeze
53
+ @block.freeze
71
54
 
72
55
  super
73
56
  end
@@ -94,8 +77,8 @@ module Utopia
94
77
 
95
78
  attr :rules
96
79
 
97
- def extract_prefix(**arguments, &block)
98
- @rules << ExtractPrefixRule.new(arguments, block)
80
+ def extract_prefix(**patterns, &block)
81
+ @rules << ExtractPrefixRule.new(patterns, block)
99
82
  end
100
83
 
101
84
  def apply(context, request, path)
data/lib/utopia/path.rb CHANGED
@@ -82,6 +82,10 @@ module Utopia
82
82
  super
83
83
  end
84
84
 
85
+ def empty?
86
+ @components.empty?
87
+ end
88
+
85
89
  def self.root
86
90
  self.new([''])
87
91
  end
@@ -70,18 +70,21 @@ module Utopia
70
70
 
71
71
  named_parts = {}
72
72
 
73
- @patterns.each_with_index do |(key, matcher), index|
73
+ @patterns.each_with_index do |(key, pattern), index|
74
74
  component = components[index]
75
75
 
76
- if matcher.is_a? Class
77
- return nil unless value = coerce(matcher, component)
76
+ if pattern.is_a? Class
77
+ return nil unless value = coerce(pattern, component)
78
78
 
79
79
  named_parts[key] = value
80
- elsif matcher
81
- return nil unless matcher === component
82
-
83
- named_parts[key] = component
80
+ elsif pattern
81
+ if result = pattern.match(component)
82
+ named_parts[key] = result
83
+ else
84
+ return nil
85
+ end
84
86
  else
87
+ # Ignore this part:
85
88
  named_parts[key] = component
86
89
  end
87
90
  end
@@ -19,5 +19,5 @@
19
19
  # THE SOFTWARE.
20
20
 
21
21
  module Utopia
22
- VERSION = "1.9.0"
22
+ VERSION = "1.9.1"
23
23
  end
@@ -52,12 +52,12 @@ module Utopia::Path::MatcherSpec
52
52
 
53
53
  it "should match regexps" do
54
54
  path = Utopia::Path['users/20/edit']
55
- matcher = Utopia::Path::Matcher[users: 'users', id: Integer, action: String]
55
+ matcher = Utopia::Path::Matcher[users: /users/, id: Integer, action: String]
56
56
 
57
57
  match_data = matcher.match(path)
58
58
  expect(match_data).to_not be_falsey
59
59
 
60
- expect(match_data[:users]).to be == 'users'
60
+ expect(match_data[:users].to_s).to be == 'users'
61
61
  expect(match_data[:id]).to be == 20
62
62
  expect(match_data[:action]).to be == 'edit'
63
63
  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: 1.9.0
4
+ version: 1.9.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: 2016-11-28 00:00:00.000000000 Z
11
+ date: 2016-11-30 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: trenni
@@ -204,6 +204,7 @@ files:
204
204
  - documentation/pages/wiki/controller.rb
205
205
  - documentation/pages/wiki/development-environment-setup/content.md
206
206
  - documentation/pages/wiki/edit.xnode
207
+ - documentation/pages/wiki/rewriting-urls/content.md
207
208
  - documentation/pages/wiki/server-setup/content.md
208
209
  - documentation/pages/wiki/show.xnode
209
210
  - documentation/pages/wiki/your-first-page/content.md
@@ -394,7 +395,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
394
395
  version: '0'
395
396
  requirements: []
396
397
  rubyforge_project:
397
- rubygems_version: 2.5.1
398
+ rubygems_version: 2.5.2
398
399
  signing_key:
399
400
  specification_version: 4
400
401
  summary: Utopia is a framework for building dynamic content-driven websites.