From 24f9108bf8f10c87557da85f3d72045e1e3072f4 Mon Sep 17 00:00:00 2001 From: ArtOfCode- <hello@artofcode.co.uk> Date: Sat, 16 May 2020 17:35:39 +0100 Subject: [PATCH] Don't count deleted answers in answer_count --- Gemfile | 1 + Gemfile.lock | 8 ++++++++ app/models/post.rb | 4 +++- db/migrate/20200516162625_update_answer_count_column.rb | 5 +++++ db/schema.rb | 6 ++++-- 5 files changed, 21 insertions(+), 3 deletions(-) create mode 100644 db/migrate/20200516162625_update_answer_count_column.rb diff --git a/Gemfile b/Gemfile index 14df82a33..2bf7f4d8d 100644 --- a/Gemfile +++ b/Gemfile @@ -2,6 +2,7 @@ source 'https://rubygems.org' # Essential gems: servers, adapters, Rails + Rails requirements, Devise gem 'coffee-rails', '~> 4.2.2' +gem 'counter_culture', '~> 2.0' gem 'devise', '~> 4.7' gem 'jquery-rails', '~> 4.3.5' gem 'mysql2', '~> 0.5.3' diff --git a/Gemfile.lock b/Gemfile.lock index 888856e64..b00d4d145 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -44,6 +44,9 @@ GEM tzinfo (~> 1.1) addressable (2.7.0) public_suffix (>= 2.0.2, < 5.0) + after_commit_action (1.1.0) + activerecord (>= 3.0.0) + activesupport (>= 3.0.0) arel (9.0.0) ast (2.4.0) awesome_print (1.8.0) @@ -83,6 +86,10 @@ GEM execjs coffee-script-source (1.12.2) concurrent-ruby (1.1.6) + counter_culture (2.5.0) + activerecord (>= 4.2) + activesupport (>= 4.2) + after_commit_action (~> 1.0) coveralls (0.8.23) json (>= 1.8, < 3) simplecov (~> 0.16.1) @@ -279,6 +286,7 @@ DEPENDENCIES byebug (~> 11.1) chartkick (~> 3.3) coffee-rails (~> 4.2.2) + counter_culture (~> 2.0) coveralls (~> 0.8) devise (~> 4.7) e2mmap (~> 0.1) diff --git a/app/models/post.rb b/app/models/post.rb index 6d5033ba2..a61f1a30b 100644 --- a/app/models/post.rb +++ b/app/models/post.rb @@ -5,7 +5,7 @@ class Post < ApplicationRecord belongs_to :user belongs_to :post_type - belongs_to :parent, class_name: 'Post', required: false, counter_cache: :answer_count + belongs_to :parent, class_name: 'Post', required: false belongs_to :closed_by, class_name: 'User', required: false belongs_to :deleted_by, class_name: 'User', required: false belongs_to :last_activity_by, class_name: 'User', required: false @@ -18,6 +18,8 @@ class Post < ApplicationRecord has_many :flags, dependent: :destroy has_many :children, class_name: 'Post', foreign_key: 'parent_id', dependent: :destroy + counter_culture :parent, column_name: proc { |model| !model.deleted? ? 'answer_count' : nil } + serialize :tags_cache, Array validates :body, presence: true, length: { minimum: 30, maximum: 30_000 } diff --git a/db/migrate/20200516162625_update_answer_count_column.rb b/db/migrate/20200516162625_update_answer_count_column.rb new file mode 100644 index 000000000..eddd83ebb --- /dev/null +++ b/db/migrate/20200516162625_update_answer_count_column.rb @@ -0,0 +1,5 @@ +class UpdateAnswerCountColumn < ActiveRecord::Migration[5.2] + def change + change_column :posts, :answer_count, :integer, null: false, default: 0 + end +end diff --git a/db/schema.rb b/db/schema.rb index ae7e6d695..8eaa2d1eb 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.define(version: 2020_05_08_115752) do +ActiveRecord::Schema.define(version: 2020_05_16_162625) do create_table "active_storage_attachments", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci", force: :cascade do |t| t.string "name", null: false @@ -218,7 +218,7 @@ ActiveRecord::Schema.define(version: 2020_05_08_115752) do t.text "body_markdown" t.integer "answer_count", default: 0, null: false t.datetime "last_activity", default: -> { "CURRENT_TIMESTAMP" }, null: false - t.text "att_source" + t.string "att_source" t.string "att_license_name" t.string "att_license_link" t.string "doc_slug" @@ -228,6 +228,7 @@ ActiveRecord::Schema.define(version: 2020_05_08_115752) do t.bigint "duplicate_post_id" t.bigint "category_id" t.bigint "license_id" + t.index ["att_source"], name: "index_posts_on_att_source" t.index ["body_markdown"], name: "index_posts_on_body_markdown", type: :fulltext t.index ["category_id"], name: "index_posts_on_category_id" t.index ["close_reason_id"], name: "index_posts_on_close_reason_id" @@ -238,6 +239,7 @@ ActiveRecord::Schema.define(version: 2020_05_08_115752) do t.index ["license_id"], name: "index_posts_on_license_id" t.index ["parent_id"], name: "index_posts_on_parent_id" t.index ["post_type_id"], name: "index_posts_on_post_type_id" + t.index ["tags_cache"], name: "index_posts_on_tags_cache" t.index ["user_id"], name: "index_posts_on_user_id" end -- GitLab