Module: Queries::Concerns::DateRanges

Extended by:
ActiveSupport::Concern
Included in:
Queries::CollectingEvent::Autocomplete, Queries::CollectingEvent::Filter, Extract::Filter
Defined in:
lib/queries/concerns/date_ranges.rb

Overview

For specs see

spec/models/collecting_event/dates_spec.rb
spec/lib/queries/collecting_event/autocomplete_spec.rb

TODO: isolate code to a gem

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.paramsObject



11
12
13
14
15
16
17
# File 'lib/queries/concerns/date_ranges.rb', line 11

def self.params
  [
    :start_date,
    :end_date,
    :partial_overlap_dates
  ]
end

Instance Method Details

#autocomplete_start_dateObject



257
258
259
260
261
262
263
# File 'lib/queries/concerns/date_ranges.rb', line 257

def autocomplete_start_date
  if a = with_start_date
    base_query.where(a.to_sql).limit(20)
  else
    nil
  end
end

#autocomplete_start_or_end_dateObject



265
266
267
268
269
270
271
# File 'lib/queries/concerns/date_ranges.rb', line 265

def autocomplete_start_or_end_date
  if a = with_parsed_date
    base_query.where(a.or(with_parsed_date(:end)).to_sql).limit(20)
  else
    nil
  end
end

#between_date_range_facetScope?

Returns:

  • (Scope, nil)


201
202
203
204
205
206
207
208
209
210
211
# File 'lib/queries/concerns/date_ranges.rb', line 201

def between_date_range_facet
  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



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

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



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

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

#earlier_start_yearObject



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

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

#empty_end_yearObject



137
138
139
# File 'lib/queries/concerns/date_ranges.rb', line 137

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

#en_stringObject

Reflects origin of variable, rename to clarify



187
188
189
190
191
192
193
194
195
196
197
198
# File 'lib/queries/concerns/date_ranges.rb', line 187

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



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

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

#end_year_betweenObject

part_2e



124
125
126
# File 'lib/queries/concerns/date_ranges.rb', line 124

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

#equal_end_monthObject



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

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

#equal_end_yearObject



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

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

#equal_or_earlier_end_dayObject



141
142
143
# File 'lib/queries/concerns/date_ranges.rb', line 141

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

#equal_or_earlier_start_dayObject



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

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

#equal_or_later_end_dayObject



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

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

#equal_or_later_start_dayObject



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

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

#equal_start_monthObject



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

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

#equal_start_yearObject



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

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

#later_end_monthObject



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

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

#later_end_yearObject



99
100
101
# File 'lib/queries/concerns/date_ranges.rb', line 99

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

#later_start_monthObject



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

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

#on_or_after_end_dateObject



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

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



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

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 of variable, rename to clarify



174
175
176
177
# File 'lib/queries/concerns/date_ranges.rb', line 174

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



150
151
152
# File 'lib/queries/concerns/date_ranges.rb', line 150

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 of variable, rename to clarify



180
181
182
183
184
# File 'lib/queries/concerns/date_ranges.rb', line 180

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



155
156
157
158
# File 'lib/queries/concerns/date_ranges.rb', line 155

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

#simple_dateDate.new?

— Methods below are not part of the between date_range code

Returns:

  • (Date.new, nil)


215
216
217
218
219
220
221
# File 'lib/queries/concerns/date_ranges.rb', line 215

def simple_date
  begin
    Date.parse(query_string)
  rescue ArgumentError
    return nil
  end
end

#st_stringObject

Reflects origin variable, rename to clarify



161
162
163
164
165
166
167
168
169
170
171
# File 'lib/queries/concerns/date_ranges.rb', line 161

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



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

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

#start_year_presentObject



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

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

#use_date_range?Boolean

Returns:

  • (Boolean)


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

def use_date_range?
  start_date.present? && end_date.present?
end

#with_parsed_date(t = :start) ⇒ Object



240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
# File 'lib/queries/concerns/date_ranges.rb', line 240

def with_parsed_date(t = :start)
  if d = simple_date
    r = []
    r.push(table["#{t}_date_day".to_sym].eq(d.day)) if d.day
    r.push(table["#{t}_date_month".to_sym].eq(d.month)) if d.month
    r.push(table["#{t}_date_year".to_sym].eq(d.year)) if d.year

    q = r.pop
    r.each do |z|
      q = q.and(z)
    end
    q
  else
    nil
  end
end

#with_start_dateObject



223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
# File 'lib/queries/concerns/date_ranges.rb', line 223

def with_start_date
  if d = simple_date
    r = []
    r.push(table[:start_date_day].eq(d.day)) if d.day
    r.push(table[:start_date_month].eq(d.month)) if d.month
    r.push(table[:start_date_year].eq(d.year)) if d.year

    q = r.pop
    r.each do |z|
      q = q.and(z)
    end
    q
  else
    nil
  end
end