aboutsummaryrefslogtreecommitdiffstats
path: root/actionpack/lib
diff options
context:
space:
mode:
Diffstat (limited to 'actionpack/lib')
-rw-r--r--actionpack/lib/action_controller/base.rb2
-rw-r--r--actionpack/lib/action_controller/log_subscriber.rb56
-rw-r--r--actionpack/lib/action_controller/metal.rb2
-rw-r--r--actionpack/lib/action_controller/metal/responder.rb18
-rw-r--r--actionpack/lib/action_controller/railtie.rb3
-rw-r--r--actionpack/lib/action_controller/railties/log_subscriber.rb56
-rw-r--r--actionpack/lib/action_controller/test_case.rb18
-rw-r--r--actionpack/lib/action_dispatch/http/request.rb5
-rw-r--r--actionpack/lib/action_dispatch/middleware/flash.rb11
-rw-r--r--actionpack/lib/action_dispatch/middleware/session/abstract_store.rb149
-rw-r--r--actionpack/lib/action_dispatch/middleware/session/cookie_store.rb43
-rw-r--r--actionpack/lib/action_dispatch/middleware/session/mem_cache_store.rb9
-rw-r--r--actionpack/lib/action_dispatch/routing/mapper.rb83
-rw-r--r--actionpack/lib/action_dispatch/routing/route_set.rb31
-rw-r--r--actionpack/lib/action_dispatch/testing/assertions/routing.rb1
-rw-r--r--actionpack/lib/action_dispatch/testing/assertions/selector.rb42
-rw-r--r--actionpack/lib/action_view/base.rb7
-rw-r--r--actionpack/lib/action_view/helpers/active_model_helper.rb18
-rw-r--r--actionpack/lib/action_view/helpers/asset_tag_helper.rb47
-rw-r--r--actionpack/lib/action_view/helpers/date_helper.rb4
-rw-r--r--actionpack/lib/action_view/helpers/form_helper.rb29
-rw-r--r--actionpack/lib/action_view/helpers/prototype_helper.rb14
-rw-r--r--actionpack/lib/action_view/helpers/scriptaculous_helper.rb6
-rw-r--r--actionpack/lib/action_view/log_subscriber.rb27
-rw-r--r--actionpack/lib/action_view/railtie.rb17
-rw-r--r--actionpack/lib/action_view/railties/log_subscriber.rb27
-rw-r--r--actionpack/lib/action_view/template/resolver.rb2
-rw-r--r--actionpack/lib/action_view/test_case.rb46
28 files changed, 478 insertions, 295 deletions
diff --git a/actionpack/lib/action_controller/base.rb b/actionpack/lib/action_controller/base.rb
index 8611d0d3c3..a70ba0d2e3 100644
--- a/actionpack/lib/action_controller/base.rb
+++ b/actionpack/lib/action_controller/base.rb
@@ -1,3 +1,5 @@
+require "action_controller/log_subscriber"
+
module ActionController
class Base < Metal
abstract!
diff --git a/actionpack/lib/action_controller/log_subscriber.rb b/actionpack/lib/action_controller/log_subscriber.rb
new file mode 100644
index 0000000000..ece270b3ce
--- /dev/null
+++ b/actionpack/lib/action_controller/log_subscriber.rb
@@ -0,0 +1,56 @@
+require 'active_support/core_ext/object/blank'
+
+module ActionController
+ class LogSubscriber < ActiveSupport::LogSubscriber
+ INTERNAL_PARAMS = %w(controller action format _method only_path)
+
+ def start_processing(event)
+ payload = event.payload
+ params = payload[:params].except(*INTERNAL_PARAMS)
+
+ info " Processing by #{payload[:controller]}##{payload[:action]} as #{payload[:formats].first.to_s.upcase}"
+ info " Parameters: #{params.inspect}" unless params.empty?
+ end
+
+ def process_action(event)
+ payload = event.payload
+ additions = ActionController::Base.log_process_action(payload)
+
+ message = "Completed #{payload[:status]} #{Rack::Utils::HTTP_STATUS_CODES[payload[:status]]} in %.0fms" % event.duration
+ message << " (#{additions.join(" | ")})" unless additions.blank?
+
+ info(message)
+ end
+
+ def send_file(event)
+ message = "Sent file %s"
+ message << " (%.1fms)"
+ info(message % [event.payload[:path], event.duration])
+ end
+
+ def redirect_to(event)
+ info "Redirected to #{event.payload[:location]}"
+ end
+
+ def send_data(event)
+ info("Sent data %s (%.1fms)" % [event.payload[:filename], event.duration])
+ end
+
+ %w(write_fragment read_fragment exist_fragment?
+ expire_fragment expire_page write_page).each do |method|
+ class_eval <<-METHOD, __FILE__, __LINE__ + 1
+ def #{method}(event)
+ key_or_path = event.payload[:key] || event.payload[:path]
+ human_name = #{method.to_s.humanize.inspect}
+ info("\#{human_name} \#{key_or_path} (%.1fms)" % event.duration)
+ end
+ METHOD
+ end
+
+ def logger
+ ActionController::Base.logger
+ end
+ end
+end
+
+ActionController::LogSubscriber.attach_to :action_controller \ No newline at end of file
diff --git a/actionpack/lib/action_controller/metal.rb b/actionpack/lib/action_controller/metal.rb
index 159d1f0748..2281c500c5 100644
--- a/actionpack/lib/action_controller/metal.rb
+++ b/actionpack/lib/action_controller/metal.rb
@@ -61,7 +61,7 @@ module ActionController
# ==== Returns
# String
def self.controller_name
- @controller_name ||= controller_path.split("/").last
+ @controller_name ||= self.name.demodulize.sub(/Controller$/, '').underscore
end
# Delegates to the class' #controller_name
diff --git a/actionpack/lib/action_controller/metal/responder.rb b/actionpack/lib/action_controller/metal/responder.rb
index 22bdcd0f3c..cb644dfd16 100644
--- a/actionpack/lib/action_controller/metal/responder.rb
+++ b/actionpack/lib/action_controller/metal/responder.rb
@@ -89,9 +89,7 @@ module ActionController #:nodoc:
def initialize(controller, resources, options={})
@controller = controller
- @request = controller.request
- @format = controller.formats.first
- @resource = resources.is_a?(Array) ? resources.last : resources
+ @resource = resources.last
@resources = resources
@options = options
@action = options.delete(:action)
@@ -101,6 +99,14 @@ module ActionController #:nodoc:
delegate :head, :render, :redirect_to, :to => :controller
delegate :get?, :post?, :put?, :delete?, :to => :request
+ def request
+ @request ||= @controller.request
+ end
+
+ def format
+ @format ||= @controller.formats.first
+ end
+
# Undefine :to_json and :to_yaml since it's defined on Object
undef_method(:to_json) if method_defined?(:to_json)
undef_method(:to_yaml) if method_defined?(:to_yaml)
@@ -147,7 +153,7 @@ module ActionController #:nodoc:
elsif has_errors? && default_action
render :action => default_action
else
- redirect_to resource_location
+ redirect_to navigation_location
end
end
@@ -160,7 +166,7 @@ module ActionController #:nodoc:
elsif has_errors?
display resource.errors, :status => :unprocessable_entity
elsif post?
- display resource, :status => :created, :location => resource_location
+ display resource, :status => :created, :location => api_location
else
head :ok
end
@@ -178,6 +184,8 @@ module ActionController #:nodoc:
def resource_location
options[:location] || resources
end
+ alias :navigation_location :resource_location
+ alias :api_location :resource_location
# If a given response block was given, use it, otherwise call render on
# controller.
diff --git a/actionpack/lib/action_controller/railtie.rb b/actionpack/lib/action_controller/railtie.rb
index 0e3cdffadc..86395c4d93 100644
--- a/actionpack/lib/action_controller/railtie.rb
+++ b/actionpack/lib/action_controller/railtie.rb
@@ -6,7 +6,6 @@ require "active_support/core_ext/class/subclasses"
require "active_support/deprecation/proxy_wrappers"
require "active_support/deprecation"
-require "action_controller/railties/log_subscriber"
require "action_controller/railties/url_helpers"
module ActionController
@@ -35,8 +34,6 @@ module ActionController
end
end
- log_subscriber :action_controller, ActionController::Railties::LogSubscriber.new
-
initializer "action_controller.set_configs" do |app|
paths = app.config.paths
ac = app.config.action_controller
diff --git a/actionpack/lib/action_controller/railties/log_subscriber.rb b/actionpack/lib/action_controller/railties/log_subscriber.rb
deleted file mode 100644
index 00ac3bdf67..0000000000
--- a/actionpack/lib/action_controller/railties/log_subscriber.rb
+++ /dev/null
@@ -1,56 +0,0 @@
-require 'active_support/core_ext/object/blank'
-
-module ActionController
- module Railties
- class LogSubscriber < Rails::LogSubscriber
- INTERNAL_PARAMS = %w(controller action format _method only_path)
-
- def start_processing(event)
- payload = event.payload
- params = payload[:params].except(*INTERNAL_PARAMS)
-
- info " Processing by #{payload[:controller]}##{payload[:action]} as #{payload[:formats].first.to_s.upcase}"
- info " Parameters: #{params.inspect}" unless params.empty?
- end
-
- def process_action(event)
- payload = event.payload
- additions = ActionController::Base.log_process_action(payload)
-
- message = "Completed #{payload[:status]} #{Rack::Utils::HTTP_STATUS_CODES[payload[:status]]} in %.0fms" % event.duration
- message << " (#{additions.join(" | ")})" unless additions.blank?
-
- info(message)
- end
-
- def send_file(event)
- message = "Sent file %s"
- message << " (%.1fms)"
- info(message % [event.payload[:path], event.duration])
- end
-
- def redirect_to(event)
- info "Redirected to #{event.payload[:location]}"
- end
-
- def send_data(event)
- info("Sent data %s (%.1fms)" % [event.payload[:filename], event.duration])
- end
-
- %w(write_fragment read_fragment exist_fragment?
- expire_fragment expire_page write_page).each do |method|
- class_eval <<-METHOD, __FILE__, __LINE__ + 1
- def #{method}(event)
- key_or_path = event.payload[:key] || event.payload[:path]
- human_name = #{method.to_s.humanize.inspect}
- info("\#{human_name} \#{key_or_path} (%.1fms)" % event.duration)
- end
- METHOD
- end
-
- def logger
- ActionController::Base.logger
- end
- end
- end
-end \ No newline at end of file
diff --git a/actionpack/lib/action_controller/test_case.rb b/actionpack/lib/action_controller/test_case.rb
index 7f9eb2cfd1..650eb16ac0 100644
--- a/actionpack/lib/action_controller/test_case.rb
+++ b/actionpack/lib/action_controller/test_case.rb
@@ -40,7 +40,7 @@ module ActionController
ActiveSupport::Notifications.unsubscribe("!render_template.action_view")
end
- # Asserts that the request was rendered with the appropriate template file or partials
+ # Asserts that the request was rendered with the appropriate template file or partials.
#
# ==== Examples
#
@@ -53,6 +53,12 @@ module ActionController
# # assert that no partials were rendered
# assert_template :partial => false
#
+ # In a view test case, you can also assert that specific locals are passed
+ # to partials:
+ #
+ # # assert that the "_customer" partial was rendered with a specific object
+ # assert_template :partial => '_customer', :locals => { :customer => @customer }
+ #
def assert_template(options = {}, message = nil)
validate_request!
@@ -72,9 +78,13 @@ module ActionController
end
when Hash
if expected_partial = options[:partial]
- if expected_count = options[:count]
+ if expected_locals = options[:locals]
+ actual_locals = @locals[expected_partial.to_s.sub(/^_/,'')]
+ expected_locals.each_pair do |k,v|
+ assert_equal(v, actual_locals[k])
+ end
+ elsif expected_count = options[:count]
actual_count = @partials[expected_partial]
- # actual_count = found.nil? ? 0 : found[1]
msg = build_message(message,
"expecting ? to be rendered ? time(s) but rendered ? time(s)",
expected_partial, expected_count, actual_count)
@@ -183,6 +193,8 @@ module ActionController
replace(session.stringify_keys)
@loaded = true
end
+
+ def exists?; true; end
end
# Superclass for ActionController functional tests. Functional tests allow you to
diff --git a/actionpack/lib/action_dispatch/http/request.rb b/actionpack/lib/action_dispatch/http/request.rb
index 98f4f5ae18..fd23b1df79 100644
--- a/actionpack/lib/action_dispatch/http/request.rb
+++ b/actionpack/lib/action_dispatch/http/request.rb
@@ -194,9 +194,12 @@ module ActionDispatch
@env['rack.input']
end
+ # TODO This should be broken apart into AD::Request::Session and probably
+ # be included by the session middleware.
def reset_session
- self.session_options.delete(:id)
+ session.destroy if session
self.session = {}
+ @env['action_dispatch.request.flash_hash'] = nil
end
def session=(session) #:nodoc:
diff --git a/actionpack/lib/action_dispatch/middleware/flash.rb b/actionpack/lib/action_dispatch/middleware/flash.rb
index 18771fe782..bfa30cf5af 100644
--- a/actionpack/lib/action_dispatch/middleware/flash.rb
+++ b/actionpack/lib/action_dispatch/middleware/flash.rb
@@ -4,7 +4,7 @@ module ActionDispatch
# read a notice you put there or <tt>flash["notice"] = "hello"</tt>
# to put a new one.
def flash
- session['flash'] ||= Flash::FlashHash.new
+ @env['action_dispatch.request.flash_hash'] ||= (session["flash"] || Flash::FlashHash.new)
end
end
@@ -176,7 +176,14 @@ module ActionDispatch
@app.call(env)
ensure
- if (session = env['rack.session']) && session.key?('flash') && session['flash'].empty?
+ session = env['rack.session'] || {}
+ flash_hash = env['action_dispatch.request.flash_hash']
+
+ if flash_hash && (!flash_hash.empty? || session.key?('flash'))
+ session["flash"] = flash_hash
+ end
+
+ if session.key?('flash') && session['flash'].empty?
session.delete('flash')
end
end
diff --git a/actionpack/lib/action_dispatch/middleware/session/abstract_store.rb b/actionpack/lib/action_dispatch/middleware/session/abstract_store.rb
index 3e8d64b0c6..08bc80dbc2 100644
--- a/actionpack/lib/action_dispatch/middleware/session/abstract_store.rb
+++ b/actionpack/lib/action_dispatch/middleware/session/abstract_store.rb
@@ -12,6 +12,35 @@ module ActionDispatch
ENV_SESSION_KEY = 'rack.session'.freeze
ENV_SESSION_OPTIONS_KEY = 'rack.session.options'.freeze
+ # thin wrapper around Hash that allows us to lazily
+ # load session id into session_options
+ class OptionsHash < Hash
+ def initialize(by, env, default_options)
+ @by = by
+ @env = env
+ @session_id_loaded = false
+ merge!(default_options)
+ end
+
+ def [](key)
+ if key == :id
+ load_session_id! unless super(:id) || has_session_id?
+ end
+ super(key)
+ end
+
+ private
+
+ def has_session_id?
+ @session_id_loaded
+ end
+
+ def load_session_id!
+ self[:id] = @by.send(:extract_session_id, @env)
+ @session_id_loaded = true
+ end
+ end
+
class SessionHash < Hash
def initialize(by, env)
super()
@@ -21,66 +50,75 @@ module ActionDispatch
end
def [](key)
- load! unless @loaded
+ load_for_read!
+ super(key.to_s)
+ end
+
+ def has_key?(key)
+ load_for_read!
super(key.to_s)
end
def []=(key, value)
- load! unless @loaded
+ load_for_write!
super(key.to_s, value)
end
def to_hash
+ load_for_read!
h = {}.replace(self)
h.delete_if { |k,v| v.nil? }
h
end
def update(hash)
- load! unless @loaded
+ load_for_write!
super(hash.stringify_keys)
end
def delete(key)
- load! unless @loaded
+ load_for_write!
super(key.to_s)
end
def inspect
- load! unless @loaded
+ load_for_read!
super
end
+ def exists?
+ return @exists if instance_variable_defined?(:@exists)
+ @exists = @by.send(:exists?, @env)
+ end
+
def loaded?
@loaded
end
+ def destroy
+ clear
+ @by.send(:destroy, @env) if @by
+ @env[ENV_SESSION_OPTIONS_KEY][:id] = nil if @env && @env[ENV_SESSION_OPTIONS_KEY]
+ @loaded = false
+ end
+
private
- def load!
- stale_session_check! do
- id, session = @by.send(:load_session, @env)
- (@env[ENV_SESSION_OPTIONS_KEY] ||= {})[:id] = id
- replace(session.stringify_keys)
- @loaded = true
- end
+
+ def load_for_read!
+ load! if !loaded? && exists?
end
- def stale_session_check!
- yield
- rescue ArgumentError => argument_error
- if argument_error.message =~ %r{undefined class/module ([\w:]*\w)}
- begin
- # Note that the regexp does not allow $1 to end with a ':'
- $1.constantize
- rescue LoadError, NameError => const_error
- raise ActionDispatch::Session::SessionRestoreError, "Session contains objects whose class definition isn't available.\nRemember to require the classes for all objects kept in the session.\n(Original exception: #{const_error.message} [#{const_error.class}])\n"
- end
-
- retry
- else
- raise
- end
+ def load_for_write!
+ load! unless loaded?
+ end
+
+ def load!
+ id, session = @by.send(:load_session, @env)
+ @env[ENV_SESSION_OPTIONS_KEY][:id] = id
+ replace(session.stringify_keys)
+ @loaded = true
end
+
end
DEFAULT_OPTIONS = {
@@ -108,8 +146,8 @@ module ActionDispatch
session_data = env[ENV_SESSION_KEY]
options = env[ENV_SESSION_OPTIONS_KEY]
- if !session_data.is_a?(AbstractStore::SessionHash) || session_data.send(:loaded?) || options[:expire_after]
- session_data.send(:load!) if session_data.is_a?(AbstractStore::SessionHash) && !session_data.send(:loaded?)
+ if !session_data.is_a?(AbstractStore::SessionHash) || session_data.loaded? || options[:expire_after]
+ session_data.send(:load!) if session_data.is_a?(AbstractStore::SessionHash) && !session_data.loaded?
sid = options[:id] || generate_sid
session_data = session_data.to_hash
@@ -133,7 +171,7 @@ module ActionDispatch
def prepare!(env)
env[ENV_SESSION_KEY] = SessionHash.new(self, env)
- env[ENV_SESSION_OPTIONS_KEY] = @default_options.dup
+ env[ENV_SESSION_OPTIONS_KEY] = OptionsHash.new(self, env, @default_options)
end
def generate_sid
@@ -141,15 +179,30 @@ module ActionDispatch
end
def set_cookie(request, options)
- request.cookie_jar[@key] = options
+ if request.cookie_jar[@key] != options[:value] || !options[:expires].nil?
+ request.cookie_jar[@key] = options
+ end
end
def load_session(env)
- request = Rack::Request.new(env)
- sid = request.cookies[@key]
- sid ||= request.params[@key] unless @cookie_only
- sid, session = get_session(env, sid)
- [sid, session]
+ stale_session_check! do
+ sid = current_session_id(env)
+ sid, session = get_session(env, sid)
+ [sid, session]
+ end
+ end
+
+ def extract_session_id(env)
+ stale_session_check! do
+ request = ActionDispatch::Request.new(env)
+ sid = request.cookies[@key]
+ sid ||= request.params[@key] unless @cookie_only
+ sid
+ end
+ end
+
+ def current_session_id(env)
+ env[ENV_SESSION_OPTIONS_KEY][:id]
end
def ensure_session_key!
@@ -161,6 +214,26 @@ module ActionDispatch
end
end
+ def stale_session_check!
+ yield
+ rescue ArgumentError => argument_error
+ if argument_error.message =~ %r{undefined class/module ([\w:]*\w)}
+ begin
+ # Note that the regexp does not allow $1 to end with a ':'
+ $1.constantize
+ rescue LoadError, NameError => const_error
+ raise ActionDispatch::Session::SessionRestoreError, "Session contains objects whose class definition isn't available.\nRemember to require the classes for all objects kept in the session.\n(Original exception: #{const_error.message} [#{const_error.class}])\n"
+ end
+ retry
+ else
+ raise
+ end
+ end
+
+ def exists?(env)
+ current_session_id(env).present?
+ end
+
def get_session(env, sid)
raise '#get_session needs to be implemented.'
end
@@ -169,6 +242,10 @@ module ActionDispatch
raise '#set_session needs to be implemented and should return ' <<
'the value to be stored in the cookie (usually the sid)'
end
+
+ def destroy(env)
+ raise '#destroy needs to be implemented.'
+ end
end
end
end
diff --git a/actionpack/lib/action_dispatch/middleware/session/cookie_store.rb b/actionpack/lib/action_dispatch/middleware/session/cookie_store.rb
index 92a86ee229..dce47c63bc 100644
--- a/actionpack/lib/action_dispatch/middleware/session/cookie_store.rb
+++ b/actionpack/lib/action_dispatch/middleware/session/cookie_store.rb
@@ -39,16 +39,6 @@ module ActionDispatch
#
# Note that changing digest or secret invalidates all existing sessions!
class CookieStore < AbstractStore
- class OptionsHash < Hash
- def initialize(by, env, default_options)
- @session_data = env[AbstractStore::ENV_SESSION_KEY]
- merge!(default_options)
- end
-
- def [](key)
- key == :id ? @session_data[:session_id] : super(key)
- end
- end
def initialize(app, options = {})
super(app, options.merge!(:cookie_only => true))
@@ -57,19 +47,32 @@ module ActionDispatch
private
- def prepare!(env)
- env[ENV_SESSION_KEY] = SessionHash.new(self, env)
- env[ENV_SESSION_OPTIONS_KEY] = OptionsHash.new(self, env, @default_options)
- end
-
def load_session(env)
- request = ActionDispatch::Request.new(env)
- data = request.cookie_jar.signed[@key]
+ data = unpacked_cookie_data(env)
data = persistent_session_id!(data)
- data.stringify_keys!
[data["session_id"], data]
end
+ def extract_session_id(env)
+ if data = unpacked_cookie_data(env)
+ data["session_id"]
+ else
+ nil
+ end
+ end
+
+ def unpacked_cookie_data(env)
+ env["action_dispatch.request.unsigned_session_cookie"] ||= begin
+ stale_session_check! do
+ request = ActionDispatch::Request.new(env)
+ if data = request.cookie_jar.signed[@key]
+ data.stringify_keys!
+ end
+ data || {}
+ end
+ end
+ end
+
def set_cookie(request, options)
request.cookie_jar.signed[@key] = options
end
@@ -78,6 +81,10 @@ module ActionDispatch
persistent_session_id!(session_data, sid)
end
+ def destroy(env)
+ # session data is stored on client; nothing to do here
+ end
+
def persistent_session_id!(data, sid=nil)
data ||= {}
data["session_id"] ||= sid || generate_sid
diff --git a/actionpack/lib/action_dispatch/middleware/session/mem_cache_store.rb b/actionpack/lib/action_dispatch/middleware/session/mem_cache_store.rb
index 8df8f977e8..28e3dbd732 100644
--- a/actionpack/lib/action_dispatch/middleware/session/mem_cache_store.rb
+++ b/actionpack/lib/action_dispatch/middleware/session/mem_cache_store.rb
@@ -42,6 +42,15 @@ module ActionDispatch
rescue MemCache::MemCacheError, Errno::ECONNREFUSED
false
end
+
+ def destroy(env)
+ if sid = current_session_id(env)
+ @pool.delete(sid)
+ end
+ rescue MemCache::MemCacheError, Errno::ECONNREFUSED
+ false
+ end
+
end
end
end
diff --git a/actionpack/lib/action_dispatch/routing/mapper.rb b/actionpack/lib/action_dispatch/routing/mapper.rb
index c31f681411..388f695187 100644
--- a/actionpack/lib/action_dispatch/routing/mapper.rb
+++ b/actionpack/lib/action_dispatch/routing/mapper.rb
@@ -55,7 +55,7 @@ module ActionDispatch
path = args.first
end
- if @scope[:module] && options[:to]
+ if @scope[:module] && options[:to] && !options[:to].is_a?(Proc)
if options[:to].to_s.include?("#")
options[:to] = "#{@scope[:module]}/#{options[:to]}"
elsif @scope[:controller].nil?
@@ -194,7 +194,7 @@ module ActionDispatch
# for root cases, where the latter is the correct one.
def self.normalize_path(path)
path = Rack::Mount::Utils.normalize_path(path)
- path.sub!(%r{/(\(+)/?:}, '\1/:') unless path =~ %r{^/\(+:.*\)$}
+ path.gsub!(%r{/(\(+)/?}, '\1/') unless path =~ %r{^/\(+[^/]+\)$}
path
end
@@ -299,6 +299,11 @@ module ActionDispatch
options = args.extract_options!
options = options.dup
+ if name_prefix = options.delete(:name_prefix)
+ options[:as] ||= name_prefix
+ ActiveSupport::Deprecation.warn ":name_prefix was deprecated in the new router syntax. Use :as instead.", caller
+ end
+
case args.first
when String
options[:path] = args.first
@@ -341,9 +346,11 @@ module ActionDispatch
scope(controller.to_sym) { yield }
end
- def namespace(path)
+ def namespace(path, options = {})
path = path.to_s
- scope(:path => path, :name_prefix => path, :module => path, :shallow_path => path, :shallow_prefix => path) { yield }
+ options = { :path => path, :as => path, :module => path,
+ :shallow_path => path, :shallow_prefix => path }.merge!(options)
+ scope(options) { yield }
end
def constraints(constraints = {})
@@ -359,10 +366,10 @@ module ActionDispatch
options = (@scope[:options] || {}).merge(options)
- if @scope[:name_prefix] && !options[:as].blank?
- options[:as] = "#{@scope[:name_prefix]}_#{options[:as]}"
- elsif @scope[:name_prefix] && options[:as] == ""
- options[:as] = @scope[:name_prefix].to_s
+ if @scope[:as] && !options[:as].blank?
+ options[:as] = "#{@scope[:as]}_#{options[:as]}"
+ elsif @scope[:as] && options[:as] == ""
+ options[:as] = @scope[:as].to_s
end
args.push(options)
@@ -382,7 +389,7 @@ module ActionDispatch
Mapper.normalize_path("#{parent}/#{child}")
end
- def merge_name_prefix_scope(parent, child)
+ def merge_as_scope(parent, child)
parent ? "#{parent}_#{child}" : child
end
@@ -411,7 +418,9 @@ module ActionDispatch
end
def merge_blocks_scope(parent, child)
- (parent || []) + [child]
+ merged = parent ? parent.dup : []
+ merged << child if child
+ merged
end
def merge_options_scope(parent, child)
@@ -434,7 +443,8 @@ module ActionDispatch
def initialize(entities, options = {})
@name = entities.to_s
@path = options.delete(:path) || @name
- @controller = options.delete(:controller) || @name.to_s.pluralize
+ @controller = (options.delete(:controller) || @name).to_s
+ @as = options.delete(:as)
@options = options
end
@@ -453,7 +463,7 @@ module ActionDispatch
end
def name
- options[:as] || @name
+ @as || @name
end
def plural
@@ -505,7 +515,7 @@ module ActionDispatch
def nested_options
{}.tap do |opts|
- opts[:name_prefix] = member_name
+ opts[:as] = member_name
opts["#{singular}_id".to_sym] = id_constraint if id_constraint?
opts[:options] = { :shallow => shallow? } unless options[:shallow].nil?
end
@@ -537,14 +547,18 @@ module ActionDispatch
[:show, :create, :update, :destroy, :new, :edit]
end
- def initialize(entity, options = {})
- super
+ def initialize(entities, options)
+ @name = entities.to_s
+ @path = options.delete(:path) || @name
+ @controller = (options.delete(:controller) || @name.to_s.pluralize).to_s
+ @as = options.delete(:as)
+ @options = options
end
def member_name
name
end
- alias_method :collection_name, :member_name
+ alias :collection_name :member_name
def nested_path
path
@@ -552,7 +566,7 @@ module ActionDispatch
def nested_options
{}.tap do |opts|
- opts[:name_prefix] = member_name
+ opts[:as] = member_name
opts[:options] = { :shallow => shallow? } unless @options[:shallow].nil?
end
end
@@ -571,6 +585,10 @@ module ActionDispatch
@scope[:path_names] = @set.resources_path_names
end
+ def resources_path_names(options)
+ @scope[:path_names].merge!(options)
+ end
+
def resource(*resources, &block)
options = resources.extract_options!
options = (@scope[:options] || {}).merge(options)
@@ -673,7 +691,7 @@ module ActionDispatch
if @scope[:shallow_path].blank?
scope(*parent_resource.nested_scope) { yield }
else
- scope(@scope[:shallow_path], :name_prefix => @scope[:shallow_prefix]) do
+ scope(@scope[:shallow_path], :as => @scope[:shallow_prefix]) do
scope(*parent_resource.nested_scope) { yield }
end
end
@@ -684,7 +702,7 @@ module ActionDispatch
end
end
- def namespace(path)
+ def namespace(path, options = {})
if resource_scope?
nested { super }
else
@@ -726,10 +744,10 @@ module ActionDispatch
return member { match(*args) }
end
- path_names = options.delete(:path_names)
+ path = options.delete(:path)
if args.first.is_a?(Symbol)
- path = path_for_action(args.first, path_names)
+ path = path_for_action(args.first, path)
options = options_for_action(args.first, options)
with_exclusive_scope do
@@ -758,7 +776,7 @@ module ActionDispatch
def root(options={})
if @scope[:scope_level] == :resources
with_scope_level(:nested) do
- scope(parent_resource.path, :name_prefix => parent_resource.collection_name) do
+ scope(parent_resource.path, :as => parent_resource.collection_name) do
super(options)
end
end
@@ -806,14 +824,14 @@ module ActionDispatch
def with_exclusive_scope
begin
- old_name_prefix, old_path = @scope[:name_prefix], @scope[:path]
- @scope[:name_prefix], @scope[:path] = nil, nil
+ old_name_prefix, old_path = @scope[:as], @scope[:path]
+ @scope[:as], @scope[:path] = nil, nil
with_scope_level(:exclusive) do
yield
end
ensure
- @scope[:name_prefix], @scope[:path] = old_name_prefix, old_path
+ @scope[:as], @scope[:path] = old_name_prefix, old_path
end
end
@@ -850,7 +868,7 @@ module ActionDispatch
end
end
- def path_for_action(action, path_names)
+ def path_for_action(action, path)
case action
when :index, :create
"#{@scope[:path]}(.:format)"
@@ -871,12 +889,12 @@ module ActionDispatch
else
case @scope[:scope_level]
when :collection, :new
- "#{@scope[:path]}/#{action_path(action)}(.:format)"
+ "#{@scope[:path]}/#{action_path(action, path)}(.:format)"
else
if parent_resource.shallow?
- "#{@scope[:shallow_path]}/#{parent_resource.path}/:id/#{action_path(action)}(.:format)"
+ "#{@scope[:shallow_path]}/#{parent_resource.path}/:id/#{action_path(action, path)}(.:format)"
else
- "#{@scope[:path]}/#{action_path(action)}(.:format)"
+ "#{@scope[:path]}/#{action_path(action, path)}(.:format)"
end
end
end
@@ -895,9 +913,8 @@ module ActionDispatch
end
end
- def action_path(name, path_names = nil)
- path_names ||= @scope[:path_names]
- path_names[name.to_sym] || name.to_s
+ def action_path(name, path = nil)
+ path || @scope[:path_names][name.to_sym] || name.to_s
end
def options_for_action(action, options)
@@ -908,7 +925,7 @@ module ActionDispatch
end
def name_for_action(action)
- name_prefix = @scope[:name_prefix].blank? ? "" : "#{@scope[:name_prefix]}_"
+ name_prefix = @scope[:as].blank? ? "" : "#{@scope[:as]}_"
shallow_prefix = @scope[:shallow_prefix].blank? ? "" : "#{@scope[:shallow_prefix]}_"
case action
diff --git a/actionpack/lib/action_dispatch/routing/route_set.rb b/actionpack/lib/action_dispatch/routing/route_set.rb
index 57a73dde75..7be79d3200 100644
--- a/actionpack/lib/action_dispatch/routing/route_set.rb
+++ b/actionpack/lib/action_dispatch/routing/route_set.rb
@@ -185,7 +185,7 @@ module ActionDispatch
end
end
- attr_accessor :routes, :named_routes
+ attr_accessor :set, :routes, :named_routes
attr_accessor :disable_clear_and_finalize, :resources_path_names
attr_accessor :default_url_options, :request_class
@@ -296,6 +296,7 @@ module ActionDispatch
@extras = extras
normalize_options!
+ normalize_recall!
normalize_controller_action_id!
use_relative_controller!
controller.sub!(%r{^/}, '') if controller
@@ -336,6 +337,15 @@ module ActionDispatch
end
end
+ def normalize_recall!
+ # If the target route is not a standard route then remove controller and action
+ # from the options otherwise they will appear in the url parameters
+ if block_or_proc_route_target?
+ recall.delete(:controller) unless segment_keys.include?(:controller)
+ recall.delete(:action) unless segment_keys.include?(:action)
+ end
+ end
+
# This pulls :controller, :action, and :id out of the recall.
# The recall key is only used if there is no key in the options
# or if the key in the options is identical. If any of
@@ -371,7 +381,7 @@ module ActionDispatch
def generate
error = ActionController::RoutingError.new("No route matches #{options.inspect}")
- path, params = @set.generate(:path_info, named_route, options, recall, opts)
+ path, params = @set.set.generate(:path_info, named_route, options, recall, opts)
raise error unless path
@@ -402,6 +412,19 @@ module ActionDispatch
return false unless current_controller
controller.to_param != current_controller.to_param
end
+
+ private
+ def named_route_exists?
+ named_route && set.named_routes[named_route]
+ end
+
+ def block_or_proc_route_target?
+ named_route_exists? && !set.named_routes[named_route].app.is_a?(Dispatcher)
+ end
+
+ def segment_keys
+ named_route_exists? ? set.named_routes[named_route].segment_keys : []
+ end
end
# Generate the path indicated by the arguments, and return an array of
@@ -415,7 +438,7 @@ module ActionDispatch
end
def generate(options, recall = {}, extras = false)
- Generator.new(options, recall, @set, extras).generate
+ Generator.new(options, recall, self, extras).generate
end
RESERVED_OPTIONS = [:anchor, :params, :only_path, :host, :protocol, :port, :trailing_slash]
@@ -447,7 +470,7 @@ module ActionDispatch
# ROUTES TODO: This can be called directly, so script_name should probably be set in the router
rewritten_url << (options[:trailing_slash] ? path.sub(/\?|\z/) { "/" + $& } : path)
- rewritten_url << "##{Rack::Utils.escape(options[:anchor].to_param.to_s)}" if options[:anchor]
+ rewritten_url << "##{Rack::Mount::Utils.escape_uri(options[:anchor].to_param.to_s)}" if options[:anchor]
rewritten_url
end
diff --git a/actionpack/lib/action_dispatch/testing/assertions/routing.rb b/actionpack/lib/action_dispatch/testing/assertions/routing.rb
index 1499c03bdf..9338fa9e70 100644
--- a/actionpack/lib/action_dispatch/testing/assertions/routing.rb
+++ b/actionpack/lib/action_dispatch/testing/assertions/routing.rb
@@ -53,7 +53,6 @@ module ActionDispatch
extras.each_key { |key| expected_options.delete key } unless extras.nil?
expected_options.stringify_keys!
- routing_diff = expected_options.diff(request.path_parameters)
msg = build_message(message, "The recognized options <?> did not match <?>, difference: <?>",
request.path_parameters, expected_options, expected_options.diff(request.path_parameters))
assert_block(msg) { request.path_parameters == expected_options }
diff --git a/actionpack/lib/action_dispatch/testing/assertions/selector.rb b/actionpack/lib/action_dispatch/testing/assertions/selector.rb
index b490547da7..2fc9e2b7d6 100644
--- a/actionpack/lib/action_dispatch/testing/assertions/selector.rb
+++ b/actionpack/lib/action_dispatch/testing/assertions/selector.rb
@@ -7,9 +7,7 @@ require 'action_controller/vendor/html-scanner'
module ActionDispatch
module Assertions
- unless const_defined?(:NO_STRIP)
- NO_STRIP = %w{pre script style textarea}
- end
+ NO_STRIP = %w{pre script style textarea}
# Adds the +assert_select+ method for use in Rails functional
# test cases, which can be used to make assertions on the response HTML of a controller
@@ -581,27 +579,25 @@ module ActionDispatch
end
protected
- unless const_defined?(:RJS_STATEMENTS)
- RJS_PATTERN_HTML = "\"((\\\\\"|[^\"])*)\""
- RJS_ANY_ID = "\"([^\"])*\""
- RJS_STATEMENTS = {
- :chained_replace => "\\$\\(#{RJS_ANY_ID}\\)\\.replace\\(#{RJS_PATTERN_HTML}\\)",
- :chained_replace_html => "\\$\\(#{RJS_ANY_ID}\\)\\.update\\(#{RJS_PATTERN_HTML}\\)",
- :replace_html => "Element\\.update\\(#{RJS_ANY_ID}, #{RJS_PATTERN_HTML}\\)",
- :replace => "Element\\.replace\\(#{RJS_ANY_ID}, #{RJS_PATTERN_HTML}\\)",
- :redirect => "window.location.href = #{RJS_ANY_ID}"
- }
- [:remove, :show, :hide, :toggle].each do |action|
- RJS_STATEMENTS[action] = "Element\\.#{action}\\(#{RJS_ANY_ID}\\)"
- end
- RJS_INSERTIONS = ["top", "bottom", "before", "after"]
- RJS_INSERTIONS.each do |insertion|
- RJS_STATEMENTS["insert_#{insertion}".to_sym] = "Element.insert\\(#{RJS_ANY_ID}, \\{ #{insertion}: #{RJS_PATTERN_HTML} \\}\\)"
- end
- RJS_STATEMENTS[:insert_html] = "Element.insert\\(#{RJS_ANY_ID}, \\{ (#{RJS_INSERTIONS.join('|')}): #{RJS_PATTERN_HTML} \\}\\)"
- RJS_STATEMENTS[:any] = Regexp.new("(#{RJS_STATEMENTS.values.join('|')})")
- RJS_PATTERN_UNICODE_ESCAPED_CHAR = /\\u([0-9a-zA-Z]{4})/
+ RJS_PATTERN_HTML = "\"((\\\\\"|[^\"])*)\""
+ RJS_ANY_ID = "\"([^\"])*\""
+ RJS_STATEMENTS = {
+ :chained_replace => "\\$\\(#{RJS_ANY_ID}\\)\\.replace\\(#{RJS_PATTERN_HTML}\\)",
+ :chained_replace_html => "\\$\\(#{RJS_ANY_ID}\\)\\.update\\(#{RJS_PATTERN_HTML}\\)",
+ :replace_html => "Element\\.update\\(#{RJS_ANY_ID}, #{RJS_PATTERN_HTML}\\)",
+ :replace => "Element\\.replace\\(#{RJS_ANY_ID}, #{RJS_PATTERN_HTML}\\)",
+ :redirect => "window.location.href = #{RJS_ANY_ID}"
+ }
+ [:remove, :show, :hide, :toggle].each do |action|
+ RJS_STATEMENTS[action] = "Element\\.#{action}\\(#{RJS_ANY_ID}\\)"
+ end
+ RJS_INSERTIONS = ["top", "bottom", "before", "after"]
+ RJS_INSERTIONS.each do |insertion|
+ RJS_STATEMENTS["insert_#{insertion}".to_sym] = "Element.insert\\(#{RJS_ANY_ID}, \\{ #{insertion}: #{RJS_PATTERN_HTML} \\}\\)"
end
+ RJS_STATEMENTS[:insert_html] = "Element.insert\\(#{RJS_ANY_ID}, \\{ (#{RJS_INSERTIONS.join('|')}): #{RJS_PATTERN_HTML} \\}\\)"
+ RJS_STATEMENTS[:any] = Regexp.new("(#{RJS_STATEMENTS.values.join('|')})")
+ RJS_PATTERN_UNICODE_ESCAPED_CHAR = /\\u([0-9a-zA-Z]{4})/
# +assert_select+ and +css_select+ call this to obtain the content in the HTML
# page, or from all the RJS statements, depending on the type of response.
diff --git a/actionpack/lib/action_view/base.rb b/actionpack/lib/action_view/base.rb
index a7ba9f374a..956c88a553 100644
--- a/actionpack/lib/action_view/base.rb
+++ b/actionpack/lib/action_view/base.rb
@@ -3,6 +3,7 @@ require 'active_support/core_ext/module/delegation'
require 'active_support/core_ext/class/attribute'
require 'active_support/core_ext/array/wrap'
require 'active_support/ordered_options'
+require 'action_view/log_subscriber'
module ActionView #:nodoc:
class NonConcattingString < ActiveSupport::SafeBuffer
@@ -204,8 +205,12 @@ module ActionView #:nodoc:
value.dup : ActionView::PathSet.new(Array.wrap(value))
end
+ def assign(new_assigns) # :nodoc:
+ self.assigns = new_assigns.each { |key, value| instance_variable_set("@#{key}", value) }
+ end
+
def initialize(lookup_context = nil, assigns_for_first_render = {}, controller = nil, formats = nil) #:nodoc:
- self.assigns = assigns_for_first_render.each { |key, value| instance_variable_set("@#{key}", value) }
+ assign(assigns_for_first_render)
self.helpers = self.class.helpers || Module.new
if @_controller = controller
diff --git a/actionpack/lib/action_view/helpers/active_model_helper.rb b/actionpack/lib/action_view/helpers/active_model_helper.rb
index 0f9b04cb5f..6bb0875bc3 100644
--- a/actionpack/lib/action_view/helpers/active_model_helper.rb
+++ b/actionpack/lib/action_view/helpers/active_model_helper.rb
@@ -36,12 +36,16 @@ module ActionView
end
end
- %w(tag content_tag to_date_select_tag to_datetime_select_tag to_time_select_tag).each do |meth|
+ %w(content_tag to_date_select_tag to_datetime_select_tag to_time_select_tag).each do |meth|
module_eval "def #{meth}(*) error_wrapping(super) end", __FILE__, __LINE__
end
+ def tag(type, options, *)
+ tag_generate_errors?(options) ? error_wrapping(super) : super
+ end
+
def error_wrapping(html_tag)
- if object.respond_to?(:errors) && object.errors.respond_to?(:full_messages) && object.errors[@method_name].any?
+ if object_has_errors?
Base.field_error_proc.call(html_tag, self)
else
html_tag
@@ -51,6 +55,16 @@ module ActionView
def error_message
object.errors[@method_name]
end
+
+ private
+
+ def object_has_errors?
+ object.respond_to?(:errors) && object.errors.respond_to?(:full_messages) && error_message.any?
+ end
+
+ def tag_generate_errors?(options)
+ options['type'] != 'hidden'
+ end
end
class FormBuilder
diff --git a/actionpack/lib/action_view/helpers/asset_tag_helper.rb b/actionpack/lib/action_view/helpers/asset_tag_helper.rb
index d094b0d8d8..a3c43d3e93 100644
--- a/actionpack/lib/action_view/helpers/asset_tag_helper.rb
+++ b/actionpack/lib/action_view/helpers/asset_tag_helper.rb
@@ -194,7 +194,19 @@ module ActionView
# RewriteEngine On
# RewriteRule ^/release-\d+/(images|javascripts|stylesheets)/(.*)$ /$1/$2 [L]
module AssetTagHelper
- JAVASCRIPT_DEFAULT_SOURCES = ['prototype', 'effects', 'dragdrop', 'controls', 'rails'].freeze unless const_defined?(:JAVASCRIPT_DEFAULT_SOURCES)
+ mattr_reader :javascript_expansions
+ @@javascript_expansions = { }
+
+ mattr_reader :stylesheet_expansions
+ @@stylesheet_expansions = {}
+
+ # You can enable or disable the asset tag timestamps cache.
+ # With the cache enabled, the asset tag helper methods will make fewer
+ # expensive file system calls. However this prevents you from modifying
+ # any asset files while the server is running.
+ #
+ # ActionView::Helpers::AssetTagHelper.cache_asset_timestamps = false
+ mattr_accessor :cache_asset_timestamps
# Returns a link tag that browsers and news readers can use to auto-detect
# an RSS or ATOM feed. The +type+ can either be <tt>:rss</tt> (default) or
@@ -351,8 +363,6 @@ module ActionView
end
end
- @@javascript_expansions = { :defaults => JAVASCRIPT_DEFAULT_SOURCES.dup }
-
# Register one or more javascript files to be included when <tt>symbol</tt>
# is passed to <tt>javascript_include_tag</tt>. This method is typically intended
# to be called from plugin initialization to register javascript files
@@ -368,8 +378,6 @@ module ActionView
@@javascript_expansions.merge!(expansions)
end
- @@stylesheet_expansions = {}
-
# Register one or more stylesheet files to be included when <tt>symbol</tt>
# is passed to <tt>stylesheet_link_tag</tt>. This method is typically intended
# to be called from plugin initialization to register stylesheet files
@@ -385,18 +393,6 @@ module ActionView
@@stylesheet_expansions.merge!(expansions)
end
- # Register one or more additional JavaScript files to be included when
- # <tt>javascript_include_tag :defaults</tt> is called. This method is
- # typically intended to be called from plugin initialization to register additional
- # .js files that the plugin installed in <tt>public/javascripts</tt>.
- def self.register_javascript_include_default(*sources)
- @@javascript_expansions[:defaults].concat(sources)
- end
-
- def self.reset_javascript_include_default #:nodoc:
- @@javascript_expansions[:defaults] = JAVASCRIPT_DEFAULT_SOURCES.dup
- end
-
# Computes the path to a stylesheet asset in the public stylesheets directory.
# If the +source+ filename has no extension, <tt>.css</tt> will be appended (except for explicit URIs).
# Full paths from the document root will be passed through.
@@ -707,23 +703,8 @@ module ActionView
tag("audio", options)
end
- def self.cache_asset_timestamps
- @@cache_asset_timestamps
- end
-
- # You can enable or disable the asset tag timestamps cache.
- # With the cache enabled, the asset tag helper methods will make fewer
- # expensive file system calls. However this prevents you from modifying
- # any asset files while the server is running.
- #
- # ActionView::Helpers::AssetTagHelper.cache_asset_timestamps = false
- def self.cache_asset_timestamps=(value)
- @@cache_asset_timestamps = value
- end
-
- @@cache_asset_timestamps = true
-
private
+
def rewrite_extension?(source, dir, ext)
source_ext = File.extname(source)[1..-1]
ext && (source_ext.blank? || (ext != source_ext && File.exist?(File.join(config.assets_dir, dir, "#{source}.#{ext}"))))
diff --git a/actionpack/lib/action_view/helpers/date_helper.rb b/actionpack/lib/action_view/helpers/date_helper.rb
index 7d7b6a1d91..f097b9a5a3 100644
--- a/actionpack/lib/action_view/helpers/date_helper.rb
+++ b/actionpack/lib/action_view/helpers/date_helper.rb
@@ -582,10 +582,10 @@ module ActionView
extend ActiveSupport::Memoizable
include ActionView::Helpers::TagHelper
- DEFAULT_PREFIX = 'date'.freeze unless const_defined?('DEFAULT_PREFIX')
+ DEFAULT_PREFIX = 'date'.freeze
POSITION = {
:year => 1, :month => 2, :day => 3, :hour => 4, :minute => 5, :second => 6
- }.freeze unless const_defined?('POSITION')
+ }.freeze
def initialize(datetime, options = {}, html_options = {})
@options = options.dup
diff --git a/actionpack/lib/action_view/helpers/form_helper.rb b/actionpack/lib/action_view/helpers/form_helper.rb
index 8efed98bd2..d1b10a9281 100644
--- a/actionpack/lib/action_view/helpers/form_helper.rb
+++ b/actionpack/lib/action_view/helpers/form_helper.rb
@@ -124,7 +124,6 @@ module ActionView
# model:
#
# <%= form_for :person do |f| %>
- # <%= f.error_messages %>
# First name: <%= f.text_field :first_name %><br />
# Last name : <%= f.text_field :last_name %><br />
# Biography : <%= f.text_area :biography %><br />
@@ -221,15 +220,15 @@ module ActionView
# <% end %>
#
# === Unobtrusive JavaScript
- #
- # Specifying:
- #
+ #
+ # Specifying:
+ #
# :remote => true
#
# in the options hash creates a form that will allow the unobtrusive JavaScript drivers to modify its
- # behaviour. The expected default behaviour is an XMLHttpRequest in the background instead of the regular
+ # behaviour. The expected default behaviour is an XMLHttpRequest in the background instead of the regular
# POST arrangement, but ultimately the behaviour is the choice of the JavaScript driver implementor.
- # Even though it's using JavaScript to serialize the form elements, the form submission will work just like
+ # Even though it's using JavaScript to serialize the form elements, the form submission will work just like
# a regular submission as viewed by the receiving side (all elements available in <tt>params</tt>).
#
# Example:
@@ -839,9 +838,9 @@ module ActionView
attr_reader :method_name, :object_name
- DEFAULT_FIELD_OPTIONS = { "size" => 30 }.freeze unless const_defined?(:DEFAULT_FIELD_OPTIONS)
- DEFAULT_RADIO_OPTIONS = { }.freeze unless const_defined?(:DEFAULT_RADIO_OPTIONS)
- DEFAULT_TEXT_AREA_OPTIONS = { "cols" => 40, "rows" => 20 }.freeze unless const_defined?(:DEFAULT_TEXT_AREA_OPTIONS)
+ DEFAULT_FIELD_OPTIONS = { "size" => 30 }.freeze
+ DEFAULT_RADIO_OPTIONS = { }.freeze
+ DEFAULT_TEXT_AREA_OPTIONS = { "cols" => 40, "rows" => 20 }.freeze
def initialize(object_name, method_name, template_object, object = nil)
@object_name, @method_name = object_name.to_s.dup, method_name.to_s.dup
@@ -898,7 +897,7 @@ module ActionView
options.delete("size")
end
options["type"] ||= field_type
- options["value"] ||= value_before_type_cast(object) unless field_type == "file"
+ options["value"] = options.fetch("value"){ value_before_type_cast(object) } unless field_type == "file"
options["value"] &&= html_escape(options["value"])
add_default_name_and_id(options)
tag("input", options)
@@ -1031,7 +1030,7 @@ module ActionView
private
def add_default_name_and_id_for_value(tag_value, options)
unless tag_value.nil?
- pretty_tag_value = tag_value.to_s.gsub(/\s/, "_").gsub(/\W/, "").downcase
+ pretty_tag_value = tag_value.to_s.gsub(/\s/, "_").gsub(/[^-\w]/, "").downcase
specified_id = options["id"]
add_default_name_and_id(options)
options["id"] += "_#{pretty_tag_value}" if specified_id.blank? && options["id"].present?
@@ -1043,14 +1042,14 @@ module ActionView
def add_default_name_and_id(options)
if options.has_key?("index")
options["name"] ||= tag_name_with_index(options["index"])
- options["id"] = options.fetch("id", tag_id_with_index(options["index"]))
+ options["id"] = options.fetch("id"){ tag_id_with_index(options["index"]) }
options.delete("index")
elsif defined?(@auto_index)
options["name"] ||= tag_name_with_index(@auto_index)
- options["id"] = options.fetch("id", tag_id_with_index(@auto_index))
+ options["id"] = options.fetch("id"){ tag_id_with_index(@auto_index) }
else
options["name"] ||= tag_name + (options.has_key?('multiple') ? '[]' : '')
- options["id"] = options.fetch("id", tag_id)
+ options["id"] = options.fetch("id"){ tag_id }
end
end
@@ -1181,7 +1180,7 @@ module ActionView
# <%= form_for @post do |f| %>
# <%= f.submit %>
# <% end %>
- #
+ #
# In the example above, if @post is a new record, it will use "Create Post" as
# submit button label, otherwise, it uses "Update Post".
#
diff --git a/actionpack/lib/action_view/helpers/prototype_helper.rb b/actionpack/lib/action_view/helpers/prototype_helper.rb
index 3038b07143..bfb8f74a00 100644
--- a/actionpack/lib/action_view/helpers/prototype_helper.rb
+++ b/actionpack/lib/action_view/helpers/prototype_helper.rb
@@ -95,14 +95,12 @@ module ActionView
# See JavaScriptGenerator for information on updating multiple elements
# on the page in an Ajax response.
module PrototypeHelper
- unless const_defined? :CALLBACKS
- 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)
- end
+ 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.
# Takes the same arguments as link_to_remote.
diff --git a/actionpack/lib/action_view/helpers/scriptaculous_helper.rb b/actionpack/lib/action_view/helpers/scriptaculous_helper.rb
index 7f7776e9c0..8610c2469e 100644
--- a/actionpack/lib/action_view/helpers/scriptaculous_helper.rb
+++ b/actionpack/lib/action_view/helpers/scriptaculous_helper.rb
@@ -4,7 +4,7 @@ require 'active_support/json'
module ActionView
# = Action View Scriptaculous Helpers
module Helpers
- # Provides a set of helpers for calling Scriptaculous[http://script.aculo.us/]
+ # Provides a set of helpers for calling Scriptaculous[http://script.aculo.us/]
# JavaScript functions, including those which create Ajax controls and visual
# effects.
#
@@ -18,9 +18,7 @@ module ActionView
# See the documentation at http://script.aculo.us for more information on
# using these helpers in your application.
module ScriptaculousHelper
- unless const_defined? :TOGGLE_EFFECTS
- TOGGLE_EFFECTS = [:toggle_appear, :toggle_slide, :toggle_blind]
- end
+ TOGGLE_EFFECTS = [:toggle_appear, :toggle_slide, :toggle_blind]
# Returns a JavaScript snippet to be used on the Ajax callbacks for
# starting visual effects.
diff --git a/actionpack/lib/action_view/log_subscriber.rb b/actionpack/lib/action_view/log_subscriber.rb
new file mode 100644
index 0000000000..4a52937c58
--- /dev/null
+++ b/actionpack/lib/action_view/log_subscriber.rb
@@ -0,0 +1,27 @@
+module ActionView
+ # = Action View Log Subscriber
+ #
+ # Provides functionality so that Rails can output logs from Action View.
+ class LogSubscriber < ActiveSupport::LogSubscriber
+ def render_template(event)
+ message = "Rendered #{from_rails_root(event.payload[:identifier])}"
+ message << " within #{from_rails_root(event.payload[:layout])}" if event.payload[:layout]
+ message << (" (%.1fms)" % event.duration)
+ info(message)
+ end
+ alias :render_partial :render_template
+ alias :render_collection :render_template
+
+ def logger
+ ActionController::Base.logger
+ end
+
+ protected
+
+ def from_rails_root(string)
+ string.sub("#{Rails.root}/", "").sub(/^app\/views\//, "")
+ end
+ end
+end
+
+ActionView::LogSubscriber.attach_to :action_view \ No newline at end of file
diff --git a/actionpack/lib/action_view/railtie.rb b/actionpack/lib/action_view/railtie.rb
index e8ea15f47c..33dfcbb803 100644
--- a/actionpack/lib/action_view/railtie.rb
+++ b/actionpack/lib/action_view/railtie.rb
@@ -5,9 +5,8 @@ module ActionView
# = Action View Railtie
class Railtie < Rails::Railtie
config.action_view = ActiveSupport::OrderedOptions.new
-
- require "action_view/railties/log_subscriber"
- log_subscriber :action_view, ActionView::Railties::LogSubscriber.new
+ config.action_view.stylesheet_expansions = {}
+ config.action_view.javascript_expansions = { :defaults => ['prototype', 'effects', 'dragdrop', 'controls', 'rails'] }
initializer "action_view.cache_asset_timestamps" do |app|
unless app.config.cache_classes
@@ -17,6 +16,18 @@ module ActionView
end
end
+ initializer "action_view.javascript_expansions" do |app|
+ ActiveSupport.on_load(:action_view) do
+ ActionView::Helpers::AssetTagHelper.register_javascript_expansion(
+ app.config.action_view.delete(:javascript_expansions)
+ )
+
+ ActionView::Helpers::AssetTagHelper.register_stylesheet_expansion(
+ app.config.action_view.delete(:stylesheet_expansions)
+ )
+ end
+ end
+
initializer "action_view.set_configs" do |app|
ActiveSupport.on_load(:action_view) do
app.config.action_view.each do |k,v|
diff --git a/actionpack/lib/action_view/railties/log_subscriber.rb b/actionpack/lib/action_view/railties/log_subscriber.rb
deleted file mode 100644
index cb2ad0711e..0000000000
--- a/actionpack/lib/action_view/railties/log_subscriber.rb
+++ /dev/null
@@ -1,27 +0,0 @@
-module ActionView
- # = Action View Log Subscriber
- #
- # Provides functionality so that Rails can output logs from Action View.
- module Railties
- class LogSubscriber < Rails::LogSubscriber
- def render_template(event)
- message = "Rendered #{from_rails_root(event.payload[:identifier])}"
- message << " within #{from_rails_root(event.payload[:layout])}" if event.payload[:layout]
- message << (" (%.1fms)" % event.duration)
- info(message)
- end
- alias :render_partial :render_template
- alias :render_collection :render_template
-
- def logger
- ActionController::Base.logger
- end
-
- protected
-
- def from_rails_root(string)
- string.sub("#{Rails.root}/", "").sub(/^app\/views\//, "")
- end
- end
- end
-end \ No newline at end of file
diff --git a/actionpack/lib/action_view/template/resolver.rb b/actionpack/lib/action_view/template/resolver.rb
index 2cf7e955ab..c9e20ca14e 100644
--- a/actionpack/lib/action_view/template/resolver.rb
+++ b/actionpack/lib/action_view/template/resolver.rb
@@ -99,7 +99,7 @@ module ActionView
def initialize(path)
raise ArgumentError, "path already is a Resolver class" if path.is_a?(Resolver)
super()
- @path = Pathname.new(path).expand_path
+ @path = File.expand_path(path)
end
def eql?(resolver)
diff --git a/actionpack/lib/action_view/test_case.rb b/actionpack/lib/action_view/test_case.rb
index b698b4cfec..757e4cf77c 100644
--- a/actionpack/lib/action_view/test_case.rb
+++ b/actionpack/lib/action_view/test_case.rb
@@ -99,10 +99,15 @@ module ActionView
end
def render(options = {}, local_assigns = {}, &block)
- @rendered << output = _view.render(options, local_assigns, &block)
+ view.assign(_assigns)
+ @rendered << output = view.render(options, local_assigns, &block)
output
end
+ def locals
+ @locals ||= {}
+ end
+
included do
setup :setup_with_controller
end
@@ -132,36 +137,51 @@ module ActionView
end
end
- def _view
- @_view ||= begin
- view = ActionView::Base.new(ActionController::Base.view_paths, _assigns, @controller)
+ module Locals
+ attr_accessor :locals
+
+ def _render_partial(options)
+ locals[options[:partial]] = options[:locals]
+ super(options)
+ end
+ end
+
+ # The instance of ActionView::Base that is used by +render+.
+ def view
+ @view ||= begin
+ view = ActionView::Base.new(ActionController::Base.view_paths, {}, @controller)
view.singleton_class.send :include, _helpers
view.singleton_class.send :include, @controller._router.url_helpers
view.singleton_class.send :delegate, :alert, :notice, :to => "request.flash"
+ view.extend(Locals)
+ view.locals = self.locals
view.output_buffer = self.output_buffer
view
end
end
+ alias_method :_view, :view
+
EXCLUDE_IVARS = %w{
+ @_assertion_wrapped
@_result
+ @controller
+ @layouts
+ @locals
+ @method_name
@output_buffer
+ @partials
@rendered
+ @request
+ @routes
@templates
- @view_context_class
- @layouts
- @partials
- @controller
-
- @method_name
- @fixture_cache
- @loaded_fixtures
@test_passed
+ @view
+ @view_context_class
}
def _instance_variables
instance_variables - EXCLUDE_IVARS
- instance_variables
end
def _assigns