DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

Simple Date Picker For Merb 0.9.3

06.29.2008
| 6400 views |
  • submit to reddit
        Bare-bones date picker for Merb. Drop it in global_helpers.rb. Must be called from inside a form_for block.

def date_control(col, attrs = {})

  attrs.merge!(:name => attrs[:name] || control_name(col))
  attrs.merge!(:id   => attrs[:id]   || control_id(col))

  date = @_obj.send(col) || Time.new
  
  # TODO: Handle :selected option
  #attrs.merge!(:selected => attrs[:selected] || control_value(col))
  
  errorify_field(attrs, col)

  month_attrs = attrs.merge(
    :selected   => date.month.to_s, 
    :name       => attrs[:name] + '[month]',
    :id         => attrs[:id] + '_month',
    :collection => [1,2,3,4,5,6,7,8,9,10,11,12]
  )
  
  day_attrs = attrs.merge(
    :selected   => date.day.to_s, 
    :name       => attrs[:name] + '[day]',
    :id         => attrs[:id] + '_day',
    :collection => (1..31).to_a,
    :label      => nil
  )
  
  year_attrs = attrs.merge(
    :selected   => date.year.to_s,
    :name       => attrs[:name] + '[year]',
    :id         => attrs[:id] + '_year',
    :collection => (1970..2020).to_a,
    :label      => nil
  )
  
  optional_label(month_attrs) {
     select_field(month_attrs) 
  } + select_field(day_attrs) + select_field(year_attrs)
end