Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Ticket #10233 (new enhancement)

Opened 9 months ago

Last modified 9 months ago

Multiple option hashes in ActionView::Helpers::FormOptionsHelper backwards compatible fix.

Reported by: leethal Assigned to: core
Priority: normal Milestone: 2.x
Component: ActionPack Version: edge
Severity: normal Keywords: tiny
Cc:

Description

This patch adds a backwards compatible change to ActionView::Helpers::FormOptionsHelper, regarding option hashes.

In the current trunk of rails, this is how you add options to the html tag on e.g. collection_select:

collection_select(:category_id, @categories, 'id', 'title', {}, :class => "cool")

In other words, there's two option hashes on that method. This is annoying because:

  • It nerfs the looks of the otherwise beautiful last-argument-as-option-hash way of writing methods in Ruby
  • It's impossible to add options properly using with_options scopes
  • It's not what people expect - other methods, such as form_for, is using :html => {:separate => "hash"} to do this.

This patch improves the behavior of the select methods by letting people use one option hash and specify html options in a hash, stored in the :html key of the original options hash.

collection_select(:category_id, @categories, 'id', 'title', :html => {:class => "cool"})

It's also backwards compatible, so it won't break the existing way of using two option hashes. This is done by parsing *args.

Tests are written, patch attached.

Attachments

option_hash_for_form_options_helper_patch.diff (25.7 kB) - added by leethal on 11/21/07 11:25:25.
option_hash_for_form_options_helper_patch_with_proper_deprecation_warning.diff (25.5 kB) - added by leethal on 11/21/07 12:11:05.
Updated the .diff with a proper deprecation warning (I hope)
option_hash_for_form_options_helper_patch_without_deprecation_warnings.diff (25.3 kB) - added by leethal on 11/26/07 09:54:07.
Same as the other files, but with deprecation warnings removed.

Change History

11/21/07 11:25:25 changed by leethal

  • attachment option_hash_for_form_options_helper_patch.diff added.

11/21/07 11:35:17 changed by leethal

  • keywords set to tiny.

Added the 'tiny' keyword. (let's hope that wasn't a silly thing to do)

11/21/07 12:08:25 changed by lifofifo

Nice. A little suggestion, the deprecation warning should be shown only if the user is using old format.

11/21/07 12:11:05 changed by leethal

  • attachment option_hash_for_form_options_helper_patch_with_proper_deprecation_warning.diff added.

Updated the .diff with a proper deprecation warning (I hope)

11/24/07 17:54:47 changed by josh

Tests pass however I see a ton of deprecation warnings that need to be fixed. I really like the idea and hopefully when the deprecation code is removed everything turns out a bit cleaner than before.

11/26/07 09:54:07 changed by leethal

  • attachment option_hash_for_form_options_helper_patch_without_deprecation_warnings.diff added.

Same as the other files, but with deprecation warnings removed.

11/26/07 09:55:55 changed by leethal

Uploaded a 3rd diff, with the deprecation warning call removed.