Changeset 1031

Show
Ignore:
Timestamp:
11/22/07 05:33:13 (11 months ago)
Author:
wayneesegu..@gmail.com
Message:

#326 select_field should be using options_from_collection_for_select and add support for :prompt and :include_blank into options_from_collection_for_select.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • plugins/merb_helpers/lib/form_helpers.rb

    r1028 r1031  
    355355          :value_method => attrs.delete(:value_method) 
    356356        } 
    357         optional_label(attrs) { open_tag('select', attrs) + options_for_select(collection, option_attrs) + "</select>"} 
     357        optional_label(attrs) { open_tag('select', attrs) + options_from_collection_for_select(collection, option_attrs) + "</select>"} 
    358358      end 
    359359       
     
    434434      # +selected+:: The value of a selected object, may be either a string or an array. 
    435435      def options_from_collection_for_select(collection, attrs = {}) 
    436           if collection.is_a?(Hash) 
    437             returning '' do |ret| 
    438               collection.each do |label, group| 
    439                 ret << open_tag("optgroup", :label => label.to_s) + options_from_collection_for_select(group, attrs) + "</optgroup>" 
    440               end 
     436        prompt     = attrs.delete(:prompt) 
     437        blank      = attrs.delete(:include_blank) 
     438        if collection.is_a?(Hash) 
     439          returning '' do |ret| 
     440                  ret << tag("option", prompt, :value => '') if prompt 
     441                  ret << tag("option", '',     :value => '') if blank 
     442            collection.each do |label, group| 
     443              ret << open_tag("optgroup", :label => label.to_s) + options_from_collection_for_select(group, attrs) + "</optgroup>" 
    441444            end 
    442           else 
    443             text_method    = attrs[:text_method] 
    444             value_method   = attrs[:value_method] 
    445             selected_value = attrs[:selected] 
    446             options_for_select( 
    447                collection.inject([]) { |options, object| options << [ object.send(value_method), object.send(text_method) ] }, 
    448                      :selected => selected_value  
    449             ) 
    450445          end 
     446        else 
     447          text_method    = attrs[:text_method] 
     448          value_method   = attrs[:value_method] 
     449          selected_value = attrs[:selected] 
     450          options_for_select( 
     451            collection.inject([]) { |options, object| options << [ object.send(value_method), object.send(text_method) ] }, 
     452            :selected => selected_value, :include_blank => blank, :prompt => prompt 
     453          ) 
     454        end 
    451455      end 
    452456