Permalink
Browse files

Allowed a way to provide default when common templates are missing

- added :default option to render
  • Loading branch information...
Reid MacDonald
Reid MacDonald committed Dec 31, 2009
1 parent 52b5243 commit 16f70a97468f489c3530d4f6eb9fa243eb0c6c01
Showing with 91 additions and 4 deletions.
  1. +2 −2 README.rdoc
  2. +2 −1 init.rb
  3. +1 −0 lib/overlay_paths.rb
  4. +17 −0 lib/overlay_render.rb
  5. +7 −1 spec/overlay_paths_spec.rb
  6. +62 −0 spec/overlay_render_spec.rb
View
@@ -70,9 +70,9 @@ Then create the following partials:
- views/artists/_artist_lastfm.html.erb # used when lastfm cobrand
If you wanted only the cobranded partials to show up, and there is no common partial don't
add "views/artists/_artist.html.erb" but make sure you rescue:
add "views/artists/_artist.html.erb" but make you can pass a default:
= render :partial => 'artist' rescue nil
= render :partial => 'artist', :default => nil
== Internationalization (and Formats)
View
@@ -1 +1,2 @@
require 'overlay_paths'
require 'overlay_render'
require 'overlay_paths'
View
@@ -17,6 +17,7 @@ def find_template_with_overlay(original_template_path, format = nil, html_fallba
elsif template = load_path["#{template_path}_#{Overlay.current}.#{I18n.locale}"]
return template
else
Thread.current[:missing_default] = true
return find_template_without_overlay(original_template_path, format, html_fallback)
end
end
View
@@ -0,0 +1,17 @@
ActionView::Base.class_eval do
def render_with_missing_default(options = {}, local_assigns = {}, &block) #:nodoc:
if options.respond_to?(:key?) && options.key?(:default)
has_default, default = true, options.delete(:default)
end
render_without_missing_default(options, local_assigns, &block)
rescue ActionView::MissingTemplate => e
if Thread.current[:missing_default]
Thread.current[:missing_default] = nil
return default if has_default
end
raise e
end
alias_method_chain :render, :missing_default
end
@@ -1,6 +1,6 @@
require File.dirname(__FILE__) + '/spec_helper'
describe Overlay do
describe ActionView::PathSet do
describe :alias_method_chain do
it 'defines chained methods' do
ActionView::PathSet.instance_methods.should be_member('find_template')
@@ -29,6 +29,12 @@
template = mock('template', :render => true)
@paths.find_template(template).should == template
end
it 'sets Thead.current on :missing_template to true' do
@paths.should_receive(:find_template_without_overlay).with('foo', 'html', false).and_return :template
Thread.current.should_receive(:[]=).with(:missing_default, true)
@paths.find_template('foo', 'html', false)
end
end
describe 'with overlay and locale match' do
@@ -0,0 +1,62 @@
require File.dirname(__FILE__) + '/spec_helper'
describe ActionView::Base do
describe :alias_method_chain do
it 'defines chained methods' do
ActionView::Base.instance_methods.should be_member('render')
ActionView::Base.instance_methods.should be_member('render_with_missing_default')
ActionView::Base.instance_methods.should be_member('render_without_missing_default')
end
end
describe :render_with_missing_template do
before do
@view = ActionView::Base.new
end
describe 'with missing template' do
before do
@view.stub!(:render_without_missing_default).and_raise ActionView::MissingTemplate.new(ActionView::PathSet.new, 'foo')
Thread.current[:missing_default] = true
end
describe 'with default' do
it 'calls render_without_missing_template without default' do
@view.should_receive(:render_without_missing_default).with({:partial => 'foo'}, {})
@view.render(:partial => 'foo', :default => 'bar')
end
it 'removes missing_default' do
@view.render(:partial => 'foo', :default => 'bar')
Thread.current[:missing_default].should be_nil
end
it 'returns default' do
@view.render(:partial => 'foo', :default => 'bar').should == 'bar'
end
it 'returns default as nil' do
@view.render(:partial => 'foo', :default => nil).should be_nil
end
end
describe 'without default' do
it 'raises error and removes missing_default' do
lambda { @view.render(:partial => 'foo') }.should raise_error(ActionView::MissingTemplate)
Thread.current[:missing_default].should be_nil
end
end
after do
Thread.current[:missing_default] = nil
end
end
describe 'without missing template' do
it 'raises other errors' do
@view.stub!(:render_without_missing_default).and_raise RuntimeError.new('suck')
lambda { @view.render(:partial => 'foo', :defauilt => nil) }.should raise_error(RuntimeError, 'suck')
end
end
end
end

0 comments on commit 16f70a9

Please sign in to comment.