From 1191e3ffaf7ced4155eb6c2b1661e27d7851ca9a Mon Sep 17 00:00:00 2001 From: rizwanreza Date: Sat, 8 Aug 2009 17:27:57 +0300 Subject: Add :include_blank option for select_tag [#1987 status:resolved] MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: José Valim Signed-off-by: Pratik Naik --- actionpack/lib/action_view/helpers/form_tag_helper.rb | 7 +++++++ actionpack/test/template/form_tag_helper_test.rb | 12 ++++++++++++ 2 files changed, 19 insertions(+) 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 # # 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_tags + else + option_tags = "" + 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/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", "", :include_blank => true + expected = %() + assert_dom_equal expected, actual + end + + def test_select_tag_with_include_blank_with_string + actual = select_tag "places", "", :include_blank => "string" + expected = %() + assert_dom_equal expected, actual + end + def test_text_area_tag_size_string actual = text_area_tag "body", "hello world", "size" => "20x40" expected = %() -- cgit v1.2.3 From c34d6279a06c80c51eefc3d345c24eab9a1e44fe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Fri, 24 Jul 2009 00:05:57 +0200 Subject: Allow radio buttons to work with booleans. Signed-off-by: Pratik Naik --- actionpack/lib/action_view/helpers/form_helper.rb | 4 ++-- actionpack/test/template/form_helper_test.rb | 10 ++++++++++ 2 files changed, 12 insertions(+), 2 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/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('', + radio_button("post", "secret", true) + ) + + assert_dom_equal('', + radio_button("post", "secret", false) + ) + end + def test_text_area assert_dom_equal( '', -- cgit v1.2.3 From 5ab94b2595836fe2de36fd632ba9577c459b1292 Mon Sep 17 00:00:00 2001 From: jzw Date: Wed, 5 Aug 2009 20:17:59 -0500 Subject: validates_length_of with maximum should allow nil [#2309 status:resolved] MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: José Valim --- activemodel/lib/active_model/validations/length.rb | 6 ++++-- activemodel/test/cases/validations/length_validation_test.rb | 10 +++++++--- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/activemodel/lib/active_model/validations/length.rb b/activemodel/lib/active_model/validations/length.rb index db0439d447..81c97238d2 100644 --- a/activemodel/lib/active_model/validations/length.rb +++ b/activemodel/lib/active_model/validations/length.rb @@ -80,8 +80,10 @@ 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) + unless option == :maximum and value.nil? + unless !value.nil? and value.size.send(validity_checks[option], option_value) + record.errors.add(attr, key, :default => custom_message, :count => option_value) + end 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..bc24900ecf 100644 --- a/activemodel/test/cases/validations/length_validation_test.rb +++ b/activemodel/test/cases/validations/length_validation_test.rb @@ -52,6 +52,13 @@ 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 + puts t.errors + assert t.valid? + end + def test_optionally_validates_length_of_using_minimum Topic.validates_length_of :title, :minimum => 5, :allow_nil => true @@ -75,9 +82,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 -- cgit v1.2.3 From c6fe49b00921cda55af2dc311dd432795c4313f5 Mon Sep 17 00:00:00 2001 From: Pratik Naik Date: Sat, 8 Aug 2009 19:08:39 +0100 Subject: Simplyfy validates_length_of and remove puts --- activemodel/lib/active_model/validations/length.rb | 11 +++++++---- activemodel/test/cases/validations/length_validation_test.rb | 1 - 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/activemodel/lib/active_model/validations/length.rb b/activemodel/lib/active_model/validations/length.rb index 81c97238d2..3e76796355 100644 --- a/activemodel/lib/active_model/validations/length.rb +++ b/activemodel/lib/active_model/validations/length.rb @@ -80,11 +80,14 @@ module ActiveModel validates_each(attrs, options) do |record, attr, value| value = options[:tokenizer].call(value) if value.kind_of?(String) - unless option == :maximum and value.nil? - unless !value.nil? and value.size.send(validity_checks[option], option_value) - record.errors.add(attr, key, :default => custom_message, :count => option_value) - end + + 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 bc24900ecf..499f6a5e31 100644 --- a/activemodel/test/cases/validations/length_validation_test.rb +++ b/activemodel/test/cases/validations/length_validation_test.rb @@ -55,7 +55,6 @@ class LengthValidationTest < ActiveModel::TestCase def test_validates_length_of_using_maximum_should_allow_nil Topic.validates_length_of :title, :maximum => 10 t = Topic.create - puts t.errors assert t.valid? end -- cgit v1.2.3 From 6464d76feb94c7547fc6c046c010ea5be9bb6fe6 Mon Sep 17 00:00:00 2001 From: Pratik Naik Date: Sat, 8 Aug 2009 20:47:14 +0100 Subject: DRY migration's rollback/forward methods --- activerecord/lib/active_record/migration.rb | 29 +++++++++++++++-------------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/activerecord/lib/active_record/migration.rb b/activerecord/lib/active_record/migration.rb index d205e57db3..adb3a3f75e 100644 --- a/activerecord/lib/active_record/migration.rb +++ b/activerecord/lib/active_record/migration.rb @@ -388,23 +388,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) @@ -440,6 +428,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) -- cgit v1.2.3 From 00544c778f53b034bf4560548479a20a06d5c22d Mon Sep 17 00:00:00 2001 From: Dan Croak Date: Sat, 8 Aug 2009 15:40:08 -0400 Subject: Add test ensuring redirect_to uses the given protocol [#2886] Signed-off-by: Pratik Naik --- actionpack/test/controller/redirect_test.rb | 10 ++++++++++ 1 file changed, 10 insertions(+) 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 -- cgit v1.2.3 From 761283ffdb5750f8a38e2ed67891d2b2b9152d7f Mon Sep 17 00:00:00 2001 From: Pratik Naik Date: Sat, 8 Aug 2009 21:51:33 +0100 Subject: Ensure hm:t#create/create! throws ActiveRecord::RecordNotSaved when the owner is new --- .../lib/active_record/associations/has_many_through_association.rb | 4 ++++ .../test/cases/associations/has_many_through_associations_test.rb | 7 +++++++ 2 files changed, 11 insertions(+) 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 e21ef90391..ed7c3a6e08 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/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) } -- cgit v1.2.3 From 5786395760f1e1906c878df4023cac3741e66e87 Mon Sep 17 00:00:00 2001 From: rizwanreza Date: Sat, 8 Aug 2009 22:21:25 +0100 Subject: Allow content_tag options to take an array [#1741 state:resolved] [rizwanreza, Nick Quaranto] Example: content_tag('p', "limelight", :class => ["song", "play"]) # =>

limelight

Signed-off-by: Pratik Naik --- actionpack/lib/action_view/helpers/tag_helper.rb | 16 +++++++--------- actionpack/test/template/tag_helper_test.rb | 13 +++++++++++++ 2 files changed, 20 insertions(+), 9 deletions(-) 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/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 '

Hello

', output_buffer end + def test_content_tag_with_escaped_array_class + str = content_tag('p', "limelight", :class => ["song", "play>"]) + assert_equal "

limelight

", str + + str = content_tag('p', "limelight", :class => ["song", "play"]) + assert_equal "

limelight

", str + end + + def test_content_tag_with_unescaped_array_class + str = content_tag('p', "limelight", {:class => ["song", "play>"]}, false) + assert_equal "

\">limelight

", str + end + def test_cdata_section assert_equal "]]>", cdata_section("") end -- cgit v1.2.3