Changeset 104

Show
Ignore:
Timestamp:
02/04/2007 12:07:32 (22 months ago)
Author:
tec
Message:
  • lib/markaby/rails.rb: give helpers access to assigns (ticket:51); new namespace
  • lib/markaby/template.rb: allow different builder classes
  • test/rails_test.rb: test that helpers have access to assigns
  • test/rails/test_helper.rb: a helper to test with
  • test/rails/test_preamble.rb: make sure the helpers can be loaded
  • test/rails/markaby/index.mab: use the helper
  • init.rb: new namespace
Location:
trunk
Files:
1 added
6 modified

Legend:

Unmodified
Added
Removed
  • trunk/init.rb

    r62 r104  
    44require 'markaby/rails' 
    55 
    6 ActionView::Base::register_template_handler 'mab', Markaby::ActionViewTemplateHandler 
     6ActionView::Base::register_template_handler 'mab', Markaby::Rails::ActionViewTemplateHandler 
    77 
    8 ActionController::Base.send :include, Markaby::ActionControllerHelpers 
     8ActionController::Base.send :include, Markaby::Rails::ActionControllerHelpers 
  • trunk/lib/markaby/rails.rb

    r96 r104  
    1313 
    1414module Markaby 
    15  
    16   # Markaby helpers for Rails. 
    17   module ActionControllerHelpers 
    18     # Returns a string of HTML built from the attached +block+.  Any +options+ are 
    19     # passed into the render method. 
    20     # 
    21     # Use this method in your controllers to output Markaby directly from inside. 
    22     def render_markaby(options = {}, &block) 
    23       render options.merge({ :text => Builder.new(options[:locals], self, &block).to_s }) 
    24     end 
    25   end 
    26  
    27   class ActionViewTemplateHandler # :nodoc: 
    28     def initialize(action_view) 
    29       @action_view = action_view 
    30     end 
    31     def render(template, local_assigns, file_path) 
    32       template = Template.new(template) 
    33       template.path = file_path 
    34       template.render(@action_view.assigns.merge(local_assigns), @action_view) 
    35     end 
    36   end 
    37  
    38   class FauxErbout < ::Builder::BlankSlate # :nodoc: 
    39     def initialize(builder) 
    40       @builder = builder 
    41     end 
    42     def nil? # see ActionView::Helpers::CaptureHelper#capture 
    43       true 
    44     end 
    45     def method_missing(*args, &block) 
    46       @builder.send *args, &block 
    47     end 
    48   end 
    49    
    50   class Builder # :nodoc: 
    51     def flash(*args) 
    52       @helpers.controller.send(:flash, *args) 
    53     end 
    54      
    55     # Emulate ERB to satisfy helpers like <tt>form_for</tt>. 
    56     def _erbout 
    57       @_erbout ||= FauxErbout.new(self) 
     15  module Rails 
     16    # Markaby helpers for Rails. 
     17    module ActionControllerHelpers 
     18      # Returns a string of HTML built from the attached +block+.  Any +options+ are 
     19      # passed into the render method. 
     20      # 
     21      # Use this method in your controllers to output Markaby directly from inside. 
     22      def render_markaby(options = {}, &block) 
     23        render options.merge({ :text => Builder.new(options[:locals], self, &block).to_s }) 
     24      end 
    5825    end 
    5926 
    60     # Content_for will store the given block in an instance variable for later use  
    61     # in another template or in the layout. 
    62     # 
    63     # The name of the instance variable is content_for_<name> to stay consistent  
    64     # with @content_for_layout which is used by ActionView's layouts. 
    65     # 
    66     # Example: 
    67     # 
    68     #   content_for("header") do 
    69     #     h1 "Half Shark and Half Lion" 
    70     #   end 
    71     # 
    72     # If used several times, the variable will contain all the parts concatenated. 
    73     def content_for(name, &block) 
    74       @helpers.assigns["content_for_#{name}"] = 
    75         eval("@content_for_#{name} = (@content_for_#{name} || '') + capture(&block)") 
     27    class ActionViewTemplateHandler # :nodoc: 
     28      def initialize(action_view) 
     29        @action_view = action_view 
     30      end 
     31      def render(template, local_assigns, file_path) 
     32        template = Template.new(template) 
     33        template.path = file_path 
     34        template.render(@action_view.assigns.merge(local_assigns), @action_view) 
     35      end 
    7636    end 
     37       
     38    class Builder < Markaby::Builder # :nodoc: 
     39      def initialize(*args, &block) 
     40        super *args, &block 
     41         
     42        @assigns.each { |k, v| @helpers.instance_variable_set("@#{k}", v) } 
     43      end 
     44       
     45      def flash(*args) 
     46        @helpers.controller.send(:flash, *args) 
     47      end 
     48     
     49      # Emulate ERB to satisfy helpers like <tt>form_for</tt>. 
     50      def _erbout 
     51        @_erbout ||= FauxErbout.new(self) 
     52      end 
     53 
     54      # Content_for will store the given block in an instance variable for later use  
     55      # in another template or in the layout. 
     56      # 
     57      # The name of the instance variable is content_for_<name> to stay consistent  
     58      # with @content_for_layout which is used by ActionView's layouts. 
     59      # 
     60      # Example: 
     61      # 
     62      #   content_for("header") do 
     63      #     h1 "Half Shark and Half Lion" 
     64      #   end 
     65      # 
     66      # If used several times, the variable will contain all the parts concatenated. 
     67      def content_for(name, &block) 
     68        @helpers.assigns["content_for_#{name}"] = 
     69          eval("@content_for_#{name} = (@content_for_#{name} || '') + capture(&block)") 
     70      end 
     71    end 
     72 
     73 
     74     
     75    Template.builder_class = Builder 
     76     
     77    class FauxErbout < ::Builder::BlankSlate # :nodoc: 
     78      def initialize(builder) 
     79        @builder = builder 
     80      end 
     81      def nil? # see ActionView::Helpers::CaptureHelper#capture 
     82        true 
     83      end 
     84      def method_missing(*args, &block) 
     85        @builder.send *args, &block 
     86      end 
     87    end 
     88 
    7789  end 
    78  
    7990end 
  • trunk/lib/markaby/template.rb

    r96 r104  
    22  class Template 
    33 
     4    def self.builder_class=(builder) 
     5      @@builder_class = builder 
     6    end 
     7       
     8    def self.builder_class 
     9      @@builder_class ||= Builder 
     10    end 
     11     
    412    attr_accessor :source, :path 
    513     
     
    917 
    1018    def render(*args) 
    11       output = Builder.new(*args) 
     19      output = self.class.builder_class.new(*args) 
    1220 
    1321      if path.nil? 
  • trunk/test/rails/markaby/index.mab

    r100 r104  
    11ul { 
     2  check_ivar_exists :monkeys 
     3   
    24  @monkeys.each { |monkey| 
    35    li monkey.name 
  • trunk/test/rails/test_preamble.rb

    r92 r104  
    1515require 'action_controller/test_process' 
    1616 
     17Dependencies.load_paths.unshift File.dirname(__FILE__) 
     18 
    1719$:.unshift MARKABY_ROOT 
    1820 
  • trunk/test/rails_test.rb

    r100 r104  
    22 
    33class MarkabyController < ActionController::Base 
     4 
     5  helper :test 
     6   
    47  @@locals = { :monkeys => Monkey.find(:all) } 
    58 
     
    3235    render :inline => mab { ul { Monkey.find(:all).each { |m| li m.name } } } 
    3336  end 
     37 
    3438end 
    3539 
     
    9296    end 
    9397  end 
     98 
    9499end