sinatra-scope 0.1.1 → 0.1.2
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +69 -0
- data/test/helper.rb +0 -6
- metadata +3 -5
- data/lib/sinatra/resource.rb +0 -69
- data/lib/sinatra/urls.rb +0 -35
- data/test/sinatra_resource_test.rb +0 -267
- data/test/sinatra_urls_test.rb +0 -35
data/README.md
ADDED
@@ -0,0 +1,69 @@
|
|
1
|
+
# Sinatra::Scope
|
2
|
+
|
3
|
+
Simple nested routes in sinatra.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
> gem install sinatra-scope
|
8
|
+
|
9
|
+
# Gemfile
|
10
|
+
gem 'sinatra-scope'
|
11
|
+
|
12
|
+
## Usage
|
13
|
+
|
14
|
+
The `scope` method is provided as a class method for `Sinatra::Base`. An
|
15
|
+
example shows this better than anything else.
|
16
|
+
|
17
|
+
```ruby
|
18
|
+
class Blog < Sinatra::Base
|
19
|
+
register Sinatra::Scope
|
20
|
+
|
21
|
+
scope :posts do
|
22
|
+
get do
|
23
|
+
@posts = Post.published.paginate(params)
|
24
|
+
erb :posts
|
25
|
+
end
|
26
|
+
|
27
|
+
get(:new) do
|
28
|
+
@post = Post.new
|
29
|
+
erb(:new_post)
|
30
|
+
end
|
31
|
+
|
32
|
+
post do
|
33
|
+
@post = Post.new(params[:post])
|
34
|
+
if @post.save
|
35
|
+
redirect "/posts/#{@post.id}"
|
36
|
+
else
|
37
|
+
erb(:new_post)
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
scope ":id" do
|
42
|
+
before { @post = Post.find(params[:id]) }
|
43
|
+
|
44
|
+
get { erb(:post) }
|
45
|
+
|
46
|
+
get(:edit) { erb(:edit_post) }
|
47
|
+
|
48
|
+
put do
|
49
|
+
if @post.update_attributes params[:post]
|
50
|
+
redirect "/posts/#{@post.id}"
|
51
|
+
else
|
52
|
+
erb(:edit_post)
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
delete do
|
57
|
+
@post.destroy
|
58
|
+
redirect "/posts"
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
63
|
+
```
|
64
|
+
|
65
|
+
## TODO
|
66
|
+
|
67
|
+
* before and after in an unamed scope leak out of the scope
|
68
|
+
* Is there a way to scope template names?
|
69
|
+
* Track scopes as named urls
|
data/test/helper.rb
CHANGED
@@ -10,10 +10,7 @@ $LOAD_PATH.unshift libdir unless $LOAD_PATH.include?(libdir)
|
|
10
10
|
|
11
11
|
require 'contest'
|
12
12
|
require 'rack/test'
|
13
|
-
require 'sinatra/base'
|
14
13
|
require 'sinatra/scope'
|
15
|
-
require 'sinatra/resource'
|
16
|
-
require 'sinatra/urls'
|
17
14
|
|
18
15
|
class Sinatra::Base
|
19
16
|
# Allow assertions in request context
|
@@ -23,9 +20,6 @@ end
|
|
23
20
|
# App that includes scope helpers
|
24
21
|
class ScopeApp < Sinatra::Base
|
25
22
|
register Sinatra::Scope
|
26
|
-
register Sinatra::Resource
|
27
|
-
register Sinatra::UrlScope
|
28
|
-
helpers Sinatra::UrlHelpers
|
29
23
|
end
|
30
24
|
|
31
25
|
Sinatra::Base.set :environment, :test
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: sinatra-scope
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.2
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -66,17 +66,15 @@ extensions: []
|
|
66
66
|
extra_rdoc_files:
|
67
67
|
- Gemfile
|
68
68
|
- Gemfile.lock
|
69
|
+
- README.md
|
69
70
|
files:
|
70
|
-
- lib/sinatra/resource.rb
|
71
71
|
- lib/sinatra/scope.rb
|
72
|
-
- lib/sinatra/urls.rb
|
73
72
|
- test/contest.rb
|
74
73
|
- test/helper.rb
|
75
|
-
- test/sinatra_resource_test.rb
|
76
74
|
- test/sinatra_scope_test.rb
|
77
|
-
- test/sinatra_urls_test.rb
|
78
75
|
- Gemfile
|
79
76
|
- Gemfile.lock
|
77
|
+
- README.md
|
80
78
|
homepage: http://github.com/myobie/sinatra-scope
|
81
79
|
licenses: []
|
82
80
|
post_install_message:
|
data/lib/sinatra/resource.rb
DELETED
@@ -1,69 +0,0 @@
|
|
1
|
-
require 'sinatra/scope'
|
2
|
-
|
3
|
-
module Sinatra
|
4
|
-
module Resource
|
5
|
-
|
6
|
-
def resource(name, &block)
|
7
|
-
scope name, &block
|
8
|
-
end
|
9
|
-
alias resources resource
|
10
|
-
|
11
|
-
def member(&block)
|
12
|
-
scope ":id", &block
|
13
|
-
end
|
14
|
-
|
15
|
-
def index(&block)
|
16
|
-
get &block
|
17
|
-
end
|
18
|
-
|
19
|
-
def _new(&block)
|
20
|
-
get "/new", &block
|
21
|
-
end
|
22
|
-
|
23
|
-
def create(&block)
|
24
|
-
post &block
|
25
|
-
end
|
26
|
-
|
27
|
-
def show(&block)
|
28
|
-
if block.arity == 0
|
29
|
-
get &block
|
30
|
-
else
|
31
|
-
get { block.call(params[:id]) }
|
32
|
-
end
|
33
|
-
end
|
34
|
-
|
35
|
-
def edit(&block)
|
36
|
-
if block.arity == 0
|
37
|
-
get "/edit", &block
|
38
|
-
else
|
39
|
-
get("/edit") { block.call(params[:id]) }
|
40
|
-
end
|
41
|
-
end
|
42
|
-
|
43
|
-
def update(&block)
|
44
|
-
if block.arity == 0
|
45
|
-
put &block
|
46
|
-
else
|
47
|
-
put { block.call(params[:id]) }
|
48
|
-
end
|
49
|
-
end
|
50
|
-
|
51
|
-
def _delete(&block)
|
52
|
-
if block.arity == 0
|
53
|
-
get "/delete", &block
|
54
|
-
else
|
55
|
-
get("/delete") { block.call(params[:id]) }
|
56
|
-
end
|
57
|
-
end
|
58
|
-
|
59
|
-
def destroy(&block)
|
60
|
-
if block.arity == 0
|
61
|
-
delete &block
|
62
|
-
else
|
63
|
-
delete { block.call(params[:id]) }
|
64
|
-
end
|
65
|
-
end
|
66
|
-
|
67
|
-
end
|
68
|
-
|
69
|
-
end
|
data/lib/sinatra/urls.rb
DELETED
@@ -1,35 +0,0 @@
|
|
1
|
-
require 'sinatra/scope'
|
2
|
-
require 'active_support/core_ext/class/attribute'
|
3
|
-
|
4
|
-
module Sinatra
|
5
|
-
|
6
|
-
class UrlTracker
|
7
|
-
class_attribute :urls
|
8
|
-
self.urls = {}
|
9
|
-
|
10
|
-
def self.add(path, scopes = [], options = {})
|
11
|
-
name = (scopes.join("_") + "_" + path.to_s).gsub(/^_|_$/, "")
|
12
|
-
|
13
|
-
if name =~ /[a-zA-z0-9_\-%]/
|
14
|
-
self.urls[name] = ("/" + scopes.join("/") + "/" + path).squeeze("/")
|
15
|
-
end
|
16
|
-
end
|
17
|
-
end
|
18
|
-
|
19
|
-
module UrlScope
|
20
|
-
|
21
|
-
def scope(path = '', options = {}, &block)
|
22
|
-
UrlTracker.add(super, @scopes, options)
|
23
|
-
end
|
24
|
-
|
25
|
-
end
|
26
|
-
|
27
|
-
module UrlHelpers
|
28
|
-
|
29
|
-
def url(name, *args)
|
30
|
-
UrlTracker.urls[name.to_s]
|
31
|
-
end
|
32
|
-
|
33
|
-
end
|
34
|
-
|
35
|
-
end
|
@@ -1,267 +0,0 @@
|
|
1
|
-
require File.expand_path 'helper', File.dirname(__FILE__)
|
2
|
-
|
3
|
-
class ResourceTest < Test::Unit::TestCase
|
4
|
-
|
5
|
-
it 'supports the resource method' do
|
6
|
-
mock_app {
|
7
|
-
resource "pages" do
|
8
|
-
get do
|
9
|
-
"Pages are great!"
|
10
|
-
end
|
11
|
-
end
|
12
|
-
}
|
13
|
-
|
14
|
-
get "/pages"
|
15
|
-
assert ok?
|
16
|
-
assert_equal "Pages are great!", body
|
17
|
-
end
|
18
|
-
|
19
|
-
it 'supports the resources method' do
|
20
|
-
mock_app {
|
21
|
-
resources "pages" do
|
22
|
-
get do
|
23
|
-
"Pages are great!"
|
24
|
-
end
|
25
|
-
end
|
26
|
-
}
|
27
|
-
|
28
|
-
get "/pages"
|
29
|
-
assert ok?
|
30
|
-
assert_equal "Pages are great!", body
|
31
|
-
end
|
32
|
-
|
33
|
-
it 'supports the member method' do
|
34
|
-
mock_app {
|
35
|
-
resource "pages" do
|
36
|
-
member do
|
37
|
-
get do
|
38
|
-
"Page id: #{params[:id]}"
|
39
|
-
end
|
40
|
-
end
|
41
|
-
end
|
42
|
-
}
|
43
|
-
|
44
|
-
get "/pages/3"
|
45
|
-
assert ok?
|
46
|
-
assert_equal "Page id: 3", body
|
47
|
-
end
|
48
|
-
|
49
|
-
it "supports the index method of a resource" do
|
50
|
-
mock_app do
|
51
|
-
resource "pages" do
|
52
|
-
index do
|
53
|
-
"Hello"
|
54
|
-
end
|
55
|
-
end
|
56
|
-
end
|
57
|
-
|
58
|
-
get "/pages"
|
59
|
-
assert ok?
|
60
|
-
assert_equal "Hello", body
|
61
|
-
end
|
62
|
-
|
63
|
-
it "supports the new (_new) method of a resource" do
|
64
|
-
mock_app do
|
65
|
-
resource "pages" do
|
66
|
-
_new do
|
67
|
-
"New page"
|
68
|
-
end
|
69
|
-
end
|
70
|
-
end
|
71
|
-
|
72
|
-
get "/pages/new"
|
73
|
-
assert ok?
|
74
|
-
assert_equal "New page", body
|
75
|
-
end
|
76
|
-
|
77
|
-
it "supports the create method of a resource" do
|
78
|
-
mock_app do
|
79
|
-
resource "pages" do
|
80
|
-
create do
|
81
|
-
"Created the page"
|
82
|
-
end
|
83
|
-
end
|
84
|
-
end
|
85
|
-
|
86
|
-
post "/pages", {}
|
87
|
-
assert ok?
|
88
|
-
assert_equal "Created the page", body
|
89
|
-
end
|
90
|
-
|
91
|
-
it "supports the show method of a resource" do
|
92
|
-
mock_app do
|
93
|
-
resource "pages" do
|
94
|
-
member do
|
95
|
-
show do
|
96
|
-
"Page #{params[:id]}"
|
97
|
-
end
|
98
|
-
end
|
99
|
-
end
|
100
|
-
end
|
101
|
-
|
102
|
-
get "/pages/1"
|
103
|
-
assert ok?
|
104
|
-
assert_equal "Page 1", body
|
105
|
-
end
|
106
|
-
|
107
|
-
it "supports the show method of a resource with the id argument" do
|
108
|
-
mock_app do
|
109
|
-
resource "pages" do
|
110
|
-
member do
|
111
|
-
show do |id|
|
112
|
-
"Page #{id}"
|
113
|
-
end
|
114
|
-
end
|
115
|
-
end
|
116
|
-
end
|
117
|
-
|
118
|
-
get "/pages/1"
|
119
|
-
assert ok?
|
120
|
-
assert_equal "Page 1", body
|
121
|
-
end
|
122
|
-
|
123
|
-
it "supports the edit method of a resource" do
|
124
|
-
mock_app do
|
125
|
-
resource "pages" do
|
126
|
-
member do
|
127
|
-
edit do
|
128
|
-
"Edit page #{params[:id]}"
|
129
|
-
end
|
130
|
-
end
|
131
|
-
end
|
132
|
-
end
|
133
|
-
|
134
|
-
get "/pages/1/edit"
|
135
|
-
assert ok?
|
136
|
-
assert_equal "Edit page 1", body
|
137
|
-
end
|
138
|
-
|
139
|
-
it "supports the edit method of a resource with the id argument" do
|
140
|
-
mock_app do
|
141
|
-
resource "pages" do
|
142
|
-
member do
|
143
|
-
edit do |id|
|
144
|
-
"Edit page #{id}"
|
145
|
-
end
|
146
|
-
end
|
147
|
-
end
|
148
|
-
end
|
149
|
-
|
150
|
-
get "/pages/1/edit"
|
151
|
-
assert ok?
|
152
|
-
assert_equal "Edit page 1", body
|
153
|
-
end
|
154
|
-
|
155
|
-
it "supports the update method of a resource" do
|
156
|
-
mock_app do
|
157
|
-
resource "pages" do
|
158
|
-
member do
|
159
|
-
update do
|
160
|
-
"Updated page #{params[:id]}"
|
161
|
-
end
|
162
|
-
end
|
163
|
-
end
|
164
|
-
end
|
165
|
-
|
166
|
-
put "/pages/1", {}
|
167
|
-
assert ok?
|
168
|
-
assert_equal "Updated page 1", body
|
169
|
-
end
|
170
|
-
|
171
|
-
it "supports the update method of a resource with the id argument" do
|
172
|
-
mock_app do
|
173
|
-
resource "pages" do
|
174
|
-
member do
|
175
|
-
update do |id|
|
176
|
-
"Updated page #{id}"
|
177
|
-
end
|
178
|
-
end
|
179
|
-
end
|
180
|
-
end
|
181
|
-
|
182
|
-
put "/pages/1", {}
|
183
|
-
assert ok?
|
184
|
-
assert_equal "Updated page 1", body
|
185
|
-
end
|
186
|
-
|
187
|
-
it "supports the delete (_delete) method of a resource" do
|
188
|
-
mock_app do
|
189
|
-
resource "pages" do
|
190
|
-
member do
|
191
|
-
_delete do
|
192
|
-
"Page #{params[:id]}"
|
193
|
-
end
|
194
|
-
end
|
195
|
-
end
|
196
|
-
end
|
197
|
-
|
198
|
-
get "/pages/1/delete"
|
199
|
-
assert ok?
|
200
|
-
assert_equal "Page 1", body
|
201
|
-
end
|
202
|
-
|
203
|
-
it "supports the delete (_delete) method of a resource with the id argument" do
|
204
|
-
mock_app do
|
205
|
-
resource "pages" do
|
206
|
-
member do
|
207
|
-
_delete do |id|
|
208
|
-
"Page #{id}"
|
209
|
-
end
|
210
|
-
end
|
211
|
-
end
|
212
|
-
end
|
213
|
-
|
214
|
-
get "/pages/1/delete"
|
215
|
-
assert ok?
|
216
|
-
assert_equal "Page 1", body
|
217
|
-
end
|
218
|
-
|
219
|
-
it "supports the destroy method of a resource" do
|
220
|
-
mock_app do
|
221
|
-
resource "pages" do
|
222
|
-
member do
|
223
|
-
destroy do
|
224
|
-
"destroyd page #{params[:id]}"
|
225
|
-
end
|
226
|
-
end
|
227
|
-
end
|
228
|
-
end
|
229
|
-
|
230
|
-
delete "/pages/1", {}
|
231
|
-
assert ok?
|
232
|
-
assert_equal "destroyd page 1", body
|
233
|
-
end
|
234
|
-
|
235
|
-
it "supports the destroy method of a resource with the id argument" do
|
236
|
-
mock_app do
|
237
|
-
resource "pages" do
|
238
|
-
member do
|
239
|
-
destroy do |id|
|
240
|
-
"destroyd page #{id}"
|
241
|
-
end
|
242
|
-
end
|
243
|
-
end
|
244
|
-
end
|
245
|
-
|
246
|
-
delete "/pages/1", {}
|
247
|
-
assert ok?
|
248
|
-
assert_equal "destroyd page 1", body
|
249
|
-
end
|
250
|
-
|
251
|
-
it 'supports using a class as a resource' do
|
252
|
-
class Page; end
|
253
|
-
|
254
|
-
mock_app {
|
255
|
-
resource Page do
|
256
|
-
_new do
|
257
|
-
"Create a new page?"
|
258
|
-
end
|
259
|
-
end
|
260
|
-
}
|
261
|
-
|
262
|
-
get "/pages/new"
|
263
|
-
assert ok?
|
264
|
-
assert_equal "Create a new page?", body
|
265
|
-
end
|
266
|
-
|
267
|
-
end
|
data/test/sinatra_urls_test.rb
DELETED
@@ -1,35 +0,0 @@
|
|
1
|
-
require File.expand_path 'helper', File.dirname(__FILE__)
|
2
|
-
|
3
|
-
class UrlHelperTest < Test::Unit::TestCase
|
4
|
-
|
5
|
-
it "supports the url method" do
|
6
|
-
mock_app do
|
7
|
-
scope "admin" do
|
8
|
-
get do
|
9
|
-
"hello #{url(:admin)}"
|
10
|
-
end
|
11
|
-
end
|
12
|
-
end
|
13
|
-
|
14
|
-
get "/admin"
|
15
|
-
assert ok?
|
16
|
-
assert_equal "hello /admin", body
|
17
|
-
end
|
18
|
-
|
19
|
-
it "supports nested scopes with the url method" do
|
20
|
-
mock_app do
|
21
|
-
scope "admin" do
|
22
|
-
scope "projects" do
|
23
|
-
get do
|
24
|
-
"hello #{url(:admin_projects)}"
|
25
|
-
end
|
26
|
-
end
|
27
|
-
end
|
28
|
-
end
|
29
|
-
|
30
|
-
get "/admin/projects"
|
31
|
-
assert ok?
|
32
|
-
assert_equal "hello /admin/projects", body
|
33
|
-
end
|
34
|
-
|
35
|
-
end
|