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)


63
64
65
66
# File 'lib/authpds/acts_as_authentic.rb', line 63

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

#institutionsObject



37
38
39
40
41
# File 'lib/authpds/acts_as_authentic.rb', line 37

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)


43
44
45
46
47
48
49
50
51
# File 'lib/authpds/acts_as_authentic.rb', line 43

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

Raises:

  • (ArgumentError)


29
30
31
32
33
34
35
# 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)
  raise ArgumentError.new(
    "Institution #{primary_institution} does not exist.\n" + 
    "Please maker sure the institutions yaml file is configured correctly.") if InstitutionList.instance.get(primary_institution).nil?
  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.



54
55
56
57
58
# File 'lib/authpds/acts_as_authentic.rb', line 54

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