rabl-rails-0.3.2/ 0000755 0001750 0001750 00000000000 12151754461 013130 5 ustar boutil boutil rabl-rails-0.3.2/CHANGELOG.md 0000644 0001750 0001750 00000003461 12151754461 014745 0 ustar boutil boutil # CHANGELOG
## 0.3.2
* Using child with a nil value will be correctly formatted as nil
* Allow controller's assigns to have symbol keys
* Does not modify in place format extracted from context
* Add JSONP support
## 0.3.1
* Add `merge` keywork
* Format can be passed as a string or a symbol
* Avoid to unexpectedly change cached templates (johnbintz)
* Add full template stack support to `glue` (fnordfish)
* Allow format to be a symbol (lloydmeta)
## 0.3.0
* Travis integration
* Add test for keywords used as variable names
* Add PList renderer
* Remove location header from post responses in responder
* Fix bug with incomplete template prefixing
## 0.2.2
* Add condition blocks
## 0.2.1
* Avoid useless render on POST request with custom responder
* Custom responder now fallback to Rails default in case the template is not found
## 0.2.0
* Add `root` in DSL to set root without changing the data source
* Add XML renderer
* Use MultiJson's preferred JSON engine as default (shmeltex)
* Default template to render with responder can be set per controller
* Reponder works out of the box with devise
* object or collection can be skipped if use with `respond_to` blocks
## 0.1.3
* Render correctly when variables are not passed via the assigns ivar but as helper methods
(decent_exposure, focused_controller)
* Add custom Responder
## 0.1.2
* Add RablRails#render method (see README or source code)
* Fix fail when JSON engine is not found. Now fallback to MultiJson.default_adapter
* Warning message printed on logger when JSON engine fail to load
## 0.1.1
* Add CHANGELOG
* Remove unused test in loop
* Speed up rendering by not double copying variable from context
* Rename private variable to avoid name conflict
* Remove sqlite3 development dependency
rabl-rails-0.3.2/Gemfile 0000644 0001750 0001750 00000000323 12151754461 014421 0 ustar boutil boutil source "http://rubygems.org"
gemspec
gem 'plist'
platforms :ruby do
gem 'oj'
end
platforms :mri do
gem 'libxml-ruby'
end
platforms :jruby do
gem 'nokogiri'
end
group :test do
gem 'rspec-mocks'
end
rabl-rails-0.3.2/metadata.yml 0000644 0001750 0001750 00000007371 12151754461 015443 0 ustar boutil boutil --- !ruby/object:Gem::Specification
name: rabl-rails
version: !ruby/object:Gem::Version
version: 0.3.2
prerelease:
platform: ruby
authors:
- Christopher Cocchi-Perrier
autorequire:
bindir: bin
cert_chain: []
date: 2013-05-28 00:00:00.000000000 Z
dependencies:
- !ruby/object:Gem::Dependency
name: activesupport
requirement: !ruby/object:Gem::Requirement
none: false
requirements:
- - ~>
- !ruby/object:Gem::Version
version: '3.0'
type: :runtime
prerelease: false
version_requirements: !ruby/object:Gem::Requirement
none: false
requirements:
- - ~>
- !ruby/object:Gem::Version
version: '3.0'
- !ruby/object:Gem::Dependency
name: railties
requirement: !ruby/object:Gem::Requirement
none: false
requirements:
- - ~>
- !ruby/object:Gem::Version
version: '3.0'
type: :runtime
prerelease: false
version_requirements: !ruby/object:Gem::Requirement
none: false
requirements:
- - ~>
- !ruby/object:Gem::Version
version: '3.0'
- !ruby/object:Gem::Dependency
name: actionpack
requirement: !ruby/object:Gem::Requirement
none: false
requirements:
- - ~>
- !ruby/object:Gem::Version
version: '3.0'
type: :development
prerelease: false
version_requirements: !ruby/object:Gem::Requirement
none: false
requirements:
- - ~>
- !ruby/object:Gem::Version
version: '3.0'
- !ruby/object:Gem::Dependency
name: rspec-mocks
requirement: !ruby/object:Gem::Requirement
none: false
requirements:
- - ! '>='
- !ruby/object:Gem::Version
version: '0'
type: :development
prerelease: false
version_requirements: !ruby/object:Gem::Requirement
none: false
requirements:
- - ! '>='
- !ruby/object:Gem::Version
version: '0'
description: Fast Rails 3+ templating system with JSON, XML and PList support
email:
- cocchi.c@gmail.com
executables: []
extensions: []
extra_rdoc_files: []
files:
- .gitignore
- .travis.yml
- CHANGELOG.md
- Gemfile
- MIT-LICENSE
- README.md
- Rakefile
- lib/rabl-rails.rb
- lib/rabl-rails/compiler.rb
- lib/rabl-rails/condition.rb
- lib/rabl-rails/handler.rb
- lib/rabl-rails/library.rb
- lib/rabl-rails/railtie.rb
- lib/rabl-rails/renderer.rb
- lib/rabl-rails/renderers/base.rb
- lib/rabl-rails/renderers/json.rb
- lib/rabl-rails/renderers/plist.rb
- lib/rabl-rails/renderers/xml.rb
- lib/rabl-rails/responder.rb
- lib/rabl-rails/template.rb
- lib/rabl-rails/version.rb
- lib/tasks/rabl-rails.rake
- rabl-rails.gemspec
- test/base_renderer_test.rb
- test/cache_templates_test.rb
- test/compiler_test.rb
- test/deep_nesting_test.rb
- test/keyword_test.rb
- test/non_restful_response_test.rb
- test/render_test.rb
- test/renderers/json_renderer_test.rb
- test/renderers/plist_renderer_test.rb
- test/renderers/xml_renderer_test.rb
- test/test_helper.rb
homepage: https://github.com/ccocchi/rabl-rails
licenses: []
post_install_message:
rdoc_options: []
require_paths:
- lib
required_ruby_version: !ruby/object:Gem::Requirement
none: false
requirements:
- - ! '>='
- !ruby/object:Gem::Version
version: '0'
required_rubygems_version: !ruby/object:Gem::Requirement
none: false
requirements:
- - ! '>='
- !ruby/object:Gem::Version
version: '0'
requirements: []
rubyforge_project:
rubygems_version: 1.8.21
signing_key:
specification_version: 3
summary: Fast Rails 3+ templating system with JSON, XML and PList support
test_files:
- test/base_renderer_test.rb
- test/cache_templates_test.rb
- test/compiler_test.rb
- test/deep_nesting_test.rb
- test/keyword_test.rb
- test/non_restful_response_test.rb
- test/render_test.rb
- test/renderers/json_renderer_test.rb
- test/renderers/plist_renderer_test.rb
- test/renderers/xml_renderer_test.rb
- test/test_helper.rb
rabl-rails-0.3.2/rabl-rails.gemspec 0000644 0001750 0001750 00000001524 12151754461 016527 0 ustar boutil boutil $:.push File.expand_path("../lib", __FILE__)
require "rabl-rails/version"
Gem::Specification.new do |s|
s.name = "rabl-rails"
s.version = RablRails::VERSION
s.platform = Gem::Platform::RUBY
s.authors = ["Christopher Cocchi-Perrier"]
s.email = ["cocchi.c@gmail.com"]
s.homepage = "https://github.com/ccocchi/rabl-rails"
s.summary = "Fast Rails 3+ templating system with JSON, XML and PList support"
s.description = "Fast Rails 3+ templating system with JSON, XML and PList support"
s.files = `git ls-files`.split("\n")
s.test_files = `git ls-files -- test/*`.split("\n")
s.require_paths = ["lib"]
s.add_dependency "activesupport", "~> 3.0"
s.add_dependency "railties", "~> 3.0"
s.add_development_dependency "actionpack", "~> 3.0"
s.add_development_dependency "rspec-mocks"
end
rabl-rails-0.3.2/test/ 0000755 0001750 0001750 00000000000 12151754461 014107 5 ustar boutil boutil rabl-rails-0.3.2/test/base_renderer_test.rb 0000644 0001750 0001750 00000001446 12151754461 020300 0 ustar boutil boutil require 'test_helper'
class TestBaseRenderer < ActiveSupport::TestCase
RablRails::Renderers::Base.class_eval do
def format_output(hash)
hash
end
end
setup do
@data = User.new(1, 'foobar', 'male')
@context = Context.new
@context.assigns['data'] = @data
@template = RablRails::CompiledTemplate.new
@template.data = :@data
end
def render_hash
RablRails::Renderers::Base.new(@context).render(@template)
end
test "child with nil data should render nil" do
@template.source = { :author => { :_data => :@nil, :name => :name } }
assert_equal({ :author => nil }, render_hash)
end
test "properly handle assigns with symbol keys" do
@context.assigns[:foo] = 'bar'
@template.source = {}
assert_nothing_raised { render_hash }
end
end rabl-rails-0.3.2/test/cache_templates_test.rb 0000644 0001750 0001750 00000002260 12151754461 020614 0 ustar boutil boutil require 'test_helper'
class CacheTemplatesTest < ActiveSupport::TestCase
setup do
RablRails::Library.reset_instance
@library = RablRails::Library.instance
RablRails.cache_templates = true
end
test "cache templates if perform_caching is active and cache_templates is enabled" do
ActionController::Base.stub(:perform_caching).and_return(true)
@library.compile_template_from_source('', 'some/path')
t = @library.compile_template_from_source("attribute :id", 'some/path')
assert_equal({}, t.source)
end
test "cached templates should not be modifiable in place" do
ActionController::Base.stub(:perform_caching).and_return(true)
t = @library.compile_template_from_source('', 'some/path')
t.merge!(:_data => :foo)
assert_equal({}, @library.compile_template_from_path('some/path').source)
end
test "don't cache templates cache_templates is enabled but perform_caching is not active" do
ActionController::Base.stub(:perform_caching).and_return(false)
@library.compile_template_from_source('', 'some/path')
t = @library.compile_template_from_source("attribute :id", 'some/path')
assert_equal({ :id => :id }, t.source)
end
end rabl-rails-0.3.2/test/renderers/ 0000755 0001750 0001750 00000000000 12151754461 016100 5 ustar boutil boutil rabl-rails-0.3.2/test/renderers/plist_renderer_test.rb 0000644 0001750 0001750 00000011773 12151754461 022516 0 ustar boutil boutil require 'test_helper'
class TestPlistRenderer < ActiveSupport::TestCase
INDENT_REGEXP = /\n(\s)*/
HEADER_REGEXP = /<\?[^>]+>]+>/
setup do
@data = User.new(1, 'foobar', 'male')
@context = Context.new
@context.assigns['data'] = @data
@template = RablRails::CompiledTemplate.new
@template.data = :@data
@template.root_name = :user
end
def render_plist_output
output = RablRails::Renderers::PLIST.new(@context).render(@template).to_s.gsub!(INDENT_REGEXP, '')
output.sub!(HEADER_REGEXP, '').gsub!(%r(?plist[^>]*>), '').sub!(%r(), '').sub(%r(), '')
end
test "plist engine should responsd to #dump" do
assert_raises(RuntimeError) { RablRails.plist_engine = Object.new }
end
test "render object wth empty template" do
@template.source = {}
assert_equal %q(), render_plist_output
end
test "render collection with empty template" do
@context.assigns['data'] = [@data]
@template.source = {}
assert_equal %q(), render_plist_output
end
test "render object with local methods (used by decent_exposure)" do
@context.stub(:user).and_return(@data)
@template.data = :user
@template.source = { :id => :id }
assert_equal %q(id1), render_plist_output
end
test "render single object attributes" do
@template.source = { :id => :id, :name => :name }
assert_equal %q(id1namefoobar), render_plist_output
end
test "render child with object association" do
@data.stub(:address).and_return(mock(:city => 'Paris'))
@template.source = { :address => { :_data => :address, :city => :city } }
assert_equal %q(addresscityParis), render_plist_output
end
test "render child with arbitrary data source" do
@template.source = { :author => { :_data => :@data, :name => :name } }
assert_equal %q(authornamefoobar), render_plist_output
end
test "render child with local methods (used by decent_exposure)" do
@context.stub(:user).and_return(@data)
@template.source = { :author => { :_data => :user, :name => :name } }
assert_equal %q(authornamefoobar), render_plist_output
end
test "render node property" do
proc = lambda { |object| object.name }
@template.source = { :name => proc }
assert_equal %q(namefoobar), render_plist_output
end
test "render node property with true condition" do
condition = lambda { |u| true }
proc = lambda { |object| object.name }
@template.source = { :name => [condition, proc] }
assert_equal %q(namefoobar), render_plist_output
end
test "render node property with false condition" do
condition = lambda { |u| false }
proc = lambda { |object| object.name }
@template.source = { :name => [condition, proc] }
assert_equal %q(), render_plist_output
end
test "node with context method call" do
@context.stub(:respond_to?).with(:@data).and_return(false)
@context.stub(:respond_to?).with(:context_method).and_return(true)
@context.stub(:context_method).and_return('marty')
proc = lambda { |object| context_method }
@template.source = { :name => proc }
assert_equal %q(namemarty), render_plist_output
end
test "partial with no values should raise an error" do
@template.data = false
@template.source = { :user => ->(s) { partial('users/base') } }
assert_raises(RablRails::Renderers::PartialError) { render_plist_output }
end
test "partial with empty values should not raise an error" do
@template.data = false
@template.source = { :users => ->(s) { partial('users/base', :object => []) } }
assert_equal %q(users), render_plist_output
end
test "condition blocks are transparent if the condition passed" do
c = RablRails::Condition.new(->(u) { true }, { :name => :name })
@template.source = { :_if0 => c }
assert_equal %q(namefoobar), render_plist_output
end
test "condition blocks are ignored if the condition is not met" do
c = RablRails::Condition.new(->(u) { false }, { :name => :name })
@template.source = { :_if0 => c }
assert_equal %q(), render_plist_output
end
test "render object with root node" do
RablRails.include_plist_root = true
@template.root_name = :author
@template.source = { :id => :id, :name => :name }
assert_equal %q(authorid1namefoobar), render_plist_output
end
test "render object with root options set to false" do
RablRails.include_plist_root = false
@template.root_name = :author
@template.source = { :id => :id, :name => :name }
assert_equal %q(id1namefoobar), render_plist_output
end
end rabl-rails-0.3.2/test/renderers/json_renderer_test.rb 0000644 0001750 0001750 00000014216 12151754461 022327 0 ustar boutil boutil require 'test_helper'
class TestJsonRenderer < ActiveSupport::TestCase
setup do
@data = User.new(1, 'foobar', 'male')
@context = Context.new
@context.assigns['data'] = @data
@template = RablRails::CompiledTemplate.new
@template.data = :@data
end
def render_json_output
RablRails::Renderers::JSON.new(@context).render(@template)
end
test "render object wth empty template" do
@template.source = {}
assert_equal %q({}), render_json_output
end
test "render collection with empty template" do
@context.assigns['data'] = [@data]
@template.source = {}
assert_equal %q([{}]), render_json_output
end
test "render object with local methods (used by decent_exposure)" do
@context.stub(:user).and_return(@data)
@template.data = :user
@template.source = { :id => :id }
assert_equal %q({"id":1}), render_json_output
end
test "render single object attributes" do
@template.source = { :id => :id, :name => :name }
assert_equal %q({"id":1,"name":"foobar"}), render_json_output
end
test "render child with object association" do
@data.stub(:address).and_return(mock(:city => 'Paris'))
@template.source = { :address => { :_data => :address, :city => :city } }
assert_equal %q({"address":{"city":"Paris"}}), render_json_output
end
test "render child with arbitrary data source" do
@template.source = { :author => { :_data => :@data, :name => :name } }
assert_equal %q({"author":{"name":"foobar"}}), render_json_output
end
test "render child with local methods (used by decent_exposure)" do
@context.stub(:user).and_return(@data)
@template.source = { :author => { :_data => :user, :name => :name } }
assert_equal %q({"author":{"name":"foobar"}}), render_json_output
end
test "render glued attributes from single object" do
@template.source = { :_glue0 => { :_data => :@data, :name => :name } }
assert_equal %q({"name":"foobar"}), render_json_output
end
test "render glued node" do
@template.source = { :_glue0 => { :_data => :@data, :foo => lambda { |u| u.name } } }
assert_equal(%q({"foo":"foobar"}), render_json_output)
end
test "render collection with attributes" do
@data = [User.new(1, 'foo', 'male'), User.new(2, 'bar', 'female')]
@context.assigns['data'] = @data
@template.source = { :uid => :id, :name => :name, :gender => :sex }
assert_equal %q([{"uid":1,"name":"foo","gender":"male"},{"uid":2,"name":"bar","gender":"female"}]), render_json_output
end
test "render node property" do
proc = lambda { |object| object.name }
@template.source = { :name => proc }
assert_equal %q({"name":"foobar"}), render_json_output
end
test "render node property with true condition" do
condition = lambda { |u| true }
proc = lambda { |object| object.name }
@template.source = { :name => [condition, proc] }
assert_equal %q({"name":"foobar"}), render_json_output
end
test "render node property with false condition" do
condition = lambda { |u| false }
proc = lambda { |object| object.name }
@template.source = { :name => [condition, proc] }
assert_equal %q({}), render_json_output
end
test "node with context method call" do
@context.stub(:respond_to?).with(:@data).and_return(false)
@context.stub(:respond_to?).with(:context_method).and_return(true)
@context.stub(:context_method).and_return('marty')
proc = lambda { |object| context_method }
@template.source = { :name => proc }
assert_equal %q({"name":"marty"}), render_json_output
end
test "partial should be evaluated at rendering time" do
# Set assigns
@context.assigns['user'] = @data
# Stub Library#get
t = RablRails::CompiledTemplate.new
t.source = { :name => :name }
RablRails::Library.reset_instance
RablRails::Library.instance.should_receive(:compile_template_from_path).with('users/base').and_return(t)
@template.data = false
@template.source = { :user => ->(s) { partial('users/base', :object => @user) } }
assert_equal %q({"user":{"name":"foobar"}}), render_json_output
end
test "partial with no values should raise an error" do
@template.data = false
@template.source = { :user => ->(s) { partial('users/base') } }
assert_raises(RablRails::Renderers::PartialError) { render_json_output }
end
test "partial with empty values should not raise an error" do
@template.data = false
@template.source = { :users => ->(s) { partial('users/base', :object => []) } }
assert_equal %q({"users":[]}), render_json_output
end
test "condition blocks are transparent if the condition passed" do
c = RablRails::Condition.new(->(u) { true }, { :name => :name })
@template.source = { :_if0 => c }
assert_equal %q({"name":"foobar"}), render_json_output
end
test "condition blocks are ignored if the condition is not met" do
c = RablRails::Condition.new(->(u) { false }, { :name => :name })
@template.source = { :_if0 => c }
assert_equal %q({}), render_json_output
end
test "render object with root node" do
RablRails.include_json_root = true
@template.root_name = :author
@template.source = { :id => :id, :name => :name }
assert_equal %q({"author":{"id":1,"name":"foobar"}}), render_json_output
end
test "render object with root options set to false" do
RablRails.include_json_root = false
@template.root_name = :author
@template.source = { :id => :id, :name => :name }
assert_equal %q({"id":1,"name":"foobar"}), render_json_output
end
test "merge should raise is return from given block is not a hash" do
@template.source = { :_merge0 => ->(c) { 'foo' } }
assert_raises(RablRails::Renderers::PartialError) { render_json_output }
end
test "result from merge is merge inside current response" do
@template.source = { :_merge0 => ->(c) { { :custom => c.name } } }
assert_equal %q({"custom":"foobar"}), render_json_output
end
test "render with jsonp callback" do
RablRails.enable_jsonp_callbacks = true
@context.stub(:params).and_return({ callback: 'some_callback' })
@template.source = { :name => :name }
assert_equal %q[some_callback({"name":"foobar"})], render_json_output
end
end rabl-rails-0.3.2/test/renderers/xml_renderer_test.rb 0000644 0001750 0001750 00000011164 12151754461 022155 0 ustar boutil boutil require 'test_helper'
class TestXmlRenderer < ActiveSupport::TestCase
INDENT_REGEXP = /\n(\s)*/
HEADER_REGEXP = /<[^>]+>/
setup do
@data = User.new(1, 'foobar', 'male')
@context = Context.new
@context.assigns['data'] = @data
@template = RablRails::CompiledTemplate.new
@template.data = :@data
@template.root_name = :user
end
def render_xml_output
RablRails::Renderers::XML.new(@context).render(@template).to_s.gsub!(INDENT_REGEXP, '').sub!(HEADER_REGEXP, '')
end
test "render object simple object" do
@template.source = {}
assert_equal %q(), render_xml_output
end
test "render collection with empty template" do
@context.assigns['data'] = [@data]
@template.source = {}
@template.root_name = :users
assert_equal %q(), render_xml_output
end
test "render object with local methods (used by decent_exposure)" do
@context.stub(:user).and_return(@data)
@template.source = { :id => :id }
assert_equal %q(1), render_xml_output
end
test "render single object attributes" do
@template.source = { :name => :name }
assert_equal %q(foobar), render_xml_output
end
test "render child with arbitrary data source" do
@template.source = { :author => { :_data => :@data, :name => :name } }
@template.root_name = :post
assert_equal %q(foobar), render_xml_output
end
test "render child with local methods (used by decent_exposure)" do
@context.stub(:user).and_return(@data)
@template.source = { :author => { :_data => :user, :name => :name } }
@template.root_name = :post
assert_equal %q(foobar), render_xml_output
end
test "render glued attributes from single object" do
@template.source = { :_glue0 => { :_data => :@data, :name => :name } }
assert_equal %q(foobar), render_xml_output
end
test "render collection with attributes" do
@data = [User.new(1, 'foo', 'male'), User.new(2, 'bar', 'female')]
@context.assigns['data'] = @data
@template.root_name = :users
@template.source = { :uid => :id, :name => :name }
assert_equal %q(1foo2bar), render_xml_output
end
test "render node property" do
proc = lambda { |object| object.name }
@template.source = { :name => proc }
assert_equal %q(foobar), render_xml_output
end
test "render node property with true condition" do
condition = lambda { |u| true }
proc = lambda { |object| object.name }
@template.source = { :name => [condition, proc] }
assert_equal %q(foobar), render_xml_output
end
test "render node property with false condition" do
condition = lambda { |u| false }
proc = lambda { |object| object.name }
@template.source = { :name => [condition, proc] }
assert_equal %q(), render_xml_output
end
test "node with context method call" do
@context.stub(:respond_to?).with(:@data).and_return(false)
@context.stub(:respond_to?).with(:context_method).and_return(true)
@context.stub(:context_method).and_return('marty')
proc = lambda { |object| context_method }
@template.source = { :name => proc }
assert_equal %q(marty), render_xml_output
end
test "partial should be evaluated at rendering time" do
# Set assigns
@context.assigns['user'] = @data
# Stub Library#get
t = RablRails::CompiledTemplate.new
t.source = { :name => :name }
RablRails::Library.reset_instance
RablRails::Library.instance.should_receive(:compile_template_from_path).with('users/base').and_return(t)
@template.data = false
@template.root_name = :post
@template.source = { :user => ->(s) { partial('users/base', :object => @user) } }
assert_equal %q(foobar), render_xml_output
end
test "partial with no values should raise an error" do
@template.data = false
@template.source = { :user => ->(s) { partial('users/base') } }
assert_raises(RablRails::Renderers::PartialError) { render_xml_output }
end
test "partial with empty values should not raise an error" do
@template.data = false
@template.root_name = :list
@template.source = { :users => ->(s) { partial('users/base', :object => []) } }
assert_equal %q(
), render_xml_output
end
end rabl-rails-0.3.2/test/non_restful_response_test.rb 0000644 0001750 0001750 00000001776 12151754461 021762 0 ustar boutil boutil require 'test_helper'
class NonRestfulResponseTest < ActiveSupport::TestCase
setup do
RablRails::Library.reset_instance
@user = User.new(1, 'foo', 'male')
@user.stub_chain(:posts, :count).and_return(10)
@user.stub(:respond_to?).with(:each).and_return(false)
@context = Context.new
@context.stub(:instance_variable_get).with(:@user).and_return(@user)
@context.stub(:instance_variable_get).with(:@virtual_path).and_return('user/show')
@context.stub(:instance_variable_get).with(:@_assigns).and_return({'user' => @user})
@context.stub(:lookup_context)
end
test "compile and render non restful resource" do
source = %{
object false
node(:post_count) { @user.posts.count }
child(:@user => :user) do
attributes :id, :name
end
}
assert_equal(MultiJson.encode({
:post_count => 10,
:user => {
:id => 1,
:name => 'foo'
}
}), RablRails::Library.instance.get_rendered_template(source, @context))
end
end rabl-rails-0.3.2/test/render_test.rb 0000644 0001750 0001750 00000004423 12151754461 016755 0 ustar boutil boutil require 'test_helper'
require 'pathname'
require 'tmpdir'
class RenderTest < ActiveSupport::TestCase
setup do
@user = User.new(1, 'Marty')
@tmp_path = Pathname.new(Dir.mktmpdir)
end
test "allow object to be passed as an option" do
File.open(@tmp_path + "nil.json.rabl", "w") do |f|
f.puts %q{
object :@user
attributes :name
}
end
assert_equal %q({"user":{"name":"Marty"}}), RablRails.render(nil, 'nil', locals: { object: @user }, view_path: @tmp_path)
end
test "load source from file" do
File.open(@tmp_path + "show.json.rabl", "w") do |f|
f.puts %q{
object :@user
attributes :id, :name
}
end
assert_equal %q({"user":{"id":1,"name":"Marty"}}), RablRails.render(@user, 'show', view_path: @tmp_path)
end
test "raise error if template is not found" do
assert_raises(RablRails::Renderer::TemplateNotFound) { RablRails.render(@user, 'not_found') }
end
test "instance variables can be passed via options[:locals]" do
File.open(@tmp_path + "instance.json.rabl", "w") do |f|
f.puts %q{
object false
node(:username) { |_| @user.name }
}
end
assert_equal %q({"username":"Marty"}), RablRails.render(nil, 'instance', view_path: @tmp_path, locals: { user: @user })
end
test "handle path for extends" do
File.open(@tmp_path + "extend.json.rabl", "w") do |f|
f.puts %q{
object :@user
extends 'base'
}
end
File.open(@tmp_path + "base.json.rabl", "w") do |f|
f.puts %q{
attribute :name, as: :extended_name
}
end
assert_equal %q({"user":{"extended_name":"Marty"}}), RablRails.render(@user, 'extend', view_path: @tmp_path)
end
test "format can be passed as symbol or a string" do
File.open(@tmp_path + "show.json.rabl", "w") do |f|
f.puts %q{
object :@user
attributes :id, :name
}
end
assert_equal %q({"user":{"id":1,"name":"Marty"}}), RablRails.render(@user, 'show', view_path: @tmp_path, format: :json)
assert_equal %q({"user":{"id":1,"name":"Marty"}}), RablRails.render(@user, 'show', view_path: @tmp_path, format: 'json')
assert_equal %q({"user":{"id":1,"name":"Marty"}}), RablRails.render(@user, 'show', view_path: @tmp_path, format: 'JSON')
end
end rabl-rails-0.3.2/test/test_helper.rb 0000644 0001750 0001750 00000002101 12151754461 016744 0 ustar boutil boutil ENV["RAILS_ENV"] = "test"
$:.unshift File.expand_path('../../lib', __FILE__)
require 'rspec/mocks'
require 'minitest/unit'
MiniTest::Unit.autorun
require 'active_support/test_case'
require 'action_controller'
require 'singleton'
class <