Module: TimestampScopes::ClassMethods

Defined in:
lib/timestamp_scopes.rb

Instance Method Summary collapse

Instance Method Details

#create_timestamp_scopesObject



10
11
12
13
14
15
16
# File 'lib/timestamp_scopes.rb', line 10

def create_timestamp_scopes
  for column in columns
    if TYPES.any? { |type| column.sql_type.include?(type) }
      create_timestamp_scopes_for_column(column.name)
    end
  end
end

#create_timestamp_scopes_for_column(name) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/timestamp_scopes.rb', line 18

def create_timestamp_scopes_for_column(name)
  full_name = "#{quoted_table_name}.#{name}"
  short_name = shorten_column_name(name)
  scope :"#{short_name}_to", lambda { |date| where("#{full_name} <= ?", date) }
  scope :"#{short_name}_from", lambda { |date| where("#{full_name} >= ?", date) }
  scope :"#{short_name}_after", lambda { |date| where("#{full_name} > ?", date) }
  scope :"#{short_name}_before", lambda { |date| where("#{full_name} < ?", date) }
  scope :"#{short_name}_between", lambda { |from, to| where("#{full_name} BETWEEN ? AND ?", from, to) }
  scope :"#{short_name}_not_between", lambda { |from, to| where("#{full_name} NOT BETWEEN ? AND ?", from, to) }
  scope :"#{short_name}_within", lambda { |from, to| where("#{full_name} > ? AND #{full_name} < ?", from, to) }
  scope :"#{short_name}_not_within", lambda { |from, to| where("#{full_name} <= ? OR #{full_name} >= ?", from, to) }
end

#shorten_column_name(name) ⇒ Object



31
32
33
# File 'lib/timestamp_scopes.rb', line 31

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