Module: Queries::Concerns::DateRanges

Extended by:
ActiveSupport::Concern
Included in:
Queries::CollectingEvent::Autocomplete, Queries::CollectingEvent::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

Instance Method Summary collapse

Instance Method Details

#autocomplete_start_dateObject



249
250
251
252
253
254
255
# File 'lib/queries/concerns/date_ranges.rb', line 249

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



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

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_rangeScope?

Returns:

  • (Scope, nil)


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

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



110
111
112
113
# File 'lib/queries/concerns/date_ranges.rb', line 110

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



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

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

#earlier_start_yearObject



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

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

#empty_end_yearObject



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

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

#en_stringObject

Reflects origin variable, rename to clarify



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

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



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

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

#end_year_betweenObject

part_2e



116
117
118
# File 'lib/queries/concerns/date_ranges.rb', line 116

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

#equal_end_monthObject



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

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

#equal_end_yearObject



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

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

#equal_or_earlier_end_dayObject



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

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

#equal_or_earlier_start_dayObject



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

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

#equal_or_later_end_dayObject



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

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

#equal_or_later_start_dayObject



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

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

#equal_start_monthObject



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

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

#equal_start_yearObject



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

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

#later_end_monthObject



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

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

#later_end_yearObject



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

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

#later_start_monthObject



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

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

#on_or_after_end_dateObject



104
105
106
107
108
# File 'lib/queries/concerns/date_ranges.rb', line 104

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



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

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



166
167
168
169
# File 'lib/queries/concerns/date_ranges.rb', line 166

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



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

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



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

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



147
148
149
150
# File 'lib/queries/concerns/date_ranges.rb', line 147

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



39
40
41
42
43
44
45
# File 'lib/queries/concerns/date_ranges.rb', line 39

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

#simple_dateDate.new?

— Methods below are not part of the between date_range code

Returns:

  • (Date.new, nil)


207
208
209
210
211
212
213
# File 'lib/queries/concerns/date_ranges.rb', line 207

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

#st_stringObject

Reflects origin variable, rename to clarify



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

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



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

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

#start_year_presentObject



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

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

#use_date_range?Boolean

Returns:

  • (Boolean)


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

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

#with_parsed_date(t = :start) ⇒ Object



232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
# File 'lib/queries/concerns/date_ranges.rb', line 232

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



215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
# File 'lib/queries/concerns/date_ranges.rb', line 215

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