require 'date' require 'ostruct' class OpenStruct def temp hash OpenStruct.new(@table.merge(hash)) end end module Merb module FormControls # # this is the main merb form control helper. It can build # either textfield, textarea, date selects, or time selects. # # <%= control_for @post, :title, :text, :id => 'foo', :size => 53 %> # <%= control_for @post, :intro, :textarea, :class => 'post_intro', # :rows => 10, :cols => 50 %> # <%= control_for @post, :created_at, :time %> # <%= control_for @post, :published_at, :date %> # # TODO : is this useful enough? Needs some love def control_for(obj, meth, type, opts={}) instance = obj obj = obj.class o = OpenStruct.new(:name => "#{obj.to_s.downcase}[#{meth}]", :value => (instance.send(meth) rescue nil), :title => (opts.has_key?(:title) ? opts.delete(:title) : nil), :html => opts) Control.send(type, o) end module Control # This is ripped wholesale from Ramaze with some modifications to work # with AR objects instead of Og. Thanks again to Michael Fellinger class << self def number(o) o.value ||= 0 text(o) end def text(o) o.value ||= "" tag = '' tag << "#{o.title}: " if o.title tag << %{} end def textarea(o) o.value ||= "" %{} end def date(o) o.value ||= Date.today selects = [] selects << date_day(o.temp(:value => o.value.day)) selects << date_month(o.temp(:value => o.value.month)) selects << date_year(o.temp(:value => o.value.year)) selects.join("\n") end def time(o) o.value ||= Time.now selects = [] selects << date_day(o.temp(:value => o.value.day)) selects << date_month(o.temp(:value => o.value.month)) selects << date_year(o.temp(:value => o.value.year)) selects << time_hour(o.temp(:value => o.value.hour)) selects << time_minute(o.temp(:value => o.value.min)) selects << time_second(o.temp(:value => o.value.sec)) selects.join("\n") end def time_second(o) select(o.name+'[second]', (0...60),o.value) end def time_minute(o) select(o.name+'[minute]', (0...60),o.value) end def time_hour(o) select(o.name+'[hour]', (0...24),o.value) end def date_day(o) select(o.name+'[day]', (1..31),o.value) end def date_month(o) select(o.name+'[month]', (1..12),o.value) end def date_year(o) select(o.name+'[year]', (1950..2050),o.value) end def select(name, range, default) out = %{\n" end end end # Control end # FormHelper end # Merb