From d49930887ac75b12c179090c6ef9e9b7bdc8f476 Mon Sep 17 00:00:00 2001 From: Marcello Barnaba Date: Mon, 27 Jan 2014 17:10:32 +0100 Subject: [PATCH] Remove format requirement when using RablRails.render As Rabl can render multiple templates, forcing the user to specify the template format in the file name is a limitation. This commit removes it, while remaining backwards compatible with the legacy format and printing out a deprecation warning. Eventually, an XML render test was added. --- lib/rabl-rails/renderer.rb | 29 +++++++++++++++++++++++++---- test/render_test.rb | 34 ++++++++++++++++++++++++++++------ 2 files changed, 53 insertions(+), 10 deletions(-) diff --git a/lib/rabl-rails/renderer.rb b/lib/rabl-rails/renderer.rb index 9894265..46bce21 100644 --- a/lib/rabl-rails/renderer.rb +++ b/lib/rabl-rails/renderer.rb @@ -19,13 +19,34 @@ def initialize(view_path, format) end # - # Manually find given rabl template file with given format. + # Manually find given rabl template file with given name. # View path can be set via options, otherwise default Rails # path is used # def find_template(name, opt, partial = false) - path = File.join(@view_path, "#{name}.#{@format}.rabl") - File.exists?(path) ? T.new(File.read(path)) : nil + path = _find_template([name, :rabl].join('.')) + path = find_legacy_template(name) unless path + + T.new(File.read(path)) if path + end + + private + + # Find legacy .json.rabl templates and prints a deprecation + # warning asking the user to rename them. + def find_legacy_template(name) + _find_template([name, @format, :rabl].join('.')).tap do |path| + if path + ActiveSupport::Deprecation.warn( + ".#@format.rabl templates are deprecated. " \ + "Please rename #@view_path/#{name}.#@format.rabl as #{name}.rabl") + end + end + end + + def _find_template(name) + path = File.join(@view_path, name) + return path if File.exists?(path) end end @@ -88,4 +109,4 @@ def render(object, template, options = {}) Library.instance.get_rendered_template(t.source, c, resource: object) end end -end \ No newline at end of file +end diff --git a/test/render_test.rb b/test/render_test.rb index 02bda38..daa9621 100644 --- a/test/render_test.rb +++ b/test/render_test.rb @@ -10,7 +10,7 @@ class RenderTest < ActiveSupport::TestCase end test "allow object to be passed as an option" do - File.open(@tmp_path + "nil.json.rabl", "w") do |f| + File.open(@tmp_path + "nil.rabl", "w") do |f| f.puts %q{ object :@user attributes :name @@ -20,12 +20,23 @@ class RenderTest < ActiveSupport::TestCase end test "load source from file" do + File.open(@tmp_path + "show.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 "handles legacy .FORMAT.rabl templates" do File.open(@tmp_path + "show.json.rabl", "w") do |f| f.puts %q{ object :@user attributes :id, :name } end + ActiveSupport::Deprecation.should_receive(:warn).with(/rename #@tmp_path\/show\.json\.rabl as show\.rabl/) assert_equal %q({"user":{"id":1,"name":"Marty"}}), RablRails.render(@user, 'show', view_path: @tmp_path) end @@ -34,7 +45,7 @@ class RenderTest < ActiveSupport::TestCase end test "instance variables can be passed via options[:locals]" do - File.open(@tmp_path + "instance.json.rabl", "w") do |f| + File.open(@tmp_path + "instance.rabl", "w") do |f| f.puts %q{ object false node(:username) { |_| @user.name } @@ -44,14 +55,14 @@ class RenderTest < ActiveSupport::TestCase end test "handle path for extends" do - File.open(@tmp_path + "extend.json.rabl", "w") do |f| + File.open(@tmp_path + "extend.rabl", "w") do |f| f.puts %q{ object :@user extends 'base' } end - File.open(@tmp_path + "base.json.rabl", "w") do |f| + File.open(@tmp_path + "base.rabl", "w") do |f| f.puts %q{ attribute :name, as: :extended_name } @@ -61,7 +72,7 @@ class RenderTest < ActiveSupport::TestCase end test "format can be passed as symbol or a string" do - File.open(@tmp_path + "show.json.rabl", "w") do |f| + File.open(@tmp_path + "show.rabl", "w") do |f| f.puts %q{ object :@user attributes :id, :name @@ -73,4 +84,15 @@ class RenderTest < ActiveSupport::TestCase assert_equal %q({"user":{"id":1,"name":"Marty"}}), RablRails.render(@user, 'show', view_path: @tmp_path, format: 'JSON') end -end \ No newline at end of file + test "render XML" do + File.open(@tmp_path + "show.rabl", "w") do |f| + f.puts %q{ + object :@user + attributes :id, :name + } + end + assert_equal "\n\n 1\n Marty\n\n", + RablRails.render(@user, 'show', view_path: @tmp_path, format: 'XML') + end + +end