Module: TemporalScopes::HasTemporalScopes

Defined in:
lib/temporal_scopes/has_temporal_scopes.rb

Defined Under Namespace

Modules: ClassMethods, InstanceMethods

Instance Method Summary collapse

Instance Method Details

#has_temporal_scopesObject



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/temporal_scopes/has_temporal_scopes.rb', line 6

def has_temporal_scopes
  
  scope :without_temporal_condition, -> { 
    relation = unscope(where: [:valid_from, :valid_to]) 
    relation.where_values.delete_if { |query| query.to_sql.include?("\"valid_from\"") || query.to_sql.include?("\"valid_to\"") }
    relation
  }
  
  scope :now, -> { 
    without_temporal_condition
    .where(arel_table[:valid_from].eq(nil).or(arel_table[:valid_from].lteq(Time.zone.now)))
    .where(arel_table[:valid_to].eq(nil).or(arel_table[:valid_to].gteq(Time.zone.now)))
  }
  scope :past, -> { without_temporal_condition.where('valid_to < ?', Time.zone.now) }
  scope :with_past, -> { without_temporal_condition }
  
  default_scope { now }
  
  extend ClassMethods
  include InstanceMethods
end