From 06dd1f2ca3398b978bb286688aa68acacd7dd712 Mon Sep 17 00:00:00 2001 From: Sam Stephenson Date: Fri, 20 Jan 2006 22:04:07 +0000 Subject: Add render :update for inline RJS git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@3441 5ecf4fe2-1ee6-0310-87b1-e25e094e27de --- actionpack/lib/action_controller/base.rb | 37 +++++++++++++++++++----- actionpack/lib/action_controller/benchmarking.rb | 6 ++-- actionpack/lib/action_controller/layout.rb | 8 ++--- 3 files changed, 37 insertions(+), 14 deletions(-) (limited to 'actionpack/lib/action_controller') diff --git a/actionpack/lib/action_controller/base.rb b/actionpack/lib/action_controller/base.rb index a5b74d6a19..40fa24a696 100755 --- a/actionpack/lib/action_controller/base.rb +++ b/actionpack/lib/action_controller/base.rb @@ -368,9 +368,9 @@ module ActionController #:nodoc: # Returns a URL that has been rewritten according to the options hash and the defined Routes. # (For doing a complete redirect, use redirect_to). - #   + #   # url_for is used to: - #   + #   # All keys given to url_for are forwarded to the Route module, save for the following: # * :anchor -- specifies the anchor name to be appended to the path. For example, # url_for :controller => 'posts', :action => 'show', :id => 10, :anchor => 'comments' @@ -386,7 +386,7 @@ module ActionController #:nodoc: # # The default Routes setup supports a typical Rails path of "controller/action/id" where action and id are optional, with # action defaulting to 'index' when not given. Here are some typical url_for statements and their corresponding URLs: - #   + #   # url_for :controller => 'posts', :action => 'recent' # => 'proto://host.com/posts/recent' # url_for :controller => 'posts', :action => 'index' # => 'proto://host.com/posts' # url_for :controller => 'posts', :action => 'show', :id => 10 # => 'proto://host.com/posts/show/10' @@ -395,7 +395,7 @@ module ActionController #:nodoc: # url_for :action => 'some_action' will retain the current controller, as expected. This behavior extends to # other parameters, including :controller, :id, and any other parameters that are placed into a Route's # path. - #   + #   # The URL helpers such as url_for have a limited form of memory: when generating a new URL, they can look for # missing values in the current request's parameters. Routes attempts to guess when a value should and should not be # taken from the defaults. There are a few simple rules on how this is performed: @@ -418,7 +418,7 @@ module ActionController #:nodoc: # answer has to do with the order in which the parameters appear in the generated path. In a nutshell, since the # value that appears in the slot for :first is not equal to default value for :first we stop using # defaults. On it's own, this rule can account for much of the typical Rails URL behavior. - #   + #   # Although a convienence, defaults can occasionaly get in your way. In some cases a default persists longer than desired. # The default may be cleared by adding :name => nil to url_for's options. # This is often required when writing form helpers, since the defaults in play may vary greatly depending upon where the @@ -571,6 +571,16 @@ module ActionController #:nodoc: # # _Deprecation_ _notice_: This used to have the signature render_template(template, status = 200, type = :rhtml) # + # === Rendering inline JavaScriptGenerator page updates + # + # In addition to rendering JavaScriptGenerator page updates with Ajax in RJS templates (see ActionView::Base for details), + # you can also pass the :update parameter to +render+, along with a block, to render page updates inline. + # + # render :update do |page| + # page.replace_html 'user_list', :partial => 'user', :collection => @users + # page.visual_effect :highlight, 'user_list' + # end + # # === Rendering nothing # # Rendering nothing is often convenient in combination with Ajax calls that perform their effect client-side or @@ -581,12 +591,16 @@ module ActionController #:nodoc: # # # Renders an empty response with status code 401 (access denied) # render :nothing => true, :status => 401 - def render(options = nil, deprecated_status = nil) #:doc: + def render(options = nil, deprecated_status = nil, &block) #:doc: raise DoubleRenderError, "Can only render or redirect once per action" if performed? # Backwards compatibility unless options.is_a?(Hash) - return render_file(options || default_template_name, deprecated_status, true) + if options == :update + options = {:update => true} + else + return render_file(options || default_template_name, deprecated_status, true) + end end if text = options[:text] @@ -613,6 +627,10 @@ module ActionController #:nodoc: render_partial(partial, ActionView::Base::ObjectWrapper.new(options[:object]), options[:locals], options[:status]) end + elsif options[:update] + generator = ActionView::Helpers::PrototypeHelper::JavaScriptGenerator.new(@template, &block) + render_javascript(generator.to_s) + elsif options[:nothing] # Safari doesn't pass the headers of the return if the response is zero length render_text(" ", options[:status]) @@ -661,6 +679,11 @@ module ActionController #:nodoc: @response.body = text end + def render_javascript(javascript, status = nil) + @response.headers['Content-type'] = 'text/javascript' + render_text(javascript, status) + end + def render_nothing(status = nil) render_text(' ', status) end diff --git a/actionpack/lib/action_controller/benchmarking.rb b/actionpack/lib/action_controller/benchmarking.rb index ceed88f2bf..eff4d97fae 100644 --- a/actionpack/lib/action_controller/benchmarking.rb +++ b/actionpack/lib/action_controller/benchmarking.rb @@ -43,14 +43,14 @@ module ActionController #:nodoc: end end - def render_with_benchmark(options = nil, deprecated_status = nil) + def render_with_benchmark(options = nil, deprecated_status = nil, &block) unless logger - render_without_benchmark(options, deprecated_status) + render_without_benchmark(options, deprecated_status, &block) else db_runtime = ActiveRecord::Base.connection.reset_runtime if Object.const_defined?("ActiveRecord") && ActiveRecord::Base.connected? render_output = nil - @rendering_runtime = Benchmark::measure{ render_output = render_without_benchmark(options, deprecated_status) }.real + @rendering_runtime = Benchmark::measure{ render_output = render_without_benchmark(options, deprecated_status, &block) }.real if Object.const_defined?("ActiveRecord") && ActiveRecord::Base.connected? @db_rt_before_render = db_runtime diff --git a/actionpack/lib/action_controller/layout.rb b/actionpack/lib/action_controller/layout.rb index baa035507d..94fa4cc4ba 100644 --- a/actionpack/lib/action_controller/layout.rb +++ b/actionpack/lib/action_controller/layout.rb @@ -211,7 +211,7 @@ module ActionController #:nodoc: active_layout.include?('/') && !nested_controller ? active_layout : "layouts/#{active_layout}" if active_layout end - def render_with_a_layout(options = nil, deprecated_status = nil, deprecated_layout = nil) #:nodoc: + def render_with_a_layout(options = nil, deprecated_status = nil, deprecated_layout = nil, &block) #:nodoc: template_with_options = options.is_a?(Hash) if apply_layout?(template_with_options, options) && (layout = pick_layout(template_with_options, options, deprecated_layout)) @@ -219,10 +219,10 @@ module ActionController #:nodoc: logger.info("Rendering #{options} within #{layout}") if logger if template_with_options - content_for_layout = render_with_no_layout(options) + content_for_layout = render_with_no_layout(options, &block) deprecated_status = options[:status] || deprecated_status else - content_for_layout = render_with_no_layout(options, deprecated_status) + content_for_layout = render_with_no_layout(options, deprecated_status, &block) end erase_render_results @@ -230,7 +230,7 @@ module ActionController #:nodoc: @template.instance_variable_set("@content_for_layout", content_for_layout) render_text(@template.render_file(layout, true), deprecated_status) else - render_with_no_layout(options, deprecated_status) + render_with_no_layout(options, deprecated_status, &block) end end -- cgit v1.2.3