提交 3e2f0800 编写于 作者: J Jeremy Kemper

Merge branch 'master' of git@github.com:rails/rails

......@@ -34,12 +34,16 @@ def concat(string, unused_binding = nil)
# Truncates a given +text+ after a given <tt>:length</tt> if +text+ is longer than <tt>:length</tt>
# (defaults to 30). The last characters will be replaced with the <tt>:omission</tt> (defaults to "...").
# Pass a <tt>:separator</tt> to truncate +text+ at a natural break.
#
# ==== Examples
#
# truncate("Once upon a time in a world far far away")
# # => Once upon a time in a world f...
#
# truncate("Once upon a time in a world far far away", :separator => ' ')
# # => Once upon a time in a world...
#
# truncate("Once upon a time in a world far far away", :length => 14)
# # => Once upon a...
#
......@@ -71,7 +75,8 @@ def truncate(text, *args)
if text
l = options[:length] - options[:omission].mb_chars.length
chars = text.mb_chars
(chars.length > options[:length] ? chars[0...l] + options[:omission] : text).to_s
stop = options[:separator] ? (chars.rindex(options[:separator].mb_chars, l) || l) : l
(chars.length > options[:length] ? chars[0...stop] + options[:omission] : text).to_s
end
end
......
......@@ -49,6 +49,9 @@ def test_truncate_with_options_hash
assert_equal "This is a string that wil[...]", truncate("This is a string that will go longer then the default truncate length of 30", :omission => "[...]")
assert_equal "Hello W...", truncate("Hello World!", :length => 10)
assert_equal "Hello[...]", truncate("Hello World!", :omission => "[...]", :length => 10)
assert_equal "Hello[...]", truncate("Hello Big World!", :omission => "[...]", :length => 13, :separator => ' ')
assert_equal "Hello Big[...]", truncate("Hello Big World!", :omission => "[...]", :length => 14, :separator => ' ')
assert_equal "Hello Big[...]", truncate("Hello Big World!", :omission => "[...]", :length => 15, :separator => ' ')
end
if RUBY_VERSION < '1.9.0'
......
......@@ -17,6 +17,13 @@ def create(attrs = nil)
end
end
def destroy(*records)
transaction do
delete_records(flatten_deeper(records))
super
end
end
# Returns the size of the collection by executing a SELECT COUNT(*) query if the collection hasn't been loaded and
# calling collection.size if it has. If it's more likely than not that the collection does have a size larger than zero,
# and you need to fetch that collection afterwards, it'll take one fewer SELECT query if you use #length.
......
......@@ -2177,7 +2177,7 @@ def subclasses #:nodoc:
# default_scope :order => 'last_name, first_name'
# end
def default_scope(options = {})
self.default_scoping << { :find => options, :create => (options.is_a?(Hash) && options.has_key?(:conditions)) ? options[:conditions] : {} }
self.default_scoping << { :find => options, :create => options[:conditions].is_a?(Hash) ? options[:conditions] : {} }
end
# Test whether the given method and optional key are scoped.
......
......@@ -93,7 +93,7 @@ def test_delete_association
end
def test_destroy_association
assert_difference "Person.count", -1 do
assert_difference ["Person.count", "Reader.count"], -1 do
posts(:welcome).people.destroy(people(:michael))
end
......@@ -102,7 +102,7 @@ def test_destroy_association
end
def test_destroy_all
assert_difference "Person.count", -1 do
assert_difference ["Person.count", "Reader.count"], -1 do
posts(:welcome).people.destroy_all
end
......@@ -110,6 +110,12 @@ def test_destroy_all
assert posts(:welcome).people(true).empty?
end
def test_should_raise_exception_for_destroying_mismatching_records
assert_no_difference ["Person.count", "Reader.count"] do
assert_raise(ActiveRecord::AssociationTypeMismatch) { posts(:welcome).people.destroy(posts(:thinking)) }
end
end
def test_replace_association
assert_queries(4){posts(:welcome);people(:david);people(:michael); posts(:welcome).people(true)}
......
......@@ -591,6 +591,16 @@ def test_default_scope
assert_equal expected, received
end
def test_default_scope_with_conditions_string
assert_equal Developer.find_all_by_name('David').map(&:id).sort, DeveloperCalledDavid.all.map(&:id).sort
assert_equal nil, DeveloperCalledDavid.create!.name
end
def test_default_scope_with_conditions_hash
assert_equal Developer.find_all_by_name('Jamis').map(&:id).sort, DeveloperCalledJamis.all.map(&:id).sort
assert_equal 'Jamis', DeveloperCalledJamis.create!.name
end
def test_default_scoping_with_threads
scope = [{ :create => {}, :find => { :order => 'salary DESC' } }]
......
......@@ -89,3 +89,13 @@ def self.all_ordered_by_name
end
end
end
class DeveloperCalledDavid < ActiveRecord::Base
self.table_name = 'developers'
default_scope :conditions => "name = 'David'"
end
class DeveloperCalledJamis < ActiveRecord::Base
self.table_name = 'developers'
default_scope :conditions => { :name => 'Jamis' }
end
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册