From 5462a1e56abf70486dc59593dde6ecb95a072026 Mon Sep 17 00:00:00 2001 From: Star Rauchenberger Date: Wed, 20 Mar 2024 11:00:31 -0400 Subject: Added some admin dashboard stats --- app/controllers/comments_controller.rb | 2 ++ app/models/global.rb | 20 ++++++++++++++++++++ app/views/admin/dashboard/index.html.haml | 20 +++++++++++++++++++- db/migrate/20240320145033_create_globals.rb | 11 +++++++++++ db/schema.rb | 10 +++++++++- test/fixtures/globals.yml | 11 +++++++++++ test/models/global_test.rb | 7 +++++++ 7 files changed, 79 insertions(+), 2 deletions(-) create mode 100644 app/models/global.rb create mode 100644 db/migrate/20240320145033_create_globals.rb create mode 100644 test/fixtures/globals.yml create mode 100644 test/models/global_test.rb diff --git a/app/controllers/comments_controller.rb b/app/controllers/comments_controller.rb index 368f587..b305d0a 100644 --- a/app/controllers/comments_controller.rb +++ b/app/controllers/comments_controller.rb @@ -35,6 +35,8 @@ class CommentsController < ApplicationController if is_blatant # I am lying. flash.notice = "Comment submitted successfully! It will need to be moderated before it shows up on the blog." + + Global.increment_filtered_comments else if is_spam @comment.status = :pending diff --git a/app/models/global.rb b/app/models/global.rb new file mode 100644 index 0000000..7c7d6d4 --- /dev/null +++ b/app/models/global.rb @@ -0,0 +1,20 @@ +class Global < ApplicationRecord + def self.get_filtered_comments + row = find_by_key("filtered_comments") + if row + row[:int_value] + else + 0 + end + end + + def self.increment_filtered_comments + row = find_by_key("filtered_comments") + if row + row.int_value += 1 + row.save + else + create(key: "filtered_comments", int_value: 1) + end + end +end diff --git a/app/views/admin/dashboard/index.html.haml b/app/views/admin/dashboard/index.html.haml index eaac627..254107c 100644 --- a/app/views/admin/dashboard/index.html.haml +++ b/app/views/admin/dashboard/index.html.haml @@ -1 +1,19 @@ -Welcome to the the ubiquitous administration panel! +%p Welcome to the the ubiquitous administration panel! +%p Stats: +%ul + %li + %strong Blog posts: + = Blog.count + %li + %strong Approved comments: + = Comment.where(status: :published).count + %li + %strong Filtered comments: + = Global.get_filtered_comments + %li + %strong Quotes: + = Quote.count + %li + %strong Votes: + = Vote.count + (+#{Vote.where(upvote: true).count} / -#{Vote.where(upvote: false).count}) diff --git a/db/migrate/20240320145033_create_globals.rb b/db/migrate/20240320145033_create_globals.rb new file mode 100644 index 0000000..6ac9a29 --- /dev/null +++ b/db/migrate/20240320145033_create_globals.rb @@ -0,0 +1,11 @@ +class CreateGlobals < ActiveRecord::Migration[7.1] + def change + create_table :globals do |t| + t.string :key + t.string :string_value + t.integer :int_value + + t.timestamps + end + end +end diff --git a/db/schema.rb b/db/schema.rb index b036d7f..9cfdf57 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema[7.1].define(version: 2023_10_30_181440) do +ActiveRecord::Schema[7.1].define(version: 2024_03_20_145033) do create_table "active_storage_attachments", force: :cascade do |t| t.string "name", null: false t.string "record_type", null: false @@ -118,6 +118,14 @@ ActiveRecord::Schema[7.1].define(version: 2023_10_30_181440) do t.date "finished_on" end + create_table "globals", force: :cascade do |t| + t.string "key" + t.string "string_value" + t.integer "int_value" + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + end + create_table "lingo_scores", force: :cascade do |t| t.integer "user_id", limit: 8 t.string "username" diff --git a/test/fixtures/globals.yml b/test/fixtures/globals.yml new file mode 100644 index 0000000..bb636b7 --- /dev/null +++ b/test/fixtures/globals.yml @@ -0,0 +1,11 @@ +# Read about fixtures at https://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html + +one: + key: MyString + string_value: MyString + int_value: 1 + +two: + key: MyString + string_value: MyString + int_value: 1 diff --git a/test/models/global_test.rb b/test/models/global_test.rb new file mode 100644 index 0000000..2e42136 --- /dev/null +++ b/test/models/global_test.rb @@ -0,0 +1,7 @@ +require "test_helper" + +class GlobalTest < ActiveSupport::TestCase + # test "the truth" do + # assert true + # end +end -- cgit 1.4.1