Skip to content

Commit

Permalink
Merge branch 'master' of git://github.com/rails/rails
Browse files Browse the repository at this point in the history
  • Loading branch information
mikel committed Jan 23, 2010
2 parents bd24c75 + 8ff2fb6 commit 1ea84c3
Show file tree
Hide file tree
Showing 31 changed files with 177 additions and 103 deletions.
37 changes: 25 additions & 12 deletions actionpack/lib/abstract_controller/rendering.rb
Expand Up @@ -98,18 +98,9 @@ def view_paths
_view_paths
end

# Normalize options, by converting render "foo" to render :template => "foo"
# and render "/foo" to render :file => "/foo".
def _normalize_options(action=nil, options={})
case action
when Hash
options, action = action, nil
when String
key = (action.index("/") == 0 ? :file : :template)
options.merge!(key => action)
end

options
# The prefix used in render "foo" shortcuts.
def _prefix
controller_path
end

# Return a string representation of a Rack-compatible response body.
Expand All @@ -126,6 +117,28 @@ def self.body_to_s(body)

private

# Normalize options, by converting render "foo" to render :template => "prefix/foo"
# and render "/foo" to render :file => "/foo".
def _normalize_options(action=nil, options={})
case action
when Hash
options, action = action, nil
when String, Symbol
action = action.to_s
case action.index("/")
when NilClass
options[:_prefix] = _prefix
options[:_template_name] = action
when 0
options[:file] = action
else
options[:template] = action
end
end

options
end

# Take in a set of options and determine the template to render
#
# ==== Options
Expand Down
23 changes: 0 additions & 23 deletions actionpack/lib/action_controller/base.rb
Expand Up @@ -81,28 +81,5 @@ def self.filter_parameter_logging(*args, &block)
filter << block if block
filter
end

def _normalize_options(action=nil, options={}, &blk)
case action
when NilClass
when Hash, String
options = super
when Symbol
options.merge! :action => action
else
options.merge! :partial => action
end

if options.key?(:action) && options[:action].to_s.index("/")
options[:template] = options.delete(:action)
end

if options[:status]
options[:status] = Rack::Utils.status_code(options[:status])
end

options[:update] = blk if block_given?
options
end
end
end
36 changes: 24 additions & 12 deletions actionpack/lib/action_controller/metal/rendering.rb
Expand Up @@ -25,18 +25,6 @@ def render_to_body(options)
end

private
def _prefix
controller_path
end

def _determine_template(options)
if (options.keys & [:partial, :file, :template, :text, :inline]).empty?
options[:_template_name] ||= options[:action]
options[:_prefix] = _prefix
end

super
end

def _render_partial(options)
options[:partial] = action_name if options[:partial] == true
Expand All @@ -54,5 +42,29 @@ def _process_options(options)
self.content_type = content_type if content_type
self.headers["Location"] = url_for(location) if location
end

def _normalize_options(action=nil, options={}, &blk)
case action
when NilClass
when Hash
options = super(action.delete(:action), action)
when String, Symbol
options = super
else
options.merge! :partial => action
end

if (options.keys & [:partial, :file, :template, :text, :inline]).empty?
options[:_template_name] ||= options[:action]
options[:_prefix] = _prefix
end

if options[:status]
options[:status] = Rack::Utils.status_code(options[:status])
end

options[:update] = blk if block_given?
options
end
end
end
14 changes: 6 additions & 8 deletions actionpack/lib/action_view/base.rb
Expand Up @@ -181,7 +181,6 @@ def config=(config)
extend ActiveSupport::Memoizable

attr_accessor :base_path, :assigns, :template_extension, :formats
attr_accessor :controller
attr_internal :captures

def reset_formats(formats)
Expand Down Expand Up @@ -277,13 +276,13 @@ def initialize(view_paths = [], assigns_for_first_render = {}, controller = nil,
@config = nil
@formats = formats
@assigns = assigns_for_first_render.each { |key, value| instance_variable_set("@#{key}", value) }
@controller = controller
@_controller = controller
@helpers = self.class.helpers || Module.new
@_content_for = Hash.new {|h,k| h[k] = ActionView::SafeBuffer.new }
self.view_paths = view_paths
end

attr_internal :template
attr_internal :controller, :template
attr_reader :view_paths

def view_paths=(paths)
Expand All @@ -298,12 +297,11 @@ def punctuate_body!(part)

# Evaluates the local assigns and controller ivars, pushes them to the view.
def _evaluate_assigns_and_ivars #: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)) }
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
end

end
end
8 changes: 4 additions & 4 deletions actionpack/lib/action_view/helpers/asset_tag_helper.rb
Expand Up @@ -634,8 +634,8 @@ def self.cache_asset_timestamps=(value)
# Prefix with <tt>/dir/</tt> if lacking a leading +/+. Account for relative URL
# roots. Rewrite the asset path for cache-busting asset ids. Include
# asset host, if configured, with the correct request protocol.
def compute_public_path(source, dir, ext = nil, include_host = true)
has_request = @controller.respond_to?(:request)
def compute_public_path(source, dir, ext = nil, include_host = true)
has_request = controller.respond_to?(:request)

source_ext = File.extname(source)[1..-1]
if ext && !is_uri?(source) && (source_ext.blank? || (ext != source_ext && File.exist?(File.join(config.assets_dir, dir, "#{source}.#{ext}"))))
Expand All @@ -658,7 +658,7 @@ def compute_public_path(source, dir, ext = nil, include_host = true)
host = compute_asset_host(source)

if has_request && !host.blank? && !is_uri?(host)
host = "#{@controller.request.protocol}#{host}"
host = "#{controller.request.protocol}#{host}"
end

"#{host}#{source}"
Expand All @@ -681,7 +681,7 @@ def compute_asset_host(source)
if host.is_a?(Proc) || host.respond_to?(:call)
case host.is_a?(Proc) ? host.arity : host.method(:call).arity
when 2
request = @controller.respond_to?(:request) && @controller.request
request = controller.respond_to?(:request) && controller.request
host.call(source, request)
else
host.call(source)
Expand Down
2 changes: 1 addition & 1 deletion actionpack/lib/action_view/helpers/cache_helper.rb
Expand Up @@ -32,7 +32,7 @@ module CacheHelper
# <i>Topics listed alphabetically</i>
# <% end %>
def cache(name = {}, options = nil, &block)
@controller.fragment_for(output_buffer, name, options, &block)
controller.fragment_for(output_buffer, name, options, &block)
end
end
end
Expand Down
12 changes: 6 additions & 6 deletions actionpack/lib/action_view/helpers/url_helper.rb
Expand Up @@ -13,7 +13,7 @@ module UrlHelper

# Need to map default url options to controller one.
def default_url_options(*args) #:nodoc:
@controller.send(:default_url_options, *args)
controller.send(:default_url_options, *args)
end

# Returns the URL for the set of +options+ provided. This takes the
Expand Down Expand Up @@ -89,10 +89,10 @@ def url_for(options = {})
when Hash
options = { :only_path => options[:host].nil? }.update(options.symbolize_keys)
escape = options.key?(:escape) ? options.delete(:escape) : false
@controller.send(:url_for, options)
controller.send(:url_for, options)
when :back
escape = false
@controller.request.env["HTTP_REFERER"] || 'javascript:history.back()'
controller.request.env["HTTP_REFERER"] || 'javascript:history.back()'
else
escape = false
polymorphic_path(options)
Expand Down Expand Up @@ -546,10 +546,10 @@ def mail_to(email_address, name = nil, html_options = {})
# # => false
def current_page?(options)
url_string = CGI.unescapeHTML(url_for(options))
request = @controller.request
# We ignore any extra parameters in the request_uri if the
request = controller.request
# We ignore any extra parameters in the request_uri if the
# submitted url doesn't have any either. This lets the function
# work with things like ?order=asc
# work with things like ?order=asc
if url_string.index("?")
request_uri = request.request_uri
else
Expand Down
35 changes: 30 additions & 5 deletions actionpack/test/abstract/render_test.rb
Expand Up @@ -6,9 +6,16 @@ module Testing
class ControllerRenderer < AbstractController::Base
include AbstractController::Rendering

def _prefix
"renderer"
end

self.view_paths = [ActionView::FixtureResolver.new(
"default.erb" => "With Default",
"template.erb" => "With Template",
"renderer/string.erb" => "With String",
"renderer/symbol.erb" => "With Symbol",
"string/with_path.erb" => "With String With Path",
"some/file.erb" => "With File",
"template_name.erb" => "With Template Name"
)]
Expand All @@ -33,8 +40,16 @@ def default
render
end

def shortcut
render "template"
def string
render "string"
end

def string_with_path
render "string/with_path"
end

def symbol
render :symbol
end

def template_name
Expand Down Expand Up @@ -77,9 +92,19 @@ def test_render_default
assert_equal "With Default", @controller.response_body
end

def test_render_template_through_shortcut
@controller.process(:shortcut)
assert_equal "With Template", @controller.response_body
def test_render_string
@controller.process(:string)
assert_equal "With String", @controller.response_body
end

def test_render_symbol
@controller.process(:symbol)
assert_equal "With Symbol", @controller.response_body
end

def test_render_string_with_path
@controller.process(:string_with_path)
assert_equal "With String With Path", @controller.response_body
end

def test_render_template_name
Expand Down
5 changes: 3 additions & 2 deletions activerecord/lib/active_record/base.rb
Expand Up @@ -1236,7 +1236,7 @@ def type_name_with_module(type_name)
end

def construct_finder_arel(options = {}, scope = nil)
relation = unscoped.apply_finder_options(options)
relation = options.is_a?(Hash) ? unscoped.apply_finder_options(options) : unscoped.merge(options)
relation = scope.merge(relation) if scope
relation
end
Expand Down Expand Up @@ -1450,7 +1450,8 @@ def default_scope(options = {})
end

def scoped_methods #:nodoc:
Thread.current[:"#{self}_scoped_methods"] ||= self.default_scoping.dup
key = :"#{self}_scoped_methods"
Thread.current[key] = Thread.current[key].presence || self.default_scoping.dup
end

def current_scoped_methods #:nodoc:
Expand Down
4 changes: 3 additions & 1 deletion activerecord/lib/active_record/relation.rb
Expand Up @@ -32,7 +32,7 @@ def create!(*args, &block)
end

def respond_to?(method, include_private = false)
return true if arel.respond_to?(method, include_private) || Array.method_defined?(method)
return true if arel.respond_to?(method, include_private) || Array.method_defined?(method) || @klass.respond_to?(method, include_private)

if match = DynamicFinderMatch.match(method)
return true if @klass.send(:all_attributes_exists?, match.attribute_names)
Expand Down Expand Up @@ -301,6 +301,8 @@ def eager_loading?
def method_missing(method, *args, &block)
if Array.method_defined?(method)
to_a.send(method, *args, &block)
elsif @klass.respond_to?(method)
@klass.send(:with_scope, self) { @klass.send(method, *args, &block) }
elsif arel.respond_to?(method)
arel.send(method, *args, &block)
elsif match = DynamicFinderMatch.match(method)
Expand Down
8 changes: 7 additions & 1 deletion activerecord/test/cases/method_scoping_test.rb
Expand Up @@ -588,7 +588,7 @@ def test_nested_scope
end

class DefaultScopingTest < ActiveRecord::TestCase
fixtures :developers
fixtures :developers, :posts

def test_default_scope
expected = Developer.find(:all, :order => 'salary DESC').collect { |dev| dev.salary }
Expand Down Expand Up @@ -657,6 +657,12 @@ def test_overwriting_default_scope
received = DeveloperOrderedBySalary.find(:all, :order => 'salary').collect { |dev| dev.salary }
assert_equal expected, received
end

def test_default_scope_using_relation
posts = PostWithComment.scoped
assert_equal 2, posts.count
assert_equal posts(:thinking), posts.first
end
end

=begin
Expand Down
9 changes: 9 additions & 0 deletions activerecord/test/cases/named_scope_test.rb
Expand Up @@ -380,6 +380,15 @@ def test_deprecated_named_scope_method
assert_deprecated('named_scope has been deprecated') { Topic.named_scope :deprecated_named_scope }
end

def test_named_scopes_on_relations
# Topic.replied
approved_topics = Topic.scoped.approved.order('id DESC')
assert_equal topics(:fourth), approved_topics.first

replied_approved_topics = approved_topics.replied
assert_equal topics(:third), replied_approved_topics.first
end

def test_index_on_named_scope
approved = Topic.approved.order('id ASC')
assert_equal topics(:second), approved[0]
Expand Down
5 changes: 5 additions & 0 deletions activerecord/test/cases/relations_test.rb
Expand Up @@ -164,6 +164,11 @@ def test_respond_to_dynamic_finders
end
end

def test_respond_to_class_methods_and_named_scopes
assert DeveloperOrderedBySalary.scoped.respond_to?(:all_ordered_by_name)
assert Topic.scoped.respond_to?(:by_lifo)
end

def test_find_with_readonly_option
Developer.scoped.each { |d| assert !d.readonly? }
Developer.scoped.readonly.each { |d| assert d.readonly? }
Expand Down
5 changes: 5 additions & 0 deletions activerecord/test/models/post.rb
Expand Up @@ -100,3 +100,8 @@ class StiPost < Post
class SubStiPost < StiPost
self.table_name = Post.table_name
end

class PostWithComment < ActiveRecord::Base
self.table_name = 'posts'
default_scope where("posts.comments_count > 0").order("posts.comments_count ASC")
end

0 comments on commit 1ea84c3

Please sign in to comment.