Skip to content

Instantly share code, notes, and snippets.

@makkura
Last active May 12, 2016 16:35
Show Gist options
  • Select an option

  • Save makkura/63b90a65e79a32367d217d6707713f9a to your computer and use it in GitHub Desktop.

Select an option

Save makkura/63b90a65e79a32367d217d6707713f9a to your computer and use it in GitHub Desktop.
# Title
# Input Requirements:
# title_search as a string
# title_search_type as 'contains' or 'starts_with', simple search would probably use starts_with
# Database Requirements
# Game table has field 'title' for game's name
title_search_type = params[:title_search_type] if params[:title_search_type].present?
title_search = params[:title_search].strip if params[:title_search].present
if title_search.length == 0
title_search_type = nil
title_search = nil
elsif title_search_type.downcase == "contains"
title_search = "%#{title_search}%"
else
title_search = "#{title_search}%"
end
# Date Range
# Input Requirements:
# start date and stop date: release_year_from and release_year_to
# We could do everything from years down to date range by day, the important part is consistency in the search form and database
# We'll assume a simple 'year' field
# Database Requirements
# Game table has release_year field
release_year_from = params[:release_year_from].to_i if params[:release_year_from].present?
release_year_to = params[:release_year_to].to_i if params[:release_year_to].present?
# Platforms
# Input Requirements
# platforms is a series of platforms to include as passed from the form
# the simple version of the form would pass them all
# Database Requirements:
# Game has_many Platform relation, Platform has platform_code field (eg: PS4, XBox1, etc)
search_platforms = params[:platforms].split(",")
# Genre
# Input Requirements:
# genre_includes is a series of genres that are required to be found
# genre_excludes is a series of genres that are requires to be ignored
# Database Requirements:
# Game has_many Genre relation, genre has genre_code field (eg: RPG, MMO, FPS, etc)
genre_includes = param[:genre_includes].split(",") if params[:genre_include].present?
genre_excludes = param[:genre_excludes].split(",") if params[:genre_exclude].present?
# Rating
# Input Requirements:
# Each rating type to be used is passed in with a float value at most 1 decimal place XX.X (1.0 to 10.0)
# Database Requirements:
# Game table has fields creativity_rating, soundtrack_rating, gameplay_rating, and replay_rating reprsenting the average rating against total reviews of the game
creativity_min = params[:creativity_min].to_f if params[:creativity_min].present?
soundtrack_min = params[:soundtrack_min].to_f if params[:soundtrack_min].present?
gameplay_min = params[:gameplay_min].to_f if params[:gameplay_min].present?
replay_min = params[:replay_min].to_f if params[:replay_min].present?
# Reviews
# Input Requirements
# Minimum number of reviews to show up on the list
# Database Requirements
# Game table has review_count representing total number of reviews
reviews_min = params[:reviews_min].to_i if params[:reviews_min].present?
# We grab everything and then start removing things since NO restriction is guarenteed
# If we have guarenteed restrictions we'd put them in to start to speed this up
games = Game.all
games = games.where("title LIKE ?", title_search) if title_search_type.present? #presanitized
games = games.where("release_year >= ?", release_year_from) if release_year_from.present?
games = games.where("release_year <= ?", release_year_to) if release_year_to.present?
games = games.joins(:platforms).where(platforms: {platform_code => search_platforms}) if search_platforms.present?
games = games.joins(:genres).where(genres: {genre_code => genre_includes}) if genre_includes.present?
games = games.joins(:genres).where.not(genres: {genre_code => genre_excludes}) if genre_excludes.present?
games = games.where("creativity_rating >= ?", creativity_min) if creativity_min.present?
games = games.where("soundtrack_rating >= ?", soundtrack_min) if soundtrack_min.present?
games = games.where("gameplay_rating >= ?", gameplay_min) if gameplay_min.present?
games = games.where("replay_rating >= ?", replay_min) if replay_min.present?
@makkura
Copy link
Author

makkura commented May 12, 2016

Some rough, verbose code on searching and filtering results of a database holding game information and reviews.
Origin thread on PCMR subreddit thread

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment