Sha256: 845778d1c8defde4a3f0b009349744853e5e3dee57499b41272d3b8c85241de8

Contents?: true

Size: 1.64 KB

Versions: 5

Compression:

Stored size: 1.64 KB

Contents

module Timely
  # Uses Date.current to be more accurate for Rails applications
  def self.current_date
    ::Date.respond_to?(:current) ? ::Date.current : ::Date.today
  end


  module ActionViewHelpers
    module FormTagHelper
      def calendar_tag(name, value = Timely.current_date, *args)
        options = args.extract_options!

        value = value.to_s(:calendar) if value.respond_to?(:day)

        name = name.to_s if name.is_a?(Symbol)

        options[:id] = options[:id] || name.gsub(/\]$/, '').gsub(/\]\[/, '[').gsub(/[\[\]]/, '_')

        options[:class] = options[:class].split(' ') if options[:class].is_a?(String)
        options[:class] ||= []
        options[:class] << 'datepicker'
        options[:class] = options[:class].join(' ') # Rails 2 requires string values

        options[:size] ||= 10
        options[:maxlength] ||= 10

        tag(:input, options.merge(:name => name, :type => 'text', :value => value)).html_safe
      end
    end


    module DateHelper
      def calendar(object_name, method, options = {})
        value = options[:object] || Timely.current_date
        calendar_tag("#{object_name}[#{method}]", value, options)
      end
    end

    module FormBuilder
      def calendar(method, options = {})
        options[:object] = @object.send(method) unless options.key?(:object)
        @template.calendar(@object_name, method, options)
      end
    end
  end
end

if defined?(ActionView)
  ActionView::Base.send :include, Timely::ActionViewHelpers::FormTagHelper
  ActionView::Base.send :include, Timely::ActionViewHelpers::DateHelper
  ActionView::Helpers::FormBuilder.send :include, Timely::ActionViewHelpers::FormBuilder
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
timely-0.5.0 lib/timely/rails/calendar_tag.rb
timely-0.4.2 lib/timely/rails/calendar_tag.rb
timely-0.4.1 lib/timely/rails/calendar_tag.rb
timely-0.4.0 lib/timely/rails/calendar_tag.rb
timely-0.3.4 lib/timely/rails/calendar_tag.rb