Changeset 245

Show
Ignore:
Timestamp:
05/30/07 20:58:53 (2 years ago)
Author:
e.@brainspl.at
Message:

added the ability to do render :partial => 'shared/foo, :locals => {:foo => 'bar'} when rendering partials. Or the shorthand, partial('shared/foo', :foo => 'bar'). Added render specs, thanks to jthop for rendering patches

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/Rakefile

    r241 r245  
    154154end 
    155155 
     156STATS_DIRECTORIES = [ 
     157  %w(Code               liib/), 
     158  %w(Unit\ tests        specs), 
     159].collect { |name, dir| [ name, "./#{dir}" ] }.select { |name, dir| File.directory?(dir) } 
     160 
     161desc "Report code statistics (KLOCs, etc) from the application" 
     162task :stats do 
     163  require __DIR__ + '/tools/code_statistics' 
     164  #require 'extra/stats' 
     165  verbose = true 
     166  CodeStatistics.new(*STATS_DIRECTORIES).to_s 
     167end 
     168 
    156169############################################################################## 
    157170# SVN 
  • trunk/lib/merb.rb

    r242 r245  
    7575 
    7676module Mongrel::Const 
    77   HTTP_COOKIE   =  'HTTP_COOKIE'.freeze 
    78   QUERY_STRING  =  'QUERY_STRING'.freeze 
    79   APPLICATION_JSON = 'application/json'.freeze  
    80   TEXT_JSON        = 'text/x-json'.freeze 
     77  HTTP_COOKIE         = 'HTTP_COOKIE'.freeze 
     78  QUERY_STRING        = 'QUERY_STRING'.freeze 
     79  APPLICATION_JSON    = 'application/json'.freeze  
     80  TEXT_JSON           = 'text/x-json'.freeze 
     81  APPLICATION_XML     = 'application/xml'.freeze  
     82  TEXT_XML            = 'text/xml'.freeze   
    8183  UPCASE_CONTENT_TYPE = 'CONTENT_TYPE'.freeze 
    8284end   
  • trunk/lib/merb/merb_controller.rb

    r242 r245  
    5252          json = MerbHash.new(json) if json.is_a? Hash 
    5353          querystring.update(json) 
    54         elsif ['application/xml'].include?(@env[Mongrel::Const::UPCASE_CONTENT_TYPE]) 
    55           querystring.update(Hash.from_xml(request.read)
     54        elsif [Mongrel::Const::APPLICATION_XML, Mongrel::Const::TEXT_XML].include?(@env[Mongrel::Const::UPCASE_CONTENT_TYPE]) 
     55          querystring.update(Hash.from_xml(request.read).with_indifferent_access
    5656        else 
    5757          querystring.update(query_parse(request.read)) 
     
    181181          ok = true 
    182182        end     
    183         case filter 
    184           when Symbol, String 
    185            send(filter) if ok 
    186           when Proc 
    187            filter.call(self) if ok 
    188         end  
     183        if ok 
     184          case filter 
     185            when Symbol, String 
     186             send(filter) 
     187            when Proc 
     188             filter.call(self) 
     189          end 
     190        end    
    189191      end 
    190192      return :filter_chain_completed 
  • trunk/lib/merb/merb_mailer.rb

    r224 r245  
    4646end 
    4747=begin 
    48 m = Merb::Mailer.new :to => 'foo@bar.com', 
    49                      :from => 'bar@foo.com', 
    50                      :subject => 'Welcome to whatever!', 
    51                      :body => partial(:sometemplate) 
    52 m.deliver!                      
     48 
    5349 
    5450Merb::Mailer.config = { 
     
    5955  :auth=>:plain # :plain, :login, or :cram_md5, default :plain 
    6056} 
     57m = Merb::Mailer.new :to => 'foo@bar.com', 
     58                     :from => 'bar@foo.com', 
     59                     :subject => 'Welcome to whatever!', 
     60                     :body => partial(:sometemplate) 
     61m.deliver!                      
     62 
    6163=end 
  • trunk/lib/merb/merb_server.rb

    r241 r245  
    196196        if @@merb_opts[:console] 
    197197          initialize_merb 
     198          Object.class_eval do  
     199            def show_routes 
     200              Merb::Router.generator.paths.each {|p| puts p.inspect} 
     201              nil 
     202            end 
     203            def url(path, o={}) 
     204              Merb::Router.generator.generate(path,o) 
     205            end 
     206          end 
    198207          ARGV.clear # Avoid passing args to IRB  
    199208          require 'irb'  
  • trunk/lib/merb/merb_view_context.rb

    r236 r245  
    3333     
    3434    def initialize(controller) 
     35      @_merb_partial_locals = {} 
    3536      @controller = controller 
    3637      (@controller.instance_variables - PROTECTED_IVARS).each do |ivar| 
     
    5758     
    5859    def respond_to?(sym, include_private=false) 
    59       old_respond_to?(sym, include_private) || @controller.respond_to?(sym, include_private)  
     60      old_respond_to?(sym, include_private) || @controller.respond_to?(sym, include_private) || @_merb_partial_locals.key?(sym) 
    6061    end 
    6162     
     
    6566      if @controller.respond_to? sym 
    6667        @controller.send(sym, *args, &blk) 
     68      elsif @_merb_partial_locals.key? sym 
     69        @_merb_partial_locals[sym] 
    6770      else 
    6871        super 
  • trunk/lib/merb/mixins/render_mixin.rb

    r242 r245  
    6161        template = find_partial(partial, opts) 
    6262        opts[:layout] = :none 
     63         
     64        # Add an instance variable that can be used to create the locals in the 
     65        # partial 
     66        if opts[:locals] 
     67          @_merb_partial_locals = opts[:locals] 
     68        end 
     69        opts[:clean_context] = true 
    6370      when js = opts[:js] 
    6471        headers['Content-Type'] = "text/javascript" 
     
    7784      when template = opts[:template] 
    7885        template = find_template(:template => template) 
    79       else   
     86      else           
    8087        template = find_template(:action => action) 
    8188      end 
     
    8491      options = { 
    8592        :file     => template, 
    86         :view_context  => (opts[:clean_context] ? clean_view_context : _view_context), 
     93        :view_context  => (opts[:clean_context] ? clean_view_context : cached_view_context), 
    8794        :opts => opts 
    8895      } 
     
    98105    # the instance variables in your controller. This is used 
    99106    # as the view context object for the Erubis templates. 
    100     def _view_context 
     107    def cached_view_context 
    101108      @_view_context_cache ||= ViewContext.new(self) 
    102109    end 
     
    135142    # if you create a views/shared directory then you can call 
    136143    # partials that live there like partial('shared/foo') 
    137     def partial(template
    138       render :partial => template 
     144    def partial(template, locals={}
     145      render :partial => template, :locals => locals 
    139146    end  
    140147     
     
    152159        end       
    153160         
    154         _view_context.instance_variable_set('@_layout_content', content) 
     161        cached_view_context.instance_variable_set('@_layout_content', content) 
    155162        engine = engine_for(layout_choice) 
    156163        options = { 
    157164          :file     => layout_choice, 
    158           :view_context  => _view_context, 
     165          :view_context  => cached_view_context, 
    159166          :opts => opts 
    160167        } 
     
    165172      def find_template(opts={}) 
    166173        if template = opts[:template] 
    167           path = MERB_VIEW_ROOT / template 
     174          path = _template_root / template 
    168175        elsif action = opts[:action] 
    169           path = 
    170             File.expand_path(MERB_VIEW_ROOT / self.class.name.snake_case / action) 
     176          path = _template_root / self.class.name.snake_case / action 
    171177        elsif _layout = opts[:layout] 
    172178          path = _layout_root / _layout 
    173179        else 
    174180          raise "called find_template without an :action or :layout"   
    175         end   
     181        end         
    176182        extensions = [_template_extensions.keys].flatten.uniq 
    177183        glob = "#{path}.{#{opts[:ext] || extensions.join(',')}}" 
  • trunk/lib/merb/template/haml.rb

    r225 r245  
    2828      class << self 
    2929         
    30         class_inheritable_accessor :haml_options 
    31         self.haml_options = { :locals => {} } 
    32          
    3330        @@hamls ||= {} 
    3431        @@mtimes ||= {} 
     
    4037        def transform(options = {}) 
    4138          opts, file, view_context = options.values_at(:opts, :file, :view_context) 
    42           opts = haml_options.merge(opts) 
     39           
    4340                begin  
    44                   if precompiled = get_precompiled(file)  
    45                     opts[:precompiled] ||= precompiled  
    46                     haml = ::Haml::Engine.new("", opts)  
    47                   else  
    48                     haml = ::Haml::Engine.new(IO.read(file), opts)  
    49                     set_precompiled(file, haml.precompiled)  
    50                   end  
    51                  
     41                  locals_code = build_locals(opts[:locals]) 
     42                  opts[:locals] = {} 
     43                   
     44                  template = load_template(file) 
     45                   
     46                  haml = ::Haml::Engine.new("#{locals_code}#{template}", opts)  
    5247                  haml.to_html(view_context)  
    5348                rescue  
     
    6055       
    6156        private 
     57          def load_template(file) 
     58            template = "" 
     59            if @@hamls[file] && !cache_template?(file) 
     60              template = @@hamls[file] 
     61            else   
     62              template = IO.read(file) 
     63              if cache_template?(file) 
     64                @@hamls[file], @@mtimes[file] = template, Time.now 
     65              end 
     66              return template 
     67            end 
     68          end 
     69           
     70          def build_locals(locals) 
     71            locals_code = "" 
     72                  if locals 
     73                    locals_code = locals.keys.inject("") do |code, key|  
     74                code << "- #{key} = @_merb_partial_locals[:#{key}]\n" 
     75              end 
     76            end 
     77          end 
    6278         
    63         def get_precompiled(path, opts={}) 
    64           if @@hamls[path] && !cache_template?(path) 
    65             return @@hamls[path] 
    66           end    
    67         end 
    68          
    69        def set_precompiled(path, precompiled) 
    70          @@hamls[path], @@mtimes[path] = precompiled, Time.now  
    71        end 
    72          
    73         def cache_template?(path) 
    74           return false unless ::Merb::Server.config[:cache_templates] 
    75           return true unless @@hamls[path] 
    76           @@mtimes[path] < File.mtime(path) || 
    77             (File.symlink?(path) && (@@mtimes[path] < File.lstat(path).mtime)) 
    78         end 
     79          def cache_template?(path) 
     80            return false unless ::Merb::Server.config[:cache_templates] 
     81            return true unless @@hamls[path] 
     82            @@mtimes[path] < File.mtime(path) || 
     83              (File.symlink?(path) && (@@mtimes[path] < File.lstat(path).mtime)) 
     84          end 
    7985         
    8086      end 
  • trunk/lib/merb/template/markaby.rb

    r195 r245  
    3434        def transform(options = {}) 
    3535          opts, file, view_context = options.values_at(:opts, :file, :view_context) 
     36           
     37          if opts[:locals] 
     38            locals = opts[:locals].keys.inject("") do |code, key|  
     39              code << "#{key} = @_merb_partial_locals[:#{key}]\n" 
     40            end 
     41          end 
     42           
    3643          mab = ::Markaby::Builder.new({}, view_context) { 
    37             instance_eval(File.read(file)
     44            instance_eval("#{locals}#{File.read(file)}"
    3845          } 
    3946          mab.to_s