From 628c31b353706877322e7613702d1bd4a59e3514 Mon Sep 17 00:00:00 2001 From: Joshua Peek Date: Tue, 22 Mar 2011 15:43:40 -0500 Subject: Add sprockets md5s to asset tags --- .../helpers/asset_tag_helpers/javascript_tag_helpers.rb | 10 ++++++++++ .../helpers/asset_tag_helpers/stylesheet_tag_helpers.rb | 10 ++++++++++ 2 files changed, 20 insertions(+) (limited to 'actionpack/lib') diff --git a/actionpack/lib/action_view/helpers/asset_tag_helpers/javascript_tag_helpers.rb b/actionpack/lib/action_view/helpers/asset_tag_helpers/javascript_tag_helpers.rb index 82bbfcc7d2..2187887b49 100644 --- a/actionpack/lib/action_view/helpers/asset_tag_helpers/javascript_tag_helpers.rb +++ b/actionpack/lib/action_view/helpers/asset_tag_helpers/javascript_tag_helpers.rb @@ -173,6 +173,16 @@ module ActionView # # javascript_include_tag :all, :cache => true, :recursive => true def javascript_include_tag(*sources) + if config.perform_caching + sources = sources.map do |source| + if source =~ /^\/assets\/(.+)/ + "/assets/#{Rails.application.assets.url($1)}" + else + source + end + end + end + @javascript_include ||= JavascriptIncludeTag.new(config, asset_paths) @javascript_include.include_tag(*sources) end diff --git a/actionpack/lib/action_view/helpers/asset_tag_helpers/stylesheet_tag_helpers.rb b/actionpack/lib/action_view/helpers/asset_tag_helpers/stylesheet_tag_helpers.rb index a48c87b49a..b9f49f37b4 100644 --- a/actionpack/lib/action_view/helpers/asset_tag_helpers/stylesheet_tag_helpers.rb +++ b/actionpack/lib/action_view/helpers/asset_tag_helpers/stylesheet_tag_helpers.rb @@ -136,6 +136,16 @@ module ActionView # stylesheet_link_tag :all, :concat => true # def stylesheet_link_tag(*sources) + if config.perform_caching + sources = sources.map do |source| + if source =~ /^\/assets\/(.+)/ + "/assets/#{Rails.application.assets.url($1)}" + else + source + end + end + end + @stylesheet_include ||= StylesheetIncludeTag.new(config, asset_paths) @stylesheet_include.include_tag(*sources) end -- cgit v1.2.3 From 41cc6430650d0e90e892e0bd9a29ddcefd8a9c64 Mon Sep 17 00:00:00 2001 From: Joshua Peek Date: Mon, 28 Mar 2011 13:54:58 -0500 Subject: Remove sprockets exception from main asset helpers --- .../helpers/asset_tag_helpers/javascript_tag_helpers.rb | 10 ---------- .../helpers/asset_tag_helpers/stylesheet_tag_helpers.rb | 10 ---------- 2 files changed, 20 deletions(-) (limited to 'actionpack/lib') diff --git a/actionpack/lib/action_view/helpers/asset_tag_helpers/javascript_tag_helpers.rb b/actionpack/lib/action_view/helpers/asset_tag_helpers/javascript_tag_helpers.rb index 2187887b49..82bbfcc7d2 100644 --- a/actionpack/lib/action_view/helpers/asset_tag_helpers/javascript_tag_helpers.rb +++ b/actionpack/lib/action_view/helpers/asset_tag_helpers/javascript_tag_helpers.rb @@ -173,16 +173,6 @@ module ActionView # # javascript_include_tag :all, :cache => true, :recursive => true def javascript_include_tag(*sources) - if config.perform_caching - sources = sources.map do |source| - if source =~ /^\/assets\/(.+)/ - "/assets/#{Rails.application.assets.url($1)}" - else - source - end - end - end - @javascript_include ||= JavascriptIncludeTag.new(config, asset_paths) @javascript_include.include_tag(*sources) end diff --git a/actionpack/lib/action_view/helpers/asset_tag_helpers/stylesheet_tag_helpers.rb b/actionpack/lib/action_view/helpers/asset_tag_helpers/stylesheet_tag_helpers.rb index b9f49f37b4..a48c87b49a 100644 --- a/actionpack/lib/action_view/helpers/asset_tag_helpers/stylesheet_tag_helpers.rb +++ b/actionpack/lib/action_view/helpers/asset_tag_helpers/stylesheet_tag_helpers.rb @@ -136,16 +136,6 @@ module ActionView # stylesheet_link_tag :all, :concat => true # def stylesheet_link_tag(*sources) - if config.perform_caching - sources = sources.map do |source| - if source =~ /^\/assets\/(.+)/ - "/assets/#{Rails.application.assets.url($1)}" - else - source - end - end - end - @stylesheet_include ||= StylesheetIncludeTag.new(config, asset_paths) @stylesheet_include.include_tag(*sources) end -- cgit v1.2.3 From 9cb264555dbc630ff5e44d96b0d70061856f568c Mon Sep 17 00:00:00 2001 From: Joshua Peek Date: Mon, 28 Mar 2011 15:19:34 -0500 Subject: Add SprocketsHelper --- actionpack/lib/action_view/helpers.rb | 2 + .../lib/action_view/helpers/sprockets_helper.rb | 77 ++++++++++++++++++++++ 2 files changed, 79 insertions(+) create mode 100644 actionpack/lib/action_view/helpers/sprockets_helper.rb (limited to 'actionpack/lib') diff --git a/actionpack/lib/action_view/helpers.rb b/actionpack/lib/action_view/helpers.rb index d338ce616a..cb1ab64121 100644 --- a/actionpack/lib/action_view/helpers.rb +++ b/actionpack/lib/action_view/helpers.rb @@ -22,6 +22,7 @@ module ActionView #:nodoc: autoload :RecordTagHelper autoload :SanitizeHelper autoload :ScriptaculousHelper + autoload :SprocketsHelper autoload :TagHelper autoload :TextHelper autoload :TranslationHelper @@ -52,6 +53,7 @@ module ActionView #:nodoc: include RecordTagHelper include SanitizeHelper include ScriptaculousHelper + include SprocketsHelper include TagHelper include TextHelper include TranslationHelper diff --git a/actionpack/lib/action_view/helpers/sprockets_helper.rb b/actionpack/lib/action_view/helpers/sprockets_helper.rb new file mode 100644 index 0000000000..4abc09e9a5 --- /dev/null +++ b/actionpack/lib/action_view/helpers/sprockets_helper.rb @@ -0,0 +1,77 @@ +require 'uri' + +module ActionView + module Helpers + module SprocketsHelper + def sprockets_javascript_path(source) + compute_sprockets_path source, 'javascripts', 'js' + end + + def sprockets_javascript_include_tag(source, options = {}) + options = { + 'type' => "application/javascript", + 'src' => sprockets_javascript_path(source) + }.merge(options.stringify_keys) + + content_tag 'script', "", options + end + + def sprockets_stylesheet_path(source) + compute_sprockets_path source, 'stylesheets', 'css' + end + + def sprockets_stylesheet_link_tag(source, options = {}) + options = { + 'rel' => "stylesheet", + 'type' => "text/css", + 'media' => "screen", + 'href' => sprockets_stylesheet_path(source) + }.merge(options.stringify_keys) + + tag 'link', options + end + + private + def compute_sprockets_path(source, dir, default_ext) + return source if URI.parse(source).host + + # Add /javscripts to relative paths + if source[0] != ?/ + source = "/#{dir}/#{source}" + end + + # Add default extension if there isn't one + if default_ext && File.extname(source).empty? + source = "#{source}.#{default_ext}" + end + + # Fingerprint url + source = Rails.application.assets.url(source) + + host = compute_asset_host(source) + + if controller.respond_to?(:request) && host && URI.parse(host).host.nil? + host = "#{controller.request.protocol}#{host}" + end + + "#{host}#{source}" + end + + def compute_asset_host(source) + if host = config.asset_host + if host.is_a?(Proc) || host.respond_to?(:call) + case host.is_a?(Proc) ? host.arity : host.method(:call).arity + when 2 + request = controller.respond_to?(:request) && controller.request + host.call(source, request) + else + host.call(source) + end + else + (host =~ /%d/) ? host % (source.hash % 4) : host + end + end + end + end + end +end -- cgit v1.2.3 From 3b4e1a91590b27f6f874927dc51f6f1755ae9f19 Mon Sep 17 00:00:00 2001 From: Joshua Peek Date: Mon, 28 Mar 2011 15:58:29 -0500 Subject: Update sprockets path generation method --- actionpack/lib/action_view/helpers/sprockets_helper.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'actionpack/lib') diff --git a/actionpack/lib/action_view/helpers/sprockets_helper.rb b/actionpack/lib/action_view/helpers/sprockets_helper.rb index 4abc09e9a5..bfc396fad5 100644 --- a/actionpack/lib/action_view/helpers/sprockets_helper.rb +++ b/actionpack/lib/action_view/helpers/sprockets_helper.rb @@ -46,7 +46,7 @@ module ActionView end # Fingerprint url - source = Rails.application.assets.url(source) + source = Rails.application.assets.path(source) host = compute_asset_host(source) -- cgit v1.2.3 From cc446d6c9f10a6258da958ecc8948ef478d9407c Mon Sep 17 00:00:00 2001 From: Gabriel Horner Date: Mon, 28 Mar 2011 23:48:17 -0400 Subject: Add docs for ActionController::Metal class methods --- actionpack/lib/action_controller/metal.rb | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) (limited to 'actionpack/lib') diff --git a/actionpack/lib/action_controller/metal.rb b/actionpack/lib/action_controller/metal.rb index e5db31061b..585bd5e5ab 100644 --- a/actionpack/lib/action_controller/metal.rb +++ b/actionpack/lib/action_controller/metal.rb @@ -201,19 +201,23 @@ module ActionController class_attribute :middleware_stack self.middleware_stack = ActionController::MiddlewareStack.new - def self.inherited(base) + def self.inherited(base) #nodoc: base.middleware_stack = self.middleware_stack.dup super end + # Adds given middleware class and its args to bottom of middleware_stack def self.use(*args, &block) middleware_stack.use(*args, &block) end + # Alias for middleware_stack def self.middleware middleware_stack end + # Makes the controller a rack endpoint that points to the action in + # the given env's action_dispatch.request.path_parameters key. def self.call(env) action(env['action_dispatch.request.path_parameters'][:action]).call(env) end -- cgit v1.2.3 From 07054fe369b0d30562642f15140f7c863dfc4328 Mon Sep 17 00:00:00 2001 From: John Firebaugh Date: Mon, 28 Mar 2011 22:17:20 -0700 Subject: Fix grammar, formatting, and cross references --- actionpack/lib/action_dispatch/testing/integration.rb | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) (limited to 'actionpack/lib') diff --git a/actionpack/lib/action_dispatch/testing/integration.rb b/actionpack/lib/action_dispatch/testing/integration.rb index 5c6416a19e..4706112a06 100644 --- a/actionpack/lib/action_dispatch/testing/integration.rb +++ b/actionpack/lib/action_dispatch/testing/integration.rb @@ -26,31 +26,31 @@ module ActionDispatch # object's @response instance variable will point to the same # response object. # - # You can also perform POST, PUT, DELETE, and HEAD requests with +post+, - # +put+, +delete+, and +head+. + # You can also perform POST, PUT, DELETE, and HEAD requests with +#post+, + # +#put+, +#delete+, and +#head+. def get(path, parameters = nil, headers = nil) process :get, path, parameters, headers end - # Performs a POST request with the given parameters. See get() for more + # Performs a POST request with the given parameters. See +#get+ for more # details. def post(path, parameters = nil, headers = nil) process :post, path, parameters, headers end - # Performs a PUT request with the given parameters. See get() for more + # Performs a PUT request with the given parameters. See +#get+ for more # details. def put(path, parameters = nil, headers = nil) process :put, path, parameters, headers end - # Performs a DELETE request with the given parameters. See get() for + # Performs a DELETE request with the given parameters. See +#get+ for # more details. def delete(path, parameters = nil, headers = nil) process :delete, path, parameters, headers end - # Performs a HEAD request with the given parameters. See get() for more + # Performs a HEAD request with the given parameters. See +#get+ for more # details. def head(path, parameters = nil, headers = nil) process :head, path, parameters, headers @@ -59,7 +59,7 @@ module ActionDispatch # Performs an XMLHttpRequest request with the given parameters, mirroring # a request from the Prototype library. # - # The request_method is :get, :post, :put, :delete or :head; the + # The request_method is +:get+, +:post+, +:put+, +:delete+ or +:head+; the # parameters are +nil+, a hash, or a url-encoded or multipart string; # the headers are a hash. Keys are automatically upcased and prefixed # with 'HTTP_' if not already. @@ -384,7 +384,7 @@ module ActionDispatch end end - # An test that spans multiple controllers and actions, + # An integration test spans multiple controllers and actions, # tying them all together to ensure they work together as expected. It tests # more completely than either unit or functional tests do, exercising the # entire stack, from the dispatcher to the database. -- cgit v1.2.3 From 1af295fc9a6772e587e438254cfe354e6da0fa19 Mon Sep 17 00:00:00 2001 From: Joshua Peek Date: Tue, 29 Mar 2011 15:42:31 -0500 Subject: Tests for SprocketsHelper --- actionpack/lib/action_view/helpers/sprockets_helper.rb | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) (limited to 'actionpack/lib') diff --git a/actionpack/lib/action_view/helpers/sprockets_helper.rb b/actionpack/lib/action_view/helpers/sprockets_helper.rb index bfc396fad5..f6f4f06d6e 100644 --- a/actionpack/lib/action_view/helpers/sprockets_helper.rb +++ b/actionpack/lib/action_view/helpers/sprockets_helper.rb @@ -9,7 +9,7 @@ module ActionView def sprockets_javascript_include_tag(source, options = {}) options = { - 'type' => "application/javascript", + 'type' => "text/javascript", 'src' => sprockets_javascript_path(source) }.merge(options.stringify_keys) @@ -33,6 +33,8 @@ module ActionView private def compute_sprockets_path(source, dir, default_ext) + source = source.to_s + return source if URI.parse(source).host # Add /javscripts to relative paths @@ -46,15 +48,15 @@ module ActionView end # Fingerprint url - source = Rails.application.assets.path(source) + source = assets.path(source) host = compute_asset_host(source) - if controller.respond_to?(:request) && host && URI.parse(host).host.nil? - host = "#{controller.request.protocol}#{host}" + if controller.respond_to?(:request) && host && URI.parse(host).host + source = "#{controller.request.protocol}#{host}#{source}" end - "#{host}#{source}" + source end def compute_asset_host(source) @@ -72,6 +74,10 @@ module ActionView end end end + + def assets + Rails.application.assets + end end end end -- cgit v1.2.3 From 375443a9c5eb28279d66f5a53677856e9a39d93f Mon Sep 17 00:00:00 2001 From: Joshua Peek Date: Tue, 29 Mar 2011 15:53:28 -0500 Subject: Use sprockets helpers if config.use_sprockets is set --- .../helpers/asset_tag_helpers/javascript_tag_helpers.rb | 14 +++++++++++--- .../helpers/asset_tag_helpers/stylesheet_tag_helpers.rb | 14 +++++++++++--- 2 files changed, 22 insertions(+), 6 deletions(-) (limited to 'actionpack/lib') diff --git a/actionpack/lib/action_view/helpers/asset_tag_helpers/javascript_tag_helpers.rb b/actionpack/lib/action_view/helpers/asset_tag_helpers/javascript_tag_helpers.rb index 82bbfcc7d2..ed95f1c018 100644 --- a/actionpack/lib/action_view/helpers/asset_tag_helpers/javascript_tag_helpers.rb +++ b/actionpack/lib/action_view/helpers/asset_tag_helpers/javascript_tag_helpers.rb @@ -86,7 +86,11 @@ module ActionView # javascript_path "http://www.railsapplication.com/js/xmlhr" # => http://www.railsapplication.com/js/xmlhr # javascript_path "http://www.railsapplication.com/js/xmlhr.js" # => http://www.railsapplication.com/js/xmlhr.js def javascript_path(source) - asset_paths.compute_public_path(source, 'javascripts', 'js') + if config.use_sprockets + sprockets_javascript_path(source) + else + asset_paths.compute_public_path(source, 'javascripts', 'js') + end end alias_method :path_to_javascript, :javascript_path # aliased to avoid conflicts with a javascript_path named route @@ -173,8 +177,12 @@ module ActionView # # javascript_include_tag :all, :cache => true, :recursive => true def javascript_include_tag(*sources) - @javascript_include ||= JavascriptIncludeTag.new(config, asset_paths) - @javascript_include.include_tag(*sources) + if config.use_sprockets + sprockets_javascript_include_tag(*sources) + else + @javascript_include ||= JavascriptIncludeTag.new(config, asset_paths) + @javascript_include.include_tag(*sources) + end end end diff --git a/actionpack/lib/action_view/helpers/asset_tag_helpers/stylesheet_tag_helpers.rb b/actionpack/lib/action_view/helpers/asset_tag_helpers/stylesheet_tag_helpers.rb index a48c87b49a..a994afb65e 100644 --- a/actionpack/lib/action_view/helpers/asset_tag_helpers/stylesheet_tag_helpers.rb +++ b/actionpack/lib/action_view/helpers/asset_tag_helpers/stylesheet_tag_helpers.rb @@ -63,7 +63,11 @@ module ActionView # stylesheet_path "http://www.railsapplication.com/css/style" # => http://www.railsapplication.com/css/style # stylesheet_path "http://www.railsapplication.com/css/style.css" # => http://www.railsapplication.com/css/style.css def stylesheet_path(source) - asset_paths.compute_public_path(source, 'stylesheets', 'css') + if config.use_sprockets + sprockets_stylesheet_path(source) + else + asset_paths.compute_public_path(source, 'stylesheets', 'css') + end end alias_method :path_to_stylesheet, :stylesheet_path # aliased to avoid conflicts with a stylesheet_path named route @@ -136,8 +140,12 @@ module ActionView # stylesheet_link_tag :all, :concat => true # def stylesheet_link_tag(*sources) - @stylesheet_include ||= StylesheetIncludeTag.new(config, asset_paths) - @stylesheet_include.include_tag(*sources) + if config.use_sprockets + sprockets_stylesheet_link_tag(*sources) + else + @stylesheet_include ||= StylesheetIncludeTag.new(config, asset_paths) + @stylesheet_include.include_tag(*sources) + end end end -- cgit v1.2.3 From bed7a1acc46c9b211b7feaa21cc10395283392ab Mon Sep 17 00:00:00 2001 From: Joshua Peek Date: Tue, 29 Mar 2011 16:17:49 -0500 Subject: Copy use_sprockets config --- actionpack/lib/abstract_controller/asset_paths.rb | 4 ++-- actionpack/lib/action_controller/railtie.rb | 1 + 2 files changed, 3 insertions(+), 2 deletions(-) (limited to 'actionpack/lib') diff --git a/actionpack/lib/abstract_controller/asset_paths.rb b/actionpack/lib/abstract_controller/asset_paths.rb index 9ca2fb742f..ad14cd6d87 100644 --- a/actionpack/lib/abstract_controller/asset_paths.rb +++ b/actionpack/lib/abstract_controller/asset_paths.rb @@ -3,7 +3,7 @@ module AbstractController extend ActiveSupport::Concern included do - config_accessor :asset_host, :asset_path, :assets_dir, :javascripts_dir, :stylesheets_dir + config_accessor :asset_host, :asset_path, :assets_dir, :javascripts_dir, :stylesheets_dir, :use_sprockets end end -end \ No newline at end of file +end diff --git a/actionpack/lib/action_controller/railtie.rb b/actionpack/lib/action_controller/railtie.rb index f0c29825ba..0f87295d47 100644 --- a/actionpack/lib/action_controller/railtie.rb +++ b/actionpack/lib/action_controller/railtie.rb @@ -21,6 +21,7 @@ module ActionController paths = app.config.paths options = app.config.action_controller + options.use_sprockets ||= app.config.use_sprockets options.assets_dir ||= paths["public"].first options.javascripts_dir ||= paths["public/javascripts"].first options.stylesheets_dir ||= paths["public/stylesheets"].first -- cgit v1.2.3 From a5f547cc7937fe1c75ea741b5432e89f7539cb2b Mon Sep 17 00:00:00 2001 From: Joshua Peek Date: Tue, 29 Mar 2011 17:27:49 -0500 Subject: Only add fingerprints if perform_caching is on --- actionpack/lib/action_view/helpers/sprockets_helper.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'actionpack/lib') diff --git a/actionpack/lib/action_view/helpers/sprockets_helper.rb b/actionpack/lib/action_view/helpers/sprockets_helper.rb index f6f4f06d6e..9fd5f6759c 100644 --- a/actionpack/lib/action_view/helpers/sprockets_helper.rb +++ b/actionpack/lib/action_view/helpers/sprockets_helper.rb @@ -48,7 +48,7 @@ module ActionView end # Fingerprint url - source = assets.path(source) + source = assets.path(source, config.perform_caching) host = compute_asset_host(source) -- cgit v1.2.3 From db3e310d6b327b2b58cfbc0318abd3b4ddca5e30 Mon Sep 17 00:00:00 2001 From: Joshua Peek Date: Tue, 29 Mar 2011 18:05:23 -0500 Subject: Change back to /assets prefix --- actionpack/lib/action_view/helpers/sprockets_helper.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'actionpack/lib') diff --git a/actionpack/lib/action_view/helpers/sprockets_helper.rb b/actionpack/lib/action_view/helpers/sprockets_helper.rb index 9fd5f6759c..4f19f4bb21 100644 --- a/actionpack/lib/action_view/helpers/sprockets_helper.rb +++ b/actionpack/lib/action_view/helpers/sprockets_helper.rb @@ -4,7 +4,7 @@ module ActionView module Helpers module SprocketsHelper def sprockets_javascript_path(source) - compute_sprockets_path source, 'javascripts', 'js' + compute_sprockets_path source, 'assets', 'js' end def sprockets_javascript_include_tag(source, options = {}) @@ -17,7 +17,7 @@ module ActionView end def sprockets_stylesheet_path(source) - compute_sprockets_path source, 'stylesheets', 'css' + compute_sprockets_path source, 'assets', 'css' end def sprockets_stylesheet_link_tag(source, options = {}) -- cgit v1.2.3 From 651d371a247b0c473fb35a62076e480d95d84a35 Mon Sep 17 00:00:00 2001 From: Joshua Peek Date: Tue, 29 Mar 2011 21:23:05 -0500 Subject: Rename option to config.asset_pipeline --- actionpack/lib/action_controller/railtie.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'actionpack/lib') diff --git a/actionpack/lib/action_controller/railtie.rb b/actionpack/lib/action_controller/railtie.rb index 0f87295d47..175b7744d7 100644 --- a/actionpack/lib/action_controller/railtie.rb +++ b/actionpack/lib/action_controller/railtie.rb @@ -21,7 +21,7 @@ module ActionController paths = app.config.paths options = app.config.action_controller - options.use_sprockets ||= app.config.use_sprockets + options.use_sprockets ||= app.config.asset_pipeline options.assets_dir ||= paths["public"].first options.javascripts_dir ||= paths["public/javascripts"].first options.stylesheets_dir ||= paths["public/stylesheets"].first -- cgit v1.2.3 From 77d8f7a4b71d7f52a7ba6f8bc7f7f6f6ac9b81e0 Mon Sep 17 00:00:00 2001 From: Joshua Peek Date: Tue, 29 Mar 2011 21:40:24 -0500 Subject: Seperate asset directories --- actionpack/lib/action_view/helpers/sprockets_helper.rb | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'actionpack/lib') diff --git a/actionpack/lib/action_view/helpers/sprockets_helper.rb b/actionpack/lib/action_view/helpers/sprockets_helper.rb index 4f19f4bb21..408a2030ab 100644 --- a/actionpack/lib/action_view/helpers/sprockets_helper.rb +++ b/actionpack/lib/action_view/helpers/sprockets_helper.rb @@ -48,7 +48,9 @@ module ActionView end # Fingerprint url - source = assets.path(source, config.perform_caching) + if source =~ /^\/#{dir}\/(.+)/ + source = assets.path($1, config.perform_caching, dir) + end host = compute_asset_host(source) -- cgit v1.2.3 From 28fee29e383258c38649e20dc9508188010020c8 Mon Sep 17 00:00:00 2001 From: Joshua Peek Date: Wed, 30 Mar 2011 21:56:15 -0500 Subject: Unify sprockets config options --- actionpack/lib/action_controller/railtie.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'actionpack/lib') diff --git a/actionpack/lib/action_controller/railtie.rb b/actionpack/lib/action_controller/railtie.rb index 175b7744d7..0dd13fa9bf 100644 --- a/actionpack/lib/action_controller/railtie.rb +++ b/actionpack/lib/action_controller/railtie.rb @@ -21,7 +21,7 @@ module ActionController paths = app.config.paths options = app.config.action_controller - options.use_sprockets ||= app.config.asset_pipeline + options.use_sprockets ||= app.config.assets.enabled options.assets_dir ||= paths["public"].first options.javascripts_dir ||= paths["public/javascripts"].first options.stylesheets_dir ||= paths["public/stylesheets"].first -- cgit v1.2.3 From a64abdda2505895fec6f0243db5928316c4df90a Mon Sep 17 00:00:00 2001 From: Amaia Castro Date: Thu, 31 Mar 2011 13:19:19 +0200 Subject: Explain in the method doc that you need to call respond_to at the class level in order to use respond_with. --- actionpack/lib/action_controller/metal/mime_responds.rb | 3 +++ 1 file changed, 3 insertions(+) (limited to 'actionpack/lib') diff --git a/actionpack/lib/action_controller/metal/mime_responds.rb b/actionpack/lib/action_controller/metal/mime_responds.rb index a2e06fe0a6..998bef6556 100644 --- a/actionpack/lib/action_controller/metal/mime_responds.rb +++ b/actionpack/lib/action_controller/metal/mime_responds.rb @@ -222,6 +222,9 @@ module ActionController #:nodoc: # is quite simple (it just needs to respond to call), you can even give # a proc to it. # + # In order to use respond_with, first you need to declare the formats your + # controller responds to in the class level with a call to respond_to. + # def respond_with(*resources, &block) raise "In order to use respond_with, first you need to declare the formats your " << "controller responds to in the class level" if self.class.mimes_for_respond_to.empty? -- cgit v1.2.3 From c17b8e4047443b416685e30c8825ae01909f8d27 Mon Sep 17 00:00:00 2001 From: Jon Cooper Date: Thu, 31 Mar 2011 16:19:18 -0700 Subject: Trivial fix to HTTP Digest auth MD5 example --- actionpack/lib/action_controller/metal/http_authentication.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'actionpack/lib') diff --git a/actionpack/lib/action_controller/metal/http_authentication.rb b/actionpack/lib/action_controller/metal/http_authentication.rb index 39c804d707..c305abf5eb 100644 --- a/actionpack/lib/action_controller/metal/http_authentication.rb +++ b/actionpack/lib/action_controller/metal/http_authentication.rb @@ -77,7 +77,7 @@ module ActionController # class PostsController < ApplicationController # REALM = "SuperSecret" # USERS = {"dhh" => "secret", #plain text password - # "dap" => Digest:MD5::hexdigest(["dap",REALM,"secret"].join(":")) #ha1 digest password + # "dap" => Digest::MD5.hexdigest(["dap",REALM,"secret"].join(":")) #ha1 digest password # # before_filter :authenticate, :except => [:index] # -- cgit v1.2.3 From cc58fe79ac6f4d5fd54a39ff6e7f087c6a04fee8 Mon Sep 17 00:00:00 2001 From: Santiago Pastorino Date: Sat, 2 Apr 2011 23:45:07 -0300 Subject: Implicit actions named not_implemented can be rendered --- actionpack/lib/abstract_controller/base.rb | 2 ++ 1 file changed, 2 insertions(+) (limited to 'actionpack/lib') diff --git a/actionpack/lib/abstract_controller/base.rb b/actionpack/lib/abstract_controller/base.rb index 07ff5ad9f3..0951267fea 100644 --- a/actionpack/lib/abstract_controller/base.rb +++ b/actionpack/lib/abstract_controller/base.rb @@ -1,3 +1,4 @@ +require 'erubis' require 'active_support/configurable' require 'active_support/descendants_tracker' require 'active_support/core_ext/module/anonymous' @@ -18,6 +19,7 @@ module AbstractController include ActiveSupport::Configurable extend ActiveSupport::DescendantsTracker + undef_method :not_implemented class << self attr_reader :abstract alias_method :abstract?, :abstract -- cgit v1.2.3 From bd3cdeea354ebff97b0d5102a0857ce85eedcfa4 Mon Sep 17 00:00:00 2001 From: Akira Matsuda Date: Sun, 3 Apr 2011 12:47:51 +0900 Subject: s/ERb/ERB/g The author of ERB sais, his eRuby implementation was originally named "ERb/ERbLight" and then renamed to "ERB" when started bundled as a Ruby standard lib. http://www2a.biglobe.ne.jp/~seki/ruby/erb.html --- actionpack/lib/action_controller/base.rb | 4 ++-- actionpack/lib/action_view/base.rb | 10 +++++----- actionpack/lib/action_view/helpers/atom_feed_helper.rb | 2 +- actionpack/lib/action_view/helpers/capture_helper.rb | 2 +- actionpack/lib/action_view/helpers/prototype_helper.rb | 2 +- actionpack/lib/action_view/helpers/url_helper.rb | 2 +- actionpack/lib/action_view/template/handlers/erb.rb | 2 +- 7 files changed, 12 insertions(+), 12 deletions(-) (limited to 'actionpack/lib') diff --git a/actionpack/lib/action_controller/base.rb b/actionpack/lib/action_controller/base.rb index e6523e56d2..5f9e082cd3 100644 --- a/actionpack/lib/action_controller/base.rb +++ b/actionpack/lib/action_controller/base.rb @@ -105,7 +105,7 @@ module ActionController # == Renders # # Action Controller sends content to the user by using one of five rendering methods. The most versatile and common is the rendering - # of a template. Included in the Action Pack is the Action View, which enables rendering of ERb templates. It's automatically configured. + # of a template. Included in the Action Pack is the Action View, which enables rendering of ERB templates. It's automatically configured. # The controller passes objects to the view by assigning instance variables: # # def show @@ -128,7 +128,7 @@ module ActionController # end # end # - # Read more about writing ERb and Builder templates in ActionView::Base. + # Read more about writing ERB and Builder templates in ActionView::Base. # # == Redirects # diff --git a/actionpack/lib/action_view/base.rb b/actionpack/lib/action_view/base.rb index ab8c6259c5..5519103627 100644 --- a/actionpack/lib/action_view/base.rb +++ b/actionpack/lib/action_view/base.rb @@ -8,13 +8,13 @@ require 'action_view/log_subscriber' module ActionView #:nodoc: # = Action View Base # - # Action View templates can be written in three ways. If the template file has a .erb (or .rhtml) extension then it uses a mixture of ERb + # Action View templates can be written in three ways. If the template file has a .erb (or .rhtml) extension then it uses a mixture of ERB # (included in Ruby) and HTML. If the template file has a .builder (or .rxml) extension then Jim Weirich's Builder::XmlMarkup library is used. # If the template file has a .rjs extension then it will use ActionView::Helpers::PrototypeHelper::JavaScriptGenerator. # - # == ERb + # == ERB # - # You trigger ERb by using embeddings such as <% %>, <% -%>, and <%= %>. The <%= %> tag set is used when you want output. Consider the + # You trigger ERB by using embeddings such as <% %>, <% -%>, and <%= %>. The <%= %> tag set is used when you want output. Consider the # following loop for names: # # Names of all the people @@ -23,7 +23,7 @@ module ActionView #:nodoc: # <% end %> # # The loop is setup in regular embedding tags <% %> and the name is written using the output embedding tag <%= %>. Note that this - # is not just a usage suggestion. Regular output functions like print or puts won't work with ERb templates. So this would be wrong: + # is not just a usage suggestion. Regular output functions like print or puts won't work with ERB templates. So this would be wrong: # # <%# WRONG %> # Hi, Mr. <% puts "Frodo" %> @@ -81,7 +81,7 @@ module ActionView #:nodoc: # # == Builder # - # Builder templates are a more programmatic alternative to ERb. They are especially useful for generating XML content. An XmlMarkup object + # Builder templates are a more programmatic alternative to ERB. They are especially useful for generating XML content. An XmlMarkup object # named +xml+ is automatically made available to templates with a .builder extension. # # Here are some basic examples: diff --git a/actionpack/lib/action_view/helpers/atom_feed_helper.rb b/actionpack/lib/action_view/helpers/atom_feed_helper.rb index db9d7a08ff..96e5722252 100644 --- a/actionpack/lib/action_view/helpers/atom_feed_helper.rb +++ b/actionpack/lib/action_view/helpers/atom_feed_helper.rb @@ -4,7 +4,7 @@ module ActionView # = Action View Atom Feed Helpers module Helpers #:nodoc: module AtomFeedHelper - # Adds easy defaults to writing Atom feeds with the Builder template engine (this does not work on ERb or any other + # Adds easy defaults to writing Atom feeds with the Builder template engine (this does not work on ERB or any other # template languages). # # Full usage example: diff --git a/actionpack/lib/action_view/helpers/capture_helper.rb b/actionpack/lib/action_view/helpers/capture_helper.rb index c88bd1efd5..9ac7dff1ec 100644 --- a/actionpack/lib/action_view/helpers/capture_helper.rb +++ b/actionpack/lib/action_view/helpers/capture_helper.rb @@ -14,7 +14,7 @@ module ActionView # variable. You can then use this variable anywhere in your templates or layout. # # ==== Examples - # The capture method can be used in ERb templates... + # The capture method can be used in ERB templates... # # <% @greeting = capture do %> # Welcome to my shiny new web page! The date and time is diff --git a/actionpack/lib/action_view/helpers/prototype_helper.rb b/actionpack/lib/action_view/helpers/prototype_helper.rb index 18e303778c..506db24dc2 100644 --- a/actionpack/lib/action_view/helpers/prototype_helper.rb +++ b/actionpack/lib/action_view/helpers/prototype_helper.rb @@ -584,7 +584,7 @@ module ActionView # Works like update_page but wraps the generated JavaScript in a # \ + # javascript_include_tag "xmlhr" + # # => # - # javascript_include_tag "xmlhr.js" # => - # + # javascript_include_tag "xmlhr.js" + # # => # - # javascript_include_tag "common.javascript", "/elsewhere/cools" # => - # - # + # javascript_include_tag "common.javascript", "/elsewhere/cools" + # # => + # # # - # javascript_include_tag "http://www.railsapplication.com/xmlhr" # => - # + # javascript_include_tag "http://www.railsapplication.com/xmlhr" + # # => # - # javascript_include_tag "http://www.railsapplication.com/xmlhr.js" # => - # + # javascript_include_tag "http://www.railsapplication.com/xmlhr.js" + # # => # - # javascript_include_tag :defaults # => - # - # - # ... - # + # javascript_include_tag :defaults + # # => + # # + # # # # * = The application.js file is only referenced if it exists # - # You can also include all javascripts in the +javascripts+ directory using :all as the source: + # You can also include all JavaScripts in the +javascripts+ directory using :all as the source: # - # javascript_include_tag :all # => - # - # - # ... - # - # - # + # javascript_include_tag :all + # # => + # # + # # + # # + # # # - # Note that the default javascript files will be included first. So Prototype and Scriptaculous are available to - # all subsequently included files. + # Note that your defaults of choice will be included first, so they will be available to all subsequently + # included files. # - # If you want Rails to search in all the subdirectories under javascripts, you should explicitly set :recursive: + # If you want Rails to search in all the subdirectories under public/javascripts, you should + # explicitly set :recursive: # # javascript_include_tag :all, :recursive => true # - # == Caching multiple javascripts into one + # == Caching multiple JavaScripts into one # - # You can also cache multiple javascripts into one file, which requires less HTTP connections to download and can better be - # compressed by gzip (leading to faster transfers). Caching will only happen if config.perform_caching - # is set to true (which is the case by default for the Rails production environment, but not for the development - # environment). + # You can also cache multiple JavaScripts into one file, which requires less HTTP connections to download + # and can better be compressed by gzip (leading to faster transfers). Caching will only happen if + # config.perform_caching is set to true (which is the case by default for the Rails + # production environment, but not for the development environment). # # ==== Examples - # javascript_include_tag :all, :cache => true # when config.perform_caching is false => - # - # - # ... - # - # - # # - # javascript_include_tag :all, :cache => true # when config.perform_caching is true => - # + # # assuming config.perform_caching is false + # javascript_include_tag :all, :cache => true + # # => + # # + # # + # # + # # + # + # # assuming config.perform_caching is true + # javascript_include_tag :all, :cache => true + # # => # - # javascript_include_tag "prototype", "cart", "checkout", :cache => "shop" # when config.perform_caching is false => - # - # - # + # # assuming config.perform_caching is false + # javascript_include_tag "jquery", "cart", "checkout", :cache => "shop" + # # => + # # + # # # - # javascript_include_tag "prototype", "cart", "checkout", :cache => "shop" # when config.perform_caching is true => - # + # # assuming config.perform_caching is true + # javascript_include_tag "jquery", "cart", "checkout", :cache => "shop" + # # => # # The :recursive option is also available for caching: # @@ -184,9 +198,7 @@ module ActionView @javascript_include.include_tag(*sources) end end - end - end end end diff --git a/actionpack/lib/action_view/railtie.rb b/actionpack/lib/action_view/railtie.rb index 501ec07b09..f20ba7e6d3 100644 --- a/actionpack/lib/action_view/railtie.rb +++ b/actionpack/lib/action_view/railtie.rb @@ -6,7 +6,7 @@ module ActionView class Railtie < Rails::Railtie config.action_view = ActiveSupport::OrderedOptions.new config.action_view.stylesheet_expansions = {} - config.action_view.javascript_expansions = { :defaults => ['prototype', 'effects', 'dragdrop', 'controls', 'rails'] } + config.action_view.javascript_expansions = { :defaults => %w(jquery rails) } initializer "action_view.cache_asset_ids" do |app| unless app.config.cache_classes -- cgit v1.2.3 From 8449da929efddb7f1eb6e6e7e39d8e480bec9484 Mon Sep 17 00:00:00 2001 From: Xavier Noria Date: Thu, 24 Mar 2011 23:48:07 +0100 Subject: removes support for RJS in link_to_function --- .../lib/action_view/helpers/javascript_helper.rb | 53 ++++------------------ 1 file changed, 9 insertions(+), 44 deletions(-) (limited to 'actionpack/lib') diff --git a/actionpack/lib/action_view/helpers/javascript_helper.rb b/actionpack/lib/action_view/helpers/javascript_helper.rb index a19ba7a968..3d77d5c13b 100644 --- a/actionpack/lib/action_view/helpers/javascript_helper.rb +++ b/actionpack/lib/action_view/helpers/javascript_helper.rb @@ -129,54 +129,19 @@ module ActionView tag(:input, html_options.merge(:type => 'button', :value => name, :onclick => onclick)) end - # Returns a link of the given +name+ that will trigger a JavaScript +function+ using the - # onclick handler and return false after the fact. + # Returns a link whose +onclick+ handler triggers the passed JavaScript. # - # The first argument +name+ is used as the link text. - # - # The next arguments are optional and may include the javascript function definition and a hash of html_options. - # - # The +function+ argument can be omitted in favor of an +update_page+ - # block, which evaluates to a string when the template is rendered - # (instead of making an Ajax request first). - # - # The +html_options+ will accept a hash of html attributes for the link tag. Some examples are :class => "nav_button", :id => "articles_nav_button" - # - # Note: if you choose to specify the javascript function in a block, but would like to pass html_options, set the +function+ parameter to nil + # The helper receives a name, JavaScript code, and an optional hash of HTML options. The + # name is used as the link text and the JavaScript code goes into the +onclick+ attribute. + # If +html_options+ has an :onclick, that one is put before +function+. Once all + # the JavaScript is set, the helper appends "; return false;". # + # The +href+ attribute of the tag is set to "#" unles +html_options+ has one. # - # Examples: - # link_to_function "Greeting", "alert('Hello world!')" - # Produces: - # Greeting - # - # link_to_function(image_tag("delete"), "if (confirm('Really?')) do_delete()") - # Produces: - # - # Delete - # - # - # link_to_function("Show me more", nil, :id => "more_link") do |page| - # page[:details].visual_effect :toggle_blind - # page[:more_link].replace_html "Show me less" - # end + # link_to_function "Greeting", "alert('Hello world!')", :class => "nav_link" # Produces: - # Show me more - # - def link_to_function(name, *args, &block) - html_options = args.extract_options!.symbolize_keys - - function = block_given? ? update_page(&block) : args[0] || '' + # Greeting + def link_to_function(name, function, html_options={}) onclick = "#{"#{html_options[:onclick]}; " if html_options[:onclick]}#{function}; return false;" href = html_options[:href] || '#' -- cgit v1.2.3 From b878757c5073eac7e4fcfd2093d38d8b841db846 Mon Sep 17 00:00:00 2001 From: Xavier Noria Date: Thu, 24 Mar 2011 23:51:17 +0100 Subject: removes assert_select_rjs --- .../action_dispatch/testing/assertions/selector.rb | 140 +-------------------- 1 file changed, 1 insertion(+), 139 deletions(-) (limited to 'actionpack/lib') diff --git a/actionpack/lib/action_dispatch/testing/assertions/selector.rb b/actionpack/lib/action_dispatch/testing/assertions/selector.rb index f41d3e5ddb..39a2a9884a 100644 --- a/actionpack/lib/action_dispatch/testing/assertions/selector.rb +++ b/actionpack/lib/action_dispatch/testing/assertions/selector.rb @@ -19,7 +19,7 @@ module ActionDispatch # from the response HTML or elements selected by the enclosing assertion. # # In addition to HTML responses, you can make the following assertions: - # * +assert_select_rjs+ - Assertions on HTML content of RJS update and insertion operations. + # # * +assert_select_encoded+ - Assertions on HTML encoded inside XML, for example for dealing with feed item descriptions. # * +assert_select_email+ - Assertions on the HTML body of an e-mail. # @@ -326,144 +326,6 @@ module ActionDispatch end end - # Selects content from the RJS response. - # - # === Narrowing down - # - # With no arguments, asserts that one or more elements are updated or - # inserted by RJS statements. - # - # Use the +id+ argument to narrow down the assertion to only statements - # that update or insert an element with that identifier. - # - # Use the first argument to narrow down assertions to only statements - # of that type. Possible values are :replace, :replace_html, - # :show, :hide, :toggle, :remove, - # :insert_html and :redirect. - # - # Use the argument :insert followed by an insertion position to narrow - # down the assertion to only statements that insert elements in that - # position. Possible values are :top, :bottom, :before - # and :after. - # - # Use the argument :redirect followed by a path to check that an statement - # which redirects to the specified path is generated. - # - # Using the :remove statement, you will be able to pass a block, but it will - # be ignored as there is no HTML passed for this statement. - # - # === Using blocks - # - # Without a block, +assert_select_rjs+ merely asserts that the response - # contains one or more RJS statements that replace or update content. - # - # With a block, +assert_select_rjs+ also selects all elements used in - # these statements and passes them to the block. Nested assertions are - # supported. - # - # Calling +assert_select_rjs+ with no arguments and using nested asserts - # asserts that the HTML content is returned by one or more RJS statements. - # Using +assert_select+ directly makes the same assertion on the content, - # but without distinguishing whether the content is returned in an HTML - # or JavaScript. - # - # ==== Examples - # - # # Replacing the element foo. - # # page.replace 'foo', ... - # assert_select_rjs :replace, "foo" - # - # # Replacing with the chained RJS proxy. - # # page[:foo].replace ... - # assert_select_rjs :chained_replace, 'foo' - # - # # Inserting into the element bar, top position. - # assert_select_rjs :insert, :top, "bar" - # - # # Remove the element bar - # assert_select_rjs :remove, "bar" - # - # # Changing the element foo, with an image. - # assert_select_rjs "foo" do - # assert_select "img[src=/images/logo.gif"" - # end - # - # # RJS inserts or updates a list with four items. - # assert_select_rjs do - # assert_select "ol>li", 4 - # end - # - # # The same, but shorter. - # assert_select "ol>li", 4 - # - # # Checking for a redirect. - # assert_select_rjs :redirect, root_path - def assert_select_rjs(*args, &block) - rjs_type = args.first.is_a?(Symbol) ? args.shift : nil - id = args.first.is_a?(String) ? args.shift : nil - - # If the first argument is a symbol, it's the type of RJS statement we're looking - # for (update, replace, insertion, etc). Otherwise, we're looking for just about - # any RJS statement. - if rjs_type - if rjs_type == :insert - position = args.shift - id = args.shift - insertion = "insert_#{position}".to_sym - raise ArgumentError, "Unknown RJS insertion type #{position}" unless RJS_STATEMENTS[insertion] - statement = "(#{RJS_STATEMENTS[insertion]})" - else - raise ArgumentError, "Unknown RJS statement type #{rjs_type}" unless RJS_STATEMENTS[rjs_type] - statement = "(#{RJS_STATEMENTS[rjs_type]})" - end - else - statement = "#{RJS_STATEMENTS[:any]}" - end - - # Next argument we're looking for is the element identifier. If missing, we pick - # any element, otherwise we replace it in the statement. - pattern = Regexp.new( - id ? statement.gsub(RJS_ANY_ID, "\"#{id}\"") : statement - ) - - # Duplicate the body since the next step involves destroying it. - matches = nil - case rjs_type - when :remove, :show, :hide, :toggle - matches = @response.body.match(pattern) - else - @response.body.gsub(pattern) do |match| - html = unescape_rjs(match) - matches ||= [] - matches.concat HTML::Document.new(html).root.children.select { |n| n.tag? } - "" - end - end - - if matches - assert_block("") { true } # to count the assertion - if block_given? && !rjs_type.among?(:remove, :show, :hide, :toggle) - begin - @selected ||= nil - in_scope, @selected = @selected, matches - yield matches - ensure - @selected = in_scope - end - end - matches - else - # RJS statement not found. - case rjs_type - when :remove, :show, :hide, :toggle - flunk_message = "No RJS statement that #{rjs_type.to_s}s '#{id}' was rendered." - else - flunk_message = "No RJS statement that replaces or inserts HTML content." - end - flunk args.shift || flunk_message - end - end - # Extracts the content of an element, treats it as encoded HTML and runs # nested assertion on it. # -- cgit v1.2.3 From 3223e04a21c27a2e612fcd341e06c46433659f8d Mon Sep 17 00:00:00 2001 From: Xavier Noria Date: Fri, 25 Mar 2011 00:28:47 +0100 Subject: removes support for RJS in button_to_function --- .../lib/action_view/helpers/javascript_helper.rb | 32 +++++----------------- 1 file changed, 7 insertions(+), 25 deletions(-) (limited to 'actionpack/lib') diff --git a/actionpack/lib/action_view/helpers/javascript_helper.rb b/actionpack/lib/action_view/helpers/javascript_helper.rb index 3d77d5c13b..136d006ad1 100644 --- a/actionpack/lib/action_view/helpers/javascript_helper.rb +++ b/actionpack/lib/action_view/helpers/javascript_helper.rb @@ -96,34 +96,16 @@ module ActionView "\n//#{cdata_section("\n#{content}\n//")}\n".html_safe end - # Returns a button with the given +name+ text that'll trigger a JavaScript +function+ using the - # onclick handler. + # Returns a button whose +onclick+ handler triggers the passed JavaScript. # - # The first argument +name+ is used as the button's value or display text. - # - # The next arguments are optional and may include the javascript function definition and a hash of html_options. - # - # The +function+ argument can be omitted in favor of an +update_page+ - # block, which evaluates to a string when the template is rendered - # (instead of making an Ajax request first). - # - # The +html_options+ will accept a hash of html attributes for the link tag. Some examples are :class => "nav_button", :id => "articles_nav_button" + # The helper receives a name, JavaScript code, and an optional hash of HTML options. The + # name is used as button label and the JavaScript code goes into its +onclick+ attribute. + # If +html_options+ has an :onclick, that one is put before +function+. # - # Note: if you choose to specify the javascript function in a block, but would like to pass html_options, set the +function+ parameter to nil + # button_to_function "Greeting", "alert('Hello world!')", :class => "ok" + # # => # - # Examples: - # button_to_function "Greeting", "alert('Hello world!')" - # button_to_function "Delete", "if (confirm('Really?')) do_delete()" - # button_to_function "Details" do |page| - # page[:details].visual_effect :toggle_slide - # end - # button_to_function "Details", :class => "details_button" do |page| - # page[:details].visual_effect :toggle_slide - # end - def button_to_function(name, *args, &block) - html_options = args.extract_options!.symbolize_keys - - function = block_given? ? update_page(&block) : args[0] || '' + def button_to_function(name, function='', html_options={}) onclick = "#{"#{html_options[:onclick]}; " if html_options[:onclick]}#{function};" tag(:input, html_options.merge(:type => 'button', :value => name, :onclick => onclick)) -- cgit v1.2.3 From f9472f064fae12d0890075d698d8e5fd81289392 Mon Sep 17 00:00:00 2001 From: Xavier Noria Date: Fri, 25 Mar 2011 00:30:29 +0100 Subject: applies API guidelines to example --- actionpack/lib/action_view/helpers/javascript_helper.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'actionpack/lib') diff --git a/actionpack/lib/action_view/helpers/javascript_helper.rb b/actionpack/lib/action_view/helpers/javascript_helper.rb index 136d006ad1..5a1d07fd24 100644 --- a/actionpack/lib/action_view/helpers/javascript_helper.rb +++ b/actionpack/lib/action_view/helpers/javascript_helper.rb @@ -121,8 +121,8 @@ module ActionView # The +href+ attribute of the tag is set to "#" unles +html_options+ has one. # # link_to_function "Greeting", "alert('Hello world!')", :class => "nav_link" - # Produces: - # Greeting + # # => Greeting + # def link_to_function(name, function, html_options={}) onclick = "#{"#{html_options[:onclick]}; " if html_options[:onclick]}#{function}; return false;" href = html_options[:href] || '#' -- cgit v1.2.3 From 25181cafee9ef087a14b5134b9fa1f85f758705a Mon Sep 17 00:00:00 2001 From: Xavier Noria Date: Fri, 25 Mar 2011 09:44:43 +0100 Subject: let the default function in button_to_function be nil I don't know which is the use case for button_to_function(name) but there's a test for it. I am focused now on RJS extraction and do not want to introduce a backwards incompatible change at this moment. Perhaps worth revisiting when the whole thing is done. --- actionpack/lib/action_view/helpers/javascript_helper.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'actionpack/lib') diff --git a/actionpack/lib/action_view/helpers/javascript_helper.rb b/actionpack/lib/action_view/helpers/javascript_helper.rb index 5a1d07fd24..066f0ab409 100644 --- a/actionpack/lib/action_view/helpers/javascript_helper.rb +++ b/actionpack/lib/action_view/helpers/javascript_helper.rb @@ -105,7 +105,7 @@ module ActionView # button_to_function "Greeting", "alert('Hello world!')", :class => "ok" # # => # - def button_to_function(name, function='', html_options={}) + def button_to_function(name, function=nil, html_options={}) onclick = "#{"#{html_options[:onclick]}; " if html_options[:onclick]}#{function};" tag(:input, html_options.merge(:type => 'button', :value => name, :onclick => onclick)) -- cgit v1.2.3 From eea66892c80d51c1b959171c2e3feac67124aaba Mon Sep 17 00:00:00 2001 From: Xavier Noria Date: Fri, 25 Mar 2011 20:08:13 +0100 Subject: removes support for render :update --- actionpack/lib/action_controller/metal/renderers.rb | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-) (limited to 'actionpack/lib') diff --git a/actionpack/lib/action_controller/metal/renderers.rb b/actionpack/lib/action_controller/metal/renderers.rb index 38711c8462..dfda6618e7 100644 --- a/actionpack/lib/action_controller/metal/renderers.rb +++ b/actionpack/lib/action_controller/metal/renderers.rb @@ -41,7 +41,7 @@ module ActionController end # Hash of available renderers, mapping a renderer name to its proc. - # Default keys are :json, :js, :xml and :update. + # Default keys are :json, :js, :xml. RENDERERS = {} # Adds a new renderer to call within controller actions. @@ -107,12 +107,5 @@ module ActionController self.content_type ||= Mime::XML self.response_body = xml.respond_to?(:to_xml) ? xml.to_xml(options) : xml end - - add :update do |proc, options| - view_context = self.view_context - generator = ActionView::Helpers::PrototypeHelper::JavaScriptGenerator.new(view_context, &proc) - self.content_type = Mime::JS - self.response_body = generator.to_s - end end end -- cgit v1.2.3 From 5850f1693546f14420bf0bc630a48650f0d606d5 Mon Sep 17 00:00:00 2001 From: Xavier Noria Date: Fri, 25 Mar 2011 23:12:09 +0100 Subject: removes the RJS template handler --- actionpack/lib/action_controller/metal/mime_responds.rb | 8 ++++---- actionpack/lib/action_view/template/handlers.rb | 2 -- actionpack/lib/action_view/template/handlers/rjs.rb | 13 ------------- 3 files changed, 4 insertions(+), 19 deletions(-) delete mode 100644 actionpack/lib/action_view/template/handlers/rjs.rb (limited to 'actionpack/lib') diff --git a/actionpack/lib/action_controller/metal/mime_responds.rb b/actionpack/lib/action_controller/metal/mime_responds.rb index 618a6cdb7d..16d48e4677 100644 --- a/actionpack/lib/action_controller/metal/mime_responds.rb +++ b/actionpack/lib/action_controller/metal/mime_responds.rb @@ -33,10 +33,10 @@ module ActionController #:nodoc: # and all actions except :edit respond to :xml and # :json. # - # respond_to :rjs, :only => :create + # respond_to :json, :only => :create # # This specifies that the :create action and no other responds - # to :rjs. + # to :json. def respond_to(*mimes) options = mimes.extract_options! @@ -106,8 +106,8 @@ module ActionController #:nodoc: # end # end # - # If the client wants HTML, we just redirect them back to the person list. If they want Javascript - # (format.js), then it is an RJS request and we render the RJS template associated with this action. + # If the client wants HTML, we just redirect them back to the person list. If they want JavaScript, + # then it is an Ajax request and we render the JavaScript template associated with this action. # Lastly, if the client wants XML, we render the created person as XML, but with a twist: we also # include the person's company in the rendered XML, so you get something like this: # diff --git a/actionpack/lib/action_view/template/handlers.rb b/actionpack/lib/action_view/template/handlers.rb index 4438199497..959afa734e 100644 --- a/actionpack/lib/action_view/template/handlers.rb +++ b/actionpack/lib/action_view/template/handlers.rb @@ -3,12 +3,10 @@ module ActionView #:nodoc: class Template module Handlers #:nodoc: autoload :ERB, 'action_view/template/handlers/erb' - autoload :RJS, 'action_view/template/handlers/rjs' autoload :Builder, 'action_view/template/handlers/builder' def self.extended(base) base.register_default_template_handler :erb, ERB.new - base.register_template_handler :rjs, RJS.new base.register_template_handler :builder, Builder.new end diff --git a/actionpack/lib/action_view/template/handlers/rjs.rb b/actionpack/lib/action_view/template/handlers/rjs.rb deleted file mode 100644 index 9d71059134..0000000000 --- a/actionpack/lib/action_view/template/handlers/rjs.rb +++ /dev/null @@ -1,13 +0,0 @@ -module ActionView - module Template::Handlers - class RJS - # Default format used by RJS. - class_attribute :default_format - self.default_format = Mime::JS - - def call(template) - "update_page do |page|;#{template.source}\nend" - end - end - end -end -- cgit v1.2.3 From 06bdaae071a43d5e149def2ac97001d04e26fa42 Mon Sep 17 00:00:00 2001 From: Xavier Noria Date: Sat, 26 Mar 2011 22:19:18 +0100 Subject: removes ActionView::Helpers::ScriptaculousHelper --- actionpack/lib/action_view/helpers.rb | 2 - .../lib/action_view/helpers/capture_helper.rb | 4 +- .../lib/action_view/helpers/javascript_helper.rb | 32 --- .../action_view/helpers/scriptaculous_helper.rb | 263 --------------------- 4 files changed, 2 insertions(+), 299 deletions(-) delete mode 100644 actionpack/lib/action_view/helpers/scriptaculous_helper.rb (limited to 'actionpack/lib') diff --git a/actionpack/lib/action_view/helpers.rb b/actionpack/lib/action_view/helpers.rb index cb1ab64121..88aafc82d5 100644 --- a/actionpack/lib/action_view/helpers.rb +++ b/actionpack/lib/action_view/helpers.rb @@ -21,7 +21,6 @@ module ActionView #:nodoc: autoload :OutputSafetyHelper autoload :RecordTagHelper autoload :SanitizeHelper - autoload :ScriptaculousHelper autoload :SprocketsHelper autoload :TagHelper autoload :TextHelper @@ -52,7 +51,6 @@ module ActionView #:nodoc: include OutputSafetyHelper include RecordTagHelper include SanitizeHelper - include ScriptaculousHelper include SprocketsHelper include TagHelper include TextHelper diff --git a/actionpack/lib/action_view/helpers/capture_helper.rb b/actionpack/lib/action_view/helpers/capture_helper.rb index 9ac7dff1ec..3808e231f1 100644 --- a/actionpack/lib/action_view/helpers/capture_helper.rb +++ b/actionpack/lib/action_view/helpers/capture_helper.rb @@ -107,8 +107,8 @@ module ActionView # <%= javascript_include_tag :defaults %> # <% end %> # - # That will place script tags for Prototype, Scriptaculous, and application.js (if it exists) - # on the page; this technique is useful if you'll only be using these scripts in a few views. + # That will place +script+ tags for your default set of JavaScript files on the page; + # this technique is useful if you'll only be using these scripts in a few views. # # Note that content_for concatenates the blocks it is given for a particular # identifier in order. For example: diff --git a/actionpack/lib/action_view/helpers/javascript_helper.rb b/actionpack/lib/action_view/helpers/javascript_helper.rb index 066f0ab409..ea8f31c45b 100644 --- a/actionpack/lib/action_view/helpers/javascript_helper.rb +++ b/actionpack/lib/action_view/helpers/javascript_helper.rb @@ -1,39 +1,7 @@ require 'action_view/helpers/tag_helper' module ActionView - # = Action View JavaScript Helpers module Helpers - # Provides functionality for working with JavaScript in your views. - # - # == Ajax, controls and visual effects - # - # * For information on using Ajax, see - # ActionView::Helpers::PrototypeHelper. - # * For information on using controls and visual effects, see - # ActionView::Helpers::ScriptaculousHelper. - # - # == Including the JavaScript libraries into your pages - # - # Rails includes the Prototype JavaScript framework and the Scriptaculous - # JavaScript controls and visual effects library. If you wish to use - # these libraries and their helpers (ActionView::Helpers::PrototypeHelper - # and ActionView::Helpers::ScriptaculousHelper), you must do one of the - # following: - # - # * Use <%= javascript_include_tag :defaults %> in the HEAD - # section of your page (recommended): This function will return - # references to the JavaScript files created by the +rails+ command in - # your public/javascripts directory. Using it is recommended as - # the browser can then cache the libraries instead of fetching all the - # functions anew on every request. - # * Use <%= javascript_include_tag 'prototype' %>: As above, but - # will only include the Prototype core library, which means you are able - # to use all basic AJAX functionality. For the Scriptaculous-based - # JavaScript helpers, like visual effects, autocompletion, drag and drop - # and so on, you should use the method described above. - # - # For documentation on +javascript_include_tag+ see - # ActionView::Helpers::AssetTagHelper. module JavaScriptHelper include PrototypeHelper diff --git a/actionpack/lib/action_view/helpers/scriptaculous_helper.rb b/actionpack/lib/action_view/helpers/scriptaculous_helper.rb deleted file mode 100644 index 8610c2469e..0000000000 --- a/actionpack/lib/action_view/helpers/scriptaculous_helper.rb +++ /dev/null @@ -1,263 +0,0 @@ -require 'action_view/helpers/javascript_helper' -require 'active_support/json' - -module ActionView - # = Action View Scriptaculous Helpers - module Helpers - # Provides a set of helpers for calling Scriptaculous[http://script.aculo.us/] - # JavaScript functions, including those which create Ajax controls and visual - # effects. - # - # To be able to use these helpers, you must include the Prototype - # JavaScript framework and the Scriptaculous JavaScript library in your - # pages. See the documentation for ActionView::Helpers::JavaScriptHelper - # for more information on including the necessary JavaScript. - # - # The Scriptaculous helpers' behavior can be tweaked with various options. - # - # See the documentation at http://script.aculo.us for more information on - # using these helpers in your application. - module ScriptaculousHelper - TOGGLE_EFFECTS = [:toggle_appear, :toggle_slide, :toggle_blind] - - # Returns a JavaScript snippet to be used on the Ajax callbacks for - # starting visual effects. - # - # If no +element_id+ is given, it assumes "element" which should be a local - # variable in the generated JavaScript execution context. This can be - # used for example with +drop_receiving_element+: - # - # <%= drop_receiving_element (...), :loading => visual_effect(:fade) %> - # - # This would fade the element that was dropped on the drop receiving - # element. - # - # For toggling visual effects, you can use :toggle_appear, :toggle_slide, and - # :toggle_blind which will alternate between appear/fade, slidedown/slideup, and - # blinddown/blindup respectively. - # - # You can change the behaviour with various options, see - # http://script.aculo.us for more documentation. - def visual_effect(name, element_id = false, js_options = {}) - element = element_id ? ActiveSupport::JSON.encode(element_id) : "element" - - js_options[:queue] = if js_options[:queue].is_a?(Hash) - '{' + js_options[:queue].map {|k, v| k == :limit ? "#{k}:#{v}" : "#{k}:'#{v}'" }.join(',') + '}' - elsif js_options[:queue] - "'#{js_options[:queue]}'" - end if js_options[:queue] - - [:endcolor, :direction, :startcolor, :scaleMode, :restorecolor].each do |option| - js_options[option] = "'#{js_options[option]}'" if js_options[option] - end - - if TOGGLE_EFFECTS.include? name.to_sym - "Effect.toggle(#{element},'#{name.to_s.gsub(/^toggle_/,'')}',#{options_for_javascript(js_options)});" - else - "new Effect.#{name.to_s.camelize}(#{element},#{options_for_javascript(js_options)});" - end - end - - # Makes the element with the DOM ID specified by +element_id+ sortable - # by drag-and-drop and make an Ajax call whenever the sort order has - # changed. By default, the action called gets the serialized sortable - # element as parameters. - # - # Example: - # - # <%= sortable_element("my_list", :url => { :action => "order" }) %> - # - # In the example, the action gets a "my_list" array parameter - # containing the values of the ids of elements the sortable consists - # of, in the current order. - # - # Important: For this to work, the sortable elements must have id - # attributes in the form "string_identifier". For example, "item_1". Only - # the identifier part of the id attribute will be serialized. - # - # Additional +options+ are: - # - # * :format - A regular expression to determine what to send as the - # serialized id to the server (the default is /^[^_]*_(.*)$/). - # - # * :constraint - Whether to constrain the dragging to either - # :horizontal or :vertical (or false to make it unconstrained). - # - # * :overlap - Calculate the item overlap in the :horizontal - # or :vertical direction. - # - # * :tag - Which children of the container element to treat as - # sortable (default is li). - # - # * :containment - Takes an element or array of elements to treat as - # potential drop targets (defaults to the original target element). - # - # * :only - A CSS class name or array of class names used to filter - # out child elements as candidates. - # - # * :scroll - Determines whether to scroll the list during drag - # operations if the list runs past the visual border. - # - # * :tree - Determines whether to treat nested lists as part of the - # main sortable list. This means that you can create multi-layer lists, - # and not only sort items at the same level, but drag and sort items - # between levels. - # - # * :hoverclass - If set, the Droppable will have this additional CSS class - # when an accepted Draggable is hovered over it. - # - # * :handle - Sets whether the element should only be draggable by an - # embedded handle. The value may be a string referencing a CSS class value - # (as of script.aculo.us V1.5). The first child/grandchild/etc. element - # found within the element that has this CSS class value will be used as - # the handle. - # - # * :ghosting - Clones the element and drags the clone, leaving - # the original in place until the clone is dropped (default is false). - # - # * :dropOnEmpty - If true the Sortable container will be made into - # a Droppable, that can receive a Draggable (as according to the containment - # rules) as a child element when there are no more elements inside (default - # is false). - # - # * :onChange - Called whenever the sort order changes while dragging. When - # dragging from one Sortable to another, the callback is called once on each - # Sortable. Gets the affected element as its parameter. - # - # * :onUpdate - Called when the drag ends and the Sortable's order is - # changed in any way. When dragging from one Sortable to another, the callback - # is called once on each Sortable. Gets the container as its parameter. - # - # See http://script.aculo.us for more documentation. - def sortable_element(element_id, options = {}) - javascript_tag(sortable_element_js(element_id, options).chop!) - end - - def sortable_element_js(element_id, options = {}) #:nodoc: - options[:with] ||= "Sortable.serialize(#{ActiveSupport::JSON.encode(element_id)})" - options[:onUpdate] ||= "function(){" + remote_function(options) + "}" - options.delete_if { |key, value| PrototypeHelper::AJAX_OPTIONS.include?(key) } - - [:tag, :overlap, :constraint, :handle].each do |option| - options[option] = "'#{options[option]}'" if options[option] - end - - options[:containment] = array_or_string_for_javascript(options[:containment]) if options[:containment] - options[:only] = array_or_string_for_javascript(options[:only]) if options[:only] - - %(Sortable.create(#{ActiveSupport::JSON.encode(element_id)}, #{options_for_javascript(options)});) - end - - # Makes the element with the DOM ID specified by +element_id+ draggable. - # - # Example: - # <%= draggable_element("my_image", :revert => true) - # - # You can change the behaviour with various options, see - # http://script.aculo.us for more documentation. - def draggable_element(element_id, options = {}) - javascript_tag(draggable_element_js(element_id, options).chop!) - end - - def draggable_element_js(element_id, options = {}) #:nodoc: - %(new Draggable(#{ActiveSupport::JSON.encode(element_id)}, #{options_for_javascript(options)});) - end - - # Makes the element with the DOM ID specified by +element_id+ receive - # dropped draggable elements (created by +draggable_element+). - # and make an AJAX call. By default, the action called gets the DOM ID - # of the element as parameter. - # - # Example: - # <%= drop_receiving_element("my_cart", :url => - # { :controller => "cart", :action => "add" }) %> - # - # You can change the behaviour with various options, see - # http://script.aculo.us for more documentation. - # - # Some of these +options+ include: - # * :accept - Set this to a string or an array of strings describing the - # allowable CSS classes that the +draggable_element+ must have in order - # to be accepted by this +drop_receiving_element+. - # - # * :confirm - Adds a confirmation dialog. Example: - # - # :confirm => "Are you sure you want to do this?" - # - # * :hoverclass - If set, the +drop_receiving_element+ will have - # this additional CSS class when an accepted +draggable_element+ is - # hovered over it. - # - # * :onDrop - Called when a +draggable_element+ is dropped onto - # this element. Override this callback with a JavaScript expression to - # change the default drop behaviour. Example: - # - # :onDrop => "function(draggable_element, droppable_element, event) { alert('I like bananas') }" - # - # This callback gets three parameters: The Draggable element, the Droppable - # element and the Event object. You can extract additional information about - # the drop - like if the Ctrl or Shift keys were pressed - from the Event object. - # - # * :with - A JavaScript expression specifying the parameters for - # the XMLHttpRequest. Any expressions should return a valid URL query string. - def drop_receiving_element(element_id, options = {}) - javascript_tag(drop_receiving_element_js(element_id, options).chop!) - end - - def drop_receiving_element_js(element_id, options = {}) #:nodoc: - options[:with] ||= "'id=' + encodeURIComponent(element.id)" - options[:onDrop] ||= "function(element){" + remote_function(options) + "}" - options.delete_if { |key, value| PrototypeHelper::AJAX_OPTIONS.include?(key) } - - options[:accept] = array_or_string_for_javascript(options[:accept]) if options[:accept] - options[:hoverclass] = "'#{options[:hoverclass]}'" if options[:hoverclass] - - # Confirmation happens during the onDrop callback, so it can be removed from the options - options.delete(:confirm) if options[:confirm] - - %(Droppables.add(#{ActiveSupport::JSON.encode(element_id)}, #{options_for_javascript(options)});) - end - - protected - def array_or_string_for_javascript(option) - if option.kind_of?(Array) - "['#{option.join('\',\'')}']" - elsif !option.nil? - "'#{option}'" - end - end - end - - module PrototypeHelper - class JavaScriptGenerator - module GeneratorMethods - # Starts a script.aculo.us visual effect. See - # ActionView::Helpers::ScriptaculousHelper for more information. - def visual_effect(name, id = nil, options = {}) - record @context.send(:visual_effect, name, id, options) - end - - # Creates a script.aculo.us sortable element. Useful - # to recreate sortable elements after items get added - # or deleted. - # See ActionView::Helpers::ScriptaculousHelper for more information. - def sortable(id, options = {}) - record @context.send(:sortable_element_js, id, options) - end - - # Creates a script.aculo.us draggable element. - # See ActionView::Helpers::ScriptaculousHelper for more information. - def draggable(id, options = {}) - record @context.send(:draggable_element_js, id, options) - end - - # Creates a script.aculo.us drop receiving element. - # See ActionView::Helpers::ScriptaculousHelper for more information. - def drop_receiving(id, options = {}) - record @context.send(:drop_receiving_element_js, id, options) - end - end - end - end - end -end -- cgit v1.2.3 From 90de26703e6e5243ebb5fffc513780f8c69c71b4 Mon Sep 17 00:00:00 2001 From: Xavier Noria Date: Sat, 26 Mar 2011 22:25:32 +0100 Subject: removes ActionView::Helpers::PrototypeHelper --- actionpack/lib/action_view/helpers.rb | 2 - .../lib/action_view/helpers/javascript_helper.rb | 2 - .../lib/action_view/helpers/prototype_helper.rb | 852 --------------------- 3 files changed, 856 deletions(-) delete mode 100644 actionpack/lib/action_view/helpers/prototype_helper.rb (limited to 'actionpack/lib') diff --git a/actionpack/lib/action_view/helpers.rb b/actionpack/lib/action_view/helpers.rb index 88aafc82d5..205116f610 100644 --- a/actionpack/lib/action_view/helpers.rb +++ b/actionpack/lib/action_view/helpers.rb @@ -17,7 +17,6 @@ module ActionView #:nodoc: autoload :FormTagHelper autoload :JavaScriptHelper, "action_view/helpers/javascript_helper" autoload :NumberHelper - autoload :PrototypeHelper autoload :OutputSafetyHelper autoload :RecordTagHelper autoload :SanitizeHelper @@ -47,7 +46,6 @@ module ActionView #:nodoc: include FormTagHelper include JavaScriptHelper include NumberHelper - include PrototypeHelper include OutputSafetyHelper include RecordTagHelper include SanitizeHelper diff --git a/actionpack/lib/action_view/helpers/javascript_helper.rb b/actionpack/lib/action_view/helpers/javascript_helper.rb index ea8f31c45b..d7228bab67 100644 --- a/actionpack/lib/action_view/helpers/javascript_helper.rb +++ b/actionpack/lib/action_view/helpers/javascript_helper.rb @@ -3,8 +3,6 @@ require 'action_view/helpers/tag_helper' module ActionView module Helpers module JavaScriptHelper - include PrototypeHelper - JS_ESCAPE_MAP = { '\\' => '\\\\', ' '<\/', diff --git a/actionpack/lib/action_view/helpers/prototype_helper.rb b/actionpack/lib/action_view/helpers/prototype_helper.rb deleted file mode 100644 index 506db24dc2..0000000000 --- a/actionpack/lib/action_view/helpers/prototype_helper.rb +++ /dev/null @@ -1,852 +0,0 @@ -require 'set' -require 'active_support/json' -require 'active_support/core_ext/object/blank' -require 'active_support/core_ext/string/output_safety' - -module ActionView - # = Action View Prototype Helpers - module Helpers - # Prototype[http://www.prototypejs.org/] is a JavaScript library that provides - # DOM[http://en.wikipedia.org/wiki/Document_Object_Model] manipulation, - # Ajax[http://www.adaptivepath.com/publications/essays/archives/000385.php] - # functionality, and more traditional object-oriented facilities for JavaScript. - # This module provides a set of helpers to make it more convenient to call - # functions from Prototype using Rails, including functionality to call remote - # Rails methods (that is, making a background request to a Rails action) using Ajax. - # This means that you can call actions in your controllers without - # reloading the page, but still update certain parts of it using - # injections into the DOM. A common use case is having a form that adds - # a new element to a list without reloading the page or updating a shopping - # cart total when a new item is added. - # - # == Usage - # To be able to use these helpers, you must first include the Prototype - # JavaScript framework in your pages. - # - # javascript_include_tag 'prototype' - # - # (See the documentation for - # ActionView::Helpers::JavaScriptHelper for more information on including - # this and other JavaScript files in your Rails templates.) - # - # Now you're ready to call a remote action either through a link... - # - # link_to_remote "Add to cart", - # :url => { :action => "add", :id => product.id }, - # :update => { :success => "cart", :failure => "error" } - # - # ...through a form... - # - # <%= form_remote_tag :url => '/shipping' do -%> - #
<%= submit_tag 'Recalculate Shipping' %>
- # <% end -%> - # - # As you can see, there are numerous ways to use Prototype's Ajax functions (and actually more than - # are listed here); check out the documentation for each method to find out more about its usage and options. - # - # === Common Options - # See link_to_remote for documentation of options common to all Ajax - # helpers; any of the options specified by link_to_remote can be used - # by the other helpers. - # - # == Designing your Rails actions for Ajax - # When building your action handlers (that is, the Rails actions that receive your background requests), it's - # important to remember a few things. First, whatever your action would normally return to the browser, it will - # return to the Ajax call. As such, you typically don't want to render with a layout. This call will cause - # the layout to be transmitted back to your page, and, if you have a full HTML/CSS, will likely mess a lot of things up. - # You can turn the layout off on particular actions by doing the following: - # - # class SiteController < ActionController::Base - # layout "standard", :except => [:ajax_method, :more_ajax, :another_ajax] - # end - # - # Optionally, you could do this in the method you wish to lack a layout: - # - # render :layout => false - # - # You can tell the type of request from within your action using the request.xhr? (XmlHttpRequest, the - # method that Ajax uses to make background requests) method. - # def name - # # Is this an XmlHttpRequest request? - # if (request.xhr?) - # render :text => @name.to_s - # else - # # No? Then render an action. - # render :action => 'view_attribute', :attr => @name - # end - # end - # - # The else clause can be left off and the current action will render with full layout and template. An extension - # to this solution was posted to Ryan Heneise's blog at ArtOfMission["http://www.artofmission.com/"]. - # - # layout proc{ |c| c.request.xhr? ? false : "application" } - # - # Dropping this in your ApplicationController turns the layout off for every request that is an "xhr" request. - # - # If you are just returning a little data or don't want to build a template for your output, you may opt to simply - # render text output, like this: - # - # render :text => 'Return this from my method!' - # - # Since whatever the method returns is injected into the DOM, this will simply inject some text (or HTML, if you - # tell it to). This is usually how small updates, such updating a cart total or a file count, are handled. - # - # == Updating multiple elements - # See JavaScriptGenerator for information on updating multiple elements - # on the page in an Ajax response. - module PrototypeHelper - CALLBACKS = Set.new([ :create, :uninitialized, :loading, :loaded, - :interactive, :complete, :failure, :success ] + - (100..599).to_a) - AJAX_OPTIONS = Set.new([ :before, :after, :condition, :url, - :asynchronous, :method, :insertion, :position, - :form, :with, :update, :script, :type ]).merge(CALLBACKS) - - # Returns the JavaScript needed for a remote function. - # See the link_to_remote documentation at https://github.com/rails/prototype_legacy_helper as it takes the same arguments. - # - # Example: - # # Generates: { :action => :update_options }) %>"> - # - # - # - def remote_function(options) - javascript_options = options_for_ajax(options) - - update = '' - if options[:update] && options[:update].is_a?(Hash) - update = [] - update << "success:'#{options[:update][:success]}'" if options[:update][:success] - update << "failure:'#{options[:update][:failure]}'" if options[:update][:failure] - update = '{' + update.join(',') + '}' - elsif options[:update] - update << "'#{options[:update]}'" - end - - function = update.empty? ? - "new Ajax.Request(" : - "new Ajax.Updater(#{update}, " - - url_options = options[:url] - function << "'#{ERB::Util.html_escape(escape_javascript(url_for(url_options)))}'" - function << ", #{javascript_options})" - - function = "#{options[:before]}; #{function}" if options[:before] - function = "#{function}; #{options[:after]}" if options[:after] - function = "if (#{options[:condition]}) { #{function}; }" if options[:condition] - function = "if (confirm('#{escape_javascript(options[:confirm])}')) { #{function}; }" if options[:confirm] - - return function.html_safe - end - - # All the methods were moved to GeneratorMethods so that - # #include_helpers_from_context has nothing to overwrite. - class JavaScriptGenerator #:nodoc: - def initialize(context, &block) #:nodoc: - @context, @lines = context, [] - include_helpers_from_context - @context.with_output_buffer(@lines) do - @context.instance_exec(self, &block) - end - end - - private - def include_helpers_from_context - extend @context.helpers if @context.respond_to?(:helpers) - extend GeneratorMethods - end - - # JavaScriptGenerator generates blocks of JavaScript code that allow you - # to change the content and presentation of multiple DOM elements. Use - # this in your Ajax response bodies, either in a \