Module: Queries::Concerns::DateRanges

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

- (Scope?) between_date_range

Returns:

  • (Scope, nil)


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

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

- (Object) date_range_in_same_year



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

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

- (Object) earlier_start_month



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

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

- (Object) earlier_start_year



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

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

- (Object) empty_end_year



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

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

- (Object) en_string

Reflects origin variable, rename to clarify



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

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

- (Object) end_month_between



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

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

- (Object) end_year_between

part_2e



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

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

- (Object) equal_end_month



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

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

- (Object) equal_end_year



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

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

- (Object) equal_or_earlier_end_day



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

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

- (Object) equal_or_earlier_start_day



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

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

- (Object) equal_or_later_end_day



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

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

- (Object) equal_or_later_start_day



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

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

- (Object) equal_start_month



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

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

- (Object) equal_start_year



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

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

- (Object) later_end_month



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

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

- (Object) later_end_year



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

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

- (Object) later_start_month



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

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

- (Object) on_or_after_end_date



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

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

- (Object) on_or_before_start_date



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

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

- (Object) part_1e

Reflects origin variable, rename to clarify



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

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

- (Object) part_1s

Reflects origin variable, rename to clarify



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

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

- (Object) part_3e

Reflects origin variable, rename to clarify



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

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

- (Object) part_3s

Reflects origin variable, rename to clarify



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

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

- (Object) set_dates(params)



36
37
38
39
40
41
42
43
# 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

- (Object) st_string

Reflects origin variable, rename to clarify



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

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

- (Object) start_year_between

part_2s



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

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

- (Object) start_year_present



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

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

- (Boolean) use_date_range?

Returns:

  • (Boolean)


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

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