okiess (owner)

Fork Of

gist: 7324 by jamie date_field helper for merb

Revisions

gist: 11756 Download_button fork
public
Public Clone URL: git://gist.github.com/11756.git
Embed All Files: show embed
date_field.rb #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
  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