|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +# Provides methods to handle the org_id hash returned to the controller |
| 4 | +# for pages that use the Org selection autocomplete widget |
| 5 | +# |
| 6 | +# This Concern handles the incoming params from a page that has one of the |
| 7 | +# Org Typeahead boxes found in app/views/shared/org_selectors/. |
| 8 | +# |
| 9 | +# The incoming hash looks like this: |
| 10 | +# { |
| 11 | +# "org_name"=>"Portland State University (PDX)", |
| 12 | +# "org_sources"=>"[ |
| 13 | +# \"3E (Belgium) (3e.eu)\", |
| 14 | +# \"etc.\" |
| 15 | +# ]", |
| 16 | +# "org_crosswalk"=>"[ |
| 17 | +# { |
| 18 | +# \"id\":1574, |
| 19 | +# \"name\":\"3E (Belgium) (3e.eu)\", |
| 20 | +# \"sort_name\":\"3E\", |
| 21 | +# \"ror\":\"https://ror.org/03d33vh19\" |
| 22 | +# }, |
| 23 | +# { |
| 24 | +# "etc." |
| 25 | +# }]", |
| 26 | +# "id"=>"{ |
| 27 | +# \"id\":62, |
| 28 | +# \"name\":\"Portland State University (PDX)\", |
| 29 | +# \"sort_name\":\"Portland State University\", |
| 30 | +# \"ror\":\"https://ror.org/00yn2fy02\", |
| 31 | +# \"fundref\":\"https://doi.org/10.13039/100007083\" |
| 32 | +# } |
| 33 | +# } |
| 34 | +# |
| 35 | +# The :org_name, :org_sources, :org_crosswalk are all relics of the JS involved in |
| 36 | +# handling the request/response from OrgsController#search AJAX action that is |
| 37 | +# used to search both the local DB and the ROR API as the user types. |
| 38 | +# :org_name = the value the user has types in |
| 39 | +# :org_sources = the pick list of Org names returned by the OrgsController#search action |
| 40 | +# :org_crosswalk = all of the info about each Org returned by the OrgsController#search action |
| 41 | +# there is JS that takes the value in :org_name and then sets the :id param |
| 42 | +# to the matching Org in the :org_crosswalk on form submission |
| 43 | +# |
| 44 | +# They are typically removed from the incoming params hash prior to doing a :save or :update |
| 45 | +# by the :remove_org_selection_params below. |
| 46 | +# TODO: Consider adding a JS method that strips those 3 params out prior to form submission |
| 47 | +# since we only need the contents of the :id param here |
| 48 | +# |
| 49 | +# The contents of :id are then used to either Create or Find the Org from the DB. |
| 50 | +# if id: { :id } is present then the Org was one pulled from the DB. If it is not |
| 51 | +# present then it is one of the following: |
| 52 | +# if :ror or :fundref are present then it was one retrieved from the ROR API |
| 53 | +# otherwise it is a free text value entered by the user |
| 54 | +# |
| 55 | +# See the comments on OrgsController#search for more info on how the typeaheads work |
| 56 | +module OrgCreationWithOrion |
| 57 | + extend ActiveSupport::Concern |
| 58 | + |
| 59 | + # rubocop:disable Metrics/BlockLength |
| 60 | + included do |
| 61 | + |
| 62 | + private |
| 63 | + |
| 64 | + # Converts the incoming params_into an Org by either locating it |
| 65 | + # via its id, identifier and/or name, or initializing a new one |
| 66 | + # the default allow_create is based off restrict_orgs |
| 67 | + def org_from_params(params_in:, |
| 68 | + allow_create: !Rails.configuration.x.application.restrict_orgs) |
| 69 | + # params_in = params_in.with_indifferent_access |
| 70 | + return nil unless params_in[:org_id].present? && |
| 71 | + params_in[:org_id].is_a?(String) |
| 72 | + |
| 73 | + hash = org_hash_from_params(params_in: params_in) |
| 74 | + return nil unless hash.present? |
| 75 | + |
| 76 | + #todo: need to create a function to create org hash |
| 77 | + org = {"name": "University of Stanford (uofa.edu)"} |
| 78 | + # org = OrgSelection::HashToOrgService.to_org(hash: hash, |
| 79 | + # allow_create: allow_create) |
| 80 | + allow_create ? create_org(org: org, params_in: params_in) : org |
| 81 | + end |
| 82 | + |
| 83 | + # Converts the incoming params_into an array of Identifiers |
| 84 | + def identifiers_from_params(params_in:) |
| 85 | + # params_in = params_in.to_h.with_indifferent_access |
| 86 | + return [] unless params_in[:org_id].present? && |
| 87 | + params_in[:org_id].is_a?(String) |
| 88 | + |
| 89 | + hash = org_hash_from_params(params_in: params_in) |
| 90 | + return [] unless hash.present? |
| 91 | + |
| 92 | + #todo: need to create a function to create org hash |
| 93 | + # OrgSelection::HashToOrgService.to_identifiers(hash: hash) |
| 94 | + end |
| 95 | + |
| 96 | + # Remove the extraneous Org Selector hidden fields so that they don't get |
| 97 | + # passed on to any save methods |
| 98 | + def remove_org_selection_params(params_in:) |
| 99 | + params_in.delete(:org_id) |
| 100 | + params_in.delete(:org_name) |
| 101 | + params_in.delete(:org_sources) |
| 102 | + params_in.delete(:org_crosswalk) |
| 103 | + params_in |
| 104 | + end |
| 105 | + |
| 106 | + # Just does a JSON parse of the org_id hash |
| 107 | + def org_hash_from_params(params_in:) |
| 108 | + JSON.parse(params_in[:org_id]) # .with_indifferent_access |
| 109 | + rescue JSON::ParserError => e |
| 110 | + Rails.logger.error "Unable to parse Org Selection JSON: #{e.message}" |
| 111 | + Rails.logger.error params_in.inspect |
| 112 | + {} |
| 113 | + end |
| 114 | + |
| 115 | + # Saves the org if its a new record |
| 116 | + def create_org(org:, params_in:) |
| 117 | + return org unless org.present? && org.new_record? |
| 118 | + |
| 119 | + # Save the Org before attaching identifiers |
| 120 | + org.save |
| 121 | + identifiers_from_params(params_in: params_in).each do |identifier| |
| 122 | + next unless identifier.value.present? |
| 123 | + |
| 124 | + identifier.identifiable = org |
| 125 | + identifier.save |
| 126 | + end |
| 127 | + org.reload |
| 128 | + end |
| 129 | + end |
| 130 | + # rubocop:enable Metrics/BlockLength |
| 131 | +end |
0 commit comments