Skip to content

Instantly share code, notes, and snippets.

@arbianchi
Created July 5, 2017 22:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save arbianchi/02d8272e895935c6666cae9162a2d089 to your computer and use it in GitHub Desktop.
Save arbianchi/02d8272e895935c6666cae9162a2d089 to your computer and use it in GitHub Desktop.
Ruby on Rails Test - Adina Bianchi
class MissionsController < ApplicationController
def index
# Gets list of unique mission type ids for current program
# Could be pulled out into a model method.
mission_type_ids = (
current_company.missions_for_program(current_program).pluck("distinct(mission_type_id)") #+
).uniq
# Gets mission types with IDs matching mission_type_ids. Could avoid multiple calls by using a join.
@mission_types = MissionType.prioritised.where id: mission_type_ids
# Gets missions for current program including associated data for later use.
@missions = current_company.missions_for_program(current_program)
.includes(:company, :mission_type, :survey => {:questions => [:translations, {:answers => :translations }] })
# Checks to see if any of the missions have the featured attribute set to true.
@featured_present = @missions.exists?(featured: true)
# Filter to single intersecting mission_type if param is present:
if params[:mission_type_id].present? && !sponsored?
@missions = @missions.with_mission_type_id (mission_type_ids & Array(params[:mission_type_id]).map(&:to_i))
# Gets MissionType by specific mission type id.
# Could use find_by instead of where...first
@mission_type = MissionType.where(id: params[:mission_type_id]).first
elsif params[:search].blank?
# If no mission type id or search term is passed in through params, or if sponsored...
if @featured_present
# Changes @missions to only include those that are featured and have specified mission type ids
@missions = @missions.featured.with_mission_type_id @mission_types.pluck(:id)
else
# If no featured present, update mission_type to the first mission type with specified IDs
@mission_type = @mission_types.first
# Sets mission type id to previously specified @mission_type id.
# Would probably store this in a new variable instead of updated the param itself.
params[:mission_type_id] = @mission_type.id
@missions = @missions.with_mission_type_id (mission_type_ids & [@mission_type.id])
end
end
# Filters @missions by those with names matching or containing the search term if it is present.
@missions = @missions.where('UPPER(name) LIKE ?', "%#{params[:search].upcase}%") if params[:search].present?
# Filters @missions by those that are sponsored.
@missions = @missions.sponsored if sponsored?
# Sorts @missions by position, date, name and id.
@missions = @missions.order 'missions.position asc, missions.mission_date asc, missions.name asc, missions.id desc'
# Gets milestones associated with @missions
@missions = @missions.includes(:milestones)
end
end
# Annotate the Code Above with an explanation of what's being done
# List the params being acted upon
# :mission_type_id and :search
# List the variables being exposed for the view and what you expect they'll contain
# @mission_type will contain the id of the first mission_type for a program
# @mission_types will contain an array of MissionTypes scoped by priority key and filtered by a search term if that is present
# @featured_present will contain a boolean specifying whether there are any featured missions
# @missions will contain an array of Missions for a particular program
# Annotate or propose how you'd improve the code for readability/maintainability
# In addition to the above comments, I would add 'scope' statements to the Mission model to pull some of this search logic out of the controller.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment