Sha256: 173c53648e76e28a13e16addbd58a075b3e84a9d8c96b7acbf6282e5d42211f9

Contents?: true

Size: 1.88 KB

Versions: 2

Compression:

Stored size: 1.88 KB

Contents

module Authpds
  module Controllers
    module AuthpdsController
      module InstitutionAttributes
        require 'institutions'

        # Set helper methods when this module is included.
        def self.included(klass)
          klass.class_eval do
            helper_method :current_primary_institution
          end
        end

        # Determine current primary institution based on:
        #   0. institutions are not being used (returns nil)
        #   1. institution query string parameter in URL
        #   2. institution associated with the client IP
        #   3. primary institution for the current user
        #   4. first default institution
        def current_primary_institution
          @current_primary_institution ||=
            (institution_param.nil? or all_institutions[institution_param].nil?) ?
              (primary_institution_from_ip.nil?) ?
                (@current_user.nil? or current_user.primary_institution.nil?) ?
                  Institutions.defaults.first :
                    current_user.primary_institution :
                      primary_institution_from_ip :
                        all_institutions[institution_param]
        end

        # Grab the first institution that matches the client IP
        def primary_institution_from_ip
          Institutions.with_ip(request.remote_ip).first unless request.nil?
        end
        private :primary_institution_from_ip

        def institution_param_key
          @institution_param_key ||= UserSession.institution_param_key
        end
        private :institution_param_key

        def institution_param
          params["#{institution_param_key}"].to_sym unless params["#{institution_param_key}"].nil?
        end
        private :institution_param

        def all_institutions
          @all_institutions ||= Institutions.institutions
        end
        private :all_institutions
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
authpds-0.2.2 lib/authpds/controllers/authpds_controller/institution_attributes.rb
authpds-0.2.1 lib/authpds/controllers/authpds_controller/institution_attributes.rb