Permalink
Browse files

[fix #361] Added test+fix on special usage of view_path within Rails …

…context.
  • Loading branch information...
1 parent e43f467 commit e8fcf2efa2285b096103aef468d7d39e0924723a @vimutter vimutter committed Feb 11, 2013
Showing with 46 additions and 1 deletion.
  1. +2 −1 lib/rabl/partials.rb
  2. +44 −0 test/partials_test.rb
View
3 lib/rabl/partials.rb
@@ -76,7 +76,8 @@ def fetch_rails_source(file, options={})
source_format = rendered_format unless rendered_format == :html
context_scope.lookup_context.find(file, [], partial, [], {:formats => [source_format]})
end }
- template = lookup_proc.call(false) rescue lookup_proc.call(true)
+ template = lookup_proc.call(false) rescue nil
+ template ||= lookup_proc.call(true) rescue nil
template.identifier if template
elsif source_format && context_scope.respond_to?(:view_paths) # Rails 2
template = context_scope.view_paths.find_template(file, source_format, false)
View
44 test/partials_test.rb
@@ -88,4 +88,48 @@ class TestPartial
Rabl.configuration.view_paths = []
end
end
+
+ context "fetch source with Rails" do
+ context "and :view_path" do
+ helper(:tmp_path) { @tmp_path ||= Pathname.new(Dir.mktmpdir) }
+
+ setup do
+ ::Rails = stub(Class.new)
+ ::ActionPack = Module.new
+ ::ActionPack::VERSION = Module.new
+ ::ActionPack::VERSION::MAJOR = 3
+ ::ActionPack::VERSION::MINOR = 2
+ @it = TestPartial.new
+
+ def @it.context_scope; @context_scope ||= Object.new; end
+ def @it.request_format; :json; end
+ context_scope = @it.context_scope
+
+ def context_scope.view_paths; []; end
+ def context_scope.lookup_context; @lookup_context ||= Object.new; end
+ lookup_context = context_scope.lookup_context
+
+ def lookup_context.rendered_format; :json; end
+ def lookup_context.find(*args)
+ raise RuntimeError, 'Something happen with Rails lookup'
+ end
+
+ File.open(tmp_path + "test.json.rabl", "w") { |f| f.puts "content" }
+
+ @it
+ end
+
+ asserts('rails lookups dont break manual') do
+ @it.fetch_source('test', :view_path => tmp_path.to_s)
+ end.equals do
+ ["content\n", (tmp_path + "test.json.rabl").to_s ]
+ end
+
+ teardown do
+ Object.send(:remove_const, :Rails)
+ Object.send(:remove_const, :ActionPack)
+ Rabl.configuration.view_paths = []
+ end
+ end
+ end
end

0 comments on commit e8fcf2e

Please sign in to comment.