From b62d43ccde70aa6fccf5341e57a695a2cfb289c1 Mon Sep 17 00:00:00 2001 From: Kelly Rauchenberger Date: Wed, 13 Mar 2019 15:48:14 -0400 Subject: Game list is sortable --- app/assets/images/down_arrow.gif | Bin 0 -> 843 bytes app/assets/images/up_arrow.gif | Bin 0 -> 835 bytes app/assets/javascripts/entries.coffee | 3 --- app/assets/stylesheets/main/games.scss | 40 +++++++++++++++++++++++++++++ app/controllers/games_controller.rb | 14 +++++++++- app/helpers/application_helper.rb | 7 +++++ app/views/games/index.html.haml | 45 ++++++++++++++++++--------------- 7 files changed, 84 insertions(+), 25 deletions(-) create mode 100644 app/assets/images/down_arrow.gif create mode 100644 app/assets/images/up_arrow.gif delete mode 100644 app/assets/javascripts/entries.coffee (limited to 'app') diff --git a/app/assets/images/down_arrow.gif b/app/assets/images/down_arrow.gif new file mode 100644 index 0000000..3438db3 Binary files /dev/null and b/app/assets/images/down_arrow.gif differ diff --git a/app/assets/images/up_arrow.gif b/app/assets/images/up_arrow.gif new file mode 100644 index 0000000..6438c97 Binary files /dev/null and b/app/assets/images/up_arrow.gif differ diff --git a/app/assets/javascripts/entries.coffee b/app/assets/javascripts/entries.coffee deleted file mode 100644 index 24f83d1..0000000 --- a/app/assets/javascripts/entries.coffee +++ /dev/null @@ -1,3 +0,0 @@ -# Place all the behaviors and hooks related to the matching controller here. -# All this logic will automatically be available in application.js. -# You can use CoffeeScript in this file: http://coffeescript.org/ diff --git a/app/assets/stylesheets/main/games.scss b/app/assets/stylesheets/main/games.scss index 98aca43..901d30d 100644 --- a/app/assets/stylesheets/main/games.scss +++ b/app/assets/stylesheets/main/games.scss @@ -18,6 +18,36 @@ td { padding: 0.5em; } + + thead { + th { + border-bottom: 1px solid #aaa; + text-align: left; + padding: 0.5em; + + a { + text-decoration: none; + + &:visited { + color: blue; + } + } + + .current { + padding-right: 12px; + background-repeat: no-repeat; + background-position: right center; + + &.asc { + background-image: image-url("up_arrow.gif"); + } + + &.desc { + background-image: image-url("down_arrow.gif"); + } + } + } + } } .game-progress p { @@ -34,3 +64,13 @@ font-size: 0.8em; color: #777; } + +h2#games-title { + background-color: #d6aeff; + display: block; + font-size: 20px; + font-family: 'Roboto', sans-serif; + padding: 0.5em 20px; + border-top: 1px solid #edf; + border-bottom: 1px solid #edf; +} diff --git a/app/controllers/games_controller.rb b/app/controllers/games_controller.rb index 8ddcf24..dcbe226 100644 --- a/app/controllers/games_controller.rb +++ b/app/controllers/games_controller.rb @@ -1,9 +1,21 @@ class GamesController < ApplicationController + helper_method :sort_column, :sort_direction + def index - @games = Game.order(started_on: :desc) + @games = Game.order(sort_column + " " + sort_direction) if params[:status] @games = @games.where(status: params[:status]) end end + + private + + def sort_column + Game.column_names.include?(params[:sort]) ? params[:sort] : "started_on" + end + + def sort_direction + %[asc desc].include?(params[:dir]) ? params[:dir] : "asc" + end end diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index a8b1c7e..8008b04 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -4,4 +4,11 @@ module ApplicationHelper content_for :title, text end + def sortable(col, title = nil) + title ||= col.titleize + css_class = (col == sort_column) ? "current #{sort_direction}" : nil + direction = (col == sort_column and sort_direction == "asc") ? "desc" : "asc" + link_to title, {:sort => col, :dir => direction}, {:class => css_class} + end + end diff --git a/app/views/games/index.html.haml b/app/views/games/index.html.haml index f7e7a44..56c5a05 100644 --- a/app/views/games/index.html.haml +++ b/app/views/games/index.html.haml @@ -1,23 +1,26 @@ - title "Games" -%h2 Games +.breadcrumb= link_to "← Back to home page", root_path +%h2#games-title Games %table#games-table - %tr - %th Title - %th Status - %th Score - %th Progress - - @games.each do |game| - %tr{ class: cycle("even", "odd") } - %td - %span.game-title= game.title - - unless game.started_on.blank? - %span.game-started - Started on - %time= game.started_on - - unless game.finished_on.blank? - %span.game-finished - Finished on - %time= game.finished_on - %td= game.status - %td= game.score - %td.game-progress= simple_format game.progress + %thead + %tr + %th= sortable "title" + %th= sortable "status" + %th= sortable "score" + %th= sortable "progress" + %tbody + - @games.each do |game| + %tr{ class: cycle("even", "odd") } + %td + %span.game-title= game.title + - unless game.started_on.blank? + %span.game-started + Started on + %time= game.started_on + - unless game.finished_on.blank? + %span.game-finished + Finished on + %time= game.finished_on + %td= game.status + %td= game.score + %td.game-progress= simple_format game.progress -- cgit 1.4.1