Method: Invoicing::TimeDependent::ClassMethods#valid_records_during
- Defined in:
- lib/invoicing/time_dependent.rb
#valid_records_during(not_before, not_after) ⇒ Object
Returns a list of records which are valid at some point during a particular date/time range. If there is a change of rate during this time interval, and one rate replaces another, then only the earliest element of each replacement chain is returned (because we can unambiguously convert from an earlier rate to a later one, but not necessarily in reverse).
The date range must not be empty (i.e. not_after must be later than not_before, not the same time or earlier). If you need the records which are valid at one particular point in time, use valid_records_at.
A typical application for this method would be where you want to offer users the ability to choose from a selection of rates, including ones which are not yet valid but will become valid within the next month, for example.
231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 |
# File 'lib/invoicing/time_dependent.rb', line 231 def valid_records_during(not_before, not_after) info = time_dependent_class_info # List of all records whose validity period intersects the selected period valid_records = all.select do |record| valid_from = info.get(record, :valid_from) valid_until = info.get(record, :valid_until) has_taken_effect = (valid_from < not_after) # N.B. less than not_yet_expired = (valid_until == nil) || (valid_until > not_before) has_taken_effect && not_yet_expired end # Select only those which do not have a predecessor which is also valid valid_records.select do |record| record.predecessors.empty? || (valid_records & record.predecessors).empty? end end |