ScottKolo/suitesparse-matrix-collection-website

View on GitHub
app/controllers/collection_matrices_controller.rb

Summary

Maintainability
A
0 mins
Test Coverage
A
94%
# frozen_string_literal: true

class CollectionMatricesController < ApplicationController
  FILTER_CHECKBOXES = {
    size: %i[min_rows max_rows min_cols max_cols min_nonzeros max_nonzeros],
    structure: %i[min_pattern_symmetry max_pattern_symmetry
                  min_numerical_symmetry max_numerical_symmetry
                  min_strongly_connected_components max_strongly_connected_components
                  structure positive_definite rb_type],
    metadata: %i[name_query group_query min_id max_id min_year max_year]
  }.freeze

  PERMITTED_PARAMTERS = [
    {
      filterrific:
        %i[reset_filterrific sorted_by search_query
           min_id max_id
           min_year max_year
           min_rows max_rows
           min_cols max_cols
           min_nonzeros max_nonzeros
           min_pattern_symmetry max_pattern_symmetry
           min_numerical_symmetry max_numerical_symmetry
           min_strongly_connected_components max_strongly_connected_components
           structure positive_definite rb_type
           name_query group_query]
    },
    :page, :per_page, :utf8, :_
  ].freeze

  ### Resources methods ########################################################

  def index
    # List of permitted fields for params
    permitted_params = params.permit(PERMITTED_PARAMTERS)

    # Initialize filterrific filtering system
    @filterrific = initialize_filterrific(
      CollectionMatrix,
      permitted_params[:filterrific],
      select_options: {
        sorted_by: CollectionMatrix.options_for_sorted_by,
        positive_definite_options: %w[Yes No],
        structure_options: %w[Square Rectangular Symmetric Unsymmetric Hermitian Skew-Symmetric],
        rutherford_boeing_options: %w[Real Complex Integer Binary]
      }
    ) or return

    # Determine which filters are visible / checkboxes are checked
    @checked = helpers.is_checked(@filterrific, FILTER_CHECKBOXES)

    # Determine which page we are displaying
    @matrices = @filterrific.find.page(permitted_params[:page])

    # Determine how many matrices to display per page
    @per_page = helpers.per_page(permitted_params, session)

    @matrices = @matrices.paginate(page: permitted_params[:page], per_page: @per_page)

    # Allow the index page to respond to HTML and javascript (ajax)
    respond_to do |format|
      format.html
      format.js
    end
  rescue ActiveRecord::RecordNotFound
    # There is an issue with the persisted param_set. Reset it.
    redirect_to(reset_filterrific_url(format: :html)) and return
  end

  # Show the details page for a matrix
  def show
    # Get matrix info from the params
    matrix_id, group, name = params.values_at(:matrix_id, :group, :name)

    @matrix = nil

    @matrix = if matrix_id.nil?
                # If no matrix_id is given, search for the matrix by group/name
                CollectionMatrix.find_by(name: name, group: group)
              else
                # If we have an id number, use that (it's faster)
                CollectionMatrix.find_by(matrix_id: matrix_id)
              end

    unless @matrix
      # If we couldn't find the matrix, render the matrix not found page
      render :not_found, status: 404, content_type: 'text/html', template: 'collection_matrices/not_found'
    end
  end

  def new
    # Get a list of kinds currently in the collection
    @kinds = helpers.kind_submission_list
    @new_matrix = SubmittedMatrix.new
  end

  def submit
    # Verify the reCaptcha
    if verify_recaptcha
      permitted_params = params[:submitted_matrix].permit(:submitter_name,
                                                          :submitter_email, :display_email, :name, :kind, :notes,
                                                          :submitter_url, :submitter_confidentiality)
      @new_matrix = SubmittedMatrix.new(permitted_params)
      @new_matrix.ip = request.remote_ip
      email = AdminNotifierMailer.send_matrix_submitted_email(@new_matrix)
      email.deliver_now if Rails.env.production? || Rails.env.production_heroku?
      flash[:success] = 'Matrix submitted successfully!'
      redirect_to :index
    else
      flash[:danger] = 'Please verify that you are not a robot.'
      redirect_to :submit
    end
  end
end