Sha256: 8358b924cf47bafadf1eb371239c3a27a7a1764bf77e04b1f5f330176da59cb4

Contents?: true

Size: 1.53 KB

Versions: 1

Compression:

Stored size: 1.53 KB

Contents

require "date_supercharger/version"
require "active_record"

module DateSupercharger
  extend ActiveSupport::Concern

  included do
    def self.method_missing(method_sym, *arguments, &block)
      return super unless descends_from_active_record?
      match = ModelDateExtensionMatch.new(self,method_sym)

      if match.match?
        operators = { after: ">", before: "<", before_or_at: "<=", after_or_at: ">=" }
        singleton_class.class_eval do
          define_method("#{match.attribute}_#{match.suffix}") do |date|
            where("#{match.attribute} #{operators[match.suffix]} ?", date)
          end
        end
        send(method_sym, *arguments)
      else
        super
      end
    end

    def self.respond_to?(method_sym, include_private = false)
      return super unless descends_from_active_record?
      if ModelDateExtensionMatch.new(self,method_sym).match?
        true
      else
        super
      end
    end
  end

  class ModelDateExtensionMatch

    attr_accessor :attribute,:suffix

    def initialize(model,method_sym)
      #TODO: implement between and between_inclusive
      if method_sym.to_s =~ /^(.+)_(before|after|before_or_at|after_or_at)$/
        date_columns = model.columns_hash.keys.select { |c| [:datetime,:date].include? model.columns_hash[c].type }.map(&:to_sym)
        if date_columns.include? $1.to_sym
          @attribute = $1.to_sym
          @suffix = $2.to_sym
        end
      end

    end

    def match?
      @attribute != nil
    end
  end

end
ActiveRecord::Base.send :include, DateSupercharger

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
date_supercharger-0.0.2 lib/date_supercharger.rb