Sha256: 0bb287926e93cb45cdc4d03dff9a500d8a4d4f1e7056a4fec57e8146d19b4a34

Contents?: true

Size: 1.65 KB

Versions: 1

Compression:

Stored size: 1.65 KB

Contents

require "active_support"
require "active_model"
require "chronic"

module BetterDatePicker
  module Model
    extend ActiveSupport::Concern

    included do
      extend ClassMethods
      class_attribute :better_date_fields
      class_attribute :better_date_defaults
    end

    module ClassMethods
      def better_date_picker(field, options = {})
        self.better_date_fields ||= []
        self.better_date_defaults ||= {}

        define_method "#{field}=" do |date_val|
          #we don't want to clobber the ivar if we're setting via the string
          val_to_set = date_val.nil? ? nil : date_val.strftime(self.class.better_date_format)
          instance_variable_set("@#{field}_date", val_to_set)

          #if the setter is not defined as a method, just set the ivar
          begin
            super(date_val)
          rescue NoMethodError => e
            instance_variable_set("@#{field}", date_val)
          end

        end

        define_method "#{field}_date" do
          string_val = instance_variable_get("@#{field}_date")
          if string_val.nil?
            if the_date = self.send(field)
              the_date.strftime(self.class.better_date_format)
            end
          else
            string_val
          end
        end

        define_method "#{field}_date=" do |stringified_date|
          instance_variable_set("@#{field}_date", stringified_date)
          self.send("#{field}=", Chronic.parse(stringified_date))
          stringified_date
        end

        self.better_date_fields << field
        self.better_date_defaults[field] = options[:default]
      end

      def better_date_format
       "%m/%d/%Y"
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
better_date_picker-0.0.3 lib/better_date_picker/model.rb