aboutsummaryrefslogtreecommitdiffstats
path: root/actionpack/lib
diff options
context:
space:
mode:
Diffstat (limited to 'actionpack/lib')
-rw-r--r--actionpack/lib/action_controller/caching/fragments.rb4
-rw-r--r--actionpack/lib/action_controller/caching/pages.rb11
-rw-r--r--actionpack/lib/action_controller/metal/helpers.rb2
-rw-r--r--actionpack/lib/action_controller/metal/request_forgery_protection.rb13
-rw-r--r--actionpack/lib/action_controller/metal/responder.rb2
-rw-r--r--actionpack/lib/action_controller/metal/streaming.rb43
-rw-r--r--actionpack/lib/action_dispatch/routing/polymorphic_routes.rb4
-rw-r--r--actionpack/lib/action_view/asset_paths.rb3
-rw-r--r--actionpack/lib/action_view/base.rb2
-rw-r--r--actionpack/lib/action_view/helpers/asset_tag_helper.rb2
-rw-r--r--actionpack/lib/action_view/helpers/asset_tag_helpers/asset_paths.rb1
-rw-r--r--actionpack/lib/action_view/helpers/atom_feed_helper.rb2
-rw-r--r--actionpack/lib/action_view/helpers/form_helper.rb25
-rw-r--r--actionpack/lib/sprockets/compressors.rb21
-rw-r--r--actionpack/lib/sprockets/helpers/rails_helper.rb45
-rw-r--r--actionpack/lib/sprockets/railtie.rb10
16 files changed, 96 insertions, 94 deletions
diff --git a/actionpack/lib/action_controller/caching/fragments.rb b/actionpack/lib/action_controller/caching/fragments.rb
index 2bdb23c4d7..abeb49d16f 100644
--- a/actionpack/lib/action_controller/caching/fragments.rb
+++ b/actionpack/lib/action_controller/caching/fragments.rb
@@ -12,13 +12,13 @@ module ActionController #:nodoc:
#
# <% cache do %>
# All the topics in the system:
- # <%= render :partial => "topic", :collection => Topic.find(:all) %>
+ # <%= render :partial => "topic", :collection => Topic.all %>
# <% end %>
#
# This cache will bind the name of the action that called it, so if
# this code was part of the view for the topics/list action, you
# would be able to invalidate it using:
- #
+ #
# expire_fragment(:controller => "topics", :action => "list")
#
# This default behavior is limited if you need to cache multiple
diff --git a/actionpack/lib/action_controller/caching/pages.rb b/actionpack/lib/action_controller/caching/pages.rb
index 8c583c7ce0..496390402b 100644
--- a/actionpack/lib/action_controller/caching/pages.rb
+++ b/actionpack/lib/action_controller/caching/pages.rb
@@ -16,9 +16,10 @@ module ActionController #:nodoc:
# caches_page :show, :new
# end
#
- # This will generate cache files such as <tt>weblog/show/5.html</tt> and <tt>weblog/new.html</tt>,
- # which match the URLs used to trigger the dynamic generation. This is how the web server is able
- # pick up a cache file when it exists and otherwise let the request pass on to Action Pack to generate it.
+ # This will generate cache files such as <tt>weblog/show/5.html</tt> and <tt>weblog/new.html</tt>, which match the URLs used
+ # that would normally trigger dynamic page generation. Page caching works by configuring a web server to first check for the
+ # existence of files on disk, and to serve them directly when found, without passing the request through to Action Pack.
+ # This is much faster than handling the full dynamic request in the usual way.
#
# Expiration of the cache is handled by deleting the cached file, which results in a lazy regeneration approach where the cache
# is not restored before another hit is made against it. The API for doing so mimics the options from +url_for+ and friends:
@@ -132,8 +133,8 @@ module ActionController #:nodoc:
end
end
- # Manually cache the +content+ in the key determined by +options+. If no content is provided, the contents of response.body is used
- # If no options are provided, the requested url is used. Example:
+ # Manually cache the +content+ in the key determined by +options+. If no content is provided, the contents of response.body is used.
+ # If no options are provided, the url of the current request being handled is used. Example:
# cache_page "I'm the cached content", :controller => "lists", :action => "show"
def cache_page(content = nil, options = nil)
return unless self.class.perform_caching && caching_allowed?
diff --git a/actionpack/lib/action_controller/metal/helpers.rb b/actionpack/lib/action_controller/metal/helpers.rb
index 75757db564..2df0e9422c 100644
--- a/actionpack/lib/action_controller/metal/helpers.rb
+++ b/actionpack/lib/action_controller/metal/helpers.rb
@@ -29,7 +29,7 @@ module ActionController
# class EventsController < ActionController::Base
# helper FormattedTimeHelper
# def index
- # @events = Event.find(:all)
+ # @events = Event.all
# end
# end
#
diff --git a/actionpack/lib/action_controller/metal/request_forgery_protection.rb b/actionpack/lib/action_controller/metal/request_forgery_protection.rb
index 2080e9b5b9..2271470334 100644
--- a/actionpack/lib/action_controller/metal/request_forgery_protection.rb
+++ b/actionpack/lib/action_controller/metal/request_forgery_protection.rb
@@ -7,17 +7,16 @@ module ActionController #:nodoc:
# Controller actions are protected from Cross-Site Request Forgery (CSRF) attacks
# by including a token in the rendered html for your application. This token is
# stored as a random string in the session, to which an attacker does not have
- # access. When a request reaches your application, \Rails then verifies the received
- # token with the token in the session. Only HTML and javascript requests are checked,
+ # access. When a request reaches your application, \Rails verifies the received
+ # token with the token in the session. Only HTML and JavaScript requests are checked,
# so this will not protect your XML API (presumably you'll have a different
# authentication scheme there anyway). Also, GET requests are not protected as these
# should be idempotent.
#
# CSRF protection is turned on with the <tt>protect_from_forgery</tt> method,
- # which will check the token and raise an ActionController::InvalidAuthenticityToken
- # if it doesn't match what was expected. A call to this method is generated for new
- # \Rails applications by default. You can customize the error message by editing
- # public/422.html.
+ # which checks the token and resets the session if it doesn't match what was expected.
+ # A call to this method is generated for new \Rails applications by default.
+ # You can customize the error message by editing public/422.html.
#
# The token parameter is named <tt>authenticity_token</tt> by default. The name and
# value of this token must be added to every layout that renders forms by including
@@ -79,6 +78,8 @@ module ActionController #:nodoc:
end
end
+ # This is the method that defines the application behaviour when a request is found to be unverified.
+ # By default, \Rails resets the session when it finds an unverified request.
def handle_unverified_request
reset_session
end
diff --git a/actionpack/lib/action_controller/metal/responder.rb b/actionpack/lib/action_controller/metal/responder.rb
index f3b7357e64..3794e277f6 100644
--- a/actionpack/lib/action_controller/metal/responder.rb
+++ b/actionpack/lib/action_controller/metal/responder.rb
@@ -9,7 +9,7 @@ module ActionController #:nodoc:
# respond_to :html, :xml, :json
#
# def index
- # @people = Person.find(:all)
+ # @people = Person.all
# respond_with(@people)
# end
# end
diff --git a/actionpack/lib/action_controller/metal/streaming.rb b/actionpack/lib/action_controller/metal/streaming.rb
index 0bb436a476..5fe5334458 100644
--- a/actionpack/lib/action_controller/metal/streaming.rb
+++ b/actionpack/lib/action_controller/metal/streaming.rb
@@ -24,20 +24,8 @@ module ActionController #:nodoc:
#
# == Examples
#
- # Streaming can be added to a controller easily, all you need to do is
- # call +stream+ in the controller class:
- #
- # class PostsController
- # stream
- # end
- #
- # The +stream+ method accepts the same options as +before_filter+ and friends:
- #
- # class PostsController
- # stream :only => :index
- # end
- #
- # You can also selectively turn on streaming for specific actions:
+ # Streaming can be added to a given template easily, all you need to do is
+ # to pass the :stream option.
#
# class PostsController
# def index
@@ -72,6 +60,9 @@ module ActionController #:nodoc:
# render :stream => true
# end
#
+ # Notice that :stream only works with templates. Rendering :json
+ # or :xml with :stream won't work.
+ #
# == Communication between layout and template
#
# When streaming, rendering happens top-down instead of inside-out.
@@ -209,33 +200,9 @@ module ActionController #:nodoc:
extend ActiveSupport::Concern
include AbstractController::Rendering
- attr_internal :stream
-
- module ClassMethods
- # Render streaming templates. It accepts :only, :except, :if and :unless as options
- # to specify when to stream, as in ActionController filters.
- def stream(options={})
- if defined?(Fiber)
- before_filter :_stream_filter, options
- else
- raise "You cannot use streaming if Fiber is not available."
- end
- end
- end
protected
- # Mark following render calls as streaming.
- def _stream_filter #:nodoc:
- self.stream = true
- end
-
- # Consider the stream option when normalazing options.
- def _normalize_options(options) #:nodoc:
- super
- options[:stream] = self.stream unless options.key?(:stream)
- end
-
# Set proper cache control and transfer encoding when streaming
def _process_options(options) #:nodoc:
super
diff --git a/actionpack/lib/action_dispatch/routing/polymorphic_routes.rb b/actionpack/lib/action_dispatch/routing/polymorphic_routes.rb
index 82c4fadb50..49aef0bf72 100644
--- a/actionpack/lib/action_dispatch/routing/polymorphic_routes.rb
+++ b/actionpack/lib/action_dispatch/routing/polymorphic_routes.rb
@@ -182,10 +182,12 @@ module ActionDispatch
if record.is_a?(Symbol) || record.is_a?(String)
route << record
- else
+ elsif record
route << ActiveModel::Naming.route_key(record)
route = [route.join("_").singularize] if inflection == :singular
route << "index" if ActiveModel::Naming.uncountable?(record) && inflection == :plural
+ else
+ raise ArgumentError, "Nil location provided. Can't build URI."
end
route << routing_type(options)
diff --git a/actionpack/lib/action_view/asset_paths.rb b/actionpack/lib/action_view/asset_paths.rb
index 4dd02755d3..96d8fd0dfe 100644
--- a/actionpack/lib/action_view/asset_paths.rb
+++ b/actionpack/lib/action_view/asset_paths.rb
@@ -1,3 +1,4 @@
+require 'zlib'
require 'active_support/core_ext/file'
module ActionView
@@ -111,7 +112,7 @@ module ActionView
args << current_request if (arity > 1 || arity < 0) && has_request?
host.call(*args)
else
- (host =~ /%d/) ? host % (source.hash % 4) : host
+ (host =~ /%d/) ? host % (Zlib.crc32(source) % 4) : host
end
end
end
diff --git a/actionpack/lib/action_view/base.rb b/actionpack/lib/action_view/base.rb
index fd2970b8e2..36a0a20066 100644
--- a/actionpack/lib/action_view/base.rb
+++ b/actionpack/lib/action_view/base.rb
@@ -194,7 +194,7 @@ module ActionView #:nodoc:
end
def initialize(context = nil, assigns = {}, controller = nil, formats = nil) #:nodoc:
- @_config = {}
+ @_config = ActiveSupport::InheritableOptions.new
# Handle all these for backwards compatibility.
# TODO Provide a new API for AV::Base and deprecate this one.
diff --git a/actionpack/lib/action_view/helpers/asset_tag_helper.rb b/actionpack/lib/action_view/helpers/asset_tag_helper.rb
index 0c3f011c92..509c29844a 100644
--- a/actionpack/lib/action_view/helpers/asset_tag_helper.rb
+++ b/actionpack/lib/action_view/helpers/asset_tag_helper.rb
@@ -1,6 +1,7 @@
require 'action_view/helpers/asset_tag_helpers/javascript_tag_helpers'
require 'action_view/helpers/asset_tag_helpers/stylesheet_tag_helpers'
require 'action_view/helpers/asset_tag_helpers/asset_paths'
+require 'action_view/helpers/tag_helper'
module ActionView
# = Action View Asset Tag Helpers
@@ -191,6 +192,7 @@ module ActionView
# RewriteEngine On
# RewriteRule ^/release-\d+/(images|javascripts|stylesheets)/(.*)$ /$1/$2 [L]
module AssetTagHelper
+ include TagHelper
include JavascriptTagHelpers
include StylesheetTagHelpers
# Returns a link tag that browsers and news readers can use to auto-detect
diff --git a/actionpack/lib/action_view/helpers/asset_tag_helpers/asset_paths.rb b/actionpack/lib/action_view/helpers/asset_tag_helpers/asset_paths.rb
index 12a304b395..8b35aa8896 100644
--- a/actionpack/lib/action_view/helpers/asset_tag_helpers/asset_paths.rb
+++ b/actionpack/lib/action_view/helpers/asset_tag_helpers/asset_paths.rb
@@ -1,3 +1,4 @@
+require 'thread'
require 'active_support/core_ext/file'
module ActionView
diff --git a/actionpack/lib/action_view/helpers/atom_feed_helper.rb b/actionpack/lib/action_view/helpers/atom_feed_helper.rb
index a087688a2c..39c37b25dc 100644
--- a/actionpack/lib/action_view/helpers/atom_feed_helper.rb
+++ b/actionpack/lib/action_view/helpers/atom_feed_helper.rb
@@ -20,7 +20,7 @@ module ActionView
# # GET /posts.html
# # GET /posts.atom
# def index
- # @posts = Post.find(:all)
+ # @posts = Post.all
#
# respond_to do |format|
# format.html
diff --git a/actionpack/lib/action_view/helpers/form_helper.rb b/actionpack/lib/action_view/helpers/form_helper.rb
index 41a503982a..3a30263b49 100644
--- a/actionpack/lib/action_view/helpers/form_helper.rb
+++ b/actionpack/lib/action_view/helpers/form_helper.rb
@@ -4,6 +4,7 @@ require 'action_view/helpers/tag_helper'
require 'action_view/helpers/form_tag_helper'
require 'active_support/core_ext/class/attribute'
require 'active_support/core_ext/hash/slice'
+require 'active_support/core_ext/module/method_names'
require 'active_support/core_ext/object/blank'
require 'active_support/core_ext/string/output_safety'
require 'active_support/core_ext/array/extract_options'
@@ -567,18 +568,13 @@ module ActionView
# ...
# <% end %>
#
- # In addition, you may want to have access to the current iteration index.
- # In that case, you can use a similar method called fields_for_with_index
- # which receives a block with an extra parameter:
+ # When projects is already an association on Person you can use
+ # +accepts_nested_attributes_for+ to define the writer method for you:
#
- # <%= form_for @person do |person_form| %>
- # ...
- # <%= person_form.fields_for_with_index :projects do |project_fields, index| %>
- # Position: <%= index %>
- # Name: <%= project_fields.text_field :name %>
- # <% end %>
- # ...
- # <% end %>
+ # class Person < ActiveRecord::Base
+ # has_many :projects
+ # accepts_nested_attributes_for :projects
+ # end
#
# If you want to destroy any of the associated models through the
# form, you have to enable it first using the <tt>:allow_destroy</tt>
@@ -1233,13 +1229,6 @@ module ActionView
RUBY_EVAL
end
- # Check +fields_for+ for docs and examples.
- def fields_for_with_index(record_name, record_object = nil, fields_options = {}, &block)
- index = fields_options[:index] || options[:child_index] || nested_child_index(@object_name)
- block_with_index = Proc.new{ |obj| block.call(obj, index) }
- fields_for(record_name, record_object, fields_options, &block_with_index)
- end
-
def fields_for(record_name, record_object = nil, fields_options = {}, &block)
fields_options, record_object = record_object, nil if record_object.is_a?(Hash) && record_object.extractable_options?
fields_options[:builder] ||= options[:builder]
diff --git a/actionpack/lib/sprockets/compressors.rb b/actionpack/lib/sprockets/compressors.rb
new file mode 100644
index 0000000000..6544953df4
--- /dev/null
+++ b/actionpack/lib/sprockets/compressors.rb
@@ -0,0 +1,21 @@
+module Sprockets
+ class NullCompressor
+ def compress(content)
+ content
+ end
+ end
+
+ class LazyCompressor
+ def initialize(&block)
+ @block = block
+ end
+
+ def compressor
+ @compressor ||= @block.call || NullCompressor.new
+ end
+
+ def compress(content)
+ compressor.compress(content)
+ end
+ end
+end \ No newline at end of file
diff --git a/actionpack/lib/sprockets/helpers/rails_helper.rb b/actionpack/lib/sprockets/helpers/rails_helper.rb
index 63820cc76c..a95e7c0649 100644
--- a/actionpack/lib/sprockets/helpers/rails_helper.rb
+++ b/actionpack/lib/sprockets/helpers/rails_helper.rb
@@ -1,4 +1,4 @@
-require "action_view/helpers"
+require "action_view"
module Sprockets
module Helpers
@@ -9,14 +9,12 @@ module Sprockets
def asset_paths
@asset_paths ||= begin
config = self.config if respond_to?(:config)
+ config ||= Rails.application.config
controller = self.controller if respond_to?(:controller)
- config ||= Rails.application.config
- if config.action_controller.present?
- config.action_controller.default_asset_host_protocol ||= :relative
- else
- config.default_asset_host_protocol ||= :relative
- end
- RailsHelper::AssetPaths.new(config, controller)
+ paths = RailsHelper::AssetPaths.new(config, controller)
+ paths.asset_environment = asset_environment
+ paths.asset_prefix = asset_prefix
+ paths
end
end
@@ -76,9 +74,28 @@ module Sprockets
params[:debug_assets] == 'true'
end
+ # Override to specify an alternative prefix for asset path generation.
+ # When combined with a custom +asset_environment+, this can be used to
+ # implement themes that can take advantage of the asset pipeline.
+ #
+ # If you only want to change where the assets are mounted, refer to
+ # +config.assets.prefix+ instead.
+ def asset_prefix
+ Rails.application.config.assets.prefix
+ end
+
+ # Override to specify an alternative asset environment for asset
+ # path generation. The environment should already have been mounted
+ # at the prefix returned by +asset_prefix+.
+ def asset_environment
+ Rails.application.assets
+ end
+
class AssetPaths < ::ActionView::AssetPaths #:nodoc:
- def compute_public_path(source, dir, ext=nil, include_host=true, protocol = nil)
- super(source, Rails.application.config.assets.prefix, ext, include_host, protocol)
+ attr_accessor :asset_environment, :asset_prefix
+
+ def compute_public_path(source, dir, ext=nil, include_host=true, protocol=nil)
+ super(source, asset_prefix, ext, include_host, protocol)
end
# Return the filesystem path for the source
@@ -90,14 +107,14 @@ module Sprockets
source = source.to_s
return nil if is_uri?(source)
source = rewrite_extension(source, nil, ext)
- assets[source]
+ asset_environment[source]
end
def rewrite_asset_path(source, dir)
if source[0] == ?/
source
else
- assets.path(source, performing_caching?, dir)
+ asset_environment.path(source, performing_caching?, dir)
end
end
@@ -109,10 +126,6 @@ module Sprockets
end
end
- def assets
- Rails.application.assets
- end
-
# When included in Sprockets::Context, we need to ask the top-level config as the controller is not available
def performing_caching?
config.action_controller.present? ? config.action_controller.perform_caching : config.perform_caching
diff --git a/actionpack/lib/sprockets/railtie.rb b/actionpack/lib/sprockets/railtie.rb
index d1e97c594e..c28bdc3061 100644
--- a/actionpack/lib/sprockets/railtie.rb
+++ b/actionpack/lib/sprockets/railtie.rb
@@ -1,8 +1,12 @@
module Sprockets
autoload :Helpers, "sprockets/helpers"
+ autoload :LazyCompressor, "sprockets/compressors"
+ autoload :NullCompressor, "sprockets/compressors"
# TODO: Get rid of config.assets.enabled
class Railtie < ::Rails::Railtie
+ config.default_asset_host_protocol = :relative
+
rake_tasks do
load "sprockets/assets.rake"
end
@@ -60,14 +64,14 @@ module Sprockets
env.logger = Rails.logger
if env.respond_to?(:cache)
- env.cache = Rails.cache
+ env.cache = assets.cache_store || Rails.cache
end
if assets.compress
# temporarily hardcode default JS compressor to uglify. Soon, it will work
# the same as SCSS, where a default plugin sets the default.
- env.js_compressor = expand_js_compressor(assets.js_compressor || :uglifier)
- env.css_compressor = expand_css_compressor(assets.css_compressor)
+ env.js_compressor = LazyCompressor.new { expand_js_compressor(assets.js_compressor || :uglifier) }
+ env.css_compressor = LazyCompressor.new { expand_css_compressor(assets.css_compressor) }
end
env