aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorEmilio Tagua <miloops@gmail.com>2009-08-10 10:40:11 -0300
committerEmilio Tagua <miloops@gmail.com>2009-08-10 10:40:11 -0300
commiteb3ae44ccaff1dc63eb31bf86d8db07c88ddc413 (patch)
tree9997bd3739e30fad0102a22e8feee40c4c4c835c
parent952014315926d370f2a0b681cb765948bf2e6883 (diff)
parent5786395760f1e1906c878df4023cac3741e66e87 (diff)
downloadrails-eb3ae44ccaff1dc63eb31bf86d8db07c88ddc413.tar.gz
rails-eb3ae44ccaff1dc63eb31bf86d8db07c88ddc413.tar.bz2
rails-eb3ae44ccaff1dc63eb31bf86d8db07c88ddc413.zip
Merge commit 'rails/master'
Conflicts: activerecord/lib/active_record/migration.rb
-rw-r--r--actionpack/lib/action_view/helpers/form_helper.rb4
-rw-r--r--actionpack/lib/action_view/helpers/form_tag_helper.rb7
-rw-r--r--actionpack/lib/action_view/helpers/tag_helper.rb16
-rw-r--r--actionpack/test/controller/redirect_test.rb10
-rw-r--r--actionpack/test/template/form_helper_test.rb10
-rw-r--r--actionpack/test/template/form_tag_helper_test.rb12
-rw-r--r--actionpack/test/template/tag_helper_test.rb13
-rw-r--r--activemodel/lib/active_model/validations/length.rb9
-rw-r--r--activemodel/test/cases/validations/length_validation_test.rb9
-rw-r--r--activerecord/lib/active_record/associations/has_many_through_association.rb4
-rw-r--r--activerecord/lib/active_record/migration.rb29
-rw-r--r--activerecord/test/cases/associations/has_many_through_associations_test.rb7
12 files changed, 100 insertions, 30 deletions
diff --git a/actionpack/lib/action_view/helpers/form_helper.rb b/actionpack/lib/action_view/helpers/form_helper.rb
index 6e2990e084..bde600f6ed 100644
--- a/actionpack/lib/action_view/helpers/form_helper.rb
+++ b/actionpack/lib/action_view/helpers/form_helper.rb
@@ -872,8 +872,8 @@ module ActionView
private
def add_default_name_and_id_for_value(tag_value, options)
- if tag_value
- pretty_tag_value = tag_value.to_s.gsub(/\s/, "_").gsub(/\W/, "").downcase
+ unless tag_value.nil?
+ pretty_tag_value = tag_value.to_s.gsub(/\s/, "_").gsub(/\W/, "").downcase
specified_id = options["id"]
add_default_name_and_id(options)
options["id"] += "_#{pretty_tag_value}" unless specified_id
diff --git a/actionpack/lib/action_view/helpers/form_tag_helper.rb b/actionpack/lib/action_view/helpers/form_tag_helper.rb
index 1abe7775e0..1d851ecbd7 100644
--- a/actionpack/lib/action_view/helpers/form_tag_helper.rb
+++ b/actionpack/lib/action_view/helpers/form_tag_helper.rb
@@ -79,6 +79,13 @@ module ActionView
# # <option>Paris</option><option>Rome</option></select>
def select_tag(name, option_tags = nil, options = {})
html_name = (options[:multiple] == true && !name.to_s.ends_with?("[]")) ? "#{name}[]" : name
+ if blank = options.delete(:include_blank)
+ if blank.kind_of?(String)
+ option_tags = "<option value=\"\">#{blank}</option>" + option_tags
+ else
+ option_tags = "<option value=\"\"></option>" + option_tags
+ end
+ end
content_tag :select, option_tags, { "name" => html_name, "id" => sanitize_to_id(name) }.update(options.stringify_keys)
end
diff --git a/actionpack/lib/action_view/helpers/tag_helper.rb b/actionpack/lib/action_view/helpers/tag_helper.rb
index eea797abb5..ff5a2134ff 100644
--- a/actionpack/lib/action_view/helpers/tag_helper.rb
+++ b/actionpack/lib/action_view/helpers/tag_helper.rb
@@ -134,16 +134,14 @@ module ActionView
def tag_options(options, escape = true)
unless options.blank?
attrs = []
- if escape
- options.each_pair do |key, value|
- if BOOLEAN_ATTRIBUTES.include?(key)
- attrs << %(#{key}="#{key}") if value
- else
- attrs << %(#{key}="#{escape_once(value)}") if !value.nil?
- end
+ options.each_pair do |key, value|
+ if BOOLEAN_ATTRIBUTES.include?(key)
+ attrs << %(#{key}="#{key}") if value
+ elsif !value.nil?
+ final_value = value.is_a?(Array) ? value.join(" ") : value
+ final_value = escape_once(final_value) if escape
+ attrs << %(#{key}="#{final_value}")
end
- else
- attrs = options.map { |key, value| %(#{key}="#{value}") }
end
" #{attrs.sort * ' '}" unless attrs.empty?
end
diff --git a/actionpack/test/controller/redirect_test.rb b/actionpack/test/controller/redirect_test.rb
index b3321303c0..7755af592d 100644
--- a/actionpack/test/controller/redirect_test.rb
+++ b/actionpack/test/controller/redirect_test.rb
@@ -34,6 +34,10 @@ class RedirectController < ActionController::Base
redirect_to({:action => "hello_world"}, {:status => 301})
end
+ def redirect_with_protocol
+ redirect_to :action => "hello_world", :protocol => "https"
+ end
+
def url_redirect_with_status
redirect_to("http://www.example.com", :status => :moved_permanently)
end
@@ -132,6 +136,12 @@ class RedirectTest < ActionController::TestCase
assert_equal "http://test.host/redirect/hello_world", redirect_to_url
end
+ def test_redirect_with_protocol
+ get :redirect_with_protocol
+ assert_response 302
+ assert_equal "https://test.host/redirect/hello_world", redirect_to_url
+ end
+
def test_url_redirect_with_status
get :url_redirect_with_status
assert_response 301
diff --git a/actionpack/test/template/form_helper_test.rb b/actionpack/test/template/form_helper_test.rb
index 99160dd8b1..2b1d80b1bf 100644
--- a/actionpack/test/template/form_helper_test.rb
+++ b/actionpack/test/template/form_helper_test.rb
@@ -307,6 +307,16 @@ class FormHelperTest < ActionView::TestCase
)
end
+ def test_radio_button_with_booleans
+ assert_dom_equal('<input id="post_secret_true" name="post[secret]" type="radio" value="true" />',
+ radio_button("post", "secret", true)
+ )
+
+ assert_dom_equal('<input id="post_secret_false" name="post[secret]" type="radio" value="false" />',
+ radio_button("post", "secret", false)
+ )
+ end
+
def test_text_area
assert_dom_equal(
'<textarea cols="40" id="post_body" name="post[body]" rows="20">Back to the hill and over it again!</textarea>',
diff --git a/actionpack/test/template/form_tag_helper_test.rb b/actionpack/test/template/form_tag_helper_test.rb
index 79004264fd..d64b9492e2 100644
--- a/actionpack/test/template/form_tag_helper_test.rb
+++ b/actionpack/test/template/form_tag_helper_test.rb
@@ -136,6 +136,18 @@ class FormTagHelperTest < ActionView::TestCase
assert_match VALID_HTML_ID, input_elem['id']
end
+ def test_select_tag_with_include_blank
+ actual = select_tag "places", "<option>Home</option><option>Work</option><option>Pub</option>", :include_blank => true
+ expected = %(<select id="places" name="places"><option value=""></option><option>Home</option><option>Work</option><option>Pub</option></select>)
+ assert_dom_equal expected, actual
+ end
+
+ def test_select_tag_with_include_blank_with_string
+ actual = select_tag "places", "<option>Home</option><option>Work</option><option>Pub</option>", :include_blank => "string"
+ expected = %(<select id="places" name="places"><option value="">string</option><option>Home</option><option>Work</option><option>Pub</option></select>)
+ assert_dom_equal expected, actual
+ end
+
def test_text_area_tag_size_string
actual = text_area_tag "body", "hello world", "size" => "20x40"
expected = %(<textarea cols="20" id="body" name="body" rows="40">hello world</textarea>)
diff --git a/actionpack/test/template/tag_helper_test.rb b/actionpack/test/template/tag_helper_test.rb
index ef88cae5b8..2aa3d5b5fa 100644
--- a/actionpack/test/template/tag_helper_test.rb
+++ b/actionpack/test/template/tag_helper_test.rb
@@ -71,6 +71,19 @@ class TagHelperTest < ActionView::TestCase
assert_equal '<p><b>Hello</b></p>', output_buffer
end
+ def test_content_tag_with_escaped_array_class
+ str = content_tag('p', "limelight", :class => ["song", "play>"])
+ assert_equal "<p class=\"song play&gt;\">limelight</p>", str
+
+ str = content_tag('p', "limelight", :class => ["song", "play"])
+ assert_equal "<p class=\"song play\">limelight</p>", str
+ end
+
+ def test_content_tag_with_unescaped_array_class
+ str = content_tag('p', "limelight", {:class => ["song", "play>"]}, false)
+ assert_equal "<p class=\"song play>\">limelight</p>", str
+ end
+
def test_cdata_section
assert_equal "<![CDATA[<hello world>]]>", cdata_section("<hello world>")
end
diff --git a/activemodel/lib/active_model/validations/length.rb b/activemodel/lib/active_model/validations/length.rb
index db0439d447..3e76796355 100644
--- a/activemodel/lib/active_model/validations/length.rb
+++ b/activemodel/lib/active_model/validations/length.rb
@@ -80,9 +80,14 @@ module ActiveModel
validates_each(attrs, options) do |record, attr, value|
value = options[:tokenizer].call(value) if value.kind_of?(String)
- unless !value.nil? and value.size.method(validity_checks[option])[option_value]
- record.errors.add(attr, key, :default => custom_message, :count => option_value)
+
+ valid_value = if option == :maximum
+ value.nil? || value.size.send(validity_checks[option], option_value)
+ else
+ value && value.size.send(validity_checks[option], option_value)
end
+
+ record.errors.add(attr, key, :default => custom_message, :count => option_value) unless valid_value
end
end
end
diff --git a/activemodel/test/cases/validations/length_validation_test.rb b/activemodel/test/cases/validations/length_validation_test.rb
index 4a2f72feab..499f6a5e31 100644
--- a/activemodel/test/cases/validations/length_validation_test.rb
+++ b/activemodel/test/cases/validations/length_validation_test.rb
@@ -52,6 +52,12 @@ class LengthValidationTest < ActiveModel::TestCase
assert_equal ["is too short (minimum is 5 characters)"], t.errors["title"]
end
+ def test_validates_length_of_using_maximum_should_allow_nil
+ Topic.validates_length_of :title, :maximum => 10
+ t = Topic.create
+ assert t.valid?
+ end
+
def test_optionally_validates_length_of_using_minimum
Topic.validates_length_of :title, :minimum => 5, :allow_nil => true
@@ -75,9 +81,6 @@ class LengthValidationTest < ActiveModel::TestCase
t.title = ""
assert t.valid?
-
- t.title = nil
- assert !t.valid?
end
def test_optionally_validates_length_of_using_maximum
diff --git a/activerecord/lib/active_record/associations/has_many_through_association.rb b/activerecord/lib/active_record/associations/has_many_through_association.rb
index 2a1d645859..2ed92ca1ba 100644
--- a/activerecord/lib/active_record/associations/has_many_through_association.rb
+++ b/activerecord/lib/active_record/associations/has_many_through_association.rb
@@ -8,6 +8,8 @@ module ActiveRecord
alias_method :new, :build
def create!(attrs = nil)
+ ensure_owner_is_not_new
+
transaction do
self << (object = attrs ? @reflection.klass.send(:with_scope, :create => attrs) { @reflection.create_association! } : @reflection.create_association!)
object
@@ -15,6 +17,8 @@ module ActiveRecord
end
def create(attrs = nil)
+ ensure_owner_is_not_new
+
transaction do
self << (object = attrs ? @reflection.klass.send(:with_scope, :create => attrs) { @reflection.create_association } : @reflection.create_association)
object
diff --git a/activerecord/lib/active_record/migration.rb b/activerecord/lib/active_record/migration.rb
index d5d4808074..09a558a636 100644
--- a/activerecord/lib/active_record/migration.rb
+++ b/activerecord/lib/active_record/migration.rb
@@ -384,23 +384,11 @@ module ActiveRecord
end
def rollback(migrations_path, steps=1)
- migrator = self.new(:down, migrations_path)
- start_index = migrator.migrations.index(migrator.current_migration)
-
- return unless start_index
-
- finish = migrator.migrations[start_index + steps]
- down(migrations_path, finish ? finish.version : 0)
+ move(:down, migrations_path, steps)
end
def forward(migrations_path, steps=1)
- migrator = self.new(:up, migrations_path)
- start_index = migrator.migrations.index(migrator.current_migration)
-
- return unless start_index
-
- finish = migrator.migrations[start_index + steps]
- up(migrations_path, finish ? finish.version : 0)
+ move(:up, migrations_path, steps)
end
def up(migrations_path, target_version = nil)
@@ -437,6 +425,19 @@ module ActiveRecord
# Use the Active Record objects own table_name, or pre/suffix from ActiveRecord::Base if name is a symbol/string
name.table_name rescue "#{ActiveRecord::Base.table_name_prefix}#{name}#{ActiveRecord::Base.table_name_suffix}"
end
+
+ private
+
+ def move(direction, migrations_path, steps)
+ migrator = self.new(direction, migrations_path)
+ start_index = migrator.migrations.index(migrator.current_migration)
+
+ if start_index
+ finish = migrator.migrations[start_index + steps]
+ version = finish ? finish.version : 0
+ send(direction, migrations_path, version)
+ end
+ end
end
def initialize(direction, migrations_path, target_version = nil)
diff --git a/activerecord/test/cases/associations/has_many_through_associations_test.rb b/activerecord/test/cases/associations/has_many_through_associations_test.rb
index 8529ff0285..f6b4a42377 100644
--- a/activerecord/test/cases/associations/has_many_through_associations_test.rb
+++ b/activerecord/test/cases/associations/has_many_through_associations_test.rb
@@ -169,6 +169,13 @@ class HasManyThroughAssociationsTest < ActiveRecord::TestCase
assert_equal peeps + 1, posts(:thinking).people.count
end
+ def test_create_on_new_record
+ p = Post.new
+
+ assert_raises(ActiveRecord::RecordNotSaved) { p.people.create(:first_name => "mew") }
+ assert_raises(ActiveRecord::RecordNotSaved) { p.people.create!(:first_name => "snow") }
+ end
+
def test_clear_associations
assert_queries(2) { posts(:welcome);posts(:welcome).people(true) }