Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding RABL support #521

Merged
merged 2 commits into from
Jun 23, 2012
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ gem 'maruku'
gem 'creole'
gem 'markaby'
gem 'radius'
gem 'rabl' unless RUBY_ENGINE == 'jruby'

if RUBY_ENGINE == 'jruby'
gem 'nokogiri', '!= 1.5.0'
Expand Down
6 changes: 6 additions & 0 deletions README.de.rdoc
Original file line number Diff line number Diff line change
Expand Up @@ -508,6 +508,12 @@ Nimmt ebenso einen Block für Inline-Templates entgegen (siehe Beispiel).

=== Slim Templates

Abhängigkeit:: {rabl}[https://github.com/nesquena/rabl]
Dateierweiterungs:: <tt>.rabl</tt>
Beispiel:: <tt>rabl :index</tt>

=== Slim Templates

Abhängigkeit:: {slim}[http://slim-lang.com/]
Dateierweiterungs:: <tt>.slim</tt>
Beispiel:: <tt>slim :index</tt>
Expand Down
6 changes: 6 additions & 0 deletions README.es.rdoc
Original file line number Diff line number Diff line change
Expand Up @@ -496,6 +496,12 @@ Ejemplos:: <tt>markaby { h1 "Bienvenido!" }</tt>

Además, acepta un bloque con la definición de la plantilla (ver el ejemplo).

=== Plantillas RABL

Dependencias:: {rabl}[https://github.com/nesquena/rabl]
Extensiones de Archivo:: <tt>.rabl</tt>
Ejemplo:: <tt>rabl :index</tt>

=== Plantillas Slim

Dependencias:: {slim}[http://slim-lang.com/]
Expand Down
6 changes: 6 additions & 0 deletions README.fr.rdoc
Original file line number Diff line number Diff line change
Expand Up @@ -508,6 +508,12 @@ Exemple:: <tt>markaby { h1 "Bienvenue !" }</tt>

Ce moteur accepte également un bloc pour des templates en ligne (voir exemple).

=== Templates RABL

Dépendances:: {rabl}[https://github.com/nesquena/rabl]
Extensions de fichier:: <tt>.rabl</tt>
Exemple:: <tt>rabl :index</tt>

=== Templates Slim

Dépendances:: {slim}[http://slim-lang.com/]
Expand Down
13 changes: 13 additions & 0 deletions README.jp.rdoc
Original file line number Diff line number Diff line change
Expand Up @@ -426,6 +426,19 @@ Markabyテンプレートを使うにはmarkabyライブラリが必要です:

<tt>./views/index.mab</tt>を表示します。

=== RABL テンプレート

RABLテンプレートを使うにはrablライブラリが必要です:

# rablを読み込みます
require 'rabl'

get '/' do
rabl :index
end

<tt>./views/index.rabl</tt>を表示します。

=== Slim テンプレート

Slimテンプレートを使うにはslimライブラリが必要です:
Expand Down
6 changes: 6 additions & 0 deletions README.ko.rdoc
Original file line number Diff line number Diff line change
Expand Up @@ -454,6 +454,12 @@ Radius 템플릿에서는 루비 메서드를 호출할 수 없기 때문에,

인라인 템플릿으로 블록을 받을 수도 있음(예제 참조).

=== RABL 템플릿

의존:: {rabl}[https://github.com/nesquena/rabl]
파일 확장자:: <tt>.rabl</tt>
예제:: <tt>rabl :index</tt>

=== Slim 템플릿

의존:: {slim}[http://slim-lang.com/]
Expand Down
6 changes: 6 additions & 0 deletions README.rdoc
Original file line number Diff line number Diff line change
Expand Up @@ -481,6 +481,12 @@ Example:: <tt>markaby { h1 "Welcome!" }</tt>

It also takes a block for inline templates (see example).

=== RABL Templates

Dependency:: {rabl}[https://github.com/nesquena/rabl]
File Extensions:: <tt>.rabl</tt>
Example:: <tt>rabl :index</tt>

=== Slim Templates

Dependency:: {slim}[http://slim-lang.com/]
Expand Down
6 changes: 6 additions & 0 deletions README.ru.rdoc
Original file line number Diff line number Diff line change
Expand Up @@ -488,6 +488,12 @@ Thin — это более производительный и функцион

Блок также используется и для встроенных шаблонов (см. пример).

=== RABL шаблоны

Зависимости:: {rabl}[https://github.com/nesquena/rabl]
Расширения файлов:: <tt>.rabl</tt>
Пример:: <tt>rabl :index</tt>

=== Slim шаблоны

Зависимости:: {slim}[http://slim-lang.com/]
Expand Down
5 changes: 5 additions & 0 deletions lib/sinatra/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -650,6 +650,11 @@ def yajl(template, options={}, locals={})
render :yajl, template, options, locals
end

def rabl(template, options={}, locals={})
Rabl.register!
render :rabl, template, options, locals
end

# Calls the given block for every possible template file in views,
# named name.ext, where ext is registered on engine.
def find_template(views, name, engine)
Expand Down
89 changes: 89 additions & 0 deletions test/rabl_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
require File.expand_path('../helper', __FILE__)

begin
require 'rabl'
require 'ostruct'
require 'json'
require 'active_support/core_ext/hash/conversions'

class RablTest < Test::Unit::TestCase
def rabl_app(&block)
mock_app {
set :views, File.dirname(__FILE__) + '/views'
get '/', &block
}
get '/'
end

it 'renders inline rabl strings' do
rabl_app do
@foo = OpenStruct.new(:baz => 'w00t')
rabl %q{
object @foo
attributes :baz
}
end
assert ok?
assert_equal '{"openstruct":{"baz":"w00t"}}', body
end
it 'renders .rabl files in views path' do
rabl_app do
@foo = OpenStruct.new(:bar => 'baz')
rabl :hello
end
assert ok?
assert_equal '{"openstruct":{"bar":"baz"}}', body
end

it "renders with file layouts" do
rabl_app {
@foo = OpenStruct.new(:bar => 'baz')
rabl :hello, :layout => :layout2
}
assert ok?
assert_equal '{"qux":{"openstruct":{"bar":"baz"}}}', body
end

it "raises error if template not found" do
mock_app {
get('/') { rabl :no_such_template }
}
assert_raise(Errno::ENOENT) { get('/') }
end

it "passes rabl options to the rabl engine" do
mock_app do
get('/') do
@foo = OpenStruct.new(:bar => 'baz')
rabl %q{
object @foo
attributes :bar
}, :format => 'xml'
end
end
get '/'
assert ok?
assert_body '<?xml version="1.0" encoding="UTF-8"?><openstruct><bar>baz</bar></openstruct>'
end

it "passes default rabl options to the rabl engine" do
mock_app do
set :rabl, :format => 'xml'
get('/') do
@foo = OpenStruct.new(:bar => 'baz')
rabl %q{
object @foo
attributes :bar
}
end
end
get '/'
assert ok?
assert_body '<?xml version="1.0" encoding="UTF-8"?><openstruct><bar>baz</bar></openstruct>'
end

end

rescue LoadError
warn "#{$!.to_s}: skipping rabl tests"
end
2 changes: 2 additions & 0 deletions test/views/hello.rabl
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
object @foo
attributes :bar
3 changes: 3 additions & 0 deletions test/views/layout2.rabl
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
node(:qux) do
::JSON.parse(yield)
end