diff options
12 files changed, 107 insertions, 102 deletions
diff --git a/actionpack/CHANGELOG b/actionpack/CHANGELOG index 878f9df6eb..014a501080 100644 --- a/actionpack/CHANGELOG +++ b/actionpack/CHANGELOG @@ -1,5 +1,7 @@ *Edge* +* Fixed that PrototypeHelper#update_page should return html_safe [DHH] + * Fixed that much of DateHelper wouldn't return html_safe? strings [DHH] * Fixed that fragment caching should return a cache hit as html_safe (or it would all just get escaped) [DHH] diff --git a/actionpack/lib/action_view/helpers/prototype_helper.rb b/actionpack/lib/action_view/helpers/prototype_helper.rb index 8c1f0ad81f..ff7bc3b34e 100644 --- a/actionpack/lib/action_view/helpers/prototype_helper.rb +++ b/actionpack/lib/action_view/helpers/prototype_helper.rb @@ -1030,7 +1030,7 @@ module ActionView # page.hide 'spinner' # end def update_page(&block) - JavaScriptGenerator.new(@template, &block).to_s + JavaScriptGenerator.new(@template, &block).to_s.html_safe! end # Works like update_page but wraps the generated JavaScript in a <script> diff --git a/activemodel/lib/active_model/validations/validates.rb b/activemodel/lib/active_model/validations/validates.rb index ea7303c70b..80924f50d6 100644 --- a/activemodel/lib/active_model/validations/validates.rb +++ b/activemodel/lib/active_model/validations/validates.rb @@ -54,7 +54,7 @@ module ActiveModel # The validators hash can also handle regular expressions, ranges and arrays: # # validates :email, :format => /@/ - # validates :genre, :inclusion => %w(mail female) + # validates :gender, :inclusion => %w(mail female) # validates :password, :length => 6..20 # # Finally, the options :if, :unless, :on, :allow_blank and :allow_nil can be given diff --git a/activemodel/test/cases/validations/validates_test.rb b/activemodel/test/cases/validations/validates_test.rb index 966690d75f..d15fb4a524 100644 --- a/activemodel/test/cases/validations/validates_test.rb +++ b/activemodel/test/cases/validations/validates_test.rb @@ -70,20 +70,20 @@ class ValidatesTest < ActiveModel::TestCase end def test_validates_with_array - Person.validates :genre, :inclusion => %w(m f) + Person.validates :gender, :inclusion => %w(m f) person = Person.new assert person.invalid? - assert_equal ['is not included in the list'], person.errors[:genre] - person.genre = "m" + assert_equal ['is not included in the list'], person.errors[:gender] + person.gender = "m" assert person.valid? end def test_validates_with_range - Person.validates :password, :length => 6..20 + Person.validates :karma, :length => 6..20 person = Person.new assert person.invalid? - assert_equal ['is too short (minimum is 6 characters)'], person.errors[:password] - person.password = '123456' + assert_equal ['is too short (minimum is 6 characters)'], person.errors[:karma] + person.karma = 'something' assert person.valid? end diff --git a/activemodel/test/models/person.rb b/activemodel/test/models/person.rb index 86e8d73bbb..cf16a38618 100644 --- a/activemodel/test/models/person.rb +++ b/activemodel/test/models/person.rb @@ -2,7 +2,7 @@ class Person include ActiveModel::Validations extend ActiveModel::Translation - attr_accessor :title, :karma, :salary, :genre, :password + attr_accessor :title, :karma, :salary, :gender def condition_is_true true diff --git a/activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb b/activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb index e731bc84f0..86ba7d72c3 100644 --- a/activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb +++ b/activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb @@ -344,12 +344,12 @@ module ActiveRecord end end - def assume_migrated_upto_version(version) + def assume_migrated_upto_version(version, migrations_path = ActiveRecord::Migrator.migrations_path) version = version.to_i sm_table = quote_table_name(ActiveRecord::Migrator.schema_migrations_table_name) migrated = select_values("SELECT version FROM #{sm_table}").map(&:to_i) - versions = Dir['db/migrate/[0-9]*_*.rb'].map do |filename| + versions = Dir["#{migrations_path}/[0-9]*_*.rb"].map do |filename| filename.split('/').last.split('_').first.to_i end diff --git a/activerecord/lib/active_record/migration.rb b/activerecord/lib/active_record/migration.rb index 0022e8cc37..aeeed24881 100644 --- a/activerecord/lib/active_record/migration.rb +++ b/activerecord/lib/active_record/migration.rb @@ -407,6 +407,10 @@ module ActiveRecord self.new(direction, migrations_path, target_version).run end + def migrations_path + 'db/migrate' + end + def schema_migrations_table_name Base.table_name_prefix + 'schema_migrations' + Base.table_name_suffix end diff --git a/activerecord/lib/active_record/schema.rb b/activerecord/lib/active_record/schema.rb index 8a32cf1ca2..a996a0ebac 100644 --- a/activerecord/lib/active_record/schema.rb +++ b/activerecord/lib/active_record/schema.rb @@ -28,6 +28,10 @@ module ActiveRecord class Schema < Migration private_class_method :new + def self.migrations_path + ActiveRecord::Migrator.migrations_path + end + # Eval the given block. All methods available to the current connection # adapter are available within the block, so you can easily use the # database definition DSL to build up your schema (+create_table+, @@ -44,7 +48,7 @@ module ActiveRecord unless info[:version].blank? initialize_schema_migrations_table - assume_migrated_upto_version info[:version] + assume_migrated_upto_version(info[:version], migrations_path) end end end diff --git a/activerecord/lib/active_record/validations/associated.rb b/activerecord/lib/active_record/validations/associated.rb index 66b78682ad..e41635134c 100644 --- a/activerecord/lib/active_record/validations/associated.rb +++ b/activerecord/lib/active_record/validations/associated.rb @@ -40,8 +40,7 @@ module ActiveRecord # not occur (e.g. <tt>:unless => :skip_validation</tt>, or <tt>:unless => Proc.new { |user| user.signup_step <= 2 }</tt>). The # method, proc or string should return or evaluate to a true or false value. def validates_associated(*attr_names) - options = attr_names.extract_options! - validates_with AssociatedValidator, options.merge(:attributes => attr_names) + validates_with AssociatedValidator, _merge_attributes(attr_names) end end end diff --git a/activerecord/lib/active_record/validations/uniqueness.rb b/activerecord/lib/active_record/validations/uniqueness.rb index 7efd312357..4ff851dfa1 100644 --- a/activerecord/lib/active_record/validations/uniqueness.rb +++ b/activerecord/lib/active_record/validations/uniqueness.rb @@ -2,10 +2,14 @@ module ActiveRecord module Validations class UniquenessValidator < ActiveModel::EachValidator def initialize(options) - @klass = options.delete(:klass) super(options.reverse_merge(:case_sensitive => true)) end + # Unfortunately, we have to tie Uniqueness validators to a class. + def setup(klass) + @klass = klass + end + def validate_each(record, attribute, value) finder_class = find_finder_class_for(record) table = finder_class.active_relation @@ -170,8 +174,7 @@ module ActiveRecord # such a case. # def validates_uniqueness_of(*attr_names) - options = attr_names.extract_options! - validates_with UniquenessValidator, options.merge(:attributes => attr_names, :klass => self) + validates_with UniquenessValidator, _merge_attributes(attr_names) end end end diff --git a/activerecord/test/cases/validations/association_validation_test.rb b/activerecord/test/cases/validations/association_validation_test.rb index 5ed997356b..1246dd4276 100644 --- a/activerecord/test/cases/validations/association_validation_test.rb +++ b/activerecord/test/cases/validations/association_validation_test.rb @@ -10,33 +10,29 @@ require 'models/interest' class AssociationValidationTest < ActiveRecord::TestCase fixtures :topics, :owners - repair_validations(Topic, Reply) + repair_validations(Topic, Reply, Owner) def test_validates_size_of_association - repair_validations(Owner) do - assert_nothing_raised { Owner.validates_size_of :pets, :minimum => 1 } - o = Owner.new('name' => 'nopets') - assert !o.save - assert o.errors[:pets].any? - pet = o.pets.build('name' => 'apet') - assert o.valid? - end + assert_nothing_raised { Owner.validates_size_of :pets, :minimum => 1 } + o = Owner.new('name' => 'nopets') + assert !o.save + assert o.errors[:pets].any? + pet = o.pets.build('name' => 'apet') + assert o.valid? end def test_validates_size_of_association_using_within - repair_validations(Owner) do - assert_nothing_raised { Owner.validates_size_of :pets, :within => 1..2 } - o = Owner.new('name' => 'nopets') - assert !o.save - assert o.errors[:pets].any? + assert_nothing_raised { Owner.validates_size_of :pets, :within => 1..2 } + o = Owner.new('name' => 'nopets') + assert !o.save + assert o.errors[:pets].any? - pet = o.pets.build('name' => 'apet') - assert o.valid? + pet = o.pets.build('name' => 'apet') + assert o.valid? - 2.times { o.pets.build('name' => 'apet') } - assert !o.save - assert o.errors[:pets].any? - end + 2.times { o.pets.build('name' => 'apet') } + assert !o.save + assert o.errors[:pets].any? end def test_validates_associated_many @@ -55,51 +51,43 @@ class AssociationValidationTest < ActiveRecord::TestCase end def test_validates_associated_one - repair_validations(Reply) do - Reply.validates_associated( :topic ) - Topic.validates_presence_of( :content ) - r = Reply.new("title" => "A reply", "content" => "with content!") - r.topic = Topic.create("title" => "uhohuhoh") - assert !r.valid? - assert r.errors[:topic].any? - r.topic.content = "non-empty" - assert r.valid? - end + Reply.validates :topic, :associated => true + Topic.validates_presence_of( :content ) + r = Reply.new("title" => "A reply", "content" => "with content!") + r.topic = Topic.create("title" => "uhohuhoh") + assert !r.valid? + assert r.errors[:topic].any? + r.topic.content = "non-empty" + assert r.valid? end def test_validates_associated_with_custom_message_using_quotes - repair_validations(Reply) do - Reply.validates_associated :topic, :message=> "This string contains 'single' and \"double\" quotes" - Topic.validates_presence_of :content - r = Reply.create("title" => "A reply", "content" => "with content!") - r.topic = Topic.create("title" => "uhohuhoh") - assert !r.valid? - assert_equal ["This string contains 'single' and \"double\" quotes"], r.errors[:topic] - end + Reply.validates_associated :topic, :message=> "This string contains 'single' and \"double\" quotes" + Topic.validates_presence_of :content + r = Reply.create("title" => "A reply", "content" => "with content!") + r.topic = Topic.create("title" => "uhohuhoh") + assert !r.valid? + assert_equal ["This string contains 'single' and \"double\" quotes"], r.errors[:topic] end def test_validates_associated_missing - repair_validations(Reply) do - Reply.validates_presence_of(:topic) - r = Reply.create("title" => "A reply", "content" => "with content!") - assert !r.valid? - assert r.errors[:topic].any? + Reply.validates_presence_of(:topic) + r = Reply.create("title" => "A reply", "content" => "with content!") + assert !r.valid? + assert r.errors[:topic].any? - r.topic = Topic.find :first - assert r.valid? - end + r.topic = Topic.find :first + assert r.valid? end def test_validates_size_of_association_utf8 - repair_validations(Owner) do - with_kcode('UTF8') do - assert_nothing_raised { Owner.validates_size_of :pets, :minimum => 1 } - o = Owner.new('name' => 'あいうえおかきくけこ') - assert !o.save - assert o.errors[:pets].any? - o.pets.build('name' => 'あいうえおかきくけこ') - assert o.valid? - end + with_kcode('UTF8') do + assert_nothing_raised { Owner.validates_size_of :pets, :minimum => 1 } + o = Owner.new('name' => 'あいうえおかきくけこ') + assert !o.save + assert o.errors[:pets].any? + o.pets.build('name' => 'あいうえおかきくけこ') + assert o.valid? end end diff --git a/activerecord/test/cases/validations/uniqueness_validation_test.rb b/activerecord/test/cases/validations/uniqueness_validation_test.rb index 8f84841fe6..9a863c25a8 100644 --- a/activerecord/test/cases/validations/uniqueness_validation_test.rb +++ b/activerecord/test/cases/validations/uniqueness_validation_test.rb @@ -38,7 +38,7 @@ end class UniquenessValidationTest < ActiveRecord::TestCase fixtures :topics, 'warehouse-things', :developers - repair_validations(Topic) + repair_validations(Topic, Reply) def test_validate_uniqueness Topic.validates_uniqueness_of(:title) @@ -58,6 +58,15 @@ class UniquenessValidationTest < ActiveRecord::TestCase assert t2.save, "Should now save t2 as unique" end + def test_validates_uniqueness_with_validates + Topic.validates :title, :uniqueness => true + t = Topic.create!('title' => 'abc') + + t2 = Topic.new('title' => 'abc') + assert !t2.valid? + assert t2.errors[:title] + end + def test_validates_uniqueness_with_newline_chars Topic.validates_uniqueness_of(:title, :case_sensitive => false) @@ -66,24 +75,22 @@ class UniquenessValidationTest < ActiveRecord::TestCase end def test_validate_uniqueness_with_scope - repair_validations(Reply) do - Reply.validates_uniqueness_of(:content, :scope => "parent_id") + Reply.validates_uniqueness_of(:content, :scope => "parent_id") - t = Topic.create("title" => "I'm unique!") + t = Topic.create("title" => "I'm unique!") - r1 = t.replies.create "title" => "r1", "content" => "hello world" - assert r1.valid?, "Saving r1" + r1 = t.replies.create "title" => "r1", "content" => "hello world" + assert r1.valid?, "Saving r1" - r2 = t.replies.create "title" => "r2", "content" => "hello world" - assert !r2.valid?, "Saving r2 first time" + r2 = t.replies.create "title" => "r2", "content" => "hello world" + assert !r2.valid?, "Saving r2 first time" - r2.content = "something else" - assert r2.save, "Saving r2 second time" + r2.content = "something else" + assert r2.save, "Saving r2 second time" - t2 = Topic.create("title" => "I'm unique too!") - r3 = t2.replies.create "title" => "r3", "content" => "hello world" - assert r3.valid?, "Saving r3" - end + t2 = Topic.create("title" => "I'm unique too!") + r3 = t2.replies.create "title" => "r3", "content" => "hello world" + assert r3.valid?, "Saving r3" end def test_validate_uniqueness_scoped_to_defining_class @@ -102,29 +109,27 @@ class UniquenessValidationTest < ActiveRecord::TestCase end def test_validate_uniqueness_with_scope_array - repair_validations(Reply) do - Reply.validates_uniqueness_of(:author_name, :scope => [:author_email_address, :parent_id]) + Reply.validates_uniqueness_of(:author_name, :scope => [:author_email_address, :parent_id]) - t = Topic.create("title" => "The earth is actually flat!") + t = Topic.create("title" => "The earth is actually flat!") - r1 = t.replies.create "author_name" => "jeremy", "author_email_address" => "jeremy@rubyonrails.com", "title" => "You're crazy!", "content" => "Crazy reply" - assert r1.valid?, "Saving r1" + r1 = t.replies.create "author_name" => "jeremy", "author_email_address" => "jeremy@rubyonrails.com", "title" => "You're crazy!", "content" => "Crazy reply" + assert r1.valid?, "Saving r1" - r2 = t.replies.create "author_name" => "jeremy", "author_email_address" => "jeremy@rubyonrails.com", "title" => "You're crazy!", "content" => "Crazy reply again..." - assert !r2.valid?, "Saving r2. Double reply by same author." + r2 = t.replies.create "author_name" => "jeremy", "author_email_address" => "jeremy@rubyonrails.com", "title" => "You're crazy!", "content" => "Crazy reply again..." + assert !r2.valid?, "Saving r2. Double reply by same author." - r2.author_email_address = "jeremy_alt_email@rubyonrails.com" - assert r2.save, "Saving r2 the second time." + r2.author_email_address = "jeremy_alt_email@rubyonrails.com" + assert r2.save, "Saving r2 the second time." - r3 = t.replies.create "author_name" => "jeremy", "author_email_address" => "jeremy_alt_email@rubyonrails.com", "title" => "You're wrong", "content" => "It's cubic" - assert !r3.valid?, "Saving r3" + r3 = t.replies.create "author_name" => "jeremy", "author_email_address" => "jeremy_alt_email@rubyonrails.com", "title" => "You're wrong", "content" => "It's cubic" + assert !r3.valid?, "Saving r3" - r3.author_name = "jj" - assert r3.save, "Saving r3 the second time." + r3.author_name = "jj" + assert r3.save, "Saving r3 the second time." - r3.author_name = "jeremy" - assert !r3.save, "Saving r3 the third time." - end + r3.author_name = "jeremy" + assert !r3.save, "Saving r3 the third time." end def test_validate_case_insensitive_uniqueness |