From 7789e2138fc0479846c20bc68d68973636a4a760 Mon Sep 17 00:00:00 2001 From: Kelly Rauchenberger Date: Tue, 12 Mar 2019 21:50:00 -0400 Subject: Started game tracker --- .gitignore | 2 + Gemfile | 2 + Gemfile.lock | 4 ++ app/assets/stylesheets/main/games.scss | 25 +++++++++++++ app/controllers/admin/games_controller.rb | 56 ++++++++++++++++++++++++++++ app/controllers/games_controller.rb | 5 +++ app/helpers/admin/games_helper.rb | 2 + app/helpers/games_helper.rb | 2 + app/models/game.rb | 13 +++++++ app/views/admin/games/_form.html.haml | 23 ++++++++++++ app/views/admin/games/edit.html.haml | 3 ++ app/views/admin/games/index.html.haml | 13 +++++++ app/views/admin/games/new.html.haml | 3 ++ app/views/games/index.html.haml | 14 +++++++ app/views/layouts/admin.html.haml | 4 ++ config/routes.rb | 4 ++ db/migrate/20190312193025_install_audited.rb | 30 +++++++++++++++ db/migrate/20190312193154_create_games.rb | 13 +++++++ db/schema.rb | 34 ++++++++++++++++- 19 files changed, 251 insertions(+), 1 deletion(-) create mode 100644 app/assets/stylesheets/main/games.scss create mode 100644 app/controllers/admin/games_controller.rb create mode 100644 app/controllers/games_controller.rb create mode 100644 app/helpers/admin/games_helper.rb create mode 100644 app/helpers/games_helper.rb create mode 100644 app/models/game.rb create mode 100644 app/views/admin/games/_form.html.haml create mode 100644 app/views/admin/games/edit.html.haml create mode 100644 app/views/admin/games/index.html.haml create mode 100644 app/views/admin/games/new.html.haml create mode 100644 app/views/games/index.html.haml create mode 100644 db/migrate/20190312193025_install_audited.rb create mode 100644 db/migrate/20190312193154_create_games.rb diff --git a/.gitignore b/.gitignore index 702ff9b..ae472fd 100644 --- a/.gitignore +++ b/.gitignore @@ -29,3 +29,5 @@ tags *.swo /public/uploads config/database.yml + +misc diff --git a/Gemfile b/Gemfile index 0bf9828..d57baba 100644 --- a/Gemfile +++ b/Gemfile @@ -72,3 +72,5 @@ gem 'pokeviewer', github: "hatkirby/pokeviewer" gem 'acts-as-taggable-on' gem 'jquery-ui-rails' gem 'js-routes' +gem 'audited', '~> 4.7' +gem 'enumerize' diff --git a/Gemfile.lock b/Gemfile.lock index 8fccc7c..62cb538 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -66,6 +66,8 @@ GEM airbrussh (1.3.1) sshkit (>= 1.6.1, != 1.7.0) arel (9.0.0) + audited (4.8.0) + activerecord (>= 4.0, < 5.3) bcrypt (3.1.12) bindex (0.5.0) builder (3.2.3) @@ -279,6 +281,7 @@ PLATFORMS DEPENDENCIES acts-as-taggable-on + audited (~> 4.7) byebug capistrano (~> 3.0) capistrano-bundler @@ -289,6 +292,7 @@ DEPENDENCIES ckeditor coffee-rails (~> 4.2) devise + enumerize haml highline jbuilder (~> 2.5) diff --git a/app/assets/stylesheets/main/games.scss b/app/assets/stylesheets/main/games.scss new file mode 100644 index 0000000..5c39701 --- /dev/null +++ b/app/assets/stylesheets/main/games.scss @@ -0,0 +1,25 @@ +// Place all the styles related to the GamesController controller here. +// They will automatically be included in application.css. +// You can use Sass (SCSS) here: http://sass-lang.com/ +#games-table { + border-spacing: 0; + width: 100%; + + tr { + &.even { + background-color: #fff; + } + + &.odd { + background-color: #edf; + } + } + + td { + padding: 0.5em; + } +} + +.game-progress p { + margin: 0; +} diff --git a/app/controllers/admin/games_controller.rb b/app/controllers/admin/games_controller.rb new file mode 100644 index 0000000..5bc6de9 --- /dev/null +++ b/app/controllers/admin/games_controller.rb @@ -0,0 +1,56 @@ +class Admin::GamesController < Admin::AdminController + before_action :set_section + + def index + @games = Game.order(created_at: :desc) + end + + def drafts + @games = Game.where(created_at: :desc) + end + + def new + @game = Game.new + end + + def create + @game = Game.new(game_params) + + if @game.save + flash.notice = "Game created successfully!" + + render :edit + else + flash.alert = "Error creating game." + + render :new + end + end + + def edit + @game = Game.find(params[:id]) + end + + def update + @game = Game.find(params[:id]) + + if @game.update_attributes(game_params) + flash.notice = "Game updated successfully!" + else + flash.alert = "Error updating game." + end + + render :edit + end + + private + + def game_params + params.require(:game).permit(:title, :description, :status, :progress, :score) + end + + def set_section + @section = "games" + end + +end diff --git a/app/controllers/games_controller.rb b/app/controllers/games_controller.rb new file mode 100644 index 0000000..7d95b2c --- /dev/null +++ b/app/controllers/games_controller.rb @@ -0,0 +1,5 @@ +class GamesController < ApplicationController + def index + @games = Game.all + end +end diff --git a/app/helpers/admin/games_helper.rb b/app/helpers/admin/games_helper.rb new file mode 100644 index 0000000..ab083dc --- /dev/null +++ b/app/helpers/admin/games_helper.rb @@ -0,0 +1,2 @@ +module Admin::GamesHelper +end diff --git a/app/helpers/games_helper.rb b/app/helpers/games_helper.rb new file mode 100644 index 0000000..2ef8c1f --- /dev/null +++ b/app/helpers/games_helper.rb @@ -0,0 +1,2 @@ +module GamesHelper +end diff --git a/app/models/game.rb b/app/models/game.rb new file mode 100644 index 0000000..de33377 --- /dev/null +++ b/app/models/game.rb @@ -0,0 +1,13 @@ +class Game < ApplicationRecord + extend Enumerize + + audited only: [:status, :progress] + + validates :title, presence: true + validates :status, presence: true + + enumerize :status, + in: [:playing, :upcoming, :held, :dropped, :finished], + default: :upcoming + +end diff --git a/app/views/admin/games/_form.html.haml b/app/views/admin/games/_form.html.haml new file mode 100644 index 0000000..4b17664 --- /dev/null +++ b/app/views/admin/games/_form.html.haml @@ -0,0 +1,23 @@ +%fieldset#content + .title-field + = f.label :title + = f.text_field :title, placeholder: "Title" + .description-field + = f.label :description + = f.cktext_area :description +%fieldset#details + - if f.object.errors.any? + #errors.details-module + %h3 Error! + %ul + - f.object.errors.full_messages.each do |error| + %li= error + .details-module + .progress-field + = f.label :progress + = f.text_area :progress + .details-module + .status-field + = f.select :status, options_for_select(Game.status.values) + = f.label :status + .details-module= f.submit diff --git a/app/views/admin/games/edit.html.haml b/app/views/admin/games/edit.html.haml new file mode 100644 index 0000000..d7a480b --- /dev/null +++ b/app/views/admin/games/edit.html.haml @@ -0,0 +1,3 @@ +- title "Editing #{@game.title}" += form_for @game, url: admin_game_url(@game), html: { id: "entry-form" } do |f| + = render partial: "form", locals: { f: f } diff --git a/app/views/admin/games/index.html.haml b/app/views/admin/games/index.html.haml new file mode 100644 index 0000000..96c69b8 --- /dev/null +++ b/app/views/admin/games/index.html.haml @@ -0,0 +1,13 @@ +- title "Games" +%table#entries + %tr + %th Title + %th Date published + %th + - @games.each do |game| + %tr{ class: cycle("even", "odd") } + %td= game.title + %td= game.created_at.strftime("%B %d, %Y, %l:%M%P") + %td + %ul.admin-actions + %li= link_to "Edit", edit_admin_game_url(game) diff --git a/app/views/admin/games/new.html.haml b/app/views/admin/games/new.html.haml new file mode 100644 index 0000000..00dfb92 --- /dev/null +++ b/app/views/admin/games/new.html.haml @@ -0,0 +1,3 @@ +- title "New game" += form_for @game, url: admin_games_url, html: { id: "entry-form" } do |f| + = render partial: "form", locals: { f: f } diff --git a/app/views/games/index.html.haml b/app/views/games/index.html.haml new file mode 100644 index 0000000..64fbfd9 --- /dev/null +++ b/app/views/games/index.html.haml @@ -0,0 +1,14 @@ +- title "Games" +%h2 Games +%table#games-table + %tr + %th Title + %th Status + %th Score + %th Progress + - @games.each do |game| + %tr{ class: cycle("even", "odd") } + %td= game.title + %td= game.status + %td= game.score + %td.game-progress= simple_format game.progress diff --git a/app/views/layouts/admin.html.haml b/app/views/layouts/admin.html.haml index 4bfe60a..1c865b0 100644 --- a/app/views/layouts/admin.html.haml +++ b/app/views/layouts/admin.html.haml @@ -33,5 +33,9 @@ = link_to "Links", admin_links_url, class: "major-link" %ul.minors %li.minor= link_to "New link", new_admin_link_url + %li{major_sidebar_attrs("games")} + = link_to "Games", admin_games_url, class: "major-link" + %ul.minors + %li.minor= link_to "New game", new_admin_game_url #main = yield diff --git a/config/routes.rb b/config/routes.rb index 512c319..240beae 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -13,6 +13,8 @@ Rails.application.routes.draw do end resources :links, except: [:show] + + resources :games, except: [:show] end mount Ckeditor::Engine => '/ckeditor' @@ -28,6 +30,8 @@ Rails.application.routes.draw do get 'thinks/:slug', to: 'streams#show', as: :stream + get 'plays', to: 'games#index' + resources :tags, only: [], param: :name do collection do get 'suggest' diff --git a/db/migrate/20190312193025_install_audited.rb b/db/migrate/20190312193025_install_audited.rb new file mode 100644 index 0000000..ef5487e --- /dev/null +++ b/db/migrate/20190312193025_install_audited.rb @@ -0,0 +1,30 @@ +class InstallAudited < ActiveRecord::Migration[5.2] + def self.up + create_table :audits, :force => true do |t| + t.column :auditable_id, :integer + t.column :auditable_type, :string + t.column :associated_id, :integer + t.column :associated_type, :string + t.column :user_id, :integer + t.column :user_type, :string + t.column :username, :string + t.column :action, :string + t.column :audited_changes, :text + t.column :version, :integer, :default => 0 + t.column :comment, :string + t.column :remote_address, :string + t.column :request_uuid, :string + t.column :created_at, :datetime + end + + add_index :audits, [:auditable_type, :auditable_id, :version], :name => 'auditable_index' + add_index :audits, [:associated_type, :associated_id], :name => 'associated_index' + add_index :audits, [:user_id, :user_type], :name => 'user_index' + add_index :audits, :request_uuid + add_index :audits, :created_at + end + + def self.down + drop_table :audits + end +end diff --git a/db/migrate/20190312193154_create_games.rb b/db/migrate/20190312193154_create_games.rb new file mode 100644 index 0000000..de30492 --- /dev/null +++ b/db/migrate/20190312193154_create_games.rb @@ -0,0 +1,13 @@ +class CreateGames < ActiveRecord::Migration[5.2] + def change + create_table :games do |t| + t.string :title + t.string :status + t.text :progress + t.text :description + t.integer :score + + t.timestamps + end + end +end diff --git a/db/schema.rb b/db/schema.rb index 93085f3..c5c794f 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,29 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 2018_08_11_215146) do +ActiveRecord::Schema.define(version: 2019_03_12_193154) do + + create_table "audits", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci", force: :cascade do |t| + t.integer "auditable_id" + t.string "auditable_type" + t.integer "associated_id" + t.string "associated_type" + t.integer "user_id" + t.string "user_type" + t.string "username" + t.string "action" + t.text "audited_changes" + t.integer "version", default: 0 + t.string "comment" + t.string "remote_address" + t.string "request_uuid" + t.datetime "created_at" + t.index ["associated_type", "associated_id"], name: "associated_index" + t.index ["auditable_type", "auditable_id", "version"], name: "auditable_index" + t.index ["created_at"], name: "index_audits_on_created_at" + t.index ["request_uuid"], name: "index_audits_on_request_uuid" + t.index ["user_id", "user_type"], name: "user_index" + end create_table "blogs", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin", force: :cascade do |t| t.string "title" @@ -35,6 +57,16 @@ ActiveRecord::Schema.define(version: 2018_08_11_215146) do t.index ["type"], name: "index_ckeditor_assets_on_type" end + create_table "games", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci", force: :cascade do |t| + t.string "title" + t.string "status" + t.text "progress" + t.text "description" + t.integer "score" + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + end + create_table "links", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci", force: :cascade do |t| t.string "title", null: false t.string "url", null: false -- cgit 1.4.1