diff options
Diffstat (limited to 'actionpack')
32 files changed, 285 insertions, 360 deletions
diff --git a/actionpack/CHANGELOG.md b/actionpack/CHANGELOG.md index 345b5aa330..67c9e04ab2 100644 --- a/actionpack/CHANGELOG.md +++ b/actionpack/CHANGELOG.md @@ -1,5 +1,17 @@ ## Rails 4.0.0 (unreleased) ## +* Add `ActionController::Flash.add_flash_types` method to allow people to register their own flash types. e.g.: + + class ApplicationController + add_flash_types :error, :warning + end + + If you add the above code, you can use `<%= error %>` in an erb, and `rediect_to /foo, :error => 'message'` in a controller. + + *kennyj* + +* Remove Active Model dependency from Action Pack. *Guillermo Iguaran* + * Support unicode characters in routes. Route will be automatically escaped, so instead of manually escaping: get Rack::Utils.escape('こんにちは') => 'home#index' @@ -12,9 +24,9 @@ * Return proper format on exceptions. *Santiago Pastorino* -* Allow to use mounted_helpers (helpers for accessing mounted engines) in ActionView::TestCase. *Piotr Sarnacki* +* Allow to use `mounted_helpers` (helpers for accessing mounted engines) in `ActionView::TestCase`. *Piotr Sarnacki* -* Include mounted_helpers (helpers for accessing mounted engines) in ActionDispatch::IntegrationTest by default. *Piotr Sarnacki* +* Include `mounted_helpers` (helpers for accessing mounted engines) in `ActionDispatch::IntegrationTest` by default. *Piotr Sarnacki* * Extracted redirect logic from `ActionController::ForceSSL::ClassMethods.force_ssl` into `ActionController::ForceSSL#force_ssl_redirect` @@ -47,7 +59,7 @@ *Piotr Sarnacki* -* `truncate` now always returns an escaped HTMl-safe string. The option `:escape` can be used as +* `truncate` now always returns an escaped HTML-safe string. The option `:escape` can be used as false to not escape the result. *Li Ellis Gallardo + Rafael Mendonça França* diff --git a/actionpack/actionpack.gemspec b/actionpack/actionpack.gemspec index 938f3d6dc2..6075e2f02b 100644 --- a/actionpack/actionpack.gemspec +++ b/actionpack/actionpack.gemspec @@ -18,7 +18,6 @@ Gem::Specification.new do |s| s.requirements << 'none' s.add_dependency('activesupport', version) - s.add_dependency('activemodel', version) s.add_dependency('rack-cache', '~> 1.2') s.add_dependency('builder', '~> 3.0.0') s.add_dependency('rack', '~> 1.4.1') @@ -26,5 +25,6 @@ Gem::Specification.new do |s| s.add_dependency('journey', '~> 1.0.1') s.add_dependency('erubis', '~> 2.7.0') + s.add_development_dependency('activemodel', version) s.add_development_dependency('tzinfo', '~> 0.3.33') end diff --git a/actionpack/lib/abstract_controller/collector.rb b/actionpack/lib/abstract_controller/collector.rb index 492329c401..09b9e7ddf0 100644 --- a/actionpack/lib/abstract_controller/collector.rb +++ b/actionpack/lib/abstract_controller/collector.rb @@ -16,6 +16,10 @@ module AbstractController generate_method_for_mime(mime) end + Mime::Type.register_callback do |mime| + generate_method_for_mime(mime) unless self.instance_methods.include?(mime.to_sym) + end + protected def method_missing(symbol, &block) diff --git a/actionpack/lib/abstract_controller/rendering.rb b/actionpack/lib/abstract_controller/rendering.rb index 7d73c6af8d..3da2834af0 100644 --- a/actionpack/lib/abstract_controller/rendering.rb +++ b/actionpack/lib/abstract_controller/rendering.rb @@ -49,9 +49,19 @@ module AbstractController module ClassMethods def view_context_class @view_context_class ||= begin - routes = _routes if respond_to?(:_routes) - helpers = _helpers if respond_to?(:_helpers) - ActionView::Base.prepare(routes, helpers) + routes = respond_to?(:_routes) && _routes + helpers = respond_to?(:_helpers) && _helpers + + Class.new(ActionView::Base) do + if routes + include routes.url_helpers + include routes.mounted_helpers + end + + if helpers + include helpers + end + end end end end diff --git a/actionpack/lib/action_controller/caching/sweeping.rb b/actionpack/lib/action_controller/caching/sweeping.rb index cc1fa23935..39da15e26a 100644 --- a/actionpack/lib/action_controller/caching/sweeping.rb +++ b/actionpack/lib/action_controller/caching/sweeping.rb @@ -72,6 +72,12 @@ module ActionController #:nodoc: self.controller = nil end + def around(controller) + before(controller) + yield + after(controller) + end + protected # gets the action cache path for the given options. def action_path_for(options) diff --git a/actionpack/lib/action_controller/metal/flash.rb b/actionpack/lib/action_controller/metal/flash.rb index bd768b634e..b078beb675 100644 --- a/actionpack/lib/action_controller/metal/flash.rb +++ b/actionpack/lib/action_controller/metal/flash.rb @@ -3,19 +3,34 @@ module ActionController #:nodoc: extend ActiveSupport::Concern included do - delegate :flash, :to => :request - delegate :alert, :notice, :to => "request.flash" - helper_method :alert, :notice + class_attribute :_flash_types, instance_accessor: false + self._flash_types = [] + + delegate :flash, to: :request + add_flash_types(:alert, :notice) end - protected - def redirect_to(options = {}, response_status_and_flash = {}) #:doc: - if alert = response_status_and_flash.delete(:alert) - flash[:alert] = alert + module ClassMethods + def add_flash_types(*types) + types.each do |type| + next if _flash_types.include?(type) + + define_method(type) do + request.flash[type] + end + helper_method type + + _flash_types << type end + end + end - if notice = response_status_and_flash.delete(:notice) - flash[:notice] = notice + protected + def redirect_to(options = {}, response_status_and_flash = {}) #:doc: + self.class._flash_types.each do |flash_type| + if type = response_status_and_flash.delete(flash_type) + flash[flash_type] = type + end end if other_flashes = response_status_and_flash.delete(:flash) diff --git a/actionpack/lib/action_controller/metal/rack_delegation.rb b/actionpack/lib/action_controller/metal/rack_delegation.rb index 544b4989c7..bdf6e88699 100644 --- a/actionpack/lib/action_controller/metal/rack_delegation.rb +++ b/actionpack/lib/action_controller/metal/rack_delegation.rb @@ -8,9 +8,8 @@ module ActionController delegate :headers, :status=, :location=, :content_type=, :status, :location, :content_type, :to => "@_response" - def dispatch(action, request, response = ActionDispatch::Response.new) - @_response ||= response - @_response.request ||= request + def dispatch(action, request) + set_response!(request) super(action, request) end @@ -22,5 +21,12 @@ module ActionController def reset_session @_request.reset_session end + + private + + def set_response!(request) + @_response = ActionDispatch::Response.new + @_response.request = request + end end end diff --git a/actionpack/lib/action_controller/metal/testing.rb b/actionpack/lib/action_controller/metal/testing.rb index d1813ee745..0377b8c4cf 100644 --- a/actionpack/lib/action_controller/metal/testing.rb +++ b/actionpack/lib/action_controller/metal/testing.rb @@ -4,30 +4,25 @@ module ActionController include RackDelegation - def recycle! - @_url_options = nil - end - - - # TODO: Clean this up - def process_with_new_base_test(request, response) - @_request = request - @_response = response - @_response.request = request - ret = process(request.parameters[:action]) - if cookies = @_request.env['action_dispatch.cookies'] - cookies.write(@_response) - end - @_response.prepare! - ret - end - # TODO : Rewrite tests using controller.headers= to use Rack env def headers=(new_headers) @_response ||= ActionDispatch::Response.new @_response.headers.replace(new_headers) end + # Behavior specific to functional tests + module Functional # :nodoc: + def set_response!(request) + end + + def recycle! + @_url_options = nil + self.response_body = nil + self.formats = nil + self.params = nil + end + end + module ClassMethods def before_filters _process_action_callbacks.find_all{|x| x.kind == :before}.map{|x| x.name} diff --git a/actionpack/lib/action_controller/model_naming.rb b/actionpack/lib/action_controller/model_naming.rb new file mode 100644 index 0000000000..785221dc3d --- /dev/null +++ b/actionpack/lib/action_controller/model_naming.rb @@ -0,0 +1,12 @@ +module ActionController + module ModelNaming + # Converts the given object to an ActiveModel compliant one. + def convert_to_model(object) + object.respond_to?(:to_model) ? object.to_model : object + end + + def model_name_from_record_or_class(record_or_class) + (record_or_class.is_a?(Class) ? record_or_class : convert_to_model(record_or_class).class).model_name + end + end +end diff --git a/actionpack/lib/action_controller/record_identifier.rb b/actionpack/lib/action_controller/record_identifier.rb index 16a5decc62..d3ac406618 100644 --- a/actionpack/lib/action_controller/record_identifier.rb +++ b/actionpack/lib/action_controller/record_identifier.rb @@ -1,4 +1,5 @@ require 'active_support/core_ext/module' +require 'action_controller/model_naming' module ActionController # The record identifier encapsulates a number of naming conventions for dealing with records, like Active Records or @@ -27,6 +28,8 @@ module ActionController module RecordIdentifier extend self + include ModelNaming + JOIN = '_'.freeze NEW = 'new'.freeze @@ -40,7 +43,7 @@ module ActionController # dom_class(post, :edit) # => "edit_post" # dom_class(Person, :edit) # => "edit_person" def dom_class(record_or_class, prefix = nil) - singular = ActiveModel::Naming.param_key(record_or_class) + singular = model_name_from_record_or_class(record_or_class).param_key prefix ? "#{prefix}#{JOIN}#{singular}" : singular end @@ -73,8 +76,7 @@ module ActionController # method that replaces all characters that are invalid inside DOM ids, with valid ones. You need to # make sure yourself that your dom ids are valid, in case you overwrite this method. def record_key_for_dom_id(record) - record = record.to_model if record.respond_to?(:to_model) - key = record.to_key + key = convert_to_model(record).to_key key ? key.join('_') : key end end diff --git a/actionpack/lib/action_controller/test_case.rb b/actionpack/lib/action_controller/test_case.rb index a1f29ea1bc..0498b9d138 100644 --- a/actionpack/lib/action_controller/test_case.rb +++ b/actionpack/lib/action_controller/test_case.rb @@ -143,6 +143,9 @@ module ActionController end class TestRequest < ActionDispatch::TestRequest #:nodoc: + DEFAULT_ENV = ActionDispatch::TestRequest::DEFAULT_ENV.dup + DEFAULT_ENV.delete 'PATH_INFO' + def initialize(env = {}) super @@ -150,10 +153,6 @@ module ActionController self.session_options = TestSession::DEFAULT_OPTIONS.merge(:id => SecureRandom.hex(16)) end - class Result < ::Array #:nodoc: - def to_s() join '/' end - end - def assign_parameters(routes, controller_path, action, parameters = {}) parameters = parameters.symbolize_keys.merge(:controller => controller_path, :action => action) extra_keys = routes.extra_keys(parameters) @@ -171,7 +170,7 @@ module ActionController non_path_parameters[key] = value else if value.is_a?(Array) - value = Result.new(value.map(&:to_param)) + value = value.map(&:to_param) else value = value.to_param end @@ -211,6 +210,12 @@ module ActionController cookie_jar.update(@set_cookies) cookie_jar.recycle! end + + private + + def default_env + DEFAULT_ENV + end end class TestResponse < ActionDispatch::TestResponse @@ -430,8 +435,13 @@ module ActionController end # Executes a request simulating HEAD HTTP method and set/volley the response - def head(action, parameters = nil, session = nil, flash = nil) - process(action, "HEAD", parameters, session, flash) + def head(action, *args) + process(action, "HEAD", *args) + end + + # Executes a request simulating OPTIONS HTTP method and set/volley the response + def options(action, *args) + process(action, "OPTIONS", *args) end def xml_http_request(request_method, action, parameters = nil, session = nil, flash = nil) @@ -471,13 +481,17 @@ module ActionController # proper params, as is the case when engaging rack. parameters = paramify_values(parameters) if html_format?(parameters) + @html_document = nil + + unless @controller.respond_to?(:recycle!) + @controller.extend(Testing::Functional) + @controller.class.class_eval { include Testing } + end + @request.recycle! @response.recycle! - @controller.response_body = nil - @controller.formats = nil - @controller.params = nil + @controller.recycle! - @html_document = nil @request.env['REQUEST_METHOD'] = http_method parameters ||= {} @@ -490,26 +504,34 @@ module ActionController @request.session.update(session) if session @request.session["flash"] = @request.flash.update(flash || {}) - @controller.request = @request + @controller.request = @request + @controller.response = @response + build_request_uri(action, parameters) - @controller.class.class_eval { include Testing } - @controller.recycle! - @controller.process_with_new_base_test(@request, @response) + + name = @request.parameters[:action] + + @controller.process(name) + + if cookies = @request.env['action_dispatch.cookies'] + cookies.write(@response) + end + @response.prepare! + @assigns = @controller.respond_to?(:view_assigns) ? @controller.view_assigns : {} @request.session.delete('flash') if @request.session['flash'].blank? @response end def setup_controller_request_and_response - @request = TestRequest.new - @response = TestResponse.new + @request = TestRequest.new + @response = TestResponse.new + @response.request = @request if klass = self.class.controller_class @controller ||= klass.new rescue nil end - @request.env.delete('PATH_INFO') - if defined?(@controller) && @controller @controller.request = @request @controller.params = {} @@ -523,7 +545,7 @@ module ActionController setup :setup_controller_request_and_response end - private + private def check_required_ivars # Sanity check for required instance variables so we can give an # understandable error message. @@ -564,8 +586,7 @@ module ActionController def html_format?(parameters) return true unless parameters.is_a?(Hash) - format = Mime[parameters[:format]] - format.nil? || format.html? + Mime.fetch(parameters[:format]) { Mime['html'] }.html? end end diff --git a/actionpack/lib/action_dispatch/http/mime_type.rb b/actionpack/lib/action_dispatch/http/mime_type.rb index ee1913dbf9..fe39c220a5 100644 --- a/actionpack/lib/action_dispatch/http/mime_type.rb +++ b/actionpack/lib/action_dispatch/http/mime_type.rb @@ -29,6 +29,11 @@ module Mime Type.lookup_by_extension(type.to_s) end + def self.fetch(type) + return type if type.is_a?(Type) + EXTENSION_LOOKUP.fetch(type.to_s) { |k| yield k } + end + # Encapsulates the notion of a mime type. Can be used at render time, for example, with: # # class PostsController < ActionController::Base @@ -53,6 +58,8 @@ module Mime cattr_reader :browser_generated_types attr_reader :symbol + @register_callbacks = [] + # A simple helper class used in parsing the accept header class AcceptItem #:nodoc: attr_accessor :order, :name, :q @@ -84,6 +91,10 @@ module Mime TRAILING_STAR_REGEXP = /(text|application)\/\*/ PARAMETER_SEPARATOR_REGEXP = /;\s*\w+="?\w+"?/ + def register_callback(&block) + @register_callbacks << block + end + def lookup(string) LOOKUP[string] end @@ -101,10 +112,15 @@ module Mime def register(string, symbol, mime_type_synonyms = [], extension_synonyms = [], skip_lookup = false) Mime.const_set(symbol.upcase, Type.new(string, symbol, mime_type_synonyms)) - SET << Mime.const_get(symbol.upcase) + new_mime = Mime.const_get(symbol.upcase) + SET << new_mime ([string] + mime_type_synonyms).each { |str| LOOKUP[str] = SET.last } unless skip_lookup ([symbol] + extension_synonyms).each { |ext| EXTENSION_LOOKUP[ext.to_s] = SET.last } + + @register_callbacks.each do |callback| + callback.call(new_mime) + end end def parse(accept_header) diff --git a/actionpack/lib/action_dispatch/routing/mapper.rb b/actionpack/lib/action_dispatch/routing/mapper.rb index 94242ad962..53a4afecb3 100644 --- a/actionpack/lib/action_dispatch/routing/mapper.rb +++ b/actionpack/lib/action_dispatch/routing/mapper.rb @@ -1316,22 +1316,6 @@ module ActionDispatch parent_resource.instance_of?(Resource) && @scope[:shallow] end - def draw(name) - path = @draw_paths.find do |_path| - File.exists? "#{_path}/#{name}.rb" - end - - unless path - msg = "Your router tried to #draw the external file #{name}.rb,\n" \ - "but the file was not found in:\n\n" - msg += @draw_paths.map { |_path| " * #{_path}" }.join("\n") - raise ArgumentError, msg - end - - route_path = "#{path}/#{name}.rb" - instance_eval(File.read(route_path), route_path.to_s) - end - # match 'path' => 'controller#action' # match 'path', to: 'controller#action' # match 'path', 'otherpath', on: :member, via: :get @@ -1581,7 +1565,6 @@ module ActionDispatch def initialize(set) #:nodoc: @set = set - @draw_paths = set.draw_paths @scope = { :path_names => @set.resources_path_names } end diff --git a/actionpack/lib/action_dispatch/routing/polymorphic_routes.rb b/actionpack/lib/action_dispatch/routing/polymorphic_routes.rb index 86ce7a83b9..3d7b8878b8 100644 --- a/actionpack/lib/action_dispatch/routing/polymorphic_routes.rb +++ b/actionpack/lib/action_dispatch/routing/polymorphic_routes.rb @@ -1,3 +1,5 @@ +require 'action_controller/model_naming' + module ActionDispatch module Routing # Polymorphic URL helpers are methods for smart resolution to a named route call when @@ -53,6 +55,8 @@ module ActionDispatch # form_for([blog, @post]) # => "/blog/posts/1" # module PolymorphicRoutes + include ActionController::ModelNaming + # Constructs a call to a named RESTful route for the given record and returns the # resulting URL string. For example: # @@ -154,10 +158,6 @@ module ActionDispatch options[:action] ? "#{options[:action]}_" : '' end - def convert_to_model(object) - object.respond_to?(:to_model) ? object.to_model : object - end - def routing_type(options) options[:routing_type] || :url end @@ -169,7 +169,7 @@ module ActionDispatch if parent.is_a?(Symbol) || parent.is_a?(String) parent else - ActiveModel::Naming.singular_route_key(parent) + model_name_from_record_or_class(parent).singular_route_key end end else @@ -181,9 +181,9 @@ module ActionDispatch route << record elsif record if inflection == :singular - route << ActiveModel::Naming.singular_route_key(record) + route << model_name_from_record_or_class(record).singular_route_key else - route << ActiveModel::Naming.route_key(record) + route << model_name_from_record_or_class(record).route_key end else raise ArgumentError, "Nil location provided. Can't build URI." diff --git a/actionpack/lib/action_dispatch/routing/route_set.rb b/actionpack/lib/action_dispatch/routing/route_set.rb index 64b1d58ae9..1d6ca0c78d 100644 --- a/actionpack/lib/action_dispatch/routing/route_set.rb +++ b/actionpack/lib/action_dispatch/routing/route_set.rb @@ -237,7 +237,6 @@ module ActionDispatch attr_accessor :formatter, :set, :named_routes, :default_scope, :router attr_accessor :disable_clear_and_finalize, :resources_path_names attr_accessor :default_url_options, :request_class, :valid_conditions - attr_accessor :draw_paths alias :routes :set @@ -249,7 +248,6 @@ module ActionDispatch self.named_routes = NamedRouteCollection.new self.resources_path_names = self.class.default_resources_path_names.dup self.default_url_options = {} - self.draw_paths = [] self.request_class = request_class @valid_conditions = { :controller => true, :action => true } diff --git a/actionpack/lib/action_dispatch/testing/test_request.rb b/actionpack/lib/action_dispatch/testing/test_request.rb index a86b510719..639ae6f398 100644 --- a/actionpack/lib/action_dispatch/testing/test_request.rb +++ b/actionpack/lib/action_dispatch/testing/test_request.rb @@ -12,7 +12,7 @@ module ActionDispatch def initialize(env = {}) env = Rails.application.env_config.merge(env) if defined?(Rails.application) && Rails.application - super(DEFAULT_ENV.merge(env)) + super(default_env.merge(env)) self.host = 'test.host' self.remote_addr = '0.0.0.0' @@ -69,5 +69,11 @@ module ActionDispatch def cookies @cookies ||= {}.with_indifferent_access end + + private + + def default_env + DEFAULT_ENV + end end end diff --git a/actionpack/lib/action_view/base.rb b/actionpack/lib/action_view/base.rb index f98648d930..7bfbc1f0aa 100644 --- a/actionpack/lib/action_view/base.rb +++ b/actionpack/lib/action_view/base.rb @@ -148,7 +148,6 @@ module ActionView #:nodoc: cattr_accessor :prefix_partial_path_with_controller_namespace @@prefix_partial_path_with_controller_namespace = true - class_attribute :helpers class_attribute :_routes class_attribute :logger @@ -166,22 +165,6 @@ module ActionView #:nodoc: def xss_safe? #:nodoc: true end - - # This method receives routes and helpers from the controller - # and return a subclass ready to be used as view context. - def prepare(routes, helpers) #:nodoc: - Class.new(self) do - if routes - include routes.url_helpers - include routes.mounted_helpers - end - - if helpers - include helpers - self.helpers = helpers - end - end - end end attr_accessor :view_renderer diff --git a/actionpack/lib/action_view/helpers/form_helper.rb b/actionpack/lib/action_view/helpers/form_helper.rb index ba2a26fd09..b34f6c8650 100644 --- a/actionpack/lib/action_view/helpers/form_helper.rb +++ b/actionpack/lib/action_view/helpers/form_helper.rb @@ -12,6 +12,7 @@ require 'active_support/core_ext/string/output_safety' require 'active_support/core_ext/array/extract_options' require 'active_support/deprecation' require 'active_support/core_ext/string/inflections' +require 'action_controller/model_naming' module ActionView # = Action View Form Helpers @@ -117,11 +118,7 @@ module ActionView include FormTagHelper include UrlHelper - - # Converts the given object to an ActiveModel compliant one. - def convert_to_model(object) - object.respond_to?(:to_model) ? object.to_model : object - end + include ActionController::ModelNaming # Creates a form that allows the user to create or update the attributes # of a specific model object. @@ -411,7 +408,7 @@ module ActionView object = nil else object = record.is_a?(Array) ? record.last : record - object_name = options[:as] || ActiveModel::Naming.param_key(object) + object_name = options[:as] || model_name_from_record_or_class(object).param_key apply_form_for_options!(record, object, options) end @@ -1128,7 +1125,7 @@ module ActionView object_name = record_name else object = record_name - object_name = ActiveModel::Naming.param_key(object) + object_name = model_name_from_record_or_class(object).param_key end builder = options[:builder] || default_form_builder @@ -1142,9 +1139,11 @@ module ActionView end class FormBuilder + include ActionController::ModelNaming + # The methods which wrap a form helper call. class_attribute :field_helpers - self.field_helpers = FormHelper.instance_methods - [:form_for, :convert_to_model] + self.field_helpers = FormHelper.instance_methods - [:form_for, :convert_to_model, :model_name_from_record_or_class] attr_accessor :object_name, :object, :options @@ -1214,7 +1213,7 @@ module ActionView end else record_object = record_name.is_a?(Array) ? record_name.last : record_name - record_name = ActiveModel::Naming.param_key(record_object) + record_name = model_name_from_record_or_class(record_object).param_key end index = if options.has_key?(:index) @@ -1396,10 +1395,6 @@ module ActionView @nested_child_index[name] ||= -1 @nested_child_index[name] += 1 end - - def convert_to_model(object) - object.respond_to?(:to_model) ? object.to_model : object - end end end diff --git a/actionpack/lib/action_view/helpers/number_helper.rb b/actionpack/lib/action_view/helpers/number_helper.rb index 8f97d1f014..9720e90429 100644 --- a/actionpack/lib/action_view/helpers/number_helper.rb +++ b/actionpack/lib/action_view/helpers/number_helper.rb @@ -59,7 +59,7 @@ module ActionView return unless number options = options.symbolize_keys - parse_float(number, true) if options[:raise] + parse_float(number, true) if options.delete(:raise) ERB::Util.html_escape(ActiveSupport::NumberHelper.number_to_phone(number, options)) end @@ -109,7 +109,9 @@ module ActionView return unless number options = escape_unsafe_delimiters_and_separators(options.symbolize_keys) - wrap_with_output_safety_handling(number, options[:raise]){ ActiveSupport::NumberHelper.number_to_currency(number, options) } + wrap_with_output_safety_handling(number, options.delete(:raise)) { + ActiveSupport::NumberHelper.number_to_currency(number, options) + } end # Formats a +number+ as a percentage string (e.g., 65%). You can @@ -152,7 +154,9 @@ module ActionView return unless number options = escape_unsafe_delimiters_and_separators(options.symbolize_keys) - wrap_with_output_safety_handling(number, options[:raise]){ ActiveSupport::NumberHelper.number_to_percentage(number, options) } + wrap_with_output_safety_handling(number, options.delete(:raise)) { + ActiveSupport::NumberHelper.number_to_percentage(number, options) + } end # Formats a +number+ with grouped thousands using +delimiter+ @@ -187,7 +191,9 @@ module ActionView def number_with_delimiter(number, options = {}) options = escape_unsafe_delimiters_and_separators(options.symbolize_keys) - wrap_with_output_safety_handling(number, options[:raise]){ ActiveSupport::NumberHelper.number_to_delimited(number, options) } + wrap_with_output_safety_handling(number, options.delete(:raise)) { + ActiveSupport::NumberHelper.number_to_delimited(number, options) + } end # Formats a +number+ with the specified level of @@ -234,7 +240,9 @@ module ActionView def number_with_precision(number, options = {}) options = escape_unsafe_delimiters_and_separators(options.symbolize_keys) - wrap_with_output_safety_handling(number, options[:raise]){ ActiveSupport::NumberHelper.number_to_rounded(number, options) } + wrap_with_output_safety_handling(number, options.delete(:raise)) { + ActiveSupport::NumberHelper.number_to_rounded(number, options) + } end @@ -288,7 +296,9 @@ module ActionView def number_to_human_size(number, options = {}) options = escape_unsafe_delimiters_and_separators(options.symbolize_keys) - wrap_with_output_safety_handling(number, options[:raise]){ ActiveSupport::NumberHelper.number_to_human_size(number, options) } + wrap_with_output_safety_handling(number, options.delete(:raise)) { + ActiveSupport::NumberHelper.number_to_human_size(number, options) + } end # Pretty prints (formats and approximates) a number in a way it @@ -392,7 +402,9 @@ module ActionView def number_to_human(number, options = {}) options = escape_unsafe_delimiters_and_separators(options.symbolize_keys) - wrap_with_output_safety_handling(number, options[:raise]){ ActiveSupport::NumberHelper.number_to_human(number, options) } + wrap_with_output_safety_handling(number, options.delete(:raise)) { + ActiveSupport::NumberHelper.number_to_human(number, options) + } end private diff --git a/actionpack/lib/action_view/renderer/abstract_renderer.rb b/actionpack/lib/action_view/renderer/abstract_renderer.rb index 72616b7463..e3d8e9d508 100644 --- a/actionpack/lib/action_view/renderer/abstract_renderer.rb +++ b/actionpack/lib/action_view/renderer/abstract_renderer.rb @@ -1,7 +1,6 @@ module ActionView class AbstractRenderer #:nodoc: - delegate :find_template, :template_exists?, :with_fallbacks, :update_details, - :with_layout_format, :formats, :to => :@lookup_context + delegate :find_template, :template_exists?, :with_fallbacks, :with_layout_format, :formats, :to => :@lookup_context def initialize(lookup_context) @lookup_context = lookup_context diff --git a/actionpack/test/controller/filters_test.rb b/actionpack/test/controller/filters_test.rb index ef7fbca675..b9cb93f0f4 100644 --- a/actionpack/test/controller/filters_test.rb +++ b/actionpack/test/controller/filters_test.rb @@ -326,6 +326,12 @@ class FilterTest < ActionController::TestCase controller.instance_variable_set(:"@after_ran", true) controller.class.execution_log << " after aroundfilter " if controller.respond_to? :execution_log end + + def around(controller) + before(controller) + yield + after(controller) + end end class AppendedAroundFilter @@ -336,6 +342,12 @@ class FilterTest < ActionController::TestCase def after(controller) controller.class.execution_log << " after appended aroundfilter " end + + def around(controller) + before(controller) + yield + after(controller) + end end class AuditController < ActionController::Base diff --git a/actionpack/test/controller/flash_test.rb b/actionpack/test/controller/flash_test.rb index e4b34125ad..8340aab4d2 100644 --- a/actionpack/test/controller/flash_test.rb +++ b/actionpack/test/controller/flash_test.rb @@ -90,6 +90,10 @@ class FlashTest < ActionController::TestCase def redirect_with_other_flashes redirect_to '/wonderland', :flash => { :joyride => "Horses!" } end + + def redirect_with_foo_flash + redirect_to "/wonderland", :foo => 'for great justice' + end end tests TestController @@ -203,6 +207,12 @@ class FlashTest < ActionController::TestCase get :redirect_with_other_flashes assert_equal "Horses!", @controller.send(:flash)[:joyride] end + + def test_redirect_to_with_adding_flash_types + @controller.class.add_flash_types :foo + get :redirect_with_foo_flash + assert_equal "for great justice", @controller.send(:flash)[:foo] + end end class FlashIntegrationTest < ActionDispatch::IntegrationTest @@ -210,9 +220,7 @@ class FlashIntegrationTest < ActionDispatch::IntegrationTest SessionSecret = 'b3c631c314c0bbca50c1b2843150fe33' class TestController < ActionController::Base - def dont_set_flash - head :ok - end + add_flash_types :bar def set_flash flash["that"] = "hello" @@ -227,6 +235,11 @@ class FlashIntegrationTest < ActionDispatch::IntegrationTest def use_flash render :inline => "flash: #{flash["that"]}" end + + def set_bar + flash[:bar] = "for great justice" + head :ok + end end def test_flash @@ -266,6 +279,14 @@ class FlashIntegrationTest < ActionDispatch::IntegrationTest end end + def test_added_flash_types_method + with_test_route_set do + get '/set_bar' + assert_response :success + assert_equal 'for great justice', @controller.bar + end + end + private # Overwrite get to send SessionSecret in env hash diff --git a/actionpack/test/controller/mime_responds_test.rb b/actionpack/test/controller/mime_responds_test.rb index bdcd5561a8..c8e036b116 100644 --- a/actionpack/test/controller/mime_responds_test.rb +++ b/actionpack/test/controller/mime_responds_test.rb @@ -505,7 +505,7 @@ class RespondToControllerTest < ActionController::TestCase end class RespondWithController < ActionController::Base - respond_to :html, :json + respond_to :html, :json, :touch respond_to :xml, :except => :using_resource_with_block respond_to :js, :only => [ :using_resource_with_block, :using_resource, 'using_hash_resource' ] @@ -623,12 +623,14 @@ class RespondWithControllerTest < ActionController::TestCase super @request.host = "www.example.com" Mime::Type.register_alias('text/html', :iphone) + Mime::Type.register_alias('text/html', :touch) Mime::Type.register('text/x-mobile', :mobile) end def teardown super Mime::Type.unregister(:iphone) + Mime::Type.unregister(:touch) Mime::Type.unregister(:mobile) end diff --git a/actionpack/test/controller/test_case_test.rb b/actionpack/test/controller/test_case_test.rb index 49137946fe..8990fc34d6 100644 --- a/actionpack/test/controller/test_case_test.rb +++ b/actionpack/test/controller/test_case_test.rb @@ -197,6 +197,11 @@ XML assert_raise(NoMethodError) { head :test_params, "document body", :id => 10 } end + def test_options + options :test_params + assert_equal 200, @response.status + end + def test_process_without_flash process :set_flash assert_equal '><', flash['test'] @@ -635,7 +640,7 @@ XML get :test_params, :path => ['hello', 'world'] assert_equal ['hello', 'world'], @request.path_parameters['path'] - assert_equal 'hello/world', @request.path_parameters['path'].to_s + assert_equal 'hello/world', @request.path_parameters['path'].to_param end end @@ -913,4 +918,4 @@ class AnonymousControllerTest < ActionController::TestCase get :index assert_equal 'anonymous', @response.body end -end
\ No newline at end of file +end diff --git a/actionpack/test/dispatch/mapper_test.rb b/actionpack/test/dispatch/mapper_test.rb index bd078d2b21..8070bdec8a 100644 --- a/actionpack/test/dispatch/mapper_test.rb +++ b/actionpack/test/dispatch/mapper_test.rb @@ -4,12 +4,11 @@ module ActionDispatch module Routing class MapperTest < ActiveSupport::TestCase class FakeSet - attr_reader :routes, :draw_paths + attr_reader :routes alias :set :routes def initialize @routes = [] - @draw_paths = [] end def resources_path_names diff --git a/actionpack/test/dispatch/mime_type_test.rb b/actionpack/test/dispatch/mime_type_test.rb index 9d77c3acc5..ed012093a7 100644 --- a/actionpack/test/dispatch/mime_type_test.rb +++ b/actionpack/test/dispatch/mime_type_test.rb @@ -118,6 +118,20 @@ class MimeTypeTest < ActiveSupport::TestCase end end + test "register callbacks" do + begin + registered_mimes = [] + Mime::Type.register_callback do |mime| + registered_mimes << mime + end + + Mime::Type.register("text/foo", :foo) + assert_equal registered_mimes, [Mime::FOO] + ensure + Mime::Type.unregister(:FOO) + end + end + test "custom type with extension aliases" do begin Mime::Type.register "text/foobar", :foobar, [], [:foo, "bar"] diff --git a/actionpack/test/dispatch/routing_test.rb b/actionpack/test/dispatch/routing_test.rb index f15dd7214b..fed26d89f8 100644 --- a/actionpack/test/dispatch/routing_test.rb +++ b/actionpack/test/dispatch/routing_test.rb @@ -2324,55 +2324,6 @@ class TestNamespaceWithControllerOption < ActionDispatch::IntegrationTest end end -class TestDrawExternalFile < ActionDispatch::IntegrationTest - class ExternalController < ActionController::Base - def index - render :text => "external#index" - end - end - - DRAW_PATH = File.expand_path('../../fixtures/routes', __FILE__) - - DefaultScopeRoutes = ActionDispatch::Routing::RouteSet.new.tap do |app| - app.draw_paths << DRAW_PATH - end - - def app - DefaultScopeRoutes - end - - def test_draw_external_file - DefaultScopeRoutes.draw do - scope :module => 'test_draw_external_file' do - draw :external - end - end - - get '/external' - assert_equal "external#index", @response.body - end - - def test_draw_nonexistent_file - exception = assert_raise ArgumentError do - DefaultScopeRoutes.draw do - draw :nonexistent - end - end - assert_match 'Your router tried to #draw the external file nonexistent.rb', exception.message - assert_match DRAW_PATH.to_s, exception.message - end - - def test_draw_bogus_file - exception = assert_raise NoMethodError do - DefaultScopeRoutes.draw do - draw :bogus - end - end - assert_match "undefined method `wrong'", exception.message - assert_match 'test/fixtures/routes/bogus.rb:1', exception.backtrace.first - end -end - class TestDefaultScope < ActionDispatch::IntegrationTest module ::Blog class PostsController < ActionController::Base diff --git a/actionpack/test/fixtures/routes/bogus.rb b/actionpack/test/fixtures/routes/bogus.rb deleted file mode 100644 index 41fbf0cd64..0000000000 --- a/actionpack/test/fixtures/routes/bogus.rb +++ /dev/null @@ -1 +0,0 @@ -wrong :route diff --git a/actionpack/test/fixtures/routes/external.rb b/actionpack/test/fixtures/routes/external.rb deleted file mode 100644 index d103c39f53..0000000000 --- a/actionpack/test/fixtures/routes/external.rb +++ /dev/null @@ -1 +0,0 @@ -get '/external' => 'external#index' diff --git a/actionpack/test/template/number_helper_i18n_test.rb b/actionpack/test/template/number_helper_i18n_test.rb deleted file mode 100644 index d6e9de9555..0000000000 --- a/actionpack/test/template/number_helper_i18n_test.rb +++ /dev/null @@ -1,122 +0,0 @@ -require 'abstract_unit' - -class NumberHelperTest < ActionView::TestCase - tests ActionView::Helpers::NumberHelper - - def setup - I18n.backend.store_translations 'ts', - :number => { - :format => { :precision => 3, :delimiter => ',', :separator => '.', :significant => false, :strip_insignificant_zeros => false }, - :currency => { :format => { :unit => '&$', :format => '%u - %n', :negative_format => '(%u - %n)', :precision => 2 } }, - :human => { - :format => { - :precision => 2, - :significant => true, - :strip_insignificant_zeros => true - }, - :storage_units => { - :format => "%n %u", - :units => { - :byte => "b", - :kb => "k" - } - }, - :decimal_units => { - :format => "%n %u", - :units => { - :deci => {:one => "Tenth", :other => "Tenths"}, - :unit => "u", - :ten => {:one => "Ten", :other => "Tens"}, - :thousand => "t", - :million => "m" , - :billion =>"b" , - :trillion =>"t" , - :quadrillion =>"q" - } - } - }, - :percentage => { :format => {:delimiter => '', :precision => 2, :strip_insignificant_zeros => true} }, - :precision => { :format => {:delimiter => '', :significant => true} } - }, - :custom_units_for_number_to_human => {:mili => "mm", :centi => "cm", :deci => "dm", :unit => "m", :ten => "dam", :hundred => "hm", :thousand => "km"} - end - - def test_number_to_i18n_currency - assert_equal("&$ - 10.00", number_to_currency(10, :locale => 'ts')) - assert_equal("(&$ - 10.00)", number_to_currency(-10, :locale => 'ts')) - assert_equal("-10.00 - &$", number_to_currency(-10, :locale => 'ts', :format => "%n - %u")) - end - - def test_number_to_currency_with_clean_i18n_settings - clean_i18n do - assert_equal("$10.00", number_to_currency(10)) - assert_equal("-$10.00", number_to_currency(-10)) - end - end - - def test_number_to_currency_without_currency_negative_format - clean_i18n do - I18n.backend.store_translations 'ts', :number => { :currency => { :format => { :unit => '@', :format => '%n %u' } } } - assert_equal("-10.00 @", number_to_currency(-10, :locale => 'ts')) - end - end - - def test_number_with_i18n_precision - #Delimiter was set to "" - assert_equal("10000", number_with_precision(10000, :locale => 'ts')) - - #Precision inherited and significant was set - assert_equal("1.00", number_with_precision(1.0, :locale => 'ts')) - - end - - def test_number_with_i18n_delimiter - #Delimiter "," and separator "." - assert_equal("1,000,000.234", number_with_delimiter(1000000.234, :locale => 'ts')) - end - - def test_number_to_i18n_percentage - # to see if strip_insignificant_zeros is true - assert_equal("1%", number_to_percentage(1, :locale => 'ts')) - # precision is 2, significant should be inherited - assert_equal("1.24%", number_to_percentage(1.2434, :locale => 'ts')) - # no delimiter - assert_equal("12434%", number_to_percentage(12434, :locale => 'ts')) - end - - def test_number_to_i18n_human_size - #b for bytes and k for kbytes - assert_equal("2 k", number_to_human_size(2048, :locale => 'ts')) - assert_equal("42 b", number_to_human_size(42, :locale => 'ts')) - end - - def test_number_to_human_with_default_translation_scope - #Using t for thousand - assert_equal "2 t", number_to_human(2000, :locale => 'ts') - #Significant was set to true with precision 2, using b for billion - assert_equal "1.2 b", number_to_human(1234567890, :locale => 'ts') - #Using pluralization (Ten/Tens and Tenth/Tenths) - assert_equal "1 Tenth", number_to_human(0.1, :locale => 'ts') - assert_equal "1.3 Tenth", number_to_human(0.134, :locale => 'ts') - assert_equal "2 Tenths", number_to_human(0.2, :locale => 'ts') - assert_equal "1 Ten", number_to_human(10, :locale => 'ts') - assert_equal "1.2 Ten", number_to_human(12, :locale => 'ts') - assert_equal "2 Tens", number_to_human(20, :locale => 'ts') - end - - def test_number_to_human_with_custom_translation_scope - #Significant was set to true with precision 2, with custom translated units - assert_equal "4.3 cm", number_to_human(0.0432, :locale => 'ts', :units => :custom_units_for_number_to_human) - end - - private - def clean_i18n - load_path = I18n.load_path.dup - I18n.load_path.clear - I18n.reload! - yield - ensure - I18n.load_path = load_path - I18n.reload! - end -end diff --git a/actionpack/test/template/number_helper_test.rb b/actionpack/test/template/number_helper_test.rb index 057cb47f53..d8fffe75ed 100644 --- a/actionpack/test/template/number_helper_test.rb +++ b/actionpack/test/template/number_helper_test.rb @@ -361,69 +361,39 @@ class NumberHelperTest < ActionView::TestCase end def test_number_helpers_should_raise_error_if_invalid_when_specified - assert_raise InvalidNumberError do + exception = assert_raise InvalidNumberError do number_to_human("x", :raise => true) end - begin - number_to_human("x", :raise => true) - rescue InvalidNumberError => e - assert_equal "x", e.number - end + assert_equal "x", exception.number - assert_raise InvalidNumberError do - number_to_human_size("x", :raise => true) - end - begin + exception = assert_raise InvalidNumberError do number_to_human_size("x", :raise => true) - rescue InvalidNumberError => e - assert_equal "x", e.number end + assert_equal "x", exception.number - assert_raise InvalidNumberError do + exception = assert_raise InvalidNumberError do number_with_precision("x", :raise => true) end - begin - number_with_precision("x", :raise => true) - rescue InvalidNumberError => e - assert_equal "x", e.number - end + assert_equal "x", exception.number - assert_raise InvalidNumberError do + exception = assert_raise InvalidNumberError do number_to_currency("x", :raise => true) end - begin - number_with_precision("x", :raise => true) - rescue InvalidNumberError => e - assert_equal "x", e.number - end + assert_equal "x", exception.number - assert_raise InvalidNumberError do + exception = assert_raise InvalidNumberError do number_to_percentage("x", :raise => true) end - begin - number_to_percentage("x", :raise => true) - rescue InvalidNumberError => e - assert_equal "x", e.number - end + assert_equal "x", exception.number - assert_raise InvalidNumberError do - number_with_delimiter("x", :raise => true) - end - begin + exception = assert_raise InvalidNumberError do number_with_delimiter("x", :raise => true) - rescue InvalidNumberError => e - assert_equal "x", e.number end + assert_equal "x", exception.number - assert_raise InvalidNumberError do + exception = assert_raise InvalidNumberError do number_to_phone("x", :raise => true) end - begin - number_to_phone("x", :raise => true) - rescue InvalidNumberError => e - assert_equal "x", e.number - end - + assert_equal "x", exception.number end - end diff --git a/actionpack/test/template/test_case_test.rb b/actionpack/test/template/test_case_test.rb index c005f040eb..387aafebd4 100644 --- a/actionpack/test/template/test_case_test.rb +++ b/actionpack/test/template/test_case_test.rb @@ -68,14 +68,14 @@ module ActionView assert_nil self.class.determine_default_helper_class("String") end - test "delegates notice to request.flash" do - view.request.flash.expects(:notice).with("this message") - view.notice("this message") + test "delegates notice to request.flash[:notice]" do + view.request.flash.expects(:[]).with(:notice) + view.notice end - test "delegates alert to request.flash" do - view.request.flash.expects(:alert).with("this message") - view.alert("this message") + test "delegates alert to request.flash[:alert]" do + view.request.flash.expects(:[]).with(:alert) + view.alert end test "uses controller lookup context" do |