Sha256: 2a8d08e15002c58b56b1e2ffbf5e474831f6b023adf4f4bfd27ef87ac2ac53e0

Contents?: true

Size: 1.11 KB

Versions: 8

Compression:

Stored size: 1.11 KB

Contents

module CustomFields
  module Types
    module Date

      extend ActiveSupport::Concern

      included do
        register_type :date, ::Date
      end

      module InstanceMethods

        def apply_date_type(klass)

          klass.class_eval <<-EOF

            def #{self.safe_alias}
              self.#{self._name}
            end

            def #{self.safe_alias}=(value)
              if value.is_a?(::String) && !value.blank?
                date = ::Date._strptime(value, I18n.t('date.formats.default'))
                value = ::Date.new(date[:year], date[:mon], date[:mday])
              end

              self.#{self._name} = value
            end

            def formatted_#{self.safe_alias}
              self.#{self._name}.strftime(I18n.t('date.formats.default')) rescue nil
            end

            alias formatted_#{self.safe_alias}= #{self.safe_alias}=
          EOF

          def add_date_validation(klass)
            if self.required?
              klass.validates_presence_of self.safe_alias.to_sym, :"formatted_#{self.safe_alias}"
            end
          end

        end

      end

    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
custom_fields-2.0.0.rc1 lib/custom_fields/types_old/date.rb
custom_fields-1.1.0.rc1 lib/custom_fields/types/date.rb
custom_fields-1.0.0.beta.25 lib/custom_fields/types/date.rb
custom_fields-1.0.0.beta.24 lib/custom_fields/types/date.rb
custom_fields-1.0.0.beta.23 lib/custom_fields/types/date.rb
custom_fields-1.0.0.beta.22 lib/custom_fields/types/date.rb
custom_fields-1.0.0.beta.21 lib/custom_fields/types/date.rb
custom_fields-1.0.0.beta.20 lib/custom_fields/types/date.rb