Module: Queries::Concerns::DateRanges

Extended by:
ActiveSupport::Concern
Defined in:
lib/queries/concerns/date_ranges.rb

Overview

See spec/models/collecting_event/dates_spec.rb for the current test coverage

TODO: isolate code to a gem

Instance Method Summary collapse

Instance Method Details

#between_date_rangeScope?

Returns:

  • (Scope, nil)


191
192
193
194
195
196
197
198
199
200
201
# File 'lib/queries/concerns/date_ranges.rb', line 191

def between_date_range 
  return nil unless use_date_range?
  q = st_string

  if partial_overlap_dates
    q = q.or(en_string).or(on_or_before_start_date.and(on_or_after_end_date) )
  else
    q = q.and(en_string) 
  end
  q
end

#date_range_in_same_yearObject



107
108
109
110
# File 'lib/queries/concerns/date_ranges.rb', line 107

def date_range_in_same_year 
  # - true == blank later on
  (start_year == end_year) or (end_year - start_year < 2) # test for whole years between date extent
end

#earlier_start_monthObject



64
65
66
# File 'lib/queries/concerns/date_ranges.rb', line 64

def earlier_start_month 
  table[:start_date_month].lt(start_month)
end

#earlier_start_yearObject



68
69
70
# File 'lib/queries/concerns/date_ranges.rb', line 68

def earlier_start_year
  table[:start_date_year].lt(start_year)
end

#empty_end_yearObject



126
127
128
# File 'lib/queries/concerns/date_ranges.rb', line 126

def empty_end_year
  table[:end_date_year].eq(nil)
end

#en_stringObject

Reflects origin variable, rename to clarify



177
178
179
180
181
182
183
184
185
186
187
188
# File 'lib/queries/concerns/date_ranges.rb', line 177

def en_string
  q = part_1e

  if start_year == end_year
    q = q.and(part_3e)
  else
    q = q.or(part_3e)
  end

  q = q.or(end_year_between) if !date_range_in_same_year
  q
end

#end_month_betweenObject



92
93
94
# File 'lib/queries/concerns/date_ranges.rb', line 92

def end_month_between 
  table[:start_date_month].between((1)..(end_month - 1))
end

#end_year_betweenObject

part_2e



113
114
115
# File 'lib/queries/concerns/date_ranges.rb', line 113

def end_year_between
  table[:end_date_year].between((start_year+1)..(end_year-1))
end

#equal_end_monthObject



76
77
78
# File 'lib/queries/concerns/date_ranges.rb', line 76

def equal_end_month
  table[:end_date_month].eq(end_month)
end

#equal_end_yearObject



72
73
74
# File 'lib/queries/concerns/date_ranges.rb', line 72

def equal_end_year
  table[:end_date_year].eq(end_year)
end

#equal_or_earlier_end_dayObject



130
131
132
# File 'lib/queries/concerns/date_ranges.rb', line 130

def equal_or_earlier_end_day
  table[:end_date_day].lteq(end_day)
end

#equal_or_earlier_start_dayObject



60
61
62
# File 'lib/queries/concerns/date_ranges.rb', line 60

def equal_or_earlier_start_day
  table[:start_date_day].lteq(start_day)
end

#equal_or_later_end_dayObject



80
81
82
# File 'lib/queries/concerns/date_ranges.rb', line 80

def equal_or_later_end_day
  table[:end_date_day].gteq(end_day)
end

#equal_or_later_start_dayObject



122
123
124
# File 'lib/queries/concerns/date_ranges.rb', line 122

def equal_or_later_start_day
  table[:start_date_day].gteq(start_day)
end

#equal_start_monthObject



56
57
58
# File 'lib/queries/concerns/date_ranges.rb', line 56

def equal_start_month
  table[:start_date_month].eq(start_month)
end

#equal_start_yearObject



52
53
54
# File 'lib/queries/concerns/date_ranges.rb', line 52

def equal_start_year
  table[:start_date_year].eq(start_year)
end

#later_end_monthObject



84
85
86
# File 'lib/queries/concerns/date_ranges.rb', line 84

def later_end_month
  table[:end_date_month].gt(end_month)
end

#later_end_yearObject



88
89
90
# File 'lib/queries/concerns/date_ranges.rb', line 88

def later_end_year
  table[:end_date_year].gt(end_year)
end

#later_start_monthObject



134
135
136
# File 'lib/queries/concerns/date_ranges.rb', line 134

def later_start_month
  table[:start_date_month].between((start_month + 1)..12)
end

#on_or_after_end_dateObject



101
102
103
104
105
# File 'lib/queries/concerns/date_ranges.rb', line 101

def on_or_after_end_date
  later_end_year.or(
    equal_end_year.and(( equal_end_month.and(equal_or_later_end_day) ).or(later_end_month) )
  )
end

#on_or_before_start_dateObject



96
97
98
99
# File 'lib/queries/concerns/date_ranges.rb', line 96

def on_or_before_start_date
  equal_start_year.and(( equal_start_month.and(equal_or_earlier_start_day) ).or(earlier_start_month) )
    .or(earlier_start_year)
end

#part_1eObject

Reflects origin variable, rename to clarify



164
165
166
167
# File 'lib/queries/concerns/date_ranges.rb', line 164

def part_1e
  empty_end_year.and(st_string).
    or((equal_end_year.and(end_month_between.or(equal_end_month.and(equal_or_earlier_end_day)))))
end

#part_1sObject

Reflects origin variable, rename to clarify



139
140
141
# File 'lib/queries/concerns/date_ranges.rb', line 139

def part_1s
  equal_start_year.and( later_start_month.or(equal_start_month.and(equal_or_later_start_day)) )
end

#part_3eObject

Reflects origin variable, rename to clarify



170
171
172
173
174
# File 'lib/queries/concerns/date_ranges.rb', line 170

def part_3e
  table[:end_date_year].eq(start_year).and(
    table[:end_date_month].gt(start_month).or(table[:end_date_month].eq(start_month).and(table[:end_date_day].gteq(start_day)))
  )
end

#part_3sObject

Reflects origin variable, rename to clarify



144
145
146
147
# File 'lib/queries/concerns/date_ranges.rb', line 144

def part_3s
  table[:start_date_year].eq(end_year)
    .and( table[:start_date_month].lt(end_month).or( table[:start_date_month].eq(end_month).and(table[:start_date_day].lteq(end_day)) ))
end

#set_dates(params) ⇒ Object



36
37
38
39
40
41
42
# File 'lib/queries/concerns/date_ranges.rb', line 36

def set_dates(params)
  self.start_date = params[:start_date] unless params[:start_date].blank?
  self.end_date = params[:end_date] unless params[:end_date].blank?

  @partial_overlap_dates = params[:partial_overlap_dates]
  @partial_overlap_dates = true if @partial_overlap_dates.nil?
end

#st_stringObject

Reflects origin variable, rename to clarify



151
152
153
154
155
156
157
158
159
160
161
# File 'lib/queries/concerns/date_ranges.rb', line 151

def st_string
  a = nil 
  if start_year == end_year 
    a = start_year_present.and(part_1s.and(part_3s))
  else
    a = start_year_present.and(part_1s.or(part_3s))
  end

  a = a.or(start_year_between) if !date_range_in_same_year
  a
end

#start_year_betweenObject

part_2s



118
119
120
# File 'lib/queries/concerns/date_ranges.rb', line 118

def start_year_between
  table[:start_date_year].between((start_year+1)..(end_year-1))
end

#start_year_presentObject



48
49
50
# File 'lib/queries/concerns/date_ranges.rb', line 48

def start_year_present
  table[:start_date_year].not_eq(nil)
end

#use_date_range?Boolean

Returns:

  • (Boolean)


44
45
46
# File 'lib/queries/concerns/date_ranges.rb', line 44

def use_date_range?
  !start_date.blank? && !end_date.blank?
end