Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Added view_paths configuration option #238

Merged
merged 2 commits into from

3 participants

@ivanvanderbyl

Added a quick view_paths configuration option, particularly useful when including Rabl in an engine and using view paths within a another Rails app.

Usage:

Rabl.configure do |config|
  config.view_paths << Rails.root.join('app/views/api')
end
@databyte
Collaborator

at a glance, looks :+1: to me.

@nesquena nesquena merged commit eb6db09 into from
@nesquena
Owner

Thanks pulled in and released 0.6.11

@ivanvanderbyl

Cheers dude!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
View
2  lib/rabl/configuration.rb
@@ -37,6 +37,7 @@ class Configuration
attr_accessor :cache_sources
attr_accessor :cache_all_output
attr_accessor :escape_all_output
+ attr_accessor :view_paths
DEFAULT_XML_OPTIONS = { :dasherize => true, :skip_types => false }
@@ -57,6 +58,7 @@ def initialize
@cache_sources = false
@cache_all_output = false
@escape_all_output = false
+ @view_paths = []
end
# @param [Symbol, String, #encode] engine_name The name of a JSON engine,
View
14 lib/rabl/partials.rb
@@ -30,21 +30,23 @@ def object_to_hash(object, options={}, &block)
# Returns source for a given relative file
# fetch_source("show", :view_path => "...") => "...contents..."
def fetch_source(file, options={})
- Rabl.source_cache(file, options[:view_path]) do
+ view_paths = Array(options[:view_path])
+ view_paths += Array(Rabl.configuration.view_paths)
+
+ Rabl.source_cache(file, view_paths) do
file_path = if defined?(Padrino) && context_scope.respond_to?(:settings)
fetch_padrino_source(file, options)
elsif defined?(Rails) && context_scope.respond_to?(:view_paths)
- view_path = Array(options[:view_path] || context_scope.view_paths.to_a)
- fetch_rails_source(file, options) || fetch_manual_template(view_path, file)
+ _view_paths = view_paths + Array(context_scope.view_paths.to_a)
+ fetch_rails_source(file, options) || fetch_manual_template(_view_paths, file)
elsif defined?(Sinatra) && context_scope.respond_to?(:settings)
fetch_sinatra_source(file, options)
else # generic template resolution
- view_path = Array(options[:view_path])
- fetch_manual_template(view_path, file)
+ fetch_manual_template(view_paths, file)
end
unless File.exist?(file_path.to_s)
- raise "Cannot find rabl template '#{file}' within registered (#{file_path}) view paths!"
+ raise "Cannot find rabl template '#{file}' within registered (#{view_paths.map(&:to_s).inspect}) view paths!"
end
[File.read(file_path.to_s), file_path.to_s] if file_path
View
1  test/configuration_test.rb
@@ -10,6 +10,7 @@
asserts(:include_json_root).equals true
asserts(:include_xml_root).equals false
asserts(:enable_json_callbacks).equals false
+ asserts(:view_paths).equals []
asserts(:json_engine).equals { json_engine }
end
View
23 test/partials_test.rb
@@ -62,4 +62,27 @@ class TestPartial
end
teardown { Object.send(:remove_const, :Sinatra) }
end
+
+ context "fetch source using configured view paths" do
+ helper(:tmp_path) { @tmp_path ||= Pathname.new(Dir.mktmpdir) }
+
+ setup do
+ Rabl.configure do |config|
+ config.view_paths = tmp_path
+ end
+
+ ::Sinatra = stub(Class.new)
+ File.open(tmp_path + "test.rabl", "w") do |f|
+ f.puts "content"
+ end
+ File.open(tmp_path + "test.json.rabl", "w") do |f|
+ f.puts "content2"
+ end
+ TestPartial.new.fetch_source('test')
+ end
+ asserts('detects file.json.rabl first') { topic }.equals do
+ ["content2\n", (tmp_path + 'test.json.rabl').to_s]
+ end
+ teardown { Object.send(:remove_const, :Sinatra) }
+ end
end
View
19 test/renderer_test.rb
@@ -61,7 +61,6 @@
attribute :name, :as => 'city'
}
- scope = Object.new
user = User.new(:name => 'irvine')
renderer = Rabl::Renderer.new(source, nil, { :format => 'json', :locals => {:object => user} })
@@ -82,6 +81,24 @@
renderer.render.split("").sort
end.equals "{\"user\":{\"age\":24,\"name\":\"irvine\"}}".split("").sort
+ asserts 'uses globally configured view paths' do
+ Rabl.configure do |config|
+ config.view_paths << tmp_path
+ end
+
+ File.open(tmp_path + "test.rabl", "w") do |f|
+ f.puts %q{
+ attributes :age
+ }
+ end
+
+ user = User.new(:name => 'irvine')
+
+ renderer = Rabl::Renderer.new('test', user)
+ renderer.render.split("").sort
+ end.equals "{\"user\":{\"age\":24,\"name\":\"irvine\"}}".split("").sort
+
+
asserts 'handles paths for extends' do
File.open(tmp_path + "test.json.rabl", "w") do |f|
f.puts %q{
Something went wrong with that request. Please try again.