Sha256: 71d57f16e3531fbb24f1c4692cd7bb31d1c3b5973c39b285af8c3fde024a86bf

Contents?: true

Size: 1.2 KB

Versions: 21

Compression:

Stored size: 1.2 KB

Contents

module DynamicFieldsets
  # Creates date tags on a form
  #
  # The date tag currently defaults to the current date if nothing is set with the year range starting 70 years in the past
  #
  # The value of the input is stored as a string so costly Date.parse calls are used to parse the string
  # This may have to be changed in the future
  class DateField < Field
    acts_as_field_with_single_answer

    # Returns the current value if set, then the default value if set, then the current date as a Date object
    #
    # @return [Date] The current time stored in the date field
    def get_date_or_today(value)
      default_date = value_or_default_for_form(value)
      if default_date.empty?
        return Date.today
      else
        # not sure why Date.parse doesn't work here
        return Time.parse(default_date)
      end
    end

    # Includes information used in the date_options argument (third) on the date select helper
    #
    # @return [Hash] Data for the date form partial
    def form_partial_locals(args)
      output = super
      output[:date_options] = { 
        :start_year => Time.now.year - 70,
        :default => get_date_or_today(args[:value])
      }
      return output
    end
  end
end

Version data entries

21 entries across 21 versions & 1 rubygems

Version Path
dynamic_fieldsets-0.1.20 app/models/dynamic_fieldsets/date_field.rb
dynamic_fieldsets-0.1.19 app/models/dynamic_fieldsets/date_field.rb
dynamic_fieldsets-0.1.18 app/models/dynamic_fieldsets/date_field.rb
dynamic_fieldsets-0.1.17 app/models/dynamic_fieldsets/date_field.rb
dynamic_fieldsets-0.1.16 app/models/dynamic_fieldsets/date_field.rb
dynamic_fieldsets-0.1.15 app/models/dynamic_fieldsets/date_field.rb
dynamic_fieldsets-0.1.14 app/models/dynamic_fieldsets/date_field.rb
dynamic_fieldsets-0.1.13 app/models/dynamic_fieldsets/date_field.rb
dynamic_fieldsets-0.1.12 app/models/dynamic_fieldsets/date_field.rb
dynamic_fieldsets-0.1.11 app/models/dynamic_fieldsets/date_field.rb
dynamic_fieldsets-0.1.10 app/models/dynamic_fieldsets/date_field.rb
dynamic_fieldsets-0.1.9 app/models/dynamic_fieldsets/date_field.rb
dynamic_fieldsets-0.1.8 app/models/dynamic_fieldsets/date_field.rb
dynamic_fieldsets-0.1.7 app/models/dynamic_fieldsets/date_field.rb
dynamic_fieldsets-0.1.6 app/models/dynamic_fieldsets/date_field.rb
dynamic_fieldsets-0.1.5 app/models/dynamic_fieldsets/date_field.rb
dynamic_fieldsets-0.1.4 app/models/dynamic_fieldsets/date_field.rb
dynamic_fieldsets-0.1.3 app/models/dynamic_fieldsets/date_field.rb
dynamic_fieldsets-0.1.2 app/models/dynamic_fieldsets/date_field.rb
dynamic_fieldsets-0.1.1 app/models/dynamic_fieldsets/date_field.rb