diff options
12 files changed, 88 insertions, 47 deletions
diff --git a/actionmailer/lib/action_mailer/base.rb b/actionmailer/lib/action_mailer/base.rb index 7101bfbb70..84d50fa1f4 100644 --- a/actionmailer/lib/action_mailer/base.rb +++ b/actionmailer/lib/action_mailer/base.rb @@ -1,5 +1,8 @@ require 'active_support/core_ext/class' +require 'active_support/core_ext/object/blank' +require 'active_support/core_ext/array/uniq_by' require 'active_support/core_ext/module/delegation' +require 'active_support/core_ext/string/inflections' require 'mail' require 'action_mailer/tmail_compat' require 'action_mailer/collector' @@ -455,6 +458,8 @@ module ActionMailer #:nodoc: def each_template(&block) #:nodoc: self.class.view_paths.each do |load_paths| templates = load_paths.find_all(action_name, {}, self.class.mailer_name) + templates = templates.uniq_by { |t| t.details[:formats] } + unless templates.empty? templates.each(&block) return diff --git a/actionmailer/test/base_test.rb b/actionmailer/test/base_test.rb index dd17bf868b..71845d4c42 100644 --- a/actionmailer/test/base_test.rb +++ b/actionmailer/test/base_test.rb @@ -34,6 +34,10 @@ class BaseTest < ActiveSupport::TestCase mail(DEFAULT_HEADERS.merge(hash)) end + def implicit_with_locale(hash = {}) + mail(DEFAULT_HEADERS.merge(hash)) + end + def explicit_multipart(hash = {}) attachments['invoice.pdf'] = 'This is test File content' if hash.delete(:attachments) mail(DEFAULT_HEADERS.merge(hash)) do |format| @@ -310,6 +314,29 @@ class BaseTest < ActiveSupport::TestCase assert_equal(1, BaseMailer.deliveries.length) end + test "implicit multipart with default locale" do + email = BaseMailer.implicit_with_locale.deliver + assert_equal(2, email.parts.size) + assert_equal("multipart/alternate", email.mime_type) + assert_equal("text/plain", email.parts[0].mime_type) + assert_equal("Implicit with locale TEXT", email.parts[0].body.encoded) + assert_equal("text/html", email.parts[1].mime_type) + assert_equal("Implicit with locale EN HTML", email.parts[1].body.encoded) + end + + test "implicit multipart with other locale" do + swap I18n, :locale => :pl do + email = BaseMailer.implicit_with_locale.deliver + assert_equal(2, email.parts.size) + assert_equal("multipart/alternate", email.mime_type) + assert_equal("text/plain", email.parts[0].mime_type) + assert_equal("Implicit with locale PL TEXT", email.parts[0].body.encoded) + assert_equal("text/html", email.parts[1].mime_type) + assert_equal("Implicit with locale HTML", email.parts[1].body.encoded) + end + end + + protected # Execute the block setting the given values and restoring old values after diff --git a/actionmailer/test/fixtures/base_mailer/implicit_with_locale.en.html.erb b/actionmailer/test/fixtures/base_mailer/implicit_with_locale.en.html.erb new file mode 100644 index 0000000000..34e39c8fdb --- /dev/null +++ b/actionmailer/test/fixtures/base_mailer/implicit_with_locale.en.html.erb @@ -0,0 +1 @@ +Implicit with locale EN HTML
\ No newline at end of file diff --git a/actionmailer/test/fixtures/base_mailer/implicit_with_locale.html.erb b/actionmailer/test/fixtures/base_mailer/implicit_with_locale.html.erb new file mode 100644 index 0000000000..5ce283f221 --- /dev/null +++ b/actionmailer/test/fixtures/base_mailer/implicit_with_locale.html.erb @@ -0,0 +1 @@ +Implicit with locale HTML
\ No newline at end of file diff --git a/actionmailer/test/fixtures/base_mailer/implicit_with_locale.pl.text.erb b/actionmailer/test/fixtures/base_mailer/implicit_with_locale.pl.text.erb new file mode 100644 index 0000000000..c49cbae60a --- /dev/null +++ b/actionmailer/test/fixtures/base_mailer/implicit_with_locale.pl.text.erb @@ -0,0 +1 @@ +Implicit with locale PL TEXT
\ No newline at end of file diff --git a/actionmailer/test/fixtures/base_mailer/implicit_with_locale.text.erb b/actionmailer/test/fixtures/base_mailer/implicit_with_locale.text.erb new file mode 100644 index 0000000000..5a18ff62cd --- /dev/null +++ b/actionmailer/test/fixtures/base_mailer/implicit_with_locale.text.erb @@ -0,0 +1 @@ +Implicit with locale TEXT
\ No newline at end of file diff --git a/activesupport/lib/active_support/core_ext/array.rb b/activesupport/lib/active_support/core_ext/array.rb index b583c7533e..4688468a8f 100644 --- a/activesupport/lib/active_support/core_ext/array.rb +++ b/activesupport/lib/active_support/core_ext/array.rb @@ -1,5 +1,6 @@ require 'active_support/core_ext/array/wrap' require 'active_support/core_ext/array/access' +require 'active_support/core_ext/array/uniq_by' require 'active_support/core_ext/array/conversions' require 'active_support/core_ext/array/extract_options' require 'active_support/core_ext/array/grouping' diff --git a/activesupport/lib/active_support/core_ext/array/conversions.rb b/activesupport/lib/active_support/core_ext/array/conversions.rb index 7fcef38372..814567a5a6 100644 --- a/activesupport/lib/active_support/core_ext/array/conversions.rb +++ b/activesupport/lib/active_support/core_ext/array/conversions.rb @@ -13,19 +13,6 @@ class Array default_two_words_connector = I18n.translate(:'support.array.two_words_connector', :locale => options[:locale]) default_last_word_connector = I18n.translate(:'support.array.last_word_connector', :locale => options[:locale]) - # Try to emulate to_sentences previous to 2.3 - if options.has_key?(:connector) || options.has_key?(:skip_last_comma) - ::ActiveSupport::Deprecation.warn(":connector has been deprecated. Use :words_connector instead", caller) if options.has_key? :connector - ::ActiveSupport::Deprecation.warn(":skip_last_comma has been deprecated. Use :last_word_connector instead", caller) if options.has_key? :skip_last_comma - - skip_last_comma = options.delete :skip_last_comma - if connector = options.delete(:connector) - options[:last_word_connector] ||= skip_last_comma ? connector : ", #{connector}" - else - options[:last_word_connector] ||= skip_last_comma ? default_two_words_connector : default_last_word_connector - end - end - options.assert_valid_keys(:words_connector, :two_words_connector, :last_word_connector, :locale) options.reverse_merge! :words_connector => default_words_connector, :two_words_connector => default_two_words_connector, :last_word_connector => default_last_word_connector diff --git a/activesupport/lib/active_support/core_ext/array/uniq_by.rb b/activesupport/lib/active_support/core_ext/array/uniq_by.rb new file mode 100644 index 0000000000..a09b2302fd --- /dev/null +++ b/activesupport/lib/active_support/core_ext/array/uniq_by.rb @@ -0,0 +1,17 @@ +class Array + # Return an unique array based on the criteria given as a proc. + # + # [1, 2, 3, 4].uniq_by { |i| i.odd? } + # #=> [1, 2] + # + def uniq_by + hash, array = {}, [] + each { |i| hash[yield(i)] ||= (array << i) } + array + end + + # Same as uniq_by, but modifies self. + def uniq_by! + replace(uniq_by{ |i| yield(i) }) + end +end diff --git a/activesupport/lib/active_support/core_ext/hash/deep_merge.rb b/activesupport/lib/active_support/core_ext/hash/deep_merge.rb index 24d0a2a481..af771c86ff 100644 --- a/activesupport/lib/active_support/core_ext/hash/deep_merge.rb +++ b/activesupport/lib/active_support/core_ext/hash/deep_merge.rb @@ -1,17 +1,16 @@ class Hash # Returns a new hash with +self+ and +other_hash+ merged recursively. def deep_merge(other_hash) - target = dup - other_hash.each_pair do |k,v| - tv = target[k] - target[k] = tv.is_a?(Hash) && v.is_a?(Hash) ? tv.deep_merge(v) : v - end - target + dup.deep_merge!(other_hash) end # Returns a new hash with +self+ and +other_hash+ merged recursively. # Modifies the receiver in place. def deep_merge!(other_hash) - replace(deep_merge(other_hash)) + other_hash.each_pair do |k,v| + tv = self[k] + self[k] = tv.is_a?(Hash) && v.is_a?(Hash) ? tv.deep_merge(v) : v + end + self end end diff --git a/activesupport/lib/active_support/core_ext/hash/keys.rb b/activesupport/lib/active_support/core_ext/hash/keys.rb index ecd63293b4..045a6944fa 100644 --- a/activesupport/lib/active_support/core_ext/hash/keys.rb +++ b/activesupport/lib/active_support/core_ext/hash/keys.rb @@ -1,10 +1,7 @@ class Hash # Return a new hash with all keys converted to strings. def stringify_keys - inject({}) do |options, (key, value)| - options[key.to_s] = value - options - end + dup.stringify_keys! end # Destructively convert all keys to strings. @@ -18,16 +15,16 @@ class Hash # Return a new hash with all keys converted to symbols, as long as # they respond to +to_sym+. def symbolize_keys - inject({}) do |options, (key, value)| - options[(key.to_sym rescue key) || key] = value - options - end + dup.symbolize_keys! end # Destructively convert all keys to symbols, as long as they respond # to +to_sym+. def symbolize_keys! - self.replace(self.symbolize_keys) + keys.each do |key| + self[(key.to_sym rescue key) || key] = delete(key) + end + self end alias_method :to_options, :symbolize_keys diff --git a/activesupport/test/core_ext/array_ext_test.rb b/activesupport/test/core_ext/array_ext_test.rb index f5f91ddd80..d4cd5ddbde 100644 --- a/activesupport/test/core_ext/array_ext_test.rb +++ b/activesupport/test/core_ext/array_ext_test.rb @@ -52,8 +52,6 @@ class ArrayExtToParamTests < Test::Unit::TestCase end class ArrayExtToSentenceTests < Test::Unit::TestCase - include ActiveSupport::Testing::Deprecation - def test_plain_array_to_sentence assert_equal "", [].to_sentence assert_equal "one", ['one'].to_sentence @@ -62,28 +60,12 @@ class ArrayExtToSentenceTests < Test::Unit::TestCase end def test_to_sentence_with_words_connector - assert_deprecated(":connector has been deprecated. Use :words_connector instead") do - assert_equal "one, two, three", ['one', 'two', 'three'].to_sentence(:connector => '') - end - - assert_deprecated(":connector has been deprecated. Use :words_connector instead") do - assert_equal "one, two, and three", ['one', 'two', 'three'].to_sentence(:connector => 'and ') - end - assert_equal "one two, and three", ['one', 'two', 'three'].to_sentence(:words_connector => ' ') assert_equal "one & two, and three", ['one', 'two', 'three'].to_sentence(:words_connector => ' & ') assert_equal "onetwo, and three", ['one', 'two', 'three'].to_sentence(:words_connector => nil) end def test_to_sentence_with_last_word_connector - assert_deprecated(":skip_last_comma has been deprecated. Use :last_word_connector instead") do - assert_equal "one, two and three", ['one', 'two', 'three'].to_sentence(:skip_last_comma => true) - end - - assert_deprecated(":skip_last_comma has been deprecated. Use :last_word_connector instead") do - assert_equal "one, two, and three", ['one', 'two', 'three'].to_sentence(:skip_last_comma => false) - end - assert_equal "one, two, and also three", ['one', 'two', 'three'].to_sentence(:last_word_connector => ', and also ') assert_equal "one, twothree", ['one', 'two', 'three'].to_sentence(:last_word_connector => nil) assert_equal "one, two three", ['one', 'two', 'three'].to_sentence(:last_word_connector => ' ') @@ -320,6 +302,28 @@ class ArrayExtractOptionsTests < Test::Unit::TestCase end end +class ArrayUniqByTests < Test::Unit::TestCase + def test_uniq_by + assert_equal [1,2], [1,2,3,4].uniq_by { |i| i.odd? } + assert_equal [1,2], [1,2,3,4].uniq_by(&:even?) + assert_equal (-5..0).to_a, (-5..5).to_a.uniq_by{ |i| i**2 } + end + + def test_uniq_by! + a = [1,2,3,4] + a.uniq_by! { |i| i.odd? } + assert_equal [1,2], a + + a = [1,2,3,4] + a.uniq_by! { |i| i.even? } + assert_equal [1,2], a + + a = (-5..5).to_a + a.uniq_by! { |i| i**2 } + assert_equal (-5..0).to_a, a + end +end + class ArrayExtRandomTests < Test::Unit::TestCase def test_random_element_from_array assert_nil [].rand |