Changeset 15

Show
Ignore:
Timestamp:
08/30/2006 10:49:09 PM (2 years ago)
Author:
matthew
Message:

Fifteen minute hack of the day: nullable text fields. No idea if/how
this will work for anything else (though I guess it mostly should).
Almost completely untested. API may change. Use at your own risk. All
those sorts of things. :)

admin_fieldset do |fieldset|
  fieldset.text_field :something
  fieldset.nullable :text_field, :something_else
end
Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/auto-admin/lib/auto_admin_simple_theme.rb

    r10 r15  
    407407      end_src 
    408408    end 
     409    def nullable(field_type, field, options={}) 
     410      check_for_nullable_field! field 
     411      common_field_translations! field 
     412    end 
    409413 
    410414    %w(outer prologue epilogue).each do |helper| 
     
    422426      translate_association_to_column! field_name 
    423427    end 
     428    def check_for_nullable_field!(field_name) 
     429      params[field_name] = nil if params.delete( "#{field_name}_NULL" ) == 'NULL' 
     430    end 
    424431    def translate_association_to_column!(field_name) 
    425432      column = get_column_from_field( field_name ) 
     
    447454    def end_fieldset 
    448455      %(</fieldset>) 
     456    end 
     457 
     458    def nullable(field_type, field_name, options={}) 
     459      wrap_field field_type, field_name, options do |*a| 
     460        null_label = options.delete(:null_label) || 'None' 
     461        standard_field = send("#{field_type}_without_theme", field_name, options) 
     462        is_null = field_value(field_name).nil? 
     463        <<-foo 
     464        <input type="radio" name="#{@object_name}[#{field_name}_NULL]" value="NULL" #{'checked="checked" ' if is_null}/> #{null_label}<br /> 
     465        <input type="radio" name="#{@object_name}[#{field_name}_NULL]" value="" #{'checked="checked" ' unless is_null}/> #{standard_field} 
     466        foo 
     467      end 
    449468    end 
    450469