remove support of symbols on classify and camelize

This commit is contained in:
Damien Mathieu 2011-09-08 10:21:50 +02:00
parent e9f48cdcf4
commit 3718ccd2a6
4 changed files with 52 additions and 34 deletions

View File

@ -21,6 +21,7 @@ module ActiveSupport
# "words".pluralize # => "words" # "words".pluralize # => "words"
# "CamelOctopus".pluralize # => "CamelOctopi" # "CamelOctopus".pluralize # => "CamelOctopi"
def pluralize(word) def pluralize(word)
word = deprecate_symbol(word)
result = word.to_str.dup result = word.to_str.dup
if word.empty? || inflections.uncountables.include?(result.downcase) if word.empty? || inflections.uncountables.include?(result.downcase)
@ -40,6 +41,7 @@ module ActiveSupport
# "word".singularize # => "word" # "word".singularize # => "word"
# "CamelOctopi".singularize # => "CamelOctopus" # "CamelOctopi".singularize # => "CamelOctopus"
def singularize(word) def singularize(word)
word = deprecate_symbol(word)
result = word.to_str.dup result = word.to_str.dup
if inflections.uncountables.any? { |inflection| result =~ /\b(#{inflection})\Z/i } if inflections.uncountables.any? { |inflection| result =~ /\b(#{inflection})\Z/i }
@ -66,6 +68,7 @@ module ActiveSupport
# #
# "SSLError".underscore.camelize # => "SslError" # "SSLError".underscore.camelize # => "SslError"
def camelize(term, uppercase_first_letter = true) def camelize(term, uppercase_first_letter = true)
term = deprecate_symbol(term)
string = term.to_str string = term.to_str
if uppercase_first_letter if uppercase_first_letter
string = string.sub(/^[a-z\d]*/) { inflections.acronyms[$&] || $&.capitalize } string = string.sub(/^[a-z\d]*/) { inflections.acronyms[$&] || $&.capitalize }
@ -88,6 +91,7 @@ module ActiveSupport
# #
# "SSLError".underscore.camelize # => "SslError" # "SSLError".underscore.camelize # => "SslError"
def underscore(camel_cased_word) def underscore(camel_cased_word)
camel_cased_word = deprecate_symbol(camel_cased_word)
word = camel_cased_word.to_str.dup word = camel_cased_word.to_str.dup
word.gsub!(/::/, '/') word.gsub!(/::/, '/')
word.gsub!(/(?:([A-Za-z\d])|^)(#{inflections.acronym_regex})(?=\b|[^a-z])/) { "#{$1}#{$1 && '_'}#{$2.downcase}" } word.gsub!(/(?:([A-Za-z\d])|^)(#{inflections.acronym_regex})(?=\b|[^a-z])/) { "#{$1}#{$1 && '_'}#{$2.downcase}" }
@ -105,6 +109,7 @@ module ActiveSupport
# "employee_salary" # => "Employee salary" # "employee_salary" # => "Employee salary"
# "author_id" # => "Author" # "author_id" # => "Author"
def humanize(lower_case_and_underscored_word) def humanize(lower_case_and_underscored_word)
lower_case_and_underscored_word = deprecate_symbol(lower_case_and_underscored_word)
result = lower_case_and_underscored_word.to_str.dup result = lower_case_and_underscored_word.to_str.dup
inflections.humans.each { |(rule, replacement)| break if result.gsub!(rule, replacement) } inflections.humans.each { |(rule, replacement)| break if result.gsub!(rule, replacement) }
result.gsub!(/_id$/, "") result.gsub!(/_id$/, "")
@ -148,6 +153,7 @@ module ActiveSupport
# Singular names are not handled correctly: # Singular names are not handled correctly:
# "business".classify # => "Busines" # "business".classify # => "Busines"
def classify(table_name) def classify(table_name)
table_name = deprecate_symbol(table_name)
# strip out any leading schema name # strip out any leading schema name
camelize(singularize(table_name.to_str.sub(/.*\./, ''))) camelize(singularize(table_name.to_str.sub(/.*\./, '')))
end end
@ -157,6 +163,7 @@ module ActiveSupport
# Example: # Example:
# "puni_puni" # => "puni-puni" # "puni_puni" # => "puni-puni"
def dasherize(underscored_word) def dasherize(underscored_word)
underscored_word = deprecate_symbol(underscored_word)
underscored_word.to_str.gsub(/_/, '-') underscored_word.to_str.gsub(/_/, '-')
end end
@ -166,6 +173,7 @@ module ActiveSupport
# "ActiveRecord::CoreExtensions::String::Inflections".demodulize # => "Inflections" # "ActiveRecord::CoreExtensions::String::Inflections".demodulize # => "Inflections"
# "Inflections".demodulize # => "Inflections" # "Inflections".demodulize # => "Inflections"
def demodulize(class_name_in_module) def demodulize(class_name_in_module)
class_name_in_module = deprecate_symbol(class_name_in_module)
class_name_in_module.to_str.gsub(/^.*::/, '') class_name_in_module.to_str.gsub(/^.*::/, '')
end end
@ -246,5 +254,13 @@ module ActiveSupport
end end
end end
end end
def deprecate_symbol(symbol)
if symbol.is_a?(Symbol)
symbol = symbol.to_s
ActiveSupport::Deprecation.warn("Using symbols in inflections is deprecated. Please use to_s to have a string.")
end
symbol
end
end end
end end

View File

@ -10,7 +10,7 @@ module Ace
end end
end end
class InflectorTest < Test::Unit::TestCase class InflectorTest < ActiveSupport::TestCase
include InflectorTestCases include InflectorTestCases
def test_pluralize_plurals def test_pluralize_plurals
@ -248,12 +248,6 @@ class InflectorTest < Test::Unit::TestCase
end end
end end
def test_classify_with_symbol
assert_nothing_raised do
assert_equal 'FooBar', ActiveSupport::Inflector.classify(:foo_bars)
end
end
def test_classify_with_leading_schema_name def test_classify_with_leading_schema_name
assert_equal 'FooBar', ActiveSupport::Inflector.classify('schema.foo_bar') assert_equal 'FooBar', ActiveSupport::Inflector.classify('schema.foo_bar')
end end
@ -319,12 +313,6 @@ class InflectorTest < Test::Unit::TestCase
end end
end end
def test_symbol_to_lower_camel
SymbolToLowerCamel.each do |symbol, lower_camel|
assert_equal(lower_camel, ActiveSupport::Inflector.camelize(symbol, false))
end
end
%w{plurals singulars uncountables humans}.each do |inflection_type| %w{plurals singulars uncountables humans}.each do |inflection_type|
class_eval <<-RUBY, __FILE__, __LINE__ + 1 class_eval <<-RUBY, __FILE__, __LINE__ + 1
def test_clear_#{inflection_type} def test_clear_#{inflection_type}
@ -380,6 +368,14 @@ class InflectorTest < Test::Unit::TestCase
ActiveSupport::Inflector.inflections.instance_variable_set :@humans, cached_values[3] ActiveSupport::Inflector.inflections.instance_variable_set :@humans, cached_values[3]
end end
[:pluralize, :singularize, :camelize, :underscore, :humanize, :titleize, :tableize, :classify, :dasherize, :demodulize].each do |method|
test "should deprecate symbols on #{method}" do
assert_deprecated(/Using symbols in inflections is deprecated/) do
ActiveSupport::Inflector.send(method, :foo)
end
end
end
Irregularities.each do |irregularity| Irregularities.each do |irregularity|
singular, plural = *irregularity singular, plural = *irregularity
ActiveSupport::Inflector.inflections do |inflect| ActiveSupport::Inflector.inflections do |inflect|

View File

@ -120,13 +120,6 @@ module InflectorTestCases
"area51_controller" => "area51Controller" "area51_controller" => "area51Controller"
} }
SymbolToLowerCamel = {
:product => 'product',
:special_guest => 'specialGuest',
:application_controller => 'applicationController',
:area51_controller => 'area51Controller'
}
CamelToUnderscoreWithoutReverse = { CamelToUnderscoreWithoutReverse = {
"HTMLTidy" => "html_tidy", "HTMLTidy" => "html_tidy",
"HTMLTidyGenerator" => "html_tidy_generator", "HTMLTidyGenerator" => "html_tidy_generator",

View File

@ -67,42 +67,41 @@ class SafeBufferTest < ActiveSupport::TestCase
assert_equal "my_test", str assert_equal "my_test", str
end end
test "Should not return safe buffer from gsub" do test "Should not return safe buffer from capitalize" do
altered_buffer = @buffer.gsub('', 'asdf') altered_buffer = "asdf".html_safe.capitalize
assert_equal 'asdf', altered_buffer assert_equal 'Asdf', altered_buffer
assert !altered_buffer.html_safe? assert !altered_buffer.html_safe?
end end
test "Should not return safe buffer from gsub!" do test "Should not return safe buffer from gsub!" do
@buffer.gsub!('', 'asdf') string = "asdf"
assert_equal 'asdf', @buffer string.capitalize!
assert !@buffer.html_safe? assert_equal 'Asdf', string
assert !string.html_safe?
end end
test "Should escape dirty buffers on add" do test "Should escape dirty buffers on add" do
clean = "hello".html_safe clean = "hello".html_safe
@buffer.gsub!('', '<>') assert_equal "hello&lt;&gt;", clean + '<>'
assert_equal "hello&lt;&gt;", clean + @buffer
end end
test "Should concat as a normal string when dirty" do test "Should concat as a normal string when dirty" do
clean = "hello".html_safe clean = "hello".html_safe
@buffer.gsub!('', '<>') assert_equal "<>hello", '<>' + clean
assert_equal "<>hello", @buffer + clean
end end
test "Should preserve dirty? status on copy" do test "Should preserve dirty? status on copy" do
@buffer.gsub!('', '<>') dirty = "<>"
assert !@buffer.dup.html_safe? assert !dirty.dup.html_safe?
end end
test "Should raise an error when safe_concat is called on dirty buffers" do test "Should raise an error when safe_concat is called on dirty buffers" do
@buffer.gsub!('', '<>') @buffer.capitalize!
assert_raise ActiveSupport::SafeBuffer::SafeConcatError do assert_raise ActiveSupport::SafeBuffer::SafeConcatError do
@buffer.safe_concat "BUSTED" @buffer.safe_concat "BUSTED"
end end
end end
test "should not fail if the returned object is not a string" do test "should not fail if the returned object is not a string" do
assert_kind_of NilClass, @buffer.slice("chipchop") assert_kind_of NilClass, @buffer.slice("chipchop")
end end
@ -112,4 +111,18 @@ class SafeBufferTest < ActiveSupport::TestCase
assert_not_nil dirty assert_not_nil dirty
assert !dirty assert !dirty
end end
["gsub", "sub"].each do |unavailable_method|
test "should raise on #{unavailable_method}" do
assert_raise NoMethodError, "#{unavailable_method} cannot be used with a Safe Buffer object. You should use object.to_str.#{unavailable_method}" do
@buffer.send(unavailable_method, '', '<>')
end
end
test "should raise on #{unavailable_method}!" do
assert_raise NoMethodError, "#{unavailable_method}! cannot be used with a Safe Buffer object. You should use object.to_str.#{unavailable_method}!" do
@buffer.send("#{unavailable_method}!", '', '<>')
end
end
end
end end