Module: Authpds::ActsAsAuthentic::InstanceMethods

Defined in:
lib/authpds/acts_as_authentic.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(klass) ⇒ Object



10
11
12
13
14
15
# File 'lib/authpds/acts_as_authentic.rb', line 10

def self.included(klass)
  klass.class_eval do
    serialize :user_attributes
    attr_accessor :expiration_date
  end
end

Instance Method Details

#expired?Boolean

Returns a boolean based on whether the User has been refreshed recently.

If User#refreshed_at is older than User#expiration_date, the User is expired and the data may need to be refreshed.

Returns:

  • (Boolean)


60
61
62
63
# File 'lib/authpds/acts_as_authentic.rb', line 60

def expired?
  # If the record is older than the expiration date, it is expired.
  (refreshed_at.nil?) ? true : refreshed_at < expiration_date 
end

#institutionsObject



34
35
36
37
38
# File 'lib/authpds/acts_as_authentic.rb', line 34

def institutions
  return nil unless InstitutionList.institutions_defined?
  user_attributes[:institutions].collect { |institution|
    InstitutionList.instance.get(institution) } unless user_attributes.nil?
end

#institutions=(institutions) ⇒ Object

Raises:

  • (ArgumentError)


40
41
42
43
44
45
46
47
48
# File 'lib/authpds/acts_as_authentic.rb', line 40

def institutions=(institutions)
  raise ArgumentError.new(
    "Institutions input should be an array.") unless institutions.is_a?(Array)
  filtered_institutions = institutions.collect { |institution|
    institution = institution.name if institution.is_a?(Institution)
    institution unless InstitutionList.instance.get(institution).nil?
  }
  self.user_attributes=({:institutions => filtered_institutions})
end

#primary_institutionObject



24
25
26
27
# File 'lib/authpds/acts_as_authentic.rb', line 24

def primary_institution
  return nil unless InstitutionList.institutions_defined?
  InstitutionList.instance.get(user_attributes[:primary_institution]) unless user_attributes.nil?
end

#primary_institution=(primary_institution) ⇒ Object



29
30
31
32
# File 'lib/authpds/acts_as_authentic.rb', line 29

def primary_institution=(primary_institution)
  primary_institution = primary_institution.name if primary_institution.is_a?(Institution)
  self.user_attributes=({:primary_institution => primary_institution})
end

#user_attributes=(new_attributes) ⇒ Object

“Smart” updating of user_attributes. Maintains user_attributes that are not explicity overwritten.



51
52
53
54
55
# File 'lib/authpds/acts_as_authentic.rb', line 51

def user_attributes=(new_attributes)
  write_attribute(:user_attributes, new_attributes) and return unless new_attributes.kind_of?(Hash)
  # Set new/updated attributes
  write_attribute(:user_attributes, (user_attributes || {}).merge(new_attributes))
end

#username=(value) ⇒ Object

Setting the username field also resets the persistence_token if the value changes.



19
20
21
22
# File 'lib/authpds/acts_as_authentic.rb', line 19

def username=(value)
  write_attribute(:username, value)
  reset_persistence_token if username_changed?
end