Class: TypeScopes::Time

Inherits:
TypeScopes show all
Defined in:
lib/type_scopes/time.rb

Constant Summary

Constants inherited from TypeScopes

VERSION

Class Method Summary collapse

Methods inherited from TypeScopes

append_scope, inject, support?

Class Method Details

.inject_for_column(model, name) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/type_scopes/time.rb', line 6

def self.inject_for_column(model, name)
  short_name = shorten_column_name(name)
  column = model.arel_table[name]

  append_scope(model, :"#{short_name}_to", lambda { |date| where(column.lteq(date)) })
  append_scope(model, :"#{short_name}_from", lambda { |date| where(column.gteq(date)) })
  append_scope(model, :"#{short_name}_after", lambda { |date| where(column.gt(date)) })
  append_scope(model, :"#{short_name}_before", lambda { |date| where(column.lt(date)) })
  append_scope(model, :"#{short_name}_between", lambda { |from, to| where(name => from..to) })
  append_scope(model, :"#{short_name}_not_between", lambda { |from, to| where.not(name => from..to) })
  append_scope(model, :"#{short_name}_within", lambda { |from, to| where(column.gt(from)).where(column.lt(to)) })
  append_scope(model, :"#{short_name}_not_within", lambda { |from, to| where(column.lteq(from).or(column.gteq(to))) })
end

.shorten_column_name(name) ⇒ Object



20
21
22
# File 'lib/type_scopes/time.rb', line 20

def self.shorten_column_name(name)
  name.chomp("_at").chomp("_on")
end

.typesObject



2
3
4
# File 'lib/type_scopes/time.rb', line 2

def self.types
  ["timestamp", "datetime", "date"].freeze
end