Skip to content

Instantly share code, notes, and snippets.

@croaky
Created November 17, 2008 22:02
Show Gist options
  • Save croaky/25926 to your computer and use it in GitHub Desktop.
Save croaky/25926 to your computer and use it in GitHub Desktop.
# test
require File.dirname(__FILE__) + '/../test_helper'
class XMLRendererTest < ActiveSupport::TestCase
context "A class with XMLRenderer mixed in" do
setup do
class ::DummyClass
include XMLRenderer
end
@dummy = DummyClass.new
end
should "respond to to_show_xml" do
assert_respond_to @dummy, :to_show_xml
end
should "respond to views_show_xml" do
assert_respond_to @dummy, :views_show_xml
end
should "respond to default_url_options on the class" do
assert_respond_to DummyClass, :default_url_options
end
should "contain the host from APP_URL_PREFIX in the default url options" do
assert_equal URI.parse(APP_URL_PREFIX).host, DummyClass.default_url_options[:host]
end
should "have ERB::Util as an ancestor" do
assert DummyClass.ancestors.include?( ERB::Util )
end
should "have ActionController::UrlWriter as an ancestor" do
assert DummyClass.ancestors.include?( ActionController::UrlWriter )
end
should "have ActionView::Helpers::UrlHelper as an ancestor" do
assert DummyClass.ancestors.include?( ActionView::Helpers::UrlHelper )
end
should "return the expected file path when sent #views_show_xml" do
assert_equal File.expand_path(File.join(RAILS_ROOT, "app", "views", "dummy_classes", "show.xml.builder")),
@dummy.views_show_xml
end
should "load the right file and eval it when sent #to_show_xml" do
xml_mock = mock
xml_mock.expects(:test_method)
@dummy.expects(:views_show_xml).returns("views_show_xml")
IO.expects(:read).with("views_show_xml").returns("xml.test_method")
Builder::XmlMarkup.expects(:new).with(:target => "").returns(xml_mock)
@dummy.to_show_xml
end
end
end
# lib
module XMLRenderer
include ERB::Util
include ActionView::Helpers::UrlHelper
include ActionController::UrlWriter
def self.included(base)
base.extend(ClassMethods)
end
module ClassMethods
def default_url_options
{ :host => URI.parse(APP_URL_PREFIX).host }
end
end
def to_show_xml
output = ""
xml = Builder::XmlMarkup.new(:target => output)
@page = self
eval(IO.read( views_show_xml ))
output
end
def views_show_xml
File.expand_path( File.join( RAILS_ROOT,
"app",
"views",
self.class.name.underscore.pluralize,
"show.xml.builder" ))
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment