diff options
Diffstat (limited to 'actionpack')
36 files changed, 608 insertions, 1134 deletions
diff --git a/actionpack/lib/abstract_controller/layouts.rb b/actionpack/lib/abstract_controller/layouts.rb index ac2154dffc..a8bd2b80e1 100644 --- a/actionpack/lib/abstract_controller/layouts.rb +++ b/actionpack/lib/abstract_controller/layouts.rb @@ -19,15 +19,20 @@ module AbstractController end end + def clear_template_caches! + @found_layouts.clear if @found_layouts + super + end + def cache_layout(details) layout = @found_layouts - values = details.values_at(:formats, :locale) + key = Thread.current[:format_locale_key] # Cache nil - if layout.key?(values) - return layout[values] + if layout.key?(key) + return layout[key] else - layout[values] = yield + layout[key] = yield end end diff --git a/actionpack/lib/abstract_controller/rendering_controller.rb b/actionpack/lib/abstract_controller/rendering_controller.rb index bb7891fbfd..feca1bc4b7 100644 --- a/actionpack/lib/abstract_controller/rendering_controller.rb +++ b/actionpack/lib/abstract_controller/rendering_controller.rb @@ -111,12 +111,21 @@ module AbstractController def _determine_template(options) name = (options[:_template_name] || action_name).to_s - options[:_template] ||= view_paths.find( - name, { :formats => formats }, options[:_prefix], options[:_partial] - ) + options[:_template] ||= with_template_cache(name) do + view_paths.find( + name, { :formats => formats }, options[:_prefix], options[:_partial] + ) + end + end + + def with_template_cache(name) + yield end module ClassMethods + def clear_template_caches! + end + # Append a path to the list of view paths for this controller. # # ==== Parameters @@ -134,6 +143,7 @@ module AbstractController # the default view path. You may also provide a custom view path # (see ActionView::ViewPathSet for more information) def prepend_view_path(path) + clear_template_caches! self.view_paths.unshift(path) end @@ -148,6 +158,7 @@ module AbstractController # paths<ViewPathSet, Object>:: If a ViewPathSet is provided, use that; # otherwise, process the parameter into a ViewPathSet. def view_paths=(paths) + clear_template_caches! self._view_paths = paths.is_a?(ActionView::PathSet) ? paths : ActionView::Base.process_view_paths(paths) end diff --git a/actionpack/lib/action_controller/metal/rendering_controller.rb b/actionpack/lib/action_controller/metal/rendering_controller.rb index 5b1be763ad..4da32ca1b3 100644 --- a/actionpack/lib/action_controller/metal/rendering_controller.rb +++ b/actionpack/lib/action_controller/metal/rendering_controller.rb @@ -1,20 +1,56 @@ module ActionController + class HashKey + @hash_keys = Hash.new {|h,k| h[k] = Hash.new {|h,k| h[k] = {} } } + + def self.get(klass, formats, locale) + @hash_keys[klass][formats][locale] ||= new(klass, formats, locale) + end + + attr_accessor :hash + def initialize(klass, formats, locale) + @formats, @locale = formats, locale + @hash = [formats, locale].hash + end + + alias_method :eql?, :equal? + + def inspect + "#<HashKey -- formats: #{@formats} locale: #{@locale}>" + end + end + module RenderingController extend ActiveSupport::Concern include AbstractController::RenderingController + module ClassMethods + def clear_template_caches! + ActionView::Partials::PartialRenderer::TEMPLATES.clear + template_cache.clear + super + end + + def template_cache + @template_cache ||= Hash.new {|h,k| h[k] = {} } + end + end + def process_action(*) self.formats = request.formats.map {|x| x.to_sym} + + super + end + + def _determine_template(*) super end def render(options) + Thread.current[:format_locale_key] = HashKey.get(self.class, formats, I18n.locale) + super - self.content_type ||= begin - mime = options[:_template].mime_type - formats.include?(mime && mime.to_sym) || formats.include?(:all) ? mime : Mime::Type.lookup_by_extension(formats.first) - end.to_s + self.content_type ||= options[:_template].mime_type.to_s response_body end @@ -34,6 +70,10 @@ module ActionController controller_path end + def with_template_cache(name) + self.class.template_cache[Thread.current[:format_locale_key]][name] ||= super + end + def _determine_template(options) if options.key?(:text) options[:_template] = ActionView::TextTemplate.new(options[:text], formats.first) diff --git a/actionpack/lib/action_controller/routing/resources.rb b/actionpack/lib/action_controller/routing/resources.rb index 4862cf7115..06506435a2 100644 --- a/actionpack/lib/action_controller/routing/resources.rb +++ b/actionpack/lib/action_controller/routing/resources.rb @@ -531,14 +531,14 @@ module ActionController with_options :controller => resource.controller do |map| map_associations(resource, options) + if block_given? + with_options(options.slice(*INHERITABLE_OPTIONS).merge(:path_prefix => resource.nesting_path_prefix, :name_prefix => resource.nesting_name_prefix), &block) + end + map_collection_actions(map, resource) map_default_collection_actions(map, resource) map_new_actions(map, resource) map_member_actions(map, resource) - - if block_given? - with_options(options.slice(*INHERITABLE_OPTIONS).merge(:path_prefix => resource.nesting_path_prefix, :name_prefix => resource.nesting_name_prefix), &block) - end end end @@ -546,16 +546,16 @@ module ActionController resource = SingletonResource.new(entities, options) with_options :controller => resource.controller do |map| - map_collection_actions(map, resource) - map_new_actions(map, resource) - map_member_actions(map, resource) - map_default_singleton_actions(map, resource) - map_associations(resource, options) if block_given? with_options(options.slice(*INHERITABLE_OPTIONS).merge(:path_prefix => resource.nesting_path_prefix, :name_prefix => resource.nesting_name_prefix), &block) end + + map_collection_actions(map, resource) + map_new_actions(map, resource) + map_member_actions(map, resource) + map_default_singleton_actions(map, resource) end end diff --git a/actionpack/lib/action_controller/routing/route_set.rb b/actionpack/lib/action_controller/routing/route_set.rb index 09f6024d39..a4f54ad662 100644 --- a/actionpack/lib/action_controller/routing/route_set.rb +++ b/actionpack/lib/action_controller/routing/route_set.rb @@ -407,22 +407,9 @@ module ActionController # don't use the recalled keys when determining which routes to check routes = routes_by_controller[controller][action][options.reject {|k,v| !v}.keys.sort_by { |x| x.object_id }] - routes[1].each_with_index do |route, index| + routes.each_with_index do |route, index| results = route.__send__(method, options, merged, expire_on) if results && (!results.is_a?(Array) || results.first) - - # Compare results with Rails 3.0 behavior - if routes[0][index] != route - routes[0].each do |route2| - new_results = route2.__send__(method, options, merged, expire_on) - if new_results && (!new_results.is_a?(Array) || new_results.first) - ActiveSupport::Deprecation.warn "The URL you generated will use the first matching route in routes.rb rather than the \"best\" match. " + - "In Rails 3.0 #{new_results} would of been generated instead of #{results}" - break - end - end - end - return results end end @@ -463,10 +450,7 @@ module ActionController @routes_by_controller ||= Hash.new do |controller_hash, controller| controller_hash[controller] = Hash.new do |action_hash, action| action_hash[action] = Hash.new do |key_hash, keys| - key_hash[keys] = [ - routes_for_controller_and_action_and_keys(controller, action, keys), - deprecated_routes_for_controller_and_action_and_keys(controller, action, keys) - ] + key_hash[keys] = routes_for_controller_and_action_and_keys(controller, action, keys) end end end @@ -487,15 +471,6 @@ module ActionController end end - def deprecated_routes_for_controller_and_action_and_keys(controller, action, keys) - selected = routes.select do |route| - route.matches_controller_and_action? controller, action - end - selected.sort_by do |route| - (keys - route.significant_keys).length - end - end - # Subclasses and plugins may override this method to extract further attributes # from the request, for use by route conditions and such. def extract_request_environment(request) diff --git a/actionpack/lib/action_controller/testing/process.rb b/actionpack/lib/action_controller/testing/process.rb index 09b1a59254..6bc7d60d76 100644 --- a/actionpack/lib/action_controller/testing/process.rb +++ b/actionpack/lib/action_controller/testing/process.rb @@ -148,7 +148,7 @@ module ActionController #:nodoc: def xml_http_request(request_method, action, parameters = nil, session = nil, flash = nil) @request.env['HTTP_X_REQUESTED_WITH'] = 'XMLHttpRequest' - @request.env['HTTP_ACCEPT'] = [Mime::JS, Mime::HTML, Mime::XML, 'text/xml', Mime::ALL].join(', ') + @request.env['HTTP_ACCEPT'] ||= [Mime::JS, Mime::HTML, Mime::XML, 'text/xml', Mime::ALL].join(', ') returning __send__(request_method, action, parameters, session, flash) do @request.env.delete 'HTTP_X_REQUESTED_WITH' @request.env.delete 'HTTP_ACCEPT' diff --git a/actionpack/lib/action_dispatch/http/request.rb b/actionpack/lib/action_dispatch/http/request.rb index b23306af62..bff030f0e4 100755 --- a/actionpack/lib/action_dispatch/http/request.rb +++ b/actionpack/lib/action_dispatch/http/request.rb @@ -106,16 +106,10 @@ module ActionDispatch @env["action_dispatch.request.accepts"] ||= begin header = @env['HTTP_ACCEPT'].to_s.strip - fallback = xhr? ? Mime::JS : Mime::HTML - if header.empty? - [content_type, fallback, Mime::ALL].compact + [content_type] else - ret = Mime::Type.parse(header) - if ret.last == Mime::ALL - ret.insert(-2, fallback) - end - ret + Mime::Type.parse(header) end end end @@ -163,26 +157,20 @@ module ActionDispatch # GET /posts/5 | request.format => Mime::HTML or MIME::JS, or request.accepts.first depending on the value of <tt>ActionController::Base.use_accept_header</tt> # def format(view_path = []) - @env["action_dispatch.request.format"] ||= - if parameters[:format] - Mime[parameters[:format]] - elsif ActionController::Base.use_accept_header && !(accepts == ONLY_ALL) - accepts.first - elsif xhr? then Mime::JS - else Mime::HTML - end + formats.first end def formats - if ActionController::Base.use_accept_header - if param = parameters[:format] - Array.wrap(Mime[param]) + accept = @env['HTTP_ACCEPT'] + + @env["action_dispatch.request.formats"] ||= + if parameters[:format] + [Mime[parameters[:format]]] + elsif xhr? || (accept && !accept.include?(?,)) + accepts else - accepts.dup + [Mime::HTML] end - else - [format] - end end # Sets the \format by string extension, which can be used to force custom formats @@ -198,7 +186,7 @@ module ActionDispatch # end def format=(extension) parameters[:format] = extension.to_s - @env["action_dispatch.request.format"] = Mime::Type.lookup_by_extension(parameters[:format]) + @env["action_dispatch.request.formats"] = [Mime::Type.lookup_by_extension(parameters[:format])] end # Returns a symbolized version of the <tt>:format</tt> parameter of the request. diff --git a/actionpack/lib/action_dispatch/middleware/params_parser.rb b/actionpack/lib/action_dispatch/middleware/params_parser.rb index e83cf9236b..ff2b2fe74b 100644 --- a/actionpack/lib/action_dispatch/middleware/params_parser.rb +++ b/actionpack/lib/action_dispatch/middleware/params_parser.rb @@ -47,6 +47,8 @@ module ActionDispatch false end rescue Exception => e # YAML, XML or Ruby code block errors + logger.debug "Error occurred while parsing request parameters.\nContents:\n\n#{request.raw_post}" + raise { "body" => request.raw_post, "content_type" => request.content_type, @@ -67,5 +69,9 @@ module ActionDispatch nil end + + def logger + defined?(Rails.logger) ? Rails.logger : Logger.new($stderr) + end end end diff --git a/actionpack/lib/action_view/base.rb b/actionpack/lib/action_view/base.rb index c171a5a8f5..ec1b07797b 100644 --- a/actionpack/lib/action_view/base.rb +++ b/actionpack/lib/action_view/base.rb @@ -175,6 +175,17 @@ module ActionView #:nodoc: attr_accessor :controller attr_internal :captures + def reset_formats(formats) + @formats = formats + + if defined?(ActionController) + # This is expensive, but we need to reset this when the format is updated, + # which currently only happens + Thread.current[:format_locale_key] = + ActionController::HashKey.get(self.class, formats, I18n.locale) + end + end + class << self delegate :erb_trim_mode=, :to => 'ActionView::TemplateHandlers::ERB' delegate :logger, :to => 'ActionController::Base', :allow_nil => true @@ -240,7 +251,7 @@ module ActionView #:nodoc: end def initialize(view_paths = [], assigns_for_first_render = {}, controller = nil, formats = nil)#:nodoc: - @formats = formats || [:html] + @formats = formats @assigns = assigns_for_first_render.each { |key, value| instance_variable_set("@#{key}", value) } @controller = controller @helpers = self.class.helpers || Module.new @@ -255,15 +266,6 @@ module ActionView #:nodoc: @view_paths = self.class.process_view_paths(paths) end - def with_template(current_template) - _evaluate_assigns_and_ivars - last_template, self.template = template, current_template - last_formats, self.formats = formats, current_template.formats - yield - ensure - self.template, self.formats = last_template, last_formats - end - def punctuate_body!(part) flush_output_buffer response.body_parts << part @@ -272,18 +274,11 @@ module ActionView #:nodoc: # Evaluates the local assigns and controller ivars, pushes them to the view. def _evaluate_assigns_and_ivars #:nodoc: - @assigns_added ||= _copy_ivars_from_controller - end - - private - - def _copy_ivars_from_controller #:nodoc: if @controller variables = @controller.instance_variable_names variables -= @controller.protected_instance_variables if @controller.respond_to?(:protected_instance_variables) variables.each { |name| instance_variable_set(name, @controller.instance_variable_get(name)) } end - true end end diff --git a/actionpack/lib/action_view/helpers/prototype_helper.rb b/actionpack/lib/action_view/helpers/prototype_helper.rb index 624b537ad2..03f1dabb4e 100644 --- a/actionpack/lib/action_view/helpers/prototype_helper.rb +++ b/actionpack/lib/action_view/helpers/prototype_helper.rb @@ -991,12 +991,13 @@ module ActionView def render(*options_for_render) old_formats = @context && @context.formats - @context.formats = [:html] if @context + + @context.reset_formats([:html]) if @context Hash === options_for_render.first ? @context.render(*options_for_render) : options_for_render.first.to_s ensure - @context.formats = old_formats if @context + @context.reset_formats(old_formats) if @context end def javascript_object_for(object) diff --git a/actionpack/lib/action_view/render/partials.rb b/actionpack/lib/action_view/render/partials.rb index 83175ab4cf..7f10f54d2e 100644 --- a/actionpack/lib/action_view/render/partials.rb +++ b/actionpack/lib/action_view/render/partials.rb @@ -173,44 +173,50 @@ module ActionView extend ActiveSupport::Concern class PartialRenderer - def self.partial_names - @partial_names ||= Hash.new {|h,k| h[k] = ActiveSupport::ConcurrentHash.new } - end + PARTIAL_NAMES = Hash.new {|h,k| h[k] = {} } + TEMPLATES = Hash.new {|h,k| h[k] = {} } - def self.formats - @formats ||= Hash.new {|h,k| h[k] = Hash.new{|h,k| h[k] = Hash.new {|h,k| h[k] = {}}}} - end + attr_reader :template def initialize(view_context, options, block) - partial = options[:partial] - - @memo = {} @view = view_context - @options = options - @locals = options[:locals] || {} - @block = block - - # Set up some instance variables to speed up memoizing - @partial_names = self.class.partial_names[@view.controller.class] - @templates = self.class.formats - @format = view_context.formats - - # Set up the object and path - @object = partial.is_a?(String) ? options[:object] : partial - @path = partial_path(partial) + @partial_names = PARTIAL_NAMES[@view.controller.class] + + key = Thread.current[:format_locale_key] + @templates = TEMPLATES[key] if key + + setup(options, block) + end + + def setup(options, block) + partial = options[:partial] + + @options = options + @locals = options[:locals] || {} + @block = block + + if String === partial + @object = options[:object] + @path = partial + else + @object = partial + @path = partial_path(partial) + end end def render - return render_collection if collection - - template = find_template - render_template(template, @object || @locals[template.variable_name]) + if @collection = collection + render_collection + else + @template = template = find_template + render_template(template, @object || @locals[template.variable_name]) + end end def render_collection - @options[:_template] = template = find_template + @template = template = find_template - return nil if collection.blank? + return nil if @collection.blank? if @options.key?(:spacer_template) spacer = find_template(@options[:spacer_template]).render(@view, @locals) @@ -223,57 +229,58 @@ module ActionView def collection_with_template(template) options = @options - segments, locals, as = [], @locals, options[:as] || :object + segments, locals, as = [], @locals, options[:as] || template.variable_name - variable_name = template.variable_name counter_name = template.counter_name locals[counter_name] = -1 - collection.each do |object| + @collection.each do |object| locals[counter_name] += 1 - locals[variable_name] = object - locals[as] = object if as + locals[as] = object segments << template.render(@view, locals) end + + @template = template segments end def collection_without_template options = @options - segments, locals, as = [], @locals, options[:as] || :object + segments, locals, as = [], @locals, options[:as] index, template = -1, nil - collection.each do |object| + @collection.each do |object| template = find_template(partial_path(object)) locals[template.counter_name] = (index += 1) locals[template.variable_name] = object - locals[as] = object if as segments << template.render(@view, locals) end - @options[:_template] = template + @template = template segments end def render_template(template, object = @object) - @options[:_template] ||= template + options, locals, view = @options, @locals, @view + locals[options[:as] || template.variable_name] = object - # TODO: is locals[:object] really necessary? - @locals[:object] = @locals[template.variable_name] = object - @locals[@options[:as]] = object if @options[:as] + content = template.render(view, locals) do |*name| + @view._layout_for(*name, &@block) + end - content = @view._render_single_template(template, @locals, &@block) - return content if @block || !@options[:layout] - find_template(@options[:layout]).render(@view, @locals) { content } + if @block || !options[:layout] + content + else + find_template(options[:layout]).render(@view, @locals) { content } + end end - private def collection - @collection ||= if @object.respond_to?(:to_ary) + if @object.respond_to?(:to_ary) @object elsif @options.key?(:collection) @options[:collection] || [] @@ -281,15 +288,19 @@ module ActionView end def find_template(path = @path) - return if !path - @templates[path][@view.controller_path][@format][I18n.locale] ||= begin - prefix = @view.controller.controller_path unless path.include?(?/) - @view.find(path, {:formats => @view.formats}, prefix, true) + unless @templates + path && _find_template(path) + else + path && @templates[path] ||= _find_template(path) end end + + def _find_template(path) + prefix = @view.controller.controller_path unless path.include?(?/) + @view.find(path, {:formats => @view.formats}, prefix, true) + end def partial_path(object = @object) - return object if object.is_a?(String) @partial_names[object.class] ||= begin return nil unless object.respond_to?(:to_model) @@ -302,14 +313,26 @@ module ActionView end def render_partial(options) - @assigns_added = false - # TODO: Handle other details here. - self.formats = options[:_details][:formats] if options[:_details] - _render_partial(options) + _evaluate_assigns_and_ivars + + details = options[:_details] + + # Is this needed + self.formats = details[:formats] if details + renderer = PartialRenderer.new(self, options, nil) + text = renderer.render + options[:_template] = renderer.template + text end def _render_partial(options, &block) #:nodoc: - PartialRenderer.new(self, options, block).render + if @renderer + @renderer.setup(options, block) + else + @renderer = PartialRenderer.new(self, options, block) + end + + @renderer.render end end diff --git a/actionpack/lib/action_view/render/rendering.rb b/actionpack/lib/action_view/render/rendering.rb index c7afc56e3b..b0b75918b7 100644 --- a/actionpack/lib/action_view/render/rendering.rb +++ b/actionpack/lib/action_view/render/rendering.rb @@ -12,8 +12,6 @@ module ActionView # as the locals hash. def render(options = {}, locals = {}, &block) #:nodoc: case options - when String, NilClass - _render_partial(:partial => options, :locals => locals || {}) when Hash layout = options[:layout] @@ -35,26 +33,8 @@ module ActionView end when :update update_page(&block) - end - end - - def _render_content(content, layout, locals) - return content unless layout - - locals ||= {} - - if controller && layout - @_layout = layout.identifier - logger.info("Rendering template within #{layout.identifier}") if logger - end - - begin - old_content, @_content_for[:layout] = @_content_for[:layout], content - - @cached_content_for_layout = @_content_for[:layout] - _render_single_template(layout, locals) - ensure - @_content_for[:layout] = old_content + else + _render_partial(:partial => options, :locals => locals) end end @@ -90,48 +70,25 @@ module ActionView # In this case, the layout would receive the block passed into <tt>render :layout</tt>, # and the Struct specified in the layout would be passed into the block. The result # would be <html>Hello David</html>. - def _layout_for(names, &block) - with_output_buffer do - # This is due to the potentially ambiguous use of yield when - # a block is passed in to a template *and* there is a content_for() - # of the same name. Suggested solution: require explicit use of content_for - # in these ambiguous cases. - # - # We would be able to continue supporting yield in all non-ambiguous - # cases. Question: should we deprecate yield in favor of content_for - # and reserve yield for cases where there is a yield into a real block? - if @_content_for.key?(names.first) || !block_given? - return @_content_for[names.first || :layout] - else - return yield(names) - end - end - end + def _layout_for(name = nil) + return @_content_for[name || :layout] if !block_given? || name - def _render_single_template(template, locals = {}, &block) - with_template(template) do - template.render(self, locals) do |*names| - _layout_for(names, &block) - end - end - rescue Exception => e - if e.is_a?(TemplateError) - e.sub_template_of(template) - raise e - else - raise TemplateError.new(template, assigns, e) + with_output_buffer do + return yield end end def _render_inline(inline, layout, options) handler = Template.handler_class_for_extension(options[:type] || "erb") template = Template.new(options[:inline], "inline #{options[:inline].inspect}", handler, {}) - content = _render_single_template(template, options[:locals] || {}) - layout ? _render_content(content, layout, options[:locals]) : content + locals = options[:locals] || {} + content = template.render(self, locals) + content = layout.render(self, locals) {|*name| _layout_for(*name) { content } } if layout + content end def _render_text(text, layout, options) - layout ? _render_content(text, layout, options[:locals]) : text + text = layout.render(self, options[:locals]) { text } if layout end # This is the API to render a ViewContext's template from a controller. @@ -141,7 +98,7 @@ module ActionView # _layout:: The layout, if any, to wrap the Template in # _partial:: true if the template is a partial def render_template(options) - @assigns_added = nil + _evaluate_assigns_and_ivars template, layout, partial = options.values_at(:_template, :_layout, :_partial) _render_template(template, layout, options, partial) end @@ -158,10 +115,18 @@ module ActionView content = if partial _render_partial_object(template, options) else - _render_single_template(template, locals) + template.render(self, locals) end - _render_content(content, layout, locals) + @cached_content_for_layout = content + @_content_for[:layout] = content + + if layout + @_layout = layout.identifier + logger.info("Rendering template within #{layout.identifier}") if logger + content = layout.render(self, locals) {|*name| _layout_for(*name) } + end + content end end end
\ No newline at end of file diff --git a/actionpack/lib/action_view/template/handler.rb b/actionpack/lib/action_view/template/handler.rb index 3071c78174..4bf58b9fa8 100644 --- a/actionpack/lib/action_view/template/handler.rb +++ b/actionpack/lib/action_view/template/handler.rb @@ -26,11 +26,7 @@ module ActionView self.default_format = Mime::HTML def self.call(template) - "#{name}.new(self).render(template, local_assigns)" - end - - def initialize(view = nil) - @view = view + raise "Need to implement #{self.class.name}#call(template)" end def render(template, local_assigns) diff --git a/actionpack/lib/action_view/template/resolver.rb b/actionpack/lib/action_view/template/resolver.rb index 10f664736f..fe657166d5 100644 --- a/actionpack/lib/action_view/template/resolver.rb +++ b/actionpack/lib/action_view/template/resolver.rb @@ -42,7 +42,7 @@ module ActionView def handler_glob @handler_glob ||= begin - e = TemplateHandlers.extensions.map{|h| ".#{h},"}.join + e = TemplateHandlers.extensions.map{|h| ".#{h}"}.join(",") "{#{e}}" end end diff --git a/actionpack/lib/action_view/template/template.rb b/actionpack/lib/action_view/template/template.rb index 33d3f79ad3..7d6964e3e3 100644 --- a/actionpack/lib/action_view/template/template.rb +++ b/actionpack/lib/action_view/template/template.rb @@ -26,9 +26,16 @@ module ActionView @details[:formats] = Array.wrap(format.to_sym) end - def render(view, locals, &blk) + def render(view, locals, &block) method_name = compile(locals, view) - view.send(method_name, locals, &blk) + view.send(method_name, locals, &block) + rescue Exception => e + if e.is_a?(TemplateError) + e.sub_template_of(self) + raise e + else + raise TemplateError.new(self, view.assigns, e) + end end # TODO: Figure out how to abstract this @@ -90,7 +97,22 @@ module ActionView raise ActionView::TemplateError.new(self, {}, e) end end - + + class LocalsKey + @hash_keys = Hash.new {|h,k| h[k] = Hash.new {|h,k| h[k] = {} } } + + def self.get(*locals) + @hash_keys[*locals] ||= new(klass, format, locale) + end + + attr_accessor :hash + def initialize(klass, format, locale) + @hash = locals.hash + end + + alias_method :eql?, :equal? + end + def build_method_name(locals) # TODO: is locals.keys.hash reliably the same? @method_names[locals.keys.hash] ||= diff --git a/actionpack/test/controller/content_type_test.rb b/actionpack/test/controller/content_type_test.rb index 511788aec8..c249788c67 100644 --- a/actionpack/test/controller/content_type_test.rb +++ b/actionpack/test/controller/content_type_test.rb @@ -46,7 +46,7 @@ class ContentTypeController < ActionController::Base def render_default_content_types_for_respond_to respond_to do |format| format.html { render :text => "hello world!" } - format.xml { render :action => "render_default_content_types_for_respond_to.rhtml" } + format.xml { render :action => "render_default_content_types_for_respond_to" } format.js { render :text => "hello world!" } format.rss { render :text => "hello world!", :content_type => Mime::XML } end diff --git a/actionpack/test/controller/mime_responds_test.rb b/actionpack/test/controller/mime_responds_test.rb index 2e2dba5aae..3f00b9ba2f 100644 --- a/actionpack/test/controller/mime_responds_test.rb +++ b/actionpack/test/controller/mime_responds_test.rb @@ -79,29 +79,20 @@ class RespondToController < ActionController::Base end end - def custom_constant_handling - Mime::Type.register("text/x-mobile", :mobile) + Mime::Type.register("text/x-mobile", :mobile) + def custom_constant_handling respond_to do |type| type.html { render :text => "HTML" } type.mobile { render :text => "Mobile" } end - ensure - Mime::SET.delete(:mobile) - Mime.module_eval { remove_const :MOBILE if const_defined?(:MOBILE) } end def custom_constant_handling_without_block - Mime::Type.register("text/x-mobile", :mobile) - respond_to do |type| type.html { render :text => "HTML" } type.mobile end - - ensure - Mime::SET.delete(:mobile) - Mime.module_eval { remove_const :MOBILE if const_defined?(:MOBILE) } end def handle_any @@ -125,32 +116,24 @@ class RespondToController < ActionController::Base end end + Mime::Type.register_alias("text/html", :iphone) + def iphone_with_html_response_type - Mime::Type.register_alias("text/html", :iphone) request.format = :iphone if request.env["HTTP_ACCEPT"] == "text/iphone" respond_to do |type| type.html { @type = "Firefox" } type.iphone { @type = "iPhone" } end - - ensure - Mime::SET.delete(:iphone) - Mime.module_eval { remove_const :IPHONE if const_defined?(:IPHONE) } end def iphone_with_html_response_type_without_layout - Mime::Type.register_alias("text/html", :iphone) request.format = "iphone" if request.env["HTTP_ACCEPT"] == "text/iphone" respond_to do |type| type.html { @type = "Firefox"; render :action => "iphone_with_html_response_type" } type.iphone { @type = "iPhone" ; render :action => "iphone_with_html_response_type" } end - - ensure - Mime::SET.delete(:iphone) - Mime.module_eval { remove_const :IPHONE if const_defined?(:IPHONE) } end def rescue_action(e) @@ -213,18 +196,20 @@ class RespondToControllerTest < ActionController::TestCase def test_js_or_html @request.accept = "text/javascript, text/html" - get :js_or_html + xhr :get, :js_or_html assert_equal 'JS', @response.body - get :html_or_xml + @request.accept = "text/javascript, text/html" + xhr :get, :html_or_xml assert_equal 'HTML', @response.body - get :just_xml + @request.accept = "text/javascript, text/html" + xhr :get, :just_xml assert_response 406 end def test_json_or_yaml - get :json_or_yaml + xhr :get, :json_or_yaml assert_equal 'JSON', @response.body get :json_or_yaml, :format => 'json' @@ -246,13 +231,13 @@ class RespondToControllerTest < ActionController::TestCase def test_js_or_anything @request.accept = "text/javascript, */*" - get :js_or_html + xhr :get, :js_or_html assert_equal 'JS', @response.body - get :html_or_xml + xhr :get, :html_or_xml assert_equal 'HTML', @response.body - get :just_xml + xhr :get, :just_xml assert_equal 'XML', @response.body end @@ -291,14 +276,16 @@ class RespondToControllerTest < ActionController::TestCase end def test_with_atom_content_type + @request.accept = "" @request.env["CONTENT_TYPE"] = "application/atom+xml" - get :made_for_content_type + xhr :get, :made_for_content_type assert_equal "ATOM", @response.body end def test_with_rss_content_type + @request.accept = "" @request.env["CONTENT_TYPE"] = "application/rss+xml" - get :made_for_content_type + xhr :get, :made_for_content_type assert_equal "RSS", @response.body end @@ -795,12 +782,8 @@ class PostController < AbstractPostController protected def with_iphone - Mime::Type.register_alias("text/html", :iphone) request.format = "iphone" if request.env["HTTP_ACCEPT"] == "text/iphone" yield - ensure - Mime::SET.delete(:iphone) - Mime.module_eval { remove_const :IPHONE if const_defined?(:IPHONE) } end end diff --git a/actionpack/test/controller/render_js_test.rb b/actionpack/test/controller/render_js_test.rb index d02fd3fd4c..bc850de733 100644 --- a/actionpack/test/controller/render_js_test.rb +++ b/actionpack/test/controller/render_js_test.rb @@ -13,7 +13,7 @@ class TestController < ActionController::Base # let's just rely on the template end - def partial + def show_partial render :partial => 'partial' end end @@ -33,7 +33,7 @@ class RenderTest < ActionController::TestCase end def test_should_render_js_partial - xhr :get, :partial, :format => 'js' + xhr :get, :show_partial, :format => 'js' assert_equal 'partial js', @response.body - end + end end
\ No newline at end of file diff --git a/actionpack/test/controller/render_test.rb b/actionpack/test/controller/render_test.rb index 0c0599679c..9c5b560c2c 100644 --- a/actionpack/test/controller/render_test.rb +++ b/actionpack/test/controller/render_test.rb @@ -486,10 +486,6 @@ class TestController < ActionController::Base render :action => "using_layout_around_block" end - def render_using_layout_around_block_with_args - render :action => "using_layout_around_block_with_args" - end - def render_using_layout_around_block_in_main_layout_and_within_content_for_layout render :action => "using_layout_around_block", :layout => "layouts/block_with_layout" end @@ -1161,11 +1157,6 @@ class RenderTest < ActionController::TestCase assert_equal "Before (Anthony)\nInside from first block in layout\nAfter\nBefore (David)\nInside from block\nAfter\nBefore (Ramm)\nInside from second block in layout\nAfter\n", @response.body end - def test_using_layout_around_block_with_args - get :render_using_layout_around_block_with_args - assert_equal "Before\narg1arg2\nAfter", @response.body - end - def test_partial_only get :partial_only assert_equal "only partial", @response.body diff --git a/actionpack/test/controller/routing_test.rb b/actionpack/test/controller/routing_test.rb index 2534c232c7..33fb6ac017 100644 --- a/actionpack/test/controller/routing_test.rb +++ b/actionpack/test/controller/routing_test.rb @@ -9,7 +9,6 @@ class MilestonesController < ActionController::Base def rescue_action(e) raise e end end -RunTimeTests = ARGV.include? 'time' ROUTING = ActionController::Routing class ROUTING::RouteBuilder @@ -60,590 +59,6 @@ class UriReservedCharactersRoutingTest < Test::Unit::TestCase end end -class SegmentTest < Test::Unit::TestCase - def test_first_segment_should_interpolate_for_structure - s = ROUTING::Segment.new - def s.interpolation_statement(array) 'hello' end - assert_equal 'hello', s.continue_string_structure([]) - end - - def test_interpolation_statement - s = ROUTING::StaticSegment.new("Hello") - assert_equal "Hello", eval(s.interpolation_statement([])) - assert_equal "HelloHello", eval(s.interpolation_statement([s])) - - s2 = ROUTING::StaticSegment.new("-") - assert_equal "Hello-Hello", eval(s.interpolation_statement([s, s2])) - - s3 = ROUTING::StaticSegment.new("World") - assert_equal "Hello-World", eval(s3.interpolation_statement([s, s2])) - end -end - -class StaticSegmentTest < Test::Unit::TestCase - def test_interpolation_chunk_should_respect_raw - s = ROUTING::StaticSegment.new('Hello World') - assert !s.raw? - assert_equal 'Hello%20World', s.interpolation_chunk - - s = ROUTING::StaticSegment.new('Hello World', :raw => true) - assert s.raw? - assert_equal 'Hello World', s.interpolation_chunk - end - - def test_value_should_not_be_double_unescaped - s = ROUTING::StaticSegment.new('%D0%9A%D0%B0%D1%80%D1%82%D0%B0') # Карта - assert_equal '%D0%9A%D0%B0%D1%80%D1%82%D0%B0', s.interpolation_chunk - end - - def test_regexp_chunk_should_escape_specials - s = ROUTING::StaticSegment.new('Hello*World') - assert_equal 'Hello\*World', s.regexp_chunk - - s = ROUTING::StaticSegment.new('HelloWorld') - assert_equal 'HelloWorld', s.regexp_chunk - end - - def test_regexp_chunk_should_add_question_mark_for_optionals - s = ROUTING::StaticSegment.new("/", :optional => true) - assert_equal "/?", s.regexp_chunk - - s = ROUTING::StaticSegment.new("hello", :optional => true) - assert_equal "(?:hello)?", s.regexp_chunk - end -end - -class DynamicSegmentTest < Test::Unit::TestCase - def segment(options = {}) - unless @segment - @segment = ROUTING::DynamicSegment.new(:a, options) - end - @segment - end - - def test_extract_value - s = ROUTING::DynamicSegment.new(:a) - - hash = {:a => '10', :b => '20'} - assert_equal '10', eval(s.extract_value) - - hash = {:b => '20'} - assert_equal nil, eval(s.extract_value) - - s.default = '20' - assert_equal '20', eval(s.extract_value) - end - - def test_default_local_name - assert_equal 'a_value', segment.local_name, - "Unexpected name -- all value_check tests will fail!" - end - - def test_presence_value_check - a_value = 10 - assert eval(segment.value_check) - end - - def test_regexp_value_check_rejects_nil - segment = segment(:regexp => /\d+/) - - a_value = nil - assert !eval(segment.value_check) - end - - def test_optional_regexp_value_check_should_accept_nil - segment = segment(:regexp => /\d+/, :optional => true) - - a_value = nil - assert eval(segment.value_check) - end - - def test_regexp_value_check_rejects_no_match - segment = segment(:regexp => /\d+/) - - a_value = "Hello20World" - assert !eval(segment.value_check) - - a_value = "20Hi" - assert !eval(segment.value_check) - end - - def test_regexp_value_check_accepts_match - segment = segment(:regexp => /\d+/) - a_value = "30" - assert eval(segment.value_check) - end - - def test_value_check_fails_on_nil - a_value = nil - assert ! eval(segment.value_check) - end - - def test_optional_value_needs_no_check - segment = segment(:optional => true) - - a_value = nil - assert_equal nil, segment.value_check - end - - def test_regexp_value_check_should_accept_match_with_default - segment = segment(:regexp => /\d+/, :default => '200') - - a_value = '100' - assert eval(segment.value_check) - end - - def test_expiry_should_not_trigger_once_expired - expired = true - hash = merged = {:a => 2, :b => 3} - options = {:b => 3} - expire_on = Hash.new { raise 'No!!!' } - - eval(segment.expiry_statement) - rescue RuntimeError - flunk "Expiry check should not have occurred!" - end - - def test_expiry_should_occur_according_to_expire_on - expired = false - hash = merged = {:a => 2, :b => 3} - options = {:b => 3} - - expire_on = {:b => true, :a => false} - eval(segment.expiry_statement) - assert !expired - assert_equal({:a => 2, :b => 3}, hash) - - expire_on = {:b => true, :a => true} - eval(segment.expiry_statement) - assert expired - assert_equal({:b => 3}, hash) - end - - def test_extraction_code_should_return_on_nil - hash = merged = {:b => 3} - options = {:b => 3} - a_value = nil - - # Local jump because of return inside eval. - assert_raise(LocalJumpError) { eval(segment.extraction_code) } - end - - def test_extraction_code_should_return_on_mismatch - segment = segment(:regexp => /\d+/) - hash = merged = {:a => 'Hi', :b => '3'} - options = {:b => '3'} - a_value = nil - - # Local jump because of return inside eval. - assert_raise(LocalJumpError) { eval(segment.extraction_code) } - end - - def test_extraction_code_should_accept_value_and_set_local - hash = merged = {:a => 'Hi', :b => '3'} - options = {:b => '3'} - a_value = nil - expired = true - - eval(segment.extraction_code) - assert_equal 'Hi', a_value - end - - def test_extraction_should_work_without_value_check - segment.default = 'hi' - hash = merged = {:b => '3'} - options = {:b => '3'} - a_value = nil - expired = true - - eval(segment.extraction_code) - assert_equal 'hi', a_value - end - - def test_extraction_code_should_perform_expiry - expired = false - hash = merged = {:a => 'Hi', :b => '3'} - options = {:b => '3'} - expire_on = {:a => true} - a_value = nil - - eval(segment.extraction_code) - assert_equal 'Hi', a_value - assert expired - assert_equal options, hash - end - - def test_interpolation_chunk_should_replace_value - a_value = 'Hi' - assert_equal a_value, eval(%("#{segment.interpolation_chunk}")) - end - - def test_interpolation_chunk_should_accept_nil - a_value = nil - assert_equal '', eval(%("#{segment.interpolation_chunk('a_value')}")) - end - - def test_value_regexp_should_be_nil_without_regexp - assert_equal nil, segment.value_regexp - end - - def test_value_regexp_should_match_exacly - segment = segment(:regexp => /\d+/) - assert_no_match segment.value_regexp, "Hello 10 World" - assert_no_match segment.value_regexp, "Hello 10" - assert_no_match segment.value_regexp, "10 World" - assert_match segment.value_regexp, "10" - end - - def test_regexp_chunk_should_return_string - segment = segment(:regexp => /\d+/) - assert_kind_of String, segment.regexp_chunk - end - - def test_build_pattern_non_optional_with_no_captures - # Non optional - a_segment = ROUTING::DynamicSegment.new(nil, :regexp => /\d+/) - assert_equal "(\\d+)stuff", a_segment.build_pattern('stuff') - end - - def test_build_pattern_non_optional_with_captures - # Non optional - a_segment = ROUTING::DynamicSegment.new(nil, :regexp => /(\d+)(.*?)/) - assert_equal "((\\d+)(.*?))stuff", a_segment.build_pattern('stuff') - end - - def test_optionality_implied - a_segment = ROUTING::DynamicSegment.new(:id) - assert a_segment.optionality_implied? - - a_segment = ROUTING::DynamicSegment.new(:action) - assert a_segment.optionality_implied? - end - - def test_modifiers_must_be_handled_sensibly - a_segment = ROUTING::DynamicSegment.new(nil, :regexp => /david|jamis/i) - assert_equal "((?i-mx:david|jamis))stuff", a_segment.build_pattern('stuff') - a_segment = ROUTING::DynamicSegment.new(nil, :regexp => /david|jamis/x) - assert_equal "((?x-mi:david|jamis))stuff", a_segment.build_pattern('stuff') - a_segment = ROUTING::DynamicSegment.new(nil, :regexp => /david|jamis/) - assert_equal "(david|jamis)stuff", a_segment.build_pattern('stuff') - end -end - -class ControllerSegmentTest < Test::Unit::TestCase - def test_regexp_should_only_match_possible_controllers - ActionController::Routing.with_controllers %w(admin/accounts admin/users account pages) do - cs = ROUTING::ControllerSegment.new :controller - regexp = %r{\A#{cs.regexp_chunk}\Z} - - ActionController::Routing.possible_controllers.each do |name| - assert_match regexp, name - assert_no_match regexp, "#{name}_fake" - - match = regexp.match name - assert_equal name, match[1] - end - end - end -end - -class PathSegmentTest < Test::Unit::TestCase - def segment(options = {}) - unless @segment - @segment = ROUTING::PathSegment.new(:path, options) - end - @segment - end - - def test_regexp_chunk_should_return_string - segment = segment(:regexp => /[a-z]+/) - assert_kind_of String, segment.regexp_chunk - end - - def test_regexp_chunk_should_be_wrapped_with_parenthesis - segment = segment(:regexp => /[a-z]+/) - assert_equal "([a-z]+)", segment.regexp_chunk - end - - def test_regexp_chunk_should_respect_options - segment = segment(:regexp => /[a-z]+/i) - assert_equal "((?i-mx:[a-z]+))", segment.regexp_chunk - end -end - -class RouteBuilderTest < Test::Unit::TestCase - def builder - @builder ||= ROUTING::RouteBuilder.new - end - - def build(path, options) - builder.build(path, options) - end - - def test_options_should_not_be_modified - requirements1 = { :id => /\w+/, :controller => /(?:[a-z](?:-?[a-z]+)*)/ } - requirements2 = requirements1.dup - - assert_equal requirements1, requirements2 - - with_options(:controller => 'folder', - :requirements => requirements2) do |m| - m.build 'folders/new', :action => 'new' - end - - assert_equal requirements1, requirements2 - end - - def test_segment_for_static - segment, rest = builder.segment_for 'ulysses' - assert_equal '', rest - assert_kind_of ROUTING::StaticSegment, segment - assert_equal 'ulysses', segment.value - end - - def test_segment_for_action - segment, rest = builder.segment_for ':action' - assert_equal '', rest - assert_kind_of ROUTING::DynamicSegment, segment - assert_equal :action, segment.key - assert_equal 'index', segment.default - end - - def test_segment_for_dynamic - segment, rest = builder.segment_for ':login' - assert_equal '', rest - assert_kind_of ROUTING::DynamicSegment, segment - assert_equal :login, segment.key - assert_equal nil, segment.default - assert ! segment.optional? - end - - def test_segment_for_with_rest - segment, rest = builder.segment_for ':login/:action' - assert_equal :login, segment.key - assert_equal '/:action', rest - segment, rest = builder.segment_for rest - assert_equal '/', segment.value - assert_equal ':action', rest - segment, rest = builder.segment_for rest - assert_equal :action, segment.key - assert_equal '', rest - end - - def test_segments_for - segments = builder.segments_for_route_path '/:controller/:action/:id' - - assert_kind_of ROUTING::DividerSegment, segments[0] - assert_equal '/', segments[2].value - - assert_kind_of ROUTING::DynamicSegment, segments[1] - assert_equal :controller, segments[1].key - - assert_kind_of ROUTING::DividerSegment, segments[2] - assert_equal '/', segments[2].value - - assert_kind_of ROUTING::DynamicSegment, segments[3] - assert_equal :action, segments[3].key - - assert_kind_of ROUTING::DividerSegment, segments[4] - assert_equal '/', segments[4].value - - assert_kind_of ROUTING::DynamicSegment, segments[5] - assert_equal :id, segments[5].key - end - - def test_segment_for_action - s, r = builder.segment_for(':action/something/else') - assert_equal '/something/else', r - assert_equal :action, s.key - end - - def test_action_default_should_not_trigger_on_prefix - s, r = builder.segment_for ':action_name/something/else' - assert_equal '/something/else', r - assert_equal :action_name, s.key - assert_equal nil, s.default - end - - def test_divide_route_options - segments = builder.segments_for_route_path '/cars/:action/:person/:car/' - defaults, requirements = builder.divide_route_options(segments, - :action => 'buy', :person => /\w+/, :car => /\w+/, - :defaults => {:person => nil, :car => nil} - ) - - assert_equal({:action => 'buy', :person => nil, :car => nil}, defaults) - assert_equal({:person => /\w+/, :car => /\w+/}, requirements) - end - - def test_assign_route_options - segments = builder.segments_for_route_path '/cars/:action/:person/:car/' - defaults = {:action => 'buy', :person => nil, :car => nil} - requirements = {:person => /\w+/, :car => /\w+/} - - route_requirements = builder.assign_route_options(segments, defaults, requirements) - assert_equal({}, route_requirements) - - assert_equal :action, segments[3].key - assert_equal 'buy', segments[3].default - - assert_equal :person, segments[5].key - assert_equal %r/\w+/, segments[5].regexp - assert segments[5].optional? - - assert_equal :car, segments[7].key - assert_equal %r/\w+/, segments[7].regexp - assert segments[7].optional? - end - - def test_assign_route_options_with_anchor_chars - segments = builder.segments_for_route_path '/cars/:action/:person/:car/' - defaults = {:action => 'buy', :person => nil, :car => nil} - requirements = {:person => /\w+/, :car => /^\w+$/} - - assert_raise ArgumentError do - route_requirements = builder.assign_route_options(segments, defaults, requirements) - end - - requirements[:car] = /[^\/]+/ - route_requirements = builder.assign_route_options(segments, defaults, requirements) - end - - def test_optional_segments_preceding_required_segments - segments = builder.segments_for_route_path '/cars/:action/:person/:car/' - defaults = {:action => 'buy', :person => nil, :car => "model-t"} - assert builder.assign_route_options(segments, defaults, {}).empty? - - 0.upto(1) { |i| assert !segments[i].optional?, "segment #{i} is optional and it shouldn't be" } - assert segments[2].optional? - - assert_equal nil, builder.warn_output # should only warn on the :person segment - end - - def test_segmentation_of_dot_path - segments = builder.segments_for_route_path '/books/:action.rss' - assert builder.assign_route_options(segments, {}, {}).empty? - assert_equal 6, segments.length # "/", "books", "/", ":action", ".", "rss" - assert !segments.any? { |seg| seg.optional? } - end - - def test_segmentation_of_dynamic_dot_path - segments = builder.segments_for_route_path '/books/:action.:format' - assert builder.assign_route_options(segments, {}, {}).empty? - assert_equal 6, segments.length # "/", "books", "/", ":action", ".", ":format" - assert !segments.any? { |seg| seg.optional? } - assert_kind_of ROUTING::DynamicSegment, segments.last - end - - def test_assignment_of_default_options - segments = builder.segments_for_route_path '/:controller/:action/:id/' - action, id = segments[-4], segments[-2] - - assert_equal :action, action.key - assert_equal :id, id.key - assert ! action.optional? - assert ! id.optional? - - builder.assign_default_route_options(segments) - - assert_equal 'index', action.default - assert action.optional? - assert id.optional? - end - - def test_assignment_of_default_options_respects_existing_defaults - segments = builder.segments_for_route_path '/:controller/:action/:id/' - action, id = segments[-4], segments[-2] - - assert_equal :action, action.key - assert_equal :id, id.key - action.default = 'show' - action.is_optional = true - - id.default = 'Welcome' - id.is_optional = true - - builder.assign_default_route_options(segments) - - assert_equal 'show', action.default - assert action.optional? - assert_equal 'Welcome', id.default - assert id.optional? - end - - def test_assignment_of_default_options_respects_regexps - segments = builder.segments_for_route_path '/:controller/:action/:id/' - action = segments[-4] - - assert_equal :action, action.key - segments[-4] = ROUTING::DynamicSegment.new(:action, :regexp => /show|in/) - - builder.assign_default_route_options(segments) - - assert_equal nil, action.default - assert ! action.optional? - end - - def test_assignment_of_is_optional_when_default - segments = builder.segments_for_route_path '/books/:action.rss' - assert_equal segments[3].key, :action - segments[3].default = 'changes' - builder.ensure_required_segments(segments) - assert ! segments[3].optional? - end - - def test_is_optional_is_assigned_to_default_segments - segments = builder.segments_for_route_path '/books/:action' - builder.assign_route_options(segments, {:action => 'index'}, {}) - - assert_equal segments[3].key, :action - assert segments[3].optional? - assert_kind_of ROUTING::DividerSegment, segments[2] - assert segments[2].optional? - end - - # XXX is optional not being set right? - # /blah/:defaulted_segment <-- is the second slash optional? it should be. - - def test_route_build - ActionController::Routing.with_controllers %w(users pages) do - r = builder.build '/:controller/:action/:id/', :action => nil - - [0, 2, 4].each do |i| - assert_kind_of ROUTING::DividerSegment, r.segments[i] - assert_equal '/', r.segments[i].value - assert r.segments[i].optional? if i > 1 - end - - assert_kind_of ROUTING::DynamicSegment, r.segments[1] - assert_equal :controller, r.segments[1].key - assert_equal nil, r.segments[1].default - - assert_kind_of ROUTING::DynamicSegment, r.segments[3] - assert_equal :action, r.segments[3].key - assert_equal 'index', r.segments[3].default - - assert_kind_of ROUTING::DynamicSegment, r.segments[5] - assert_equal :id, r.segments[5].key - assert r.segments[5].optional? - end - end - - def test_slashes_are_implied - routes = [ - builder.build('/:controller/:action/:id/', :action => nil), - builder.build('/:controller/:action/:id', :action => nil), - builder.build(':controller/:action/:id', :action => nil), - builder.build('/:controller/:action/:id/', :action => nil) - ] - expected = routes.first.segments.length - routes.each_with_index do |route, i| - found = route.segments.length - assert_equal expected, found, "Route #{i + 1} has #{found} segments, expected #{expected}" - end - end -end - class RoutingTest < Test::Unit::TestCase def test_possible_controllers true_controller_paths = ActionController::Routing.controller_paths @@ -759,7 +174,7 @@ class LegacyRouteSetTests < Test::Unit::TestCase ActionController::Routing.use_controllers! %w(content admin/user admin/news_feed) end - + def teardown @rs.clear! end @@ -815,52 +230,6 @@ class LegacyRouteSetTests < Test::Unit::TestCase map.resources :pages map.connect ':controller/:action/:id' } - n = 1000 - if RunTimeTests - GC.start - rectime = Benchmark.realtime do - n.times do - rs.recognize_path("/videos/1234567", {:method => :get}) - rs.recognize_path("/videos/1234567/abuse", {:method => :get}) - rs.recognize_path("/users/1234567/settings", {:method => :get}) - rs.recognize_path("/channels/1234567", {:method => :get}) - rs.recognize_path("/session/new", {:method => :get}) - rs.recognize_path("/admin/user/show/10", {:method => :get}) - end - end - puts "\n\nRecognition (#{rs.routes.size} routes):" - per_url = rectime / (n * 6) - puts "#{per_url * 1000} ms/url" - puts "#{1 / per_url} url/s\n\n" - end - end - - def test_time_generation - n = 5000 - if RunTimeTests - GC.start - pairs = [ - [{:controller => 'content', :action => 'index'}, {:controller => 'content', :action => 'show'}], - [{:controller => 'content'}, {:controller => 'content', :action => 'index'}], - [{:controller => 'content', :action => 'list'}, {:controller => 'content', :action => 'index'}], - [{:controller => 'content', :action => 'show', :id => '10'}, {:controller => 'content', :action => 'list'}], - [{:controller => 'admin/user', :action => 'index'}, {:controller => 'admin/user', :action => 'show'}], - [{:controller => 'admin/user'}, {:controller => 'admin/user', :action => 'index'}], - [{:controller => 'admin/user', :action => 'list'}, {:controller => 'admin/user', :action => 'index'}], - [{:controller => 'admin/user', :action => 'show', :id => '10'}, {:controller => 'admin/user', :action => 'list'}], - ] - p = nil - gentime = Benchmark.realtime do - n.times do - pairs.each {|(a, b)| rs.generate(a, b)} - end - end - - puts "\n\nGeneration (RouteSet): (#{(n * 8)} urls)" - per_url = gentime / (n * 8) - puts "#{per_url * 1000} ms/url" - puts "#{1 / per_url} url/s\n\n" - end end def test_route_with_colon_first @@ -1138,8 +507,7 @@ class LegacyRouteSetTests < Test::Unit::TestCase map.connect '*path', :controller => 'content', :action => 'show_file' end - recall_path = ActionController::Routing::PathSegment::Result.new(%w(pages boo)) - assert_equal '/pages/boo', rs.generate({}, :controller => 'content', :action => 'show_file', :path => recall_path) + assert_equal '/pages/boo', rs.generate({}, :controller => 'content', :action => 'show_file', :path => %w(pages boo)) end def test_backwards @@ -1455,161 +823,6 @@ class LegacyRouteSetTests < Test::Unit::TestCase end end -class RouteTest < Test::Unit::TestCase - def setup - @route = ROUTING::Route.new - end - - def slash_segment(is_optional = false) - ROUTING::DividerSegment.new('/', :optional => is_optional) - end - - def default_route - unless defined?(@default_route) - segments = [] - segments << ROUTING::StaticSegment.new('/', :raw => true) - segments << ROUTING::DynamicSegment.new(:controller) - segments << slash_segment(:optional) - segments << ROUTING::DynamicSegment.new(:action, :default => 'index', :optional => true) - segments << slash_segment(:optional) - segments << ROUTING::DynamicSegment.new(:id, :optional => true) - segments << slash_segment(:optional) - @default_route = ROUTING::Route.new(segments).freeze - end - @default_route - end - - def test_default_route_recognition - expected = {:controller => 'accounts', :action => 'show', :id => '10'} - assert_equal expected, default_route.recognize('/accounts/show/10') - assert_equal expected, default_route.recognize('/accounts/show/10/') - - expected[:id] = 'jamis' - assert_equal expected, default_route.recognize('/accounts/show/jamis/') - - expected.delete :id - assert_equal expected, default_route.recognize('/accounts/show') - assert_equal expected, default_route.recognize('/accounts/show/') - - expected[:action] = 'index' - assert_equal expected, default_route.recognize('/accounts/') - assert_equal expected, default_route.recognize('/accounts') - - assert_equal nil, default_route.recognize('/') - assert_equal nil, default_route.recognize('/accounts/how/goood/it/is/to/be/free') - end - - def test_default_route_should_omit_default_action - o = {:controller => 'accounts', :action => 'index'} - assert_equal '/accounts', default_route.generate(o, o, {}) - end - - def test_default_route_should_include_default_action_when_id_present - o = {:controller => 'accounts', :action => 'index', :id => '20'} - assert_equal '/accounts/index/20', default_route.generate(o, o, {}) - end - - def test_default_route_should_work_with_action_but_no_id - o = {:controller => 'accounts', :action => 'list_all'} - assert_equal '/accounts/list_all', default_route.generate(o, o, {}) - end - - def test_default_route_should_uri_escape_pluses - expected = { :controller => 'accounts', :action => 'show', :id => 'hello world' } - assert_equal expected, default_route.recognize('/accounts/show/hello world') - assert_equal expected, default_route.recognize('/accounts/show/hello%20world') - assert_equal '/accounts/show/hello%20world', default_route.generate(expected, expected, {}) - - expected[:id] = 'hello+world' - assert_equal expected, default_route.recognize('/accounts/show/hello+world') - assert_equal expected, default_route.recognize('/accounts/show/hello%2Bworld') - assert_equal '/accounts/show/hello+world', default_route.generate(expected, expected, {}) - end - - def test_matches_controller_and_action - # requirement_for should only be called for the action and controller _once_ - @route.expects(:requirement_for).with(:controller).times(1).returns('pages') - @route.expects(:requirement_for).with(:action).times(1).returns('show') - - @route.requirements = {:controller => 'pages', :action => 'show'} - assert @route.matches_controller_and_action?('pages', 'show') - assert !@route.matches_controller_and_action?('not_pages', 'show') - assert !@route.matches_controller_and_action?('pages', 'not_show') - end - - def test_parameter_shell - page_url = ROUTING::Route.new - page_url.requirements = {:controller => 'pages', :action => 'show', :id => /\d+/} - assert_equal({:controller => 'pages', :action => 'show'}, page_url.parameter_shell) - end - - def test_defaults - route = ROUTING::RouteBuilder.new.build '/users/:id.:format', :controller => "users", :action => "show", :format => "html" - assert_equal( - { :controller => "users", :action => "show", :format => "html" }, - route.defaults) - end - - def test_builder_complains_without_controller - assert_raise(ArgumentError) do - ROUTING::RouteBuilder.new.build '/contact', :contoller => "contact", :action => "index" - end - end - - def test_significant_keys_for_default_route - keys = default_route.significant_keys.sort_by {|k| k.to_s } - assert_equal [:action, :controller, :id], keys - end - - def test_significant_keys - segments = [] - segments << ROUTING::StaticSegment.new('/', :raw => true) - segments << ROUTING::StaticSegment.new('user') - segments << ROUTING::StaticSegment.new('/', :raw => true, :optional => true) - segments << ROUTING::DynamicSegment.new(:user) - segments << ROUTING::StaticSegment.new('/', :raw => true, :optional => true) - - requirements = {:controller => 'users', :action => 'show'} - - user_url = ROUTING::Route.new(segments, requirements) - keys = user_url.significant_keys.sort_by { |k| k.to_s } - assert_equal [:action, :controller, :user], keys - end - - def test_build_empty_query_string - assert_equal '', @route.build_query_string({}) - end - - def test_build_query_string_with_nil_value - assert_equal '', @route.build_query_string({:x => nil}) - end - - def test_simple_build_query_string - assert_equal '?x=1&y=2', order_query_string(@route.build_query_string(:x => '1', :y => '2')) - end - - def test_convert_ints_build_query_string - assert_equal '?x=1&y=2', order_query_string(@route.build_query_string(:x => 1, :y => 2)) - end - - def test_escape_spaces_build_query_string - assert_equal '?x=hello+world&y=goodbye+world', order_query_string(@route.build_query_string(:x => 'hello world', :y => 'goodbye world')) - end - - def test_expand_array_build_query_string - assert_equal '?x%5B%5D=1&x%5B%5D=2', order_query_string(@route.build_query_string(:x => [1, 2])) - end - - def test_escape_spaces_build_query_string_selected_keys - assert_equal '?x=hello+world', order_query_string(@route.build_query_string({:x => 'hello world', :y => 'goodbye world'}, [:x])) - end - - private - def order_query_string(qs) - '?' + qs[1..-1].split('&').sort.join('&') - end -end - class RouteSetTest < ActiveSupport::TestCase def set @set ||= ROUTING::RouteSet.new @@ -1619,6 +832,19 @@ class RouteSetTest < ActiveSupport::TestCase @request ||= ActionController::TestRequest.new end + def default_route_set + @default_route_set ||= begin + set = nil + ActionController::Routing.with_controllers(['accounts']) do + set = ROUTING::RouteSet.new + set.draw do |map| + map.connect '/:controller/:action/:id/' + end + end + set + end + end + def test_generate_extras set.draw { |m| m.connect ':controller/:action/:id' } path, extras = set.generate_extras(:controller => "foo", :action => "bar", :id => 15, :this => "hello", :that => "world") @@ -2185,18 +1411,6 @@ class RouteSetTest < ActiveSupport::TestCase Object.send(:remove_const, :Api) end - def test_generate_finds_best_fit - set.draw do |map| - map.connect "/people", :controller => "people", :action => "index" - map.connect "/ws/people", :controller => "people", :action => "index", :ws => true - end - - assert_deprecated { - url = set.generate(:controller => "people", :action => "index", :ws => true) - assert_equal "/ws/people", url - } - end - def test_generate_changes_controller_module set.draw { |map| map.connect ':controller/:action/:id' } current = { :controller => "bling/bloop", :action => "bap", :id => 9 } @@ -2504,6 +1718,213 @@ class RouteSetTest < ActiveSupport::TestCase assert_equal({:controller => 'pages', :action => 'show', :name => :as_symbol}, set.recognize_path('/named')) end + + def test_interpolation_chunk_should_respect_raw + ActionController::Routing.with_controllers(['hello']) do + set.draw do |map| + map.connect '/Hello World', :controller => 'hello' + end + + assert_equal '/Hello%20World', set.generate(:controller => 'hello') + assert_equal({:controller => "hello", :action => "index"}, set.recognize_path('/Hello World')) + assert_raise(ActionController::RoutingError) { set.recognize_path('/Hello%20World') } + end + end + + def test_value_should_not_be_double_unescaped + ActionController::Routing.with_controllers(['foo']) do + set.draw do |map| + map.connect '/Карта', :controller => 'foo' + end + + assert_equal '/%D0%9A%D0%B0%D1%80%D1%82%D0%B0', set.generate(:controller => 'foo') + assert_equal({:controller => "foo", :action => "index"}, set.recognize_path('/Карта')) + assert_raise(ActionController::RoutingError) { set.recognize_path('/%D0%9A%D0%B0%D1%80%D1%82%D0%B0') } + end + end + + def test_regexp_chunk_should_escape_specials + ActionController::Routing.with_controllers(['foo', 'bar']) do + set.draw do |map| + map.connect '/Hello*World', :controller => 'foo' + map.connect '/HelloWorld', :controller => 'bar' + end + + assert_equal '/Hello*World', set.generate(:controller => 'foo') + assert_equal '/HelloWorld', set.generate(:controller => 'bar') + + assert_equal({:controller => "foo", :action => "index"}, set.recognize_path('/Hello*World')) + assert_equal({:controller => "bar", :action => "index"}, set.recognize_path('/HelloWorld')) + end + end + + def test_regexp_chunk_should_add_question_mark_for_optionals + ActionController::Routing.with_controllers(['foo', 'bar']) do + set.draw do |map| + map.connect '/', :controller => 'foo' + map.connect '/hello', :controller => 'bar' + end + + assert_equal '/', set.generate(:controller => 'foo') + assert_equal '/hello', set.generate(:controller => 'bar') + + assert_equal({:controller => "foo", :action => "index"}, set.recognize_path('/')) + assert_equal({:controller => "bar", :action => "index"}, set.recognize_path('/hello')) + end + end + + def test_assign_route_options_with_anchor_chars + ActionController::Routing.with_controllers(['cars']) do + set.draw do |map| + map.connect '/cars/:action/:person/:car/', :controller => 'cars' + end + + assert_equal '/cars/buy/1/2', set.generate(:controller => 'cars', :action => 'buy', :person => '1', :car => '2') + + assert_equal({:controller => "cars", :action => "buy", :person => "1", :car => "2"}, set.recognize_path('/cars/buy/1/2')) + end + end + + def test_segmentation_of_dot_path + ActionController::Routing.with_controllers(['books']) do + set.draw do |map| + map.connect '/books/:action.rss', :controller => 'books' + end + + assert_equal '/books/list.rss', set.generate(:controller => 'books', :action => 'list') + + assert_equal({:controller => "books", :action => "list"}, set.recognize_path('/books/list.rss')) + end + end + + def test_segmentation_of_dynamic_dot_path + ActionController::Routing.with_controllers(['books']) do + set.draw do |map| + map.connect '/books/:action.:format', :controller => 'books' + end + + assert_equal '/books/list.rss', set.generate(:controller => 'books', :action => 'list', :format => 'rss') + assert_equal '/books/list.xml', set.generate(:controller => 'books', :action => 'list', :format => 'xml') + assert_equal '/books/list', set.generate(:controller => 'books', :action => 'list') + assert_equal '/books', set.generate(:controller => 'books', :action => 'index') + + assert_equal({:controller => "books", :action => "list", :format => "rss"}, set.recognize_path('/books/list.rss')) + assert_equal({:controller => "books", :action => "list", :format => "xml"}, set.recognize_path('/books/list.xml')) + assert_equal({:controller => "books", :action => "list"}, set.recognize_path('/books/list')) + assert_equal({:controller => "books", :action => "index"}, set.recognize_path('/books')) + end + end + + def test_slashes_are_implied + ActionController::Routing.with_controllers(['foo']) do + ['/:controller/:action/:id/', '/:controller/:action/:id', + ':controller/:action/:id', '/:controller/:action/:id/' + ].each do |path| + @set = nil + set.draw { |map| map.connect(path) } + + assert_equal '/foo', set.generate(:controller => 'foo', :action => 'index') + assert_equal '/foo/list', set.generate(:controller => 'foo', :action => 'list') + assert_equal '/foo/show/1', set.generate(:controller => 'foo', :action => 'show', :id => '1') + + assert_equal({:controller => "foo", :action => "index"}, set.recognize_path('/foo')) + assert_equal({:controller => "foo", :action => "index"}, set.recognize_path('/foo/index')) + assert_equal({:controller => "foo", :action => "list"}, set.recognize_path('/foo/list')) + assert_equal({:controller => "foo", :action => "show", :id => "1"}, set.recognize_path('/foo/show/1')) + end + end + end + + def test_default_route_recognition + expected = {:controller => 'accounts', :action => 'show', :id => '10'} + assert_equal expected, default_route_set.recognize_path('/accounts/show/10') + assert_equal expected, default_route_set.recognize_path('/accounts/show/10/') + + expected[:id] = 'jamis' + assert_equal expected, default_route_set.recognize_path('/accounts/show/jamis/') + + expected.delete :id + assert_equal expected, default_route_set.recognize_path('/accounts/show') + assert_equal expected, default_route_set.recognize_path('/accounts/show/') + + expected[:action] = 'index' + assert_equal expected, default_route_set.recognize_path('/accounts/') + assert_equal expected, default_route_set.recognize_path('/accounts') + + assert_raise(ActionController::RoutingError) { default_route_set.recognize_path('/') } + assert_raise(ActionController::RoutingError) { default_route_set.recognize_path('/accounts/how/goood/it/is/to/be/free') } + end + + def test_default_route_should_omit_default_action + assert_equal '/accounts', default_route_set.generate({:controller => 'accounts', :action => 'index'}) + end + + def test_default_route_should_include_default_action_when_id_present + assert_equal '/accounts/index/20', default_route_set.generate({:controller => 'accounts', :action => 'index', :id => '20'}) + end + + def test_default_route_should_work_with_action_but_no_id + assert_equal '/accounts/list_all', default_route_set.generate({:controller => 'accounts', :action => 'list_all'}) + end + + def test_default_route_should_uri_escape_pluses + expected = { :controller => 'accounts', :action => 'show', :id => 'hello world' } + assert_equal expected, default_route_set.recognize_path('/accounts/show/hello world') + assert_equal expected, default_route_set.recognize_path('/accounts/show/hello%20world') + assert_equal '/accounts/show/hello%20world', default_route_set.generate(expected, expected) + + expected[:id] = 'hello+world' + assert_equal expected, default_route_set.recognize_path('/accounts/show/hello+world') + assert_equal expected, default_route_set.recognize_path('/accounts/show/hello%2Bworld') + assert_equal '/accounts/show/hello+world', default_route_set.generate(expected, expected) + end + + def test_parameter_shell + page_url = ROUTING::Route.new + page_url.requirements = {:controller => 'pages', :action => 'show', :id => /\d+/} + assert_equal({:controller => 'pages', :action => 'show'}, page_url.parameter_shell) + end + + def test_defaults + route = ROUTING::RouteBuilder.new.build '/users/:id.:format', :controller => "users", :action => "show", :format => "html" + assert_equal( + { :controller => "users", :action => "show", :format => "html" }, + route.defaults) + end + + def test_builder_complains_without_controller + assert_raise(ArgumentError) do + ROUTING::RouteBuilder.new.build '/contact', :contoller => "contact", :action => "index" + end + end + + def test_build_empty_query_string + assert_equal '/foo', default_route_set.generate({:controller => 'foo'}) + end + + def test_build_query_string_with_nil_value + assert_equal '/foo', default_route_set.generate({:controller => 'foo', :x => nil}) + end + + def test_simple_build_query_string + assert_equal '/foo?x=1&y=2', default_route_set.generate({:controller => 'foo', :x => '1', :y => '2'}) + end + + def test_convert_ints_build_query_string + assert_equal '/foo?x=1&y=2', default_route_set.generate({:controller => 'foo', :x => 1, :y => 2}) + end + + def test_escape_spaces_build_query_string + assert_equal '/foo?x=hello+world&y=goodbye+world', default_route_set.generate({:controller => 'foo', :x => 'hello world', :y => 'goodbye world'}) + end + + def test_expand_array_build_query_string + assert_equal '/foo?x%5B%5D=1&x%5B%5D=2', default_route_set.generate({:controller => 'foo', :x => [1, 2]}) + end + + def test_escape_spaces_build_query_string_selected_keys + assert_equal '/foo?x=hello+world', default_route_set.generate({:controller => 'foo', :x => 'hello world'}) + end end class RouteLoadingTest < Test::Unit::TestCase @@ -2567,10 +1988,10 @@ class RouteLoadingTest < Test::Unit::TestCase routes.reload end - + def test_load_multiple_configurations routes.add_configuration_file("engines.rb") - + File.expects(:stat).at_least_once.returns(@stat) routes.expects(:load).with('./config/routes.rb') diff --git a/actionpack/test/dispatch/mime_type_test.rb b/actionpack/test/dispatch/mime_type_test.rb index 4ea0fedb8f..0832943d4c 100644 --- a/actionpack/test/dispatch/mime_type_test.rb +++ b/actionpack/test/dispatch/mime_type_test.rb @@ -56,7 +56,7 @@ class MimeTypeTest < ActiveSupport::TestCase test "type convenience methods" do # Don't test Mime::ALL, since it Mime::ALL#html? == true - types = Mime::SET.symbols.uniq - [:all] + types = Mime::SET.symbols.uniq - [:all, :iphone] # Remove custom Mime::Type instances set in other tests, like Mime::GIF and Mime::IPHONE types.delete_if { |type| !Mime.const_defined?(type.to_s.upcase) } diff --git a/actionpack/test/dispatch/request/json_params_parsing_test.rb b/actionpack/test/dispatch/request/json_params_parsing_test.rb index a3dde72c4e..db6cf7b330 100644 --- a/actionpack/test/dispatch/request/json_params_parsing_test.rb +++ b/actionpack/test/dispatch/request/json_params_parsing_test.rb @@ -30,16 +30,36 @@ class JsonParamsParsingTest < ActionController::IntegrationTest ) end + test "logs error if parsing unsuccessful" do + with_test_routing do + begin + $stderr = StringIO.new + json = "[\"person]\": {\"name\": \"David\"}}" + post "/parse", json, {'CONTENT_TYPE' => 'application/json'} + assert_response :error + $stderr.rewind && err = $stderr.read + assert err =~ /Error occurred while parsing request parameters/ + ensure + $stderr = STDERR + end + end + end + private def assert_parses(expected, actual, headers = {}) + with_test_routing do + post "/parse", actual, headers + assert_response :ok + assert_equal(expected, TestController.last_request_parameters) + end + end + + def with_test_routing with_routing do |set| set.draw do |map| map.connect ':action', :controller => "json_params_parsing_test/test" end - - post "/parse", actual, headers - assert_response :ok - assert_equal(expected, TestController.last_request_parameters) + yield end end end diff --git a/actionpack/test/dispatch/request/xml_params_parsing_test.rb b/actionpack/test/dispatch/request/xml_params_parsing_test.rb index ee764e726e..521002b519 100644 --- a/actionpack/test/dispatch/request/xml_params_parsing_test.rb +++ b/actionpack/test/dispatch/request/xml_params_parsing_test.rb @@ -38,6 +38,21 @@ class XmlParamsParsingTest < ActionController::IntegrationTest end end + test "logs error if parsing unsuccessful" do + with_test_routing do + begin + $stderr = StringIO.new + xml = "<person><name>David</name><avatar type='file' name='me.jpg' content_type='image/jpg'>#{ActiveSupport::Base64.encode64('ABC')}</avatar></pineapple>" + post "/parse", xml, default_headers + assert_response :error + $stderr.rewind && err = $stderr.read + assert err =~ /Error occurred while parsing request parameters/ + ensure + $stderr = STDERR + end + end + end + test "parses multiple files" do xml = <<-end_body <person> @@ -85,4 +100,4 @@ class LegacyXmlParamsParsingTest < XmlParamsParsingTest def default_headers {'HTTP_X_POST_DATA_FORMAT' => 'xml'} end -end +end
\ No newline at end of file diff --git a/actionpack/test/dispatch/request_test.rb b/actionpack/test/dispatch/request_test.rb index b626063df4..239fda98e0 100644 --- a/actionpack/test/dispatch/request_test.rb +++ b/actionpack/test/dispatch/request_test.rb @@ -366,12 +366,12 @@ class RequestTest < ActiveSupport::TestCase end test "XMLHttpRequest" do - with_accept_header false do - request = stub_request 'HTTP_X_REQUESTED_WITH' => 'XMLHttpRequest' - request.expects(:parameters).at_least_once.returns({}) - assert request.xhr? - assert_equal Mime::JS, request.format - end + request = stub_request 'HTTP_X_REQUESTED_WITH' => 'XMLHttpRequest', + 'HTTP_ACCEPT' => + [Mime::JS, Mime::HTML, Mime::XML, 'text/xml', Mime::ALL].join(",") + request.expects(:parameters).at_least_once.returns({}) + assert request.xhr? + assert_equal Mime::JS, request.format end test "content type" do @@ -420,37 +420,34 @@ class RequestTest < ActiveSupport::TestCase end test "formats with accept header" do - with_accept_header true do - request = stub_request 'HTTP_ACCEPT' => 'text/html' - request.expects(:parameters).at_least_once.returns({}) - assert_equal [ Mime::HTML ], request.formats - - request = stub_request 'CONTENT_TYPE' => 'application/xml; charset=UTF-8' - request.expects(:parameters).at_least_once.returns({}) - assert_equal with_set(Mime::XML, Mime::HTML, Mime::ALL), request.formats - end + request = stub_request 'HTTP_ACCEPT' => 'text/html' + request.expects(:parameters).at_least_once.returns({}) + assert_equal [ Mime::HTML ], request.formats - with_accept_header false do - request = stub_request - request.expects(:parameters).at_least_once.returns({ :format => :txt }) - assert_equal with_set(Mime::TEXT), request.formats - end + request = stub_request 'CONTENT_TYPE' => 'application/xml; charset=UTF-8', + 'HTTP_X_REQUESTED_WITH' => "XMLHttpRequest" + request.expects(:parameters).at_least_once.returns({}) + assert_equal with_set(Mime::XML), request.formats + + request = stub_request + request.expects(:parameters).at_least_once.returns({ :format => :txt }) + assert_equal with_set(Mime::TEXT), request.formats end test "negotiate_mime" do - with_accept_header true do - request = stub_request 'HTTP_ACCEPT' => 'text/html' - request.expects(:parameters).at_least_once.returns({}) - - assert_equal nil, request.negotiate_mime([Mime::XML, Mime::JSON]) - assert_equal Mime::HTML, request.negotiate_mime([Mime::XML, Mime::HTML]) - assert_equal Mime::HTML, request.negotiate_mime([Mime::XML, Mime::ALL]) - - request = stub_request 'CONTENT_TYPE' => 'application/xml; charset=UTF-8' - request.expects(:parameters).at_least_once.returns({}) - assert_equal Mime::XML, request.negotiate_mime([Mime::XML, Mime::CSV]) - assert_equal Mime::CSV, request.negotiate_mime([Mime::CSV, Mime::YAML]) - end + request = stub_request 'HTTP_ACCEPT' => 'text/html', + 'HTTP_X_REQUESTED_WITH' => "XMLHttpRequest" + + request.expects(:parameters).at_least_once.returns({}) + + assert_equal nil, request.negotiate_mime([Mime::XML, Mime::JSON]) + assert_equal Mime::HTML, request.negotiate_mime([Mime::XML, Mime::HTML]) + assert_equal Mime::HTML, request.negotiate_mime([Mime::XML, Mime::ALL]) + + request = stub_request 'CONTENT_TYPE' => 'application/xml; charset=UTF-8', + 'HTTP_X_REQUESTED_WITH' => "XMLHttpRequest" + request.expects(:parameters).at_least_once.returns({}) + assert_equal Mime::XML, request.negotiate_mime([Mime::XML, Mime::CSV]) end protected diff --git a/actionpack/test/fixtures/content_type/render_default_content_types_for_respond_to.rhtml b/actionpack/test/fixtures/content_type/render_default_content_types_for_respond_to.xml.erb index 25dc746886..25dc746886 100644 --- a/actionpack/test/fixtures/content_type/render_default_content_types_for_respond_to.rhtml +++ b/actionpack/test/fixtures/content_type/render_default_content_types_for_respond_to.xml.erb diff --git a/actionpack/test/fixtures/test/_customer_with_var.erb b/actionpack/test/fixtures/test/_customer_with_var.erb index c28824936b..00047dd20e 100644 --- a/actionpack/test/fixtures/test/_customer_with_var.erb +++ b/actionpack/test/fixtures/test/_customer_with_var.erb @@ -1 +1 @@ -<%= customer.name %> <%= customer.name %> <%= customer_with_var.name %>
\ No newline at end of file +<%= customer.name %> <%= customer.name %> <%= customer.name %>
\ No newline at end of file diff --git a/actionpack/test/fixtures/test/_hash_object.erb b/actionpack/test/fixtures/test/_hash_object.erb index 55c03afb27..34a92c6a56 100644 --- a/actionpack/test/fixtures/test/_hash_object.erb +++ b/actionpack/test/fixtures/test/_hash_object.erb @@ -1,2 +1,2 @@ <%= hash_object[:first_name] %> -<%= object[:first_name].reverse %> +<%= hash_object[:first_name].reverse %> diff --git a/actionpack/test/fixtures/test/using_layout_around_block_with_args.html.erb b/actionpack/test/fixtures/test/using_layout_around_block_with_args.html.erb deleted file mode 100644 index 71b1f30ad0..0000000000 --- a/actionpack/test/fixtures/test/using_layout_around_block_with_args.html.erb +++ /dev/null @@ -1 +0,0 @@ -<% render(:layout => "layout_for_block_with_args") do |*args| %><%= args.join %><% end %>
\ No newline at end of file diff --git a/actionpack/test/lib/fixture_template.rb b/actionpack/test/lib/fixture_template.rb index ee526b5de5..8da92180d1 100644 --- a/actionpack/test/lib/fixture_template.rb +++ b/actionpack/test/lib/fixture_template.rb @@ -4,7 +4,7 @@ module ActionView #:nodoc: super(options) @hash = hash end - + def find_templates(name, details, prefix, partial) if regexp = details_to_regexp(name, details, prefix, partial) cached(regexp) do @@ -16,26 +16,26 @@ module ActionView #:nodoc: end end end - + private - + def formats_regexp @formats_regexp ||= begin formats = Mime::SET.symbols '(?:' + formats.map { |l| "\\.#{Regexp.escape(l.to_s)}" }.join('|') + ')?' end end - + def handler_regexp e = TemplateHandlers.extensions.map{|h| "\\.#{Regexp.escape(h.to_s)}"}.join("|") - "(?:#{e})?" + "(?:#{e})" end - + def details_to_regexp(name, details, prefix, partial) path = "" path << "#{prefix}/" unless prefix.empty? path << (partial ? "_#{name}" : name) - + extensions = "" [:locales, :formats].each do |k| extensions << if exts = details[k] @@ -47,7 +47,7 @@ module ActionView #:nodoc: %r'^#{Regexp.escape(path)}#{extensions}#{handler_regexp}$' end - + # TODO: fix me # :api: plugin def path_to_details(path) @@ -56,10 +56,10 @@ module ActionView #:nodoc: partial = m[1] == '_' details = (m[2]||"").split('.').reject { |e| e.empty? } handler = Template.handler_class_for_extension(m[3]) - + format = Mime[details.last] && details.pop.to_sym locale = details.last && details.pop.to_sym - + return handler, :format => format, :locale => locale, :partial => partial end end diff --git a/actionpack/test/new_base/content_type_test.rb b/actionpack/test/new_base/content_type_test.rb index cfc03a3024..ceee508224 100644 --- a/actionpack/test/new_base/content_type_test.rb +++ b/actionpack/test/new_base/content_type_test.rb @@ -75,7 +75,7 @@ module ContentType end test "sets Content-Type as application/xml when rendering *.xml.erb" do - get "/content_type/implied/i_am_xml_erb" + get "/content_type/implied/i_am_xml_erb", "format" => "xml" assert_header "Content-Type", "application/xml; charset=utf-8" end @@ -87,7 +87,7 @@ module ContentType end test "sets Content-Type as application/xml when rendering *.xml.builder" do - get "/content_type/implied/i_am_xml_builder" + get "/content_type/implied/i_am_xml_builder", "format" => "xml" assert_header "Content-Type", "application/xml; charset=utf-8" end diff --git a/actionpack/test/new_base/render_layout_test.rb b/actionpack/test/new_base/render_layout_test.rb index 279b807a5f..933eef58e7 100644 --- a/actionpack/test/new_base/render_layout_test.rb +++ b/actionpack/test/new_base/render_layout_test.rb @@ -83,7 +83,7 @@ module ControllerLayouts testing ControllerLayouts::MismatchFormatController test "if JS is selected, an HTML template is not also selected" do - get :index + get :index, "format" => "js" assert_response "$(\"test\").omg();" end diff --git a/actionpack/test/new_base/render_rjs_test.rb b/actionpack/test/new_base/render_rjs_test.rb index bd4c87b3bf..3d3e516905 100644 --- a/actionpack/test/new_base/render_rjs_test.rb +++ b/actionpack/test/new_base/render_rjs_test.rb @@ -21,24 +21,23 @@ module RenderRjs def index_locale old_locale, I18n.locale = I18n.locale, :da end - end class TestBasic < SimpleRouteCase testing BasicController test "rendering a partial in an RJS template should pick the JS template over the HTML one" do - get :index + get :index, "format" => "js" assert_response("$(\"customer\").update(\"JS Partial\");") end test "replacing an element with a partial in an RJS template should pick the HTML template over the JS one" do - get :index_html + get :index_html, "format" => "js" assert_response("$(\"customer\").update(\"HTML Partial\");") end test "replacing an element with a partial in an RJS template with a locale should pick the localed HTML template" do - get :index_locale, :format => :js + get :index_locale, "format" => "js" assert_response("$(\"customer\").update(\"Danish HTML Partial\");") end diff --git a/actionpack/test/new_base/render_template_test.rb b/actionpack/test/new_base/render_template_test.rb index 94ea38fc7b..967cbd07b0 100644 --- a/actionpack/test/new_base/render_template_test.rb +++ b/actionpack/test/new_base/render_template_test.rb @@ -73,7 +73,7 @@ module RenderTemplate end test "rendering a builder template" do - get :builder_template + get :builder_template, "format" => "xml" assert_response "<html>\n <p>Hello</p>\n</html>\n" end end diff --git a/actionpack/test/template/javascript_helper_test.rb b/actionpack/test/template/javascript_helper_test.rb index 8caabfc3e1..f0f686f6e2 100644 --- a/actionpack/test/template/javascript_helper_test.rb +++ b/actionpack/test/template/javascript_helper_test.rb @@ -7,6 +7,10 @@ class JavaScriptHelperTest < ActionView::TestCase attr_accessor :formats, :output_buffer + def reset_formats(format) + @format = format + end + def setup super @template = self diff --git a/actionpack/test/template/prototype_helper_test.rb b/actionpack/test/template/prototype_helper_test.rb index acbf311212..313a769088 100644 --- a/actionpack/test/template/prototype_helper_test.rb +++ b/actionpack/test/template/prototype_helper_test.rb @@ -36,6 +36,10 @@ class Author::Nested < Author; end class PrototypeHelperBaseTest < ActionView::TestCase attr_accessor :formats, :output_buffer + def reset_formats(format) + @format = format + end + def setup super @template = self diff --git a/actionpack/test/template/render_test.rb b/actionpack/test/template/render_test.rb index 7f30ae88a1..c86d5215cd 100644 --- a/actionpack/test/template/render_test.rb +++ b/actionpack/test/template/render_test.rb @@ -2,10 +2,14 @@ require 'abstract_unit' require 'controller/fake_models' +class TestController < ActionController::Base +end + module RenderTestCases def setup_view(paths) @assigns = { :secret => 'in the sauce' } @view = ActionView::Base.new(paths, @assigns) + @controller_view = ActionView::Base.for_controller(TestController.new) # Reload and register danish language for testing I18n.reload! @@ -138,7 +142,7 @@ module RenderTestCases end def test_render_partial_collection_without_as - assert_equal "local_inspector,local_inspector_counter,object", + assert_equal "local_inspector,local_inspector_counter", @view.render(:partial => "test/local_inspector", :collection => [ Customer.new("mary") ]) end @@ -158,6 +162,25 @@ module RenderTestCases assert_nil @view.render(:partial => []) end + def test_render_partial_using_string + assert_equal "Hello: Anonymous", @controller_view.render('customer') + end + + def test_render_partial_with_locals_using_string + assert_equal "Hola: david", @controller_view.render('customer_greeting', :greeting => 'Hola', :customer_greeting => Customer.new("david")) + end + + def test_render_partial_using_object + assert_equal "Hello: lifo", + @controller_view.render(Customer.new("lifo"), :greeting => "Hello") + end + + def test_render_partial_using_collection + customers = [ Customer.new("Amazon"), Customer.new("Yahoo") ] + assert_equal "Hello: AmazonHello: Yahoo", + @controller_view.render(customers, :greeting => "Hello") + end + # TODO: The reason for this test is unclear, improve documentation def test_render_partial_and_fallback_to_layout assert_equal "Before (Josh)\n\nAfter", @view.render(:partial => "test/layout_for_partial", :locals => { :name => "Josh" }) @@ -167,6 +190,8 @@ module RenderTestCases def test_render_missing_xml_partial_and_raise_missing_template @view.formats = [:xml] assert_raise(ActionView::MissingTemplate) { @view.render(:partial => "test/layout_for_partial") } + ensure + @view.formats = nil end def test_render_inline @@ -196,17 +221,6 @@ module RenderTestCases assert_equal 'source: "Hello, <%= name %>!"', @view.render(:inline => "Hello, <%= name %>!", :locals => { :name => "Josh" }, :type => :foo) end - class LegacyHandler < ActionView::TemplateHandler - def render(template, local_assigns) - "source: #{template.source}; locals: #{local_assigns.inspect}" - end - end - - def test_render_legacy_handler_with_custom_type - ActionView::Template.register_template_handler :foo, LegacyHandler - assert_equal 'source: Hello, <%= name %>!; locals: {:name=>"Josh"}', @view.render(:inline => "Hello, <%= name %>!", :locals => { :name => "Josh" }, :type => :foo) - end - def test_render_ignores_templates_with_malformed_template_handlers %w(malformed malformed.erb malformed.html.erb malformed.en.html.erb).each do |name| assert_raise(ActionView::MissingTemplate) { @view.render(:file => "test/malformed/#{name}") } |