Sha256: e82cb44a90a3dad02867cd9c0c39de7cd25f5e609fa1688ae434d95817fe5971
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.first) 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.1 | lib/date_supercharger.rb |