ncbo/bioportal_web_ui

View on GitHub
app/views/users/show.html.haml

Summary

Maintainability
Test Coverage
- @title = "Account Information"

%div{class: 'container account-info my-4'}
  %h4{class: 'pb-2 mb-4 border-bottom'}
    Account basics
    = tag.small("(#{@user.username})", class: 'text-muted')

  %table.table
    %tr
      %th First name
      %td= @user.firstName
    %tr
      %th Last name
      %td= @user.lastName
    %tr
      %th Email
      %td= @user.email
    %tr
      %th GitHub username
      %td= @user.githubId
    %tr
      %th ORCID iD
      %td= @user.orcidId

  %div{class: 'd-flex justify-content-end'}
    = link_to('Edit account', edit_user_path(@user.username), class: 'btn btn-sm btn-outline-secondary')
    = link_to('Change password', edit_password_path, class: 'btn btn-sm btn-outline-secondary ms-2')

  -# User's API key
  %h4{class: 'pb-2 my-4 border-bottom'} API Key
  = tag.p("Your API key can be used to access the #{$SITE} RESTful API and services", class: 'text-muted')
  %div{'data-controller': 'clipboard'}
    %span{'data-clipboard-target': 'source'}= session[:user].apikey
    %div{class: 'd-flex align-items-center mt-2'}
      = tag.button 'Copy to clipboard', 'data-action': 'clipboard#copy',  class: 'btn btn-sm btn-outline-secondary'
      %span{'data-clipboard-target': 'copiedIndicator', class: 'hidden text-success fw-semibold ms-2'} Copied!

  -# User's subscriptions
  - unless @user.subscription.blank?
    %h4{class: 'pb-2 mt-5 mb-4 border-bottom'} Subscriptions
    %table.table
      %thead.table-light
        %tr
          %th Ontology
          %th Subscription type
          %th  
      - @user.subscription.each do |subscription|
        - ont = (!subscription[:ontology].nil? ? subscription[:ontology].split('/').last: nil) # ensure we get the acronym
        - type = (!subscription[:notification_type].nil? ? subscription[:notification_type].downcase : nil)
        %tr
          %td
            = link_to(ont, ontology_path(ont))
          %td
            - if type == 'notes'
              = link_to(type, ontology_path(ont, p: 'notes'))
            - else
              = type
          %td
            .subscribe_to_ontology
              = raw subscribe_ontology_button(ont, @user)

  -# Projects this user created
  - unless @user_projects.blank?
    %h4{class: 'pb-2 mt-5 mb-4 border-bottom'} Projects
    = tag.p("Projects you created in #{$SITE}", class: 'text-muted')
    %ul.list-unstyled
      - @user_projects.each do |p|
        %li
          = link_to(p.name, project_path(p.acronym))

  -# Ontologies this user administers
  - unless @admin_ontologies.blank?
    %h4{class: 'pb-2 mt-5 mb-4 border-bottom'} Ontologies
    = tag.p("Ontologies you have administrative access to in #{$SITE}", class: 'text-muted')
    %ul.list-unstyled
      - @admin_ontologies.each do |ont|
        %li
          = link_to(ont.name, ontology_path(ont.acronym))

  :javascript
    jQuery(document).ready(function(){
      jQuery("#edit_custom_ontologies").click(editCustomOntologies);
      // Wire up subscriptions button activity
      jQuery("a.subscribe_to_ontology").live("click", function(){
        subscribeToOntology(this);
      });
    });

    function subscribeToOntology(button) {
      var ontologyId = jQuery(button).attr("data-bp_ontology_id");
      var isSubbed = jQuery(button).attr("data-bp_is_subbed");
      var userId = jQuery(button).attr("data-bp_user_id");
      let encodedUserId = encodeURIComponent(userId);

      jQuery(".subscribe_error").html("");
      jQuery(".subscribe_spinner").show();
      jQuery.ajax({
        type: "POST",
        url: `/subscriptions?user_id=${encodedUserId}&ontology_id=${ontologyId}&subbed=${isSubbed}`,
        dataType: "json",
        success: function(data) {
          jQuery(".subscribe_spinner").hide();
          // Toggle subbed value on a element
          var subbedVal = (isSubbed === "true") ? "false" : "true";
          jQuery("a.subscribe_to_ontology").attr("data-bp_is_subbed", subbedVal);
          // Change button text
          var txt = jQuery("a.subscribe_to_ontology span.ui-button-text").html();
          var newButtonText = txt.match("Unsubscribe") ? txt.replace("Unsubscribe", "Subscribe") : txt.replace("Subscribe", "Unsubscribe");
          jQuery("a.subscribe_to_ontology span.ui-button-text").html(newButtonText);
        },
        error: function(data) {
          jQuery(".subscribe_spinner").hide();
          jQuery(".subscribe_error").html("Problem subscribing to emails, please try again");
        }
      });
    }

    function editCustomOntologies() {
      jQuery("#custom_ontologies_picker").css("left", "").css("position", "");
      jQuery("#custom_ontologies").hide();
    }

  %h4{class: 'pb-2 mt-5 mb-4 border-bottom'} Custom ontology set
  #custom_ontologies.enable-lists
    - if at_slice?
      = custom_ontology_set_slice_text
    - else
      = custom_ontology_set_intro_text
      = button_tag('Select ontologies', type: 'button', id: 'edit_custom_ontologies', class: 'btn btn-primary')
      - if @user_ontologies.present?
        %ul
          - @user_ontologies.each do |ont|
            - ont = LinkedData::Client::Models::Ontology.get(ont)
            %li
              #{ont.name} (#{ont.acronym})
      - else
        = tag.p("You haven't picked any ontologies yet", class: 'mt-3 mb-5')

  #custom_ontologies_picker{style: "left: -9999px; position: absolute;"}
    = form_tag custom_ontologies_path(url_encode(@user.username)) do
      - selected = @user.customOntology.map {|o| LinkedData::Client::Models::Ontology.get(o).acronym}
      - locals = { custom_ontologies: @all_ontologies, selected_ontologies: selected, sel_text: "Select Custom Ontologies" }
      = render partial: "shared/ontology_picker", locals: locals
      = submit_tag "Save Custom Ontologies", class: "link_button"