Skip to content

Commit

Permalink
Merge pull request #404 from vimutter/problem-361-view_path
Browse files Browse the repository at this point in the history
[fix #361] Added test+fix on special usage of view_path within Rails con...
  • Loading branch information
nesquena committed Feb 11, 2013
2 parents bde2663 + e8fcf2e commit 4109c92
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 1 deletion.
3 changes: 2 additions & 1 deletion lib/rabl/partials.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
44 changes: 44 additions & 0 deletions test/partials_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 4109c92

Please sign in to comment.