sinatra-support 1.0.3 → 1.0.4
Sign up to get free protection for your applications and to get access to all the features.
- data/HISTORY.md +6 -0
- data/lib/sinatra/support.rb +1 -0
- data/lib/sinatra/support/multirender.rb +75 -0
- data/lib/sinatra/support/version.rb +1 -1
- data/test/fixtures/multirender/views_1/dupe.erb +1 -0
- data/test/fixtures/multirender/views_1/dupe.haml +1 -0
- data/test/fixtures/multirender/views_1/home.haml +1 -0
- data/test/fixtures/multirender/views_2/contact.haml +1 -0
- data/test/fixtures/multirender/views_2/home.haml +1 -0
- data/test/test_multirender.rb +60 -0
- metadata +10 -3
data/HISTORY.md
CHANGED
data/lib/sinatra/support.rb
CHANGED
@@ -11,6 +11,7 @@ module Sinatra
|
|
11
11
|
autoload :Numeric, File.expand_path('../support/numeric', __FILE__)
|
12
12
|
autoload :IfHelpers, File.expand_path('../support/ifhelpers', __FILE__)
|
13
13
|
autoload :I18nSupport, File.expand_path('../support/i18nsupport', __FILE__)
|
14
|
+
autoload :MultiRender, File.expand_path('../support/multirender', __FILE__)
|
14
15
|
|
15
16
|
module Support
|
16
17
|
end
|
@@ -0,0 +1,75 @@
|
|
1
|
+
# Allows rendering from multiple engines or renderers.
|
2
|
+
#
|
3
|
+
# == Usage
|
4
|
+
#
|
5
|
+
# require 'sinatra/support/multirender'
|
6
|
+
#
|
7
|
+
# class Main < Sinatra::Base
|
8
|
+
# register Sinatra::MultiRender
|
9
|
+
#
|
10
|
+
# # These two settings are optional.
|
11
|
+
# set :multi_views, [ './views', './skin/default' ]
|
12
|
+
# set :multi_engines, [ :erb, :haml ]
|
13
|
+
#
|
14
|
+
# get '/' do
|
15
|
+
# show :home
|
16
|
+
# end
|
17
|
+
# end
|
18
|
+
#
|
19
|
+
# Using #show will automatically find the appropriate template, trying the
|
20
|
+
# following variations:
|
21
|
+
#
|
22
|
+
# ./views/home.erb
|
23
|
+
# ./views/home.haml
|
24
|
+
# ./skin/default/home.erb
|
25
|
+
# ./skin/default/home.haml
|
26
|
+
#
|
27
|
+
module Sinatra::MultiRender
|
28
|
+
def self.registered(app)
|
29
|
+
views = app.views || './views'
|
30
|
+
|
31
|
+
app.set :multi_engines, Tilt.mappings.keys unless app.respond_to?(:template_engines)
|
32
|
+
app.set :multi_views, views unless app.respond_to?(:multi_views)
|
33
|
+
|
34
|
+
app.helpers Helpers
|
35
|
+
end
|
36
|
+
|
37
|
+
module Helpers
|
38
|
+
# Shows a given template.
|
39
|
+
#
|
40
|
+
# You may pass +options[:engine]+ to specify which engines you will want
|
41
|
+
# to limit the search to. This defaults to +App.multi_engines+, or Tilt's
|
42
|
+
# supported engines if that's not defined.
|
43
|
+
#
|
44
|
+
# You may pass +options[:views]+ to specify which directories to scour.
|
45
|
+
# This defaults to +App.multi_views+, or +App.views+.
|
46
|
+
#
|
47
|
+
# == Examples
|
48
|
+
#
|
49
|
+
# # Only HAML
|
50
|
+
# show :home, engine: :haml
|
51
|
+
#
|
52
|
+
# # HAML or ERB, favoring HAML first
|
53
|
+
# show :home, engine: [:haml, :erb]
|
54
|
+
#
|
55
|
+
# # Only look at certain paths
|
56
|
+
# show :home, views: [ './views', './skins/default' ]
|
57
|
+
#
|
58
|
+
def show(template, options={}, locals={}, &block)
|
59
|
+
paths = [*(options[:views] || settings.multi_views)].join(',')
|
60
|
+
engines = [*(options[:engine] || settings.multi_engines)].join(',')
|
61
|
+
|
62
|
+
t = @template_cache.fetch template, options do
|
63
|
+
template = Dir["{#{paths}}/#{template}.{#{engines}}"].first or raise Errno::ENOENT
|
64
|
+
|
65
|
+
ext = File.extname(template)[1..-1].to_sym
|
66
|
+
options = settings.send(ext).merge(options) if settings.respond_to?(ext)
|
67
|
+
engine = Tilt[ext] or raise "Template engine not found: #{ext}"
|
68
|
+
|
69
|
+
engine.new(template, 0, options)
|
70
|
+
end
|
71
|
+
|
72
|
+
t.render(self, locals, &block) if t
|
73
|
+
end
|
74
|
+
end
|
75
|
+
end
|
@@ -0,0 +1 @@
|
|
1
|
+
<span>From ERB</span>
|
@@ -0,0 +1 @@
|
|
1
|
+
%span From HAML
|
@@ -0,0 +1 @@
|
|
1
|
+
%html from 1
|
@@ -0,0 +1 @@
|
|
1
|
+
%html contact
|
@@ -0,0 +1 @@
|
|
1
|
+
%html from 2
|
@@ -0,0 +1,60 @@
|
|
1
|
+
require File.expand_path('../helper', __FILE__)
|
2
|
+
|
3
|
+
class MultiRenderTest < Test::Unit::TestCase
|
4
|
+
include Rack::Test::Methods
|
5
|
+
|
6
|
+
class App < Sinatra::Base
|
7
|
+
register Sinatra::MultiRender
|
8
|
+
|
9
|
+
set :multi_views, [
|
10
|
+
File.expand_path('../fixtures/multirender/views_1', __FILE__),
|
11
|
+
File.expand_path('../fixtures/multirender/views_2', __FILE__)
|
12
|
+
]
|
13
|
+
|
14
|
+
get('/') { show :home }
|
15
|
+
get('/contact') { show :contact }
|
16
|
+
get('/dupe/haml') { show :dupe, engine: :haml }
|
17
|
+
get('/dupe/erb') { show :dupe, engine: [:erb] }
|
18
|
+
get('/dupe/erb2') { show :dupe, engine: [:erb, :haml] }
|
19
|
+
end
|
20
|
+
|
21
|
+
def app
|
22
|
+
App.new
|
23
|
+
end
|
24
|
+
|
25
|
+
test "picking one" do
|
26
|
+
get '/'
|
27
|
+
assert last_response.body.strip == '<html>from 1</html>'
|
28
|
+
end
|
29
|
+
|
30
|
+
test "picking one from the second path" do
|
31
|
+
get '/contact'
|
32
|
+
assert last_response.body.strip == '<html>contact</html>'
|
33
|
+
end
|
34
|
+
|
35
|
+
test "specifying engines" do
|
36
|
+
get '/dupe/haml'
|
37
|
+
assert last_response.body.strip == '<span>From HAML</span>'
|
38
|
+
end
|
39
|
+
|
40
|
+
test "specifying engines (2)" do
|
41
|
+
get '/dupe/erb'
|
42
|
+
assert last_response.body.strip == '<span>From ERB</span>'
|
43
|
+
end
|
44
|
+
|
45
|
+
test "specifying engines (2)" do
|
46
|
+
get '/dupe/erb2'
|
47
|
+
assert last_response.body.strip == '<span>From ERB</span>'
|
48
|
+
end
|
49
|
+
|
50
|
+
test "single view path" do
|
51
|
+
old = App.multi_views
|
52
|
+
|
53
|
+
App.set :multi_views, fixture_path('multirender/views_2')
|
54
|
+
|
55
|
+
get '/'
|
56
|
+
assert last_response.body.strip == '<html>from 2</html>'
|
57
|
+
|
58
|
+
App.set :multi_views, old
|
59
|
+
end
|
60
|
+
end
|
metadata
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
name: sinatra-support
|
3
3
|
version: !ruby/object:Gem::Version
|
4
4
|
prerelease:
|
5
|
-
version: 1.0.
|
5
|
+
version: 1.0.4
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
8
8
|
- Cyril David
|
@@ -11,7 +11,7 @@ autorequire:
|
|
11
11
|
bindir: bin
|
12
12
|
cert_chain: []
|
13
13
|
|
14
|
-
date: 2011-
|
14
|
+
date: 2011-05-15 00:00:00 +08:00
|
15
15
|
default_executable:
|
16
16
|
dependencies:
|
17
17
|
- !ruby/object:Gem::Dependency
|
@@ -112,12 +112,18 @@ files:
|
|
112
112
|
- lib/sinatra/support/i18nsupport.rb
|
113
113
|
- lib/sinatra/support/ifhelpers.rb
|
114
114
|
- lib/sinatra/support/jssupport.rb
|
115
|
+
- lib/sinatra/support/multirender.rb
|
115
116
|
- lib/sinatra/support/numeric.rb
|
116
117
|
- lib/sinatra/support/ohmerrorhelpers.rb
|
117
118
|
- lib/sinatra/support/version.rb
|
118
119
|
- lib/sinatra/support.rb
|
119
120
|
- test/fixtures/i18n/en.yml
|
120
121
|
- test/fixtures/i18n/tl.yml
|
122
|
+
- test/fixtures/multirender/views_1/dupe.erb
|
123
|
+
- test/fixtures/multirender/views_1/dupe.haml
|
124
|
+
- test/fixtures/multirender/views_1/home.haml
|
125
|
+
- test/fixtures/multirender/views_2/contact.haml
|
126
|
+
- test/fixtures/multirender/views_2/home.haml
|
121
127
|
- test/helper.rb
|
122
128
|
- test/test_country.rb
|
123
129
|
- test/test_date.rb
|
@@ -126,6 +132,7 @@ files:
|
|
126
132
|
- test/test_html.rb
|
127
133
|
- test/test_htmlhelpers.rb
|
128
134
|
- test/test_i18n.rb
|
135
|
+
- test/test_multirender.rb
|
129
136
|
- test/test_numeric.rb
|
130
137
|
- test/test_ohmerror.rb
|
131
138
|
- HISTORY.md
|
@@ -154,7 +161,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
154
161
|
requirements: []
|
155
162
|
|
156
163
|
rubyforge_project:
|
157
|
-
rubygems_version: 1.
|
164
|
+
rubygems_version: 1.6.2
|
158
165
|
signing_key:
|
159
166
|
specification_version: 3
|
160
167
|
summary: A gem with many essential helpers for creating web apps with Sinatra.
|