sinatra-pages 0.8.2 → 0.9.0

Sign up to get free protection for your applications and to get access to all the features.
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 "with no Layout file" do
31
- it "should render just the Home page if the given route is either empty or root." do
32
- File.exist?("views/#{file_of.('Layout')}.haml").should be_false
33
- File.exist?("views/#{file_of.('Home')}.haml").should be_true
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
- ['/', ''].each do |route|
36
- get route
36
+ ['/', ''].each do |route|
37
+ get route
37
38
 
38
- last_response.should be_ok
39
- last_response.body.chomp.should == file_of.('Home')
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
- end
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
- it "should render just an existing page if the given route match the '/:page' or '/*/:page' patterns." do
44
- Dir.glob 'views/**/*.haml' do |file|
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?(file).should be_true
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 "#{directory}/#{File.basename(file, '.haml')}"
64
+ get "/#{file_of.('Do Not Exist')}"
51
65
 
52
- last_response.should be_ok
53
- last_response.body.chomp.should == File.basename(file, '.haml')
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
- it "should render just the Not Found page if a given route can't find its static page on 'views/'." do
58
- File.exist?("views/#{file_of.('Layout')}.haml").should be_false
59
- File.exist?("views/#{file_of.('Do Not Exist')}.haml").should be_false
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
- get "/#{file_of.('Do Not Exist')}"
81
+ ['/', ''].each do |route|
82
+ get route
62
83
 
63
- last_response.should be_not_found
64
- last_response.body.chomp.should == file_of.('Not Found')
65
- end
66
- end
67
-
68
- context "with a Layout file" do
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
- ['/', ''].each do |route|
78
- get route
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
- last_response.should be_ok
81
- separate.(last_response.body).first.should == 'Layout'
82
- separate.(last_response.body).last.should == file_of.('Home')
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
- it "should render both the Layout and an existing page if the given route match the '/:page' or '/*/:page' patterns." do
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?(file).should be_true
90
-
91
- directory = File.dirname(file)[5].nil? ? '' : File.dirname(file)[5, File.dirname(file).size]
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
- last_response.should be_ok
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 == File.basename(file, '.haml')
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
- it "should render both the Layout and the Not Found page if a given route can't find its static page on 'views/'." do
102
- File.exist?("views/#{file_of.('Layout')}.haml").should be_true
103
- File.exist?("views/#{file_of.('Do Not Exist')}.haml").should be_false
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
- get "/#{file_of.('Do Not Exist')}"
147
+ request "#{directory}/#{File.basename(file, '.haml')}", :method => 'GET', :xhr => true
106
148
 
107
- last_response.should be_not_found
108
- separate.(last_response.body).first.should == 'Layout'
109
- separate.(last_response.body).last.should == file_of.('Not Found')
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
- after :all do
113
- FileUtils.rm 'views/layout.haml'
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
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sinatra-pages
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.8.2
4
+ version: 0.9.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Julio Javier Cicchelli