Changeset 1292

Show
Ignore:
Timestamp:
03/31/08 14:13:50 (8 months ago)
Author:
sandal
Message:

fix for r385, inspired by Brian's implementation

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • ruport/trunk/lib/ruport/controller.rb

    r1291 r1292  
    185185    end 
    186186  end 
     187 
    187188   
    188189   
    189190  class << self 
     191 
     192    def built_in_formats 
     193     { :html => Ruport::Formatter::HTML, 
     194       :csv  => Ruport::Formatter::CSV, 
     195       :pdf  => Ruport::Formatter::PDF, 
     196       :text => Ruport::Formatter::Text } 
     197    end 
     198 
     199    def formatter(*a,&b) 
     200      case a[0] 
     201      when Symbol 
     202        klass = Class.new(built_in_formats[a[0]]) 
     203        klass.renders a[0], :for => self 
     204      when Hash 
     205        k,v = a[0].to_a[0] 
     206        klass = Class.new(v) 
     207        klass.renders k, :for => self 
     208      end 
     209      klass.class_eval(&b) 
     210    end 
    190211     
    191212    attr_accessor :first_stage,:final_stage,:required_options,:stages #:nodoc:  
  • ruport/trunk/test/controller_test.rb

    r1291 r1292  
    586586end 
    587587 
     588class CustomFormatter < Ruport::Formatter 
     589  def custom_helper 
     590    output << "Custom!" 
     591  end 
     592end 
     593 
     594class ControllerWithAnonymousFormatters < Ruport::Controller 
     595 
     596  stage :report 
     597 
     598  formatter :html do 
     599    build :report do 
     600      output << textile("h1. Hi there") 
     601    end 
     602  end 
     603 
     604  formatter :csv do 
     605    build :report do 
     606      build_row([1,2,3]) 
     607    end 
     608  end 
     609 
     610  formatter :pdf do 
     611    build :report do  
     612      add_text "hello world" 
     613    end 
     614  end 
     615 
     616  formatter :text do 
     617    build :report do 
     618      output << "Hello world" 
     619    end 
     620  end 
     621 
     622  formatter :custom => CustomFormatter do 
     623 
     624    build :report do 
     625      output << "This is " 
     626      custom_helper 
     627    end 
     628 
     629  end 
     630 
     631end 
     632 
     633class TestAnonymousFormatter < Test::Unit::TestCase 
     634  context "When using built in Ruport formatters" do 
     635 
     636    def specify_text_formatter_shortcut_is_accessible 
     637      assert_equal "Hello world", ControllerWithAnonymousFormatters.render_text 
     638      assert_equal "1,2,3\n", ControllerWithAnonymousFormatters.render_csv 
     639      assert_equal "<h1>Hi there</h1>", ControllerWithAnonymousFormatters.render_html 
     640      assert_not_nil ControllerWithAnonymousFormatters.render_pdf 
     641    end 
     642     
     643  end 
     644 
     645  context "When using custom formatters" do 
     646    def specify_custom_formatter_shortcut_is_accessible 
     647      assert_equal "This is Custom!", ControllerWithAnonymousFormatters.render_custom 
     648    end 
     649  end 
     650 
     651end 
     652 
    588653class TestControllerHooks < Test::Unit::TestCase 
    589654