Module: UserGroupMembershipMixins::ValidityRange::ClassMethods

Defined in:
app/models/user_group_membership_mixins/validity_range.rb

Overview

Temporal scopes

Instance Method Summary collapse

Instance Method Details

#at_time(time) ⇒ Object

This scope returns limits the query to memberships whose validity ranges match the given time.

Example:

UserGroupMembership.find_all_by_user( u ).at_time( 1.hour.ago ).count


136
137
138
139
140
# File 'app/models/user_group_membership_mixins/validity_range.rb', line 136

def at_time( time )
  with_invalid
    .where("valid_from IS NULL OR valid_from <= ?", time)
    .where("valid_to IS NULL OR valid_to >= ?", time)
end

#in_the_pastObject

This scope limits the query to memberships that are invalid at the present time.



179
180
181
# File 'app/models/user_group_membership_mixins/validity_range.rb', line 179

def in_the_past
  only_invalid
end

#nowObject

This scope limits the query to memberships that are valid at the present time. This is the default bahaviour.



173
174
175
# File 'app/models/user_group_membership_mixins/validity_range.rb', line 173

def now
  only_valid
end

#now_and_in_the_pastObject

This scope widens the query such that also memberships that are not valid at the present time are returned.



186
187
188
# File 'app/models/user_group_membership_mixins/validity_range.rb', line 186

def now_and_in_the_past
  with_invalid
end

#only_invalidObject

This scope limits the query to memberships that are invalid at the present time.



166
167
168
# File 'app/models/user_group_membership_mixins/validity_range.rb', line 166

def only_invalid
  with_invalid.where("valid_to < ?", Time.zone.now)
end

#only_validObject

This scope limits the query to memberships that are valid at the present time. This is the default bahaviour.



145
146
147
148
# File 'app/models/user_group_membership_mixins/validity_range.rb', line 145

def only_valid
  where("valid_from IS NULL OR valid_from <= ?", Time.zone.now)
  .where("valid_to IS NULL OR valid_to >= ?", Time.zone.now)
end

#started_after(time) ⇒ Object



194
195
196
# File 'app/models/user_group_membership_mixins/validity_range.rb', line 194

def started_after(time)
  where('NOT valid_from IS NULL').where("valid_from >= ?", time)
end

#this_yearObject



190
191
192
# File 'app/models/user_group_membership_mixins/validity_range.rb', line 190

def this_year
  with_invalid.where("valid_from >= ?", "#{Time.zone.now.year}-01-01 00:00:00")
end

#with_invalidObject

This scope widens the query such that also memberships that are not valid at the present time are returned.



153
154
155
156
157
158
159
160
161
162
# File 'app/models/user_group_membership_mixins/validity_range.rb', line 153

def with_invalid

  # TODO: Replace the brute-force `unscoped` by a more specific term like
  # `except(:valid_from).except(:valid_to)`. 
  # But so far, this has caused several tests to fail, which would have to be 
  # fixed.
  #
  unscoped

end