module Merb::Helpers::Form # Available options known from rails: # :use_month_numbers # :default # :order => [:day, :month, :year] # :start_year # :end_year # :include_blank def date_field(*args) if bound?(*args) current_form_context.bound_date_field(*args) else current_form_context.unbound_date_field(*args) end end end module Merb::Helpers::Form::Builder class Base def bound_date_field(method, attrs = {}) name = control_name(method) update_bound_controls(method, attrs, "text") unbound_date_field({:name => name, :value => @obj.send(method)}.merge(attrs)) end def unbound_date_field(attrs) update_unbound_controls(attrs, "date") if attrs[:name] =~ /\[(.*)\]/ date = @obj.send($1) end date = (attrs[:default] ? attrs[:default] : nil) if date.nil? month_attrs = attrs.merge( :class => "date month", :selected => date.nil? ? '' : date.month.to_s, :name => attrs[:name] + '[month]', :id => attrs[:id] + '_month', :collection => attrs[:use_month_numbers] ? (1..12).map{|i|i.to_s} : (1..12).map{|i|[i, DateTime::MONTHNAMES[i]]}, :label => nil ) month_attrs[:collection] = [''] + month_attrs[:collection] if attrs[:include_blank] day_attrs = attrs.merge( :class => "date day", :selected => date.nil? ? '' : date.day.to_s, :name => attrs[:name] + '[day]', :id => attrs[:id] + '_day', :collection => (1..31).map{|i|i.to_s}, :label => nil ) day_attrs[:collection] = [''] + day_attrs[:collection] if attrs[:include_blank] year_attrs = attrs.merge( :class => "date year", :selected => date.nil? ? '' : date.year.to_s, :name => attrs[:name] + '[year]', :id => attrs[:id] + '_year', :collection => ((attrs[:start_year] ? attrs[:start_year].to_i : 1900)..(attrs[:end_year] ? attrs[:end_year].to_i : Time.now.year)).map{|i|i.to_s}.reverse, :label => nil ) year_attrs[:collection] = [''] + year_attrs[:collection] if attrs[:include_blank] attrs[:order] ||= [] [:year, :month, :day].each {|o| attrs[:order].push(o) unless attrs[:order].include?(o) } select_date = '' attrs[:order].each_with_index do |o, index| case o when :year select_date << unbound_select(filter_date_select_attributes(year_attrs, index == 0, attrs)) when :month select_date << unbound_select(filter_date_select_attributes(month_attrs, index == 0, attrs)) when :day select_date << unbound_select(filter_date_select_attributes(day_attrs, index == 0, attrs)) end end select_date end def filter_date_select_attributes(attrs, first = false, global_attrs = {}) attrs.delete(:use_month_numbers) attrs.delete(:default) attrs.delete(:order) attrs.delete(:start_year) attrs.delete(:end_year) attrs.delete(:include_blank) attrs[:label] = global_attrs[:label] if first attrs end end end