Sha256: a4a35188b57a6aa50ffa48bf848f5012fc7d596cccc3cce5f78408bc4cc30439

Contents?: true

Size: 1.79 KB

Versions: 2

Compression:

Stored size: 1.79 KB

Contents

require 'active_record/connection_adapters/abstract/quoting'

module ActiveRecord
  module ConnectionAdapters
    module Quoting
      def quote_with_time_of_day(value, column = nil)
        if column && column.type == :time && value.is_a?(Integer)
          value = TimeOfDay.new(value / 3600, (value % 3600) / 60, value % 60)
        end
        return "'#{value.to_s(:db)}'" if value.is_a?(TimeOfDay)
        quote_without_time_of_day(value, column)
      end

      alias_method_chain :quote, :time_of_day

      def type_cast_with_time_of_day(value, column)
        return value.to_s if value.is_a?(TimeOfDay)
        type_cast_without_time_of_day(value, column)
      end

      alias_method_chain :type_cast, :time_of_day
    end
  end
end

module Activerecord
  module Time
    module DummyTime
      def klass
        return TimeOfDay if type == :time
        super
      end

      module ClassMethods
        def string_to_dummy_time(value)
          return value if value.is_a? TimeOfDay
          return value.time_of_day if value.is_a? ::Time
          return nil if value.empty?
          TimeOfDay.parse(value)
        end
      end

      def self.prepended(base)
        class << base
          prepend ClassMethods
        end
      end
    end
  end
end

ActiveRecord::ConnectionAdapters::Column.prepend Activerecord::Time::DummyTime

module Arel
  module Visitors
    class Visitor
      # rubocop: disable Naming/MethodName
      # TODO(uwe): Simplify when we stop support for AR < 5
      if Gem::Version.new(Arel::VERSION) >= Gem::Version.new('5.0.0')
        def visit_TimeOfDay(object, _collector)
          "'#{object.to_s(:db)}'"
        end
      else
        def visit_TimeOfDay(object)
          "'#{object.to_s(:db)}'"
        end
      end
      # rubocop: enable Naming/MethodName
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
activerecord-time-0.10.1 lib/activerecord-time/extension_until_4_1.rb
activerecord-time-0.10.0 lib/activerecord-time/extension_until_4_1.rb