Skip to content

Commit

Permalink
Add the ability to call JavaScriptGenerator methods from helpers call…
Browse files Browse the repository at this point in the history
…ed in update blocks

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@3470 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information
sstephenson committed Jan 23, 2006
1 parent d921b79 commit 8436147
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 16 deletions.
14 changes: 14 additions & 0 deletions actionpack/CHANGELOG
Original file line number Original file line Diff line number Diff line change
@@ -1,5 +1,19 @@
*SVN* *SVN*


* Add the ability to call JavaScriptGenerator methods from helpers called in update blocks. [Sam Stephenson] Example:
module ApplicationHelper
def update_time
page.replace_html 'time', Time.now.to_s(:db)
page.visual_effect :highlight, 'time'
end
end

class UserController < ApplicationController
def poll
render :update { |page| page.update_time }
end
end

* Fixed that SSL would not correctly be detected when running lighttpd/fcgi behind lighttpd w/mod_proxy #3548 [stephen_purcell@yahoo.com] * Fixed that SSL would not correctly be detected when running lighttpd/fcgi behind lighttpd w/mod_proxy #3548 [stephen_purcell@yahoo.com]


* Added the possibility to specify atomatic expiration for the memcachd session container #3571 [Stefan Kaes] * Added the possibility to specify atomatic expiration for the memcachd session container #3571 [Stefan Kaes]
Expand Down
5 changes: 3 additions & 2 deletions actionpack/lib/action_controller/base.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -687,7 +687,7 @@ def render_text(text = nil, status = nil)
end end


def render_javascript(javascript, status = nil) def render_javascript(javascript, status = nil)
@response.headers['Content-type'] = 'text/javascript' @response.headers['Content-Type'] = 'text/javascript'
render_text(javascript, status) render_text(javascript, status)
end end


Expand Down Expand Up @@ -719,7 +719,8 @@ def erase_render_results
@response.body = nil @response.body = nil
@performed_render = false @performed_render = false
end end



# Clears the redirected results from the headers, resets the status to 200 and returns # Clears the redirected results from the headers, resets the status to 200 and returns
# the URL that was used to redirect or nil if there was no redirected URL # the URL that was used to redirect or nil if there was no redirected URL
# Note that +redirect_to+ will change the body of the response to indicate a redirection. # Note that +redirect_to+ will change the body of the response to indicate a redirection.
Expand Down
58 changes: 44 additions & 14 deletions actionpack/lib/action_view/helpers/prototype_helper.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -370,34 +370,52 @@ def observe_form(form_id, options = {})
# this in your Ajax response bodies, either in a <script> tag or as plain # this in your Ajax response bodies, either in a <script> tag or as plain
# JavaScript sent with a Content-type of "text/javascript". # JavaScript sent with a Content-type of "text/javascript".
# #
# Create new instances with PrototypeHelper#update_page, then call # Create new instances with PrototypeHelper#update_page or with
# #insert_html, #replace_html, #remove, #show, or #hide on the yielded # ActionController::Base#render, then call #insert_html, #replace_html,
# generator in any order you like to modify the content and appearance of # #remove, #show, #hide, #visual_effect, or any other of the built-in
# the current page. (You can also call other helper methods which # methods on the yielded generator in any order you like to modify the
# return JavaScript, such as # content and appearance of the current page.
# ActionView::Helpers::ScriptaculousHelper#visual_effect.)
# #
# Example: # Example:
# #
# update_page do |page| # update_page do |page|
# page.insert_html :bottom, 'list', '<li>Last item</li>' # page.insert_html :bottom, 'list', "<li>#{@item.name}</li>"
# page.visual_effect :highlight, 'list' # page.visual_effect :highlight, 'list'
# page.hide 'status-indicator', 'cancel-link' # page.hide 'status-indicator', 'cancel-link'
# end # end
# #
# generates the following JavaScript: # generates the following JavaScript:
# #
# new Insertion.Bottom("list", "<li>Last item</li>"); # new Insertion.Bottom("list", "<li>Some item</li>");
# new Effect.Highlight("list"); # new Effect.Highlight("list");
# ["status-indicator", "cancel-link"].each(Element.hide); # ["status-indicator", "cancel-link"].each(Element.hide);
# #
# Helper methods can be used in conjunction with JavaScriptGenerator.
# When a helper method is called inside an update block on the +page+
# object, that method will also have access to a +page+ object.
#
# Example:
#
# module ApplicationHelper
# def update_time
# page.replace_html 'time', Time.now.to_s(:db)
# page.visual_effect :highlight, 'time'
# end
# end
#
# # Controller action
# def poll
# render :update { |page| page.update_time }
# end
#
# You can also use PrototypeHelper#update_page_tag instead of # You can also use PrototypeHelper#update_page_tag instead of
# PrototypeHelper#update_page to wrap the generated JavaScript in a # PrototypeHelper#update_page to wrap the generated JavaScript in a
# <script> tag. # <script> tag.
class JavaScriptGenerator class JavaScriptGenerator
def initialize(context) #:nodoc: def initialize(context, &block) #:nodoc:
@context, @lines = context, [] @context, @lines = context, []
yield self include_helpers_from_context
@context.instance_exec(self, &block)
end end


def to_s #:nodoc: def to_s #:nodoc:
Expand Down Expand Up @@ -501,12 +519,24 @@ def delay(seconds = 1)
yield yield
record "}, #{(seconds * 1000).to_i})" record "}, #{(seconds * 1000).to_i})"
end end


# Starts a Scriptaculous visual effect. See
# ActionView::Helpers::ScriptaculousHelper for more information.
def visual_effect(name, id, options = {})
record ScriptaculousHelper::visual_effect(name, id, options)
end

private private
def method_missing(method, *arguments, &block) def include_helpers_from_context
record(@context.send(method, *arguments, &block)) @context.extended_by.each do |mod|
extend mod unless mod.name =~ /^ActionView::Helpers/
end
end end


def page
self
end

def record(line) def record(line)
returning line = "#{line.to_s.chomp.gsub /\;$/, ''};" do returning line = "#{line.to_s.chomp.gsub /\;$/, ''};" do
self << line self << line
Expand Down

0 comments on commit 8436147

Please sign in to comment.