DEV:add uploaded_meta option in category for category meta image (#6724)

DEV:add uploaded_meta option in category for category meta image (#6724)
From 9e3143445bac0d7591cbc52be298ddd55df190de Mon Sep 17 00:00:00 2001
From: Saurabh Patel <saurabh.finch@gmail.com>
Date: Fri, 7 Dec 2018 22:24:08 +0700
Subject: [PATCH] DEV:add uploaded_meta option in category for category meta
 image (#6724)


diff --git a/app/controllers/categories_controller.rb b/app/controllers/categories_controller.rb
index e2ab78d..b51882f 100644
--- a/app/controllers/categories_controller.rb
+++ b/app/controllers/categories_controller.rb
@@ -276,6 +276,7 @@ class CategoriesController < ApplicationController
                       :auto_close_based_on_last_post,
                       :uploaded_logo_id,
                       :uploaded_background_id,
+                      :uploaded_meta_id,
                       :slug,
                       :allow_badges,
                       :topic_template,
diff --git a/app/jobs/scheduled/clean_up_uploads.rb b/app/jobs/scheduled/clean_up_uploads.rb
index 3c4f37c..13dc588 100644
--- a/app/jobs/scheduled/clean_up_uploads.rb
+++ b/app/jobs/scheduled/clean_up_uploads.rb
@@ -55,7 +55,7 @@ module Jobs
         .joins("LEFT JOIN users u ON u.uploaded_avatar_id = uploads.id")
         .joins("LEFT JOIN user_avatars ua ON ua.gravatar_upload_id = uploads.id OR ua.custom_upload_id = uploads.id")
         .joins("LEFT JOIN user_profiles up ON up.profile_background = uploads.url OR up.card_background = uploads.url")
-        .joins("LEFT JOIN categories c ON c.uploaded_logo_id = uploads.id OR c.uploaded_background_id = uploads.id")
+        .joins("LEFT JOIN categories c ON c.uploaded_logo_id = uploads.id OR c.uploaded_background_id = uploads.id OR c.uploaded_meta_id = uploads.id")
         .joins("LEFT JOIN custom_emojis ce ON ce.upload_id = uploads.id")
         .joins("LEFT JOIN theme_fields tf ON tf.upload_id = uploads.id")
         .joins("LEFT JOIN user_exports ue ON ue.upload_id = uploads.id")
diff --git a/app/models/category.rb b/app/models/category.rb
index 5dcceb5..af32b64 100644
--- a/app/models/category.rb
+++ b/app/models/category.rb
@@ -28,6 +28,7 @@ class Category < ActiveRecord::Base
   belongs_to :latest_post, class_name: "Post"
   belongs_to :uploaded_logo, class_name: "Upload"
   belongs_to :uploaded_background, class_name: "Upload"
+  belongs_to :uploaded_meta, class_name: "Upload"
 
   has_many :topics
   has_many :category_users
@@ -667,6 +668,7 @@ end
 #  sort_ascending                    :boolean
 #  uploaded_logo_id                  :integer
 #  uploaded_background_id            :integer
+#  uploaded_meta_id                  :integer
 #  topic_featured_link_allowed       :boolean          default(TRUE)
 #  all_topics_wiki                   :boolean          default(FALSE), not null
 #  show_subcategory_list             :boolean          default(FALSE)
diff --git a/app/models/category_list.rb b/app/models/category_list.rb
index f59f36c..86631af 100644
--- a/app/models/category_list.rb
+++ b/app/models/category_list.rb
@@ -69,6 +69,7 @@ class CategoryList
     @categories = Category.includes(
       :uploaded_background,
       :uploaded_logo,
+      :uploaded_meta,
       :topic_only_relative_url,
       subcategories: [:topic_only_relative_url]
     ).secured(@guardian)
diff --git a/app/models/site.rb b/app/models/site.rb
index e6a0acc..8577329 100644
--- a/app/models/site.rb
+++ b/app/models/site.rb
@@ -28,7 +28,7 @@ class Site
   def categories
     @categories ||= begin
       categories = Category
-        .includes(:uploaded_logo, :uploaded_background)
+        .includes(:uploaded_logo, :uploaded_background, :uploaded_meta)
         .secured(@guardian)
         .joins('LEFT JOIN topics t on t.id = categories.topic_id')
         .select('categories.*, t.slug topic_slug')
diff --git a/app/serializers/basic_category_serializer.rb b/app/serializers/basic_category_serializer.rb
index 6be2df2..2074314 100644
--- a/app/serializers/basic_category_serializer.rb
+++ b/app/serializers/basic_category_serializer.rb
@@ -30,6 +30,7 @@ class BasicCategorySerializer < ApplicationSerializer
 
   has_one :uploaded_logo, embed: :object, serializer: CategoryUploadSerializer
   has_one :uploaded_background, embed: :object, serializer: CategoryUploadSerializer
+  has_one :uploaded_meta, embed: :object, serializer: CategoryUploadSerializer
 
   def include_parent_category_id?
     parent_category_id
diff --git a/db/migrate/20181204123042_add_uploaded_meta_id_to_categories.rb b/db/migrate/20181204123042_add_uploaded_meta_id_to_categories.rb
new file mode 100644
index 0000000..ea64d6c
--- /dev/null
+++ b/db/migrate/20181204123042_add_uploaded_meta_id_to_categories.rb
@@ -0,0 +1,5 @@
+class AddUploadedMetaIdToCategories < ActiveRecord::Migration[5.2]
+  def change
+    add_column :categories, :uploaded_meta_id, :integer
+  end
+end
diff --git a/spec/requests/categories_controller_spec.rb b/spec/requests/categories_controller_spec.rb
index ec9b281..80c7622 100644
--- a/spec/requests/categories_controller_spec.rb
+++ b/spec/requests/categories_controller_spec.rb
@@ -132,7 +132,8 @@ describe CategoriesController do
             permissions: {
               "everyone" => readonly,
               "staff" => create_post
-            }
+            },
+            uploaded_meta_id: 2
           }
 
           expect(response.status).to eq(200)
@@ -145,6 +146,7 @@ describe CategoriesController do
           expect(category.color).to eq("ff0")
           expect(category.auto_close_hours).to eq(72)
           expect(UserHistory.count).to eq(4) # 1 + 3 (bootstrap mode)
+          expect(category.uploaded_meta_id).to eq(2)
         end
       end
     end

GitHub