sinatra-pages 0.8.2 → 0.9.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README.markdown +1 -1
- data/lib/sinatra/pages.rb +2 -2
- data/spec/pages_spec.rb +158 -58
- metadata +1 -1
data/README.markdown
CHANGED
@@ -1,5 +1,5 @@
|
|
1
1
|
# Sinatra Pages
|
2
|
-
This is a [Sinatra Extension][1] that renders any page or sub-pages located under the directory defined as *:views* and the layout file defined as *:layout* (if there is any) inside your [Sinatra][2] application.
|
2
|
+
This is a [Sinatra Extension][1] that renders for browser and AJAX calls any page or sub-pages located under the directory defined as *:views* and the layout file defined as *:layout* (if there is any) inside your [Sinatra][2] application.
|
3
3
|
|
4
4
|
### Installation
|
5
5
|
In order to install this gem, you just need to install the gem from your command line like this:
|
data/lib/sinatra/pages.rb
CHANGED
@@ -11,7 +11,7 @@ module Sinatra
|
|
11
11
|
page_to_render << params[:page]
|
12
12
|
|
13
13
|
begin
|
14
|
-
haml page_to_render.to_sym
|
14
|
+
haml page_to_render.to_sym, :layout => !request.xhr?
|
15
15
|
rescue Errno::ENOENT
|
16
16
|
halt 404
|
17
17
|
end
|
@@ -21,7 +21,7 @@ module Sinatra
|
|
21
21
|
not_found do
|
22
22
|
params[:page] = 'not_found'
|
23
23
|
|
24
|
-
haml :not_found
|
24
|
+
haml :not_found, :layout => !request.xhr?
|
25
25
|
end
|
26
26
|
end
|
27
27
|
end
|
data/spec/pages_spec.rb
CHANGED
@@ -27,90 +27,190 @@ describe Sinatra::Pages do
|
|
27
27
|
end
|
28
28
|
|
29
29
|
context "uses HTTP GET request method" do
|
30
|
-
context "
|
31
|
-
|
32
|
-
|
33
|
-
|
30
|
+
context "in synchronous mode" do
|
31
|
+
context "with no Layout file" do
|
32
|
+
it "should render just the Home page if the given route is either empty or root." do
|
33
|
+
File.exist?("views/#{file_of.('Layout')}.haml").should be_false
|
34
|
+
File.exist?("views/#{file_of.('Home')}.haml").should be_true
|
34
35
|
|
35
|
-
|
36
|
-
|
36
|
+
['/', ''].each do |route|
|
37
|
+
get route
|
37
38
|
|
38
|
-
|
39
|
-
|
39
|
+
last_request.should_not be_xhr
|
40
|
+
last_response.should be_ok
|
41
|
+
last_response.body.chomp.should == file_of.('Home')
|
42
|
+
end
|
40
43
|
end
|
41
|
-
|
44
|
+
|
45
|
+
it "should render just an existing page if the given route match the '/:page' or '/*/:page' patterns." do
|
46
|
+
Dir.glob 'views/**/*.haml' do |file|
|
47
|
+
File.exist?("views/#{file_of.('Layout')}.haml").should be_false
|
48
|
+
File.exist?(file).should be_true
|
49
|
+
|
50
|
+
directory = File.dirname(file)[5].nil? ? '' : File.dirname(file)[5, File.dirname(file).size]
|
51
|
+
|
52
|
+
get "#{directory}/#{File.basename(file, '.haml')}"
|
42
53
|
|
43
|
-
|
44
|
-
|
54
|
+
last_request.should_not be_xhr
|
55
|
+
last_response.should be_ok
|
56
|
+
last_response.body.chomp.should == File.basename(file, '.haml')
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
it "should render just the Not Found page if a given route can't find its static page on 'views/'." do
|
45
61
|
File.exist?("views/#{file_of.('Layout')}.haml").should be_false
|
46
|
-
File.exist?(
|
47
|
-
|
48
|
-
directory = File.dirname(file)[5].nil? ? '' : File.dirname(file)[5, File.dirname(file).size]
|
62
|
+
File.exist?("views/#{file_of.('Do Not Exist')}.haml").should be_false
|
49
63
|
|
50
|
-
get "
|
64
|
+
get "/#{file_of.('Do Not Exist')}"
|
51
65
|
|
52
|
-
|
53
|
-
last_response.
|
66
|
+
last_request.should_not be_xhr
|
67
|
+
last_response.should be_not_found
|
68
|
+
last_response.body.chomp.should == file_of.('Not Found')
|
54
69
|
end
|
55
70
|
end
|
71
|
+
|
72
|
+
context "with Layout file" do
|
73
|
+
before :all do
|
74
|
+
create_file_for.('Layout', 'views', ['Layout', '= yield'])
|
75
|
+
end
|
56
76
|
|
57
|
-
|
58
|
-
|
59
|
-
|
77
|
+
it "should render both the Layout and Home page if the given route is either empty or root." do
|
78
|
+
File.exist?("views/#{file_of.('Layout')}.haml").should be_true
|
79
|
+
File.exist?("views/#{file_of.('Home')}.haml").should be_true
|
60
80
|
|
61
|
-
|
81
|
+
['/', ''].each do |route|
|
82
|
+
get route
|
62
83
|
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
before :all do
|
70
|
-
create_file_for.('Layout', 'views', ['Layout', '= yield'])
|
71
|
-
end
|
72
|
-
|
73
|
-
it "should render both the Layout and Home page if the given route is either empty or root." do
|
74
|
-
File.exist?("views/#{file_of.('Layout')}.haml").should be_true
|
75
|
-
File.exist?("views/#{file_of.('Home')}.haml").should be_true
|
84
|
+
last_request.should_not be_xhr
|
85
|
+
last_response.should be_ok
|
86
|
+
separate.(last_response.body).first.should == 'Layout'
|
87
|
+
separate.(last_response.body).last.should == file_of.('Home')
|
88
|
+
end
|
89
|
+
end
|
76
90
|
|
77
|
-
|
78
|
-
|
91
|
+
it "should render both the Layout and an existing page if the given route match the '/:page' or '/*/:page' patterns." do
|
92
|
+
Dir.glob('views/**/*.haml').reject{|file| file =~ /layout/}.each do |file|
|
93
|
+
File.exist?("views/#{file_of.('Layout')}.haml").should be_true
|
94
|
+
File.exist?(file).should be_true
|
79
95
|
|
80
|
-
|
81
|
-
|
82
|
-
|
96
|
+
directory = File.dirname(file)[5].nil? ? '' : File.dirname(file)[5, File.dirname(file).size]
|
97
|
+
|
98
|
+
get "#{directory}/#{File.basename(file, '.haml')}"
|
99
|
+
|
100
|
+
last_request.should_not be_xhr
|
101
|
+
last_response.should be_ok
|
102
|
+
separate.(last_response.body).first.should == 'Layout'
|
103
|
+
separate.(last_response.body).last.should == File.basename(file, '.haml')
|
104
|
+
end
|
83
105
|
end
|
84
|
-
end
|
85
106
|
|
86
|
-
|
87
|
-
Dir.glob('views/**/*.haml').reject{|file| file =~ /layout/}.each do |file|
|
107
|
+
it "should render both the Layout and the Not Found page if a given route can't find its static page on 'views/'." do
|
88
108
|
File.exist?("views/#{file_of.('Layout')}.haml").should be_true
|
89
|
-
File.exist?(
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
get "#{directory}/#{File.basename(file, '.haml')}"
|
109
|
+
File.exist?("views/#{file_of.('Do Not Exist')}.haml").should be_false
|
110
|
+
|
111
|
+
get "/#{file_of.('Do Not Exist')}"
|
94
112
|
|
95
|
-
|
113
|
+
last_request.should_not be_xhr
|
114
|
+
last_response.should be_not_found
|
96
115
|
separate.(last_response.body).first.should == 'Layout'
|
97
|
-
separate.(last_response.body).last.should ==
|
116
|
+
separate.(last_response.body).last.should == file_of.('Not Found')
|
117
|
+
end
|
118
|
+
|
119
|
+
after :all do
|
120
|
+
FileUtils.rm 'views/layout.haml'
|
98
121
|
end
|
99
122
|
end
|
123
|
+
end
|
124
|
+
|
125
|
+
context "in asynchronous mode" do
|
126
|
+
context "with no Layout file" do
|
127
|
+
it "should render just the Home page if the given route is either empty or root." do
|
128
|
+
File.exist?("views/#{file_of.('Layout')}.haml").should be_false
|
129
|
+
File.exist?("views/#{file_of.('Home')}.haml").should be_true
|
130
|
+
|
131
|
+
['/', ''].each do |route|
|
132
|
+
request route, :method => 'GET', :xhr => true
|
100
133
|
|
101
|
-
|
102
|
-
|
103
|
-
|
134
|
+
last_request.should be_xhr
|
135
|
+
last_response.should be_ok
|
136
|
+
last_response.body.chomp.should == file_of.('Home')
|
137
|
+
end
|
138
|
+
end
|
139
|
+
|
140
|
+
it "should render just an existing page if the given route match the '/:page' or '/*/:page' patterns." do
|
141
|
+
Dir.glob 'views/**/*.haml' do |file|
|
142
|
+
File.exist?("views/#{file_of.('Layout')}.haml").should be_false
|
143
|
+
File.exist?(file).should be_true
|
144
|
+
|
145
|
+
directory = File.dirname(file)[5].nil? ? '' : File.dirname(file)[5, File.dirname(file).size]
|
104
146
|
|
105
|
-
|
147
|
+
request "#{directory}/#{File.basename(file, '.haml')}", :method => 'GET', :xhr => true
|
106
148
|
|
107
|
-
|
108
|
-
|
109
|
-
|
149
|
+
last_request.should be_xhr
|
150
|
+
last_response.should be_ok
|
151
|
+
last_response.body.chomp.should == File.basename(file, '.haml')
|
152
|
+
end
|
153
|
+
end
|
154
|
+
|
155
|
+
it "should render just the Not Found page if a given route can't find its static page on 'views/." do
|
156
|
+
File.exist?("views/#{file_of.('Layout')}.haml").should be_false
|
157
|
+
File.exist?("views/#{file_of.('Do Not Exist')}.haml").should be_false
|
158
|
+
|
159
|
+
request "/#{file_of.('Do Not Exist')}", :method => 'GET', :xhr => true
|
160
|
+
|
161
|
+
last_request.should be_xhr
|
162
|
+
last_response.should be_not_found
|
163
|
+
last_response.body.chomp.should == file_of.('Not Found')
|
164
|
+
end
|
110
165
|
end
|
111
166
|
|
112
|
-
|
113
|
-
|
167
|
+
context "with a Layout file" do
|
168
|
+
before :all do
|
169
|
+
create_file_for.('Layout', 'views', ['Layout', '= yield'])
|
170
|
+
end
|
171
|
+
|
172
|
+
it "should render both the Layout and Home page if the given route is either empty or root." do
|
173
|
+
File.exist?("views/#{file_of.('Layout')}.haml").should be_true
|
174
|
+
File.exist?("views/#{file_of.('Home')}.haml").should be_true
|
175
|
+
|
176
|
+
['/', ''].each do |route|
|
177
|
+
request route, :method => 'GET', :xhr => true
|
178
|
+
|
179
|
+
last_request.should be_xhr
|
180
|
+
last_response.should be_ok
|
181
|
+
last_response.body.chomp.should == file_of.('Home')
|
182
|
+
end
|
183
|
+
end
|
184
|
+
|
185
|
+
it "should render both the Layout and an existing page if the given route match the '/:page' or '/*/:page' patterns." do
|
186
|
+
Dir.glob('views/**/*.haml').reject{|file| file =~ /layout/}.each do |file|
|
187
|
+
File.exist?("views/#{file_of.('Layout')}.haml").should be_true
|
188
|
+
File.exist?(file).should be_true
|
189
|
+
|
190
|
+
directory = File.dirname(file)[5].nil? ? '' : File.dirname(file)[5, File.dirname(file).size]
|
191
|
+
|
192
|
+
request "#{directory}/#{File.basename(file, '.haml')}", :method => 'GET', :xhr => true
|
193
|
+
|
194
|
+
last_request.should be_xhr
|
195
|
+
last_response.should be_ok
|
196
|
+
last_response.body.chomp.should == File.basename(file, '.haml')
|
197
|
+
end
|
198
|
+
end
|
199
|
+
|
200
|
+
it "should render both the Layout and the Not Found page if a given route can't find its static page on 'views/'." do
|
201
|
+
File.exist?("views/#{file_of.('Layout')}.haml").should be_true
|
202
|
+
File.exist?("views/#{file_of.('Do Not Exist')}.haml").should be_false
|
203
|
+
|
204
|
+
request "/#{file_of.('Do Not Exist')}", :method => 'GET', :xhr => true
|
205
|
+
|
206
|
+
last_request.should be_xhr
|
207
|
+
last_response.should be_not_found
|
208
|
+
last_response.body.chomp.should == file_of.('Not Found')
|
209
|
+
end
|
210
|
+
|
211
|
+
after :all do
|
212
|
+
FileUtils.rm 'views/layout.haml'
|
213
|
+
end
|
114
214
|
end
|
115
215
|
end
|
116
216
|
end
|