Module: UserGroupMembershipMixins::ValidityRange

Extended by:
ActiveSupport::Concern
Included in:
UserGroupMembership
Defined in:
app/models/user_group_membership_mixins/validity_range.rb

Overview

In this project, user group memberships do not neccessarily last forever. They can begin at some time and end at some time. This is expressed by the ValidityRange of a membership.

Examples:

membership.valid_from  # =>  time
membership.valid_to    # =>  time
membership.invalidate

Scopes:

UserGroupMembership.with_invalid
UserGroupMembership.only_valid
UserGroupMembership.only_invalid
UserGroupMembership.at_time(time)

By default, the ‘only_valid` scope is applied, i.e. only memberships are found that are valid at present time. To override this scope, use either `with_invalid` or `unscoped`.

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Instance Method Details

#can_be_invalidated?Boolean

This method determines whether the membership can be invalidated. Direct memberships can be invalidated, whereas indirect memberships cannot. The validity of indirect memberships is derived from the validity of the direct ones.

Returns:

  • (Boolean)


94
95
96
# File 'app/models/user_group_membership_mixins/validity_range.rb', line 94

def can_be_invalidated?
  self.direct?
end

#currently_valid?Boolean

This method checks whether the present time lies within the validity range of the membership.

Returns:

  • (Boolean)


119
120
121
# File 'app/models/user_group_membership_mixins/validity_range.rb', line 119

def currently_valid?
  valid_at?(Time.zone.now)
end

#invalidate(time = Time.zone.now) ⇒ Object

This is just an alias for ‘make_invalid`.



86
87
88
# File 'app/models/user_group_membership_mixins/validity_range.rb', line 86

def invalidate(time = Time.zone.now)
  self.make_invalid(time)
end

#make_invalid(time = Time.zone.now) ⇒ Object

This method ends the membership, i.e. sets the end of the validity range to the given time.

The following examples are equivalent (despite the return value):

membership.make_invalid
membership.make_invalid at: Time.zone.now
membership.make_invalid Time.zone.now
membership.invalidate                                    #  => membership
membership.update_attribute :valid_to, Time.zone.now     #  => true


78
79
80
81
82
# File 'app/models/user_group_membership_mixins/validity_range.rb', line 78

def make_invalid(time = Time.zone.now)
  time = time[:at] if time.kind_of?(Hash) && time[:at]
  self.update_attribute(:valid_to, time)
  return self
end

#set_valid_from_to_now(force = false) ⇒ Object



46
47
48
49
# File 'app/models/user_group_membership_mixins/validity_range.rb', line 46

def set_valid_from_to_now(force = false)
  self.valid_from ||= Time.zone.now if self.new_record? or force
  return self
end

#valid_at?(time) ⇒ Boolean

This method checks whether the membership is valid at the given time.

This is not to be confused with ActiveRecord’s ‘valid` method, which checks whether the record matches the requirements to store it in the database.

The following examples are equivalent:

membership.currently_valid?
membership.valid_at? Time.zone.now

Returns:

  • (Boolean)


112
113
114
# File 'app/models/user_group_membership_mixins/validity_range.rb', line 112

def valid_at?(time)
  (self.valid_from == nil || self.valid_from <= time) && (self.valid_to == nil || self.valid_to >= time)
end

#valid_from_localized_dateObject

Attributes in the database



38
39
40
# File 'app/models/user_group_membership_mixins/validity_range.rb', line 38

def valid_from_localized_date
  self.valid_from ? I18n.localize(self.valid_from.try(:to_date)) : ""
end

#valid_from_localized_date=(new_date) ⇒ Object



41
42
43
44
# File 'app/models/user_group_membership_mixins/validity_range.rb', line 41

def valid_from_localized_date=(new_date)
  self.valid_from = new_date.to_datetime
  valid_from_will_change!
end

#valid_to_localized_dateObject



51
52
53
# File 'app/models/user_group_membership_mixins/validity_range.rb', line 51

def valid_to_localized_date
  self.valid_to ? I18n.localize(self.valid_to.try(:to_date)) : ""
end

#valid_to_localized_date=(new_date) ⇒ Object



54
55
56
57
58
59
60
61
# File 'app/models/user_group_membership_mixins/validity_range.rb', line 54

def valid_to_localized_date=(new_date)
  if new_date == "-"
    self.valid_to = nil
  else
    self.valid_to = new_date.to_datetime
  end
  valid_to_will_change!
end