Module: TimestampScopes::ClassMethods

Defined in:
lib/timestamp_scopes.rb

Instance Method Summary collapse

Instance Method Details

#create_timestamp_scopesObject



8
9
10
11
12
13
14
# File 'lib/timestamp_scopes.rb', line 8

def create_timestamp_scopes
  for column in columns
    if column.sql_type.index("timestamp") == 0
      create_timestamp_scopes_for_column(column.name)
    end
  end
end

#create_timestamp_scopes_for_column(name) ⇒ Object



16
17
18
19
20
21
22
23
# File 'lib/timestamp_scopes.rb', line 16

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

#shorten_column_name(name) ⇒ Object



25
26
27
# File 'lib/timestamp_scopes.rb', line 25

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