Permalink
Browse files

Merge commit 'rails/master'

Conflicts:
	activerecord/lib/active_record/migration.rb
  • Loading branch information...
2 parents 9520143 + 5786395 commit eb3ae44ccaff1dc63eb31bf86d8db07c88ddc413 @miloops miloops committed Aug 10, 2009
@@ -872,8 +872,8 @@ def radio_button_checked?(value, checked_value)
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
@@ -79,6 +79,13 @@ def form_tag(url_for_options = {}, options = {}, *parameters_for_url, &block)
# # <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
@@ -134,16 +134,14 @@ def content_tag_string(name, content, options, escape = true)
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
@@ -34,6 +34,10 @@ def redirect_with_status_hash
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 @@ def test_redirect_with_status_hash
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
@@ -307,6 +307,16 @@ def test_radio_button_respects_passed_in_id
)
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>',
@@ -136,6 +136,18 @@ def test_select_tag_id_sanitized
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>)
@@ -71,6 +71,19 @@ def test_content_tag_nested_in_content_tag_in_erb
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
@@ -80,9 +80,14 @@ def validates_length_of(*attrs)
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
@@ -52,6 +52,12 @@ def test_validates_length_of_using_minimum
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 @@ def test_validates_length_of_using_maximum
t.title = ""
assert t.valid?
-
- t.title = nil
- assert !t.valid?
end
def test_optionally_validates_length_of_using_maximum
@@ -8,13 +8,17 @@ class HasManyThroughAssociation < HasManyAssociation #:nodoc:
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
end
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
@@ -384,23 +384,11 @@ def migrate(migrations_path, target_version = nil)
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 @@ def proper_table_name(name)
# 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)
@@ -169,6 +169,13 @@ def test_associate_with_create_exclamation_and_no_options
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) }

0 comments on commit eb3ae44

Please sign in to comment.