Skip to content

Commit

Permalink
fix for r385, inspired by Brian's implementation
Browse files Browse the repository at this point in the history
git-svn-id: http://stonecode.svnrepository.com/svn/ruport/ruport/trunk@1292 bb2e8eb0-7117-0410-aac4-c024b40ed5f7
  • Loading branch information
sandal committed Mar 31, 2008
1 parent 96fdb86 commit bc1b6e8
Show file tree
Hide file tree
Showing 3 changed files with 129 additions and 0 deletions.
43 changes: 43 additions & 0 deletions examples/anon.rb
@@ -0,0 +1,43 @@
# Demonstrates building a parent controller which provides additional 'built in'
# formats, allowing anonymous formatter support to use the simple interface
# rather than the :format => FormatterClass approach.

require "ruport"
module FooCorp
class Controller < Ruport::Controller
def self.built_in_formats
super.merge(:xml => FooCorp::Formatter::XML)
end
end

class Formatter
class XML < Ruport::Formatter

def xmlify(stuff)
output << "Wouldn't you like to see #{stuff} in XML?"
end
end
end

class MyController < FooCorp::Controller
stage :foo

formatter :xml do
build :foo do
xmlify "Red Snapper"
end
end

formatter :text do
build :foo do
output << "Red Snapper"
end
end
end
end

puts "XML:"
puts FooCorp::MyController.render_xml

puts "Text:"
puts FooCorp::MyController.render_text
21 changes: 21 additions & 0 deletions lib/ruport/controller.rb
Expand Up @@ -184,9 +184,30 @@ def save_as(file,options={})
as(format.to_sym, options.merge(:file => file))
end
end



class << self

def built_in_formats
{ :html => Ruport::Formatter::HTML,
:csv => Ruport::Formatter::CSV,
:pdf => Ruport::Formatter::PDF,
:text => Ruport::Formatter::Text }
end

def formatter(*a,&b)
case a[0]
when Symbol
klass = Class.new(built_in_formats[a[0]])
klass.renders a[0], :for => self
when Hash
k,v = a[0].to_a[0]
klass = Class.new(v)
klass.renders k, :for => self
end
klass.class_eval(&b)
end

attr_accessor :first_stage,:final_stage,:required_options,:stages #:nodoc:

Expand Down
65 changes: 65 additions & 0 deletions test/renderer_test.rb → test/controller_test.rb
Expand Up @@ -585,6 +585,71 @@ def test_render_block_should_be_called_before_setup

end

class CustomFormatter < Ruport::Formatter
def custom_helper
output << "Custom!"
end
end

class ControllerWithAnonymousFormatters < Ruport::Controller

stage :report

formatter :html do
build :report do
output << textile("h1. Hi there")
end
end

formatter :csv do
build :report do
build_row([1,2,3])
end
end

formatter :pdf do
build :report do
add_text "hello world"
end
end

formatter :text do
build :report do
output << "Hello world"
end
end

formatter :custom => CustomFormatter do

build :report do
output << "This is "
custom_helper
end

end

end

class TestAnonymousFormatter < Test::Unit::TestCase
context "When using built in Ruport formatters" do

def specify_text_formatter_shortcut_is_accessible
assert_equal "Hello world", ControllerWithAnonymousFormatters.render_text
assert_equal "1,2,3\n", ControllerWithAnonymousFormatters.render_csv
assert_equal "<h1>Hi there</h1>", ControllerWithAnonymousFormatters.render_html
assert_not_nil ControllerWithAnonymousFormatters.render_pdf
end

end

context "When using custom formatters" do
def specify_custom_formatter_shortcut_is_accessible
assert_equal "This is Custom!", ControllerWithAnonymousFormatters.render_custom
end
end

end

class TestControllerHooks < Test::Unit::TestCase

context "when renderable_data omitted" do
Expand Down

0 comments on commit bc1b6e8

Please sign in to comment.