Drop unused tables (#5630)
This commit is contained in:
parent
adc46ac56c
commit
8e48b339fa
|
@ -23,9 +23,6 @@ class Category < ActiveRecord::Base
|
|||
has_many :category_featured_topics
|
||||
has_many :featured_topics, through: :category_featured_topics, source: :topic
|
||||
|
||||
has_many :category_featured_users
|
||||
has_many :featured_users, through: :category_featured_users, source: :user
|
||||
|
||||
has_many :category_groups, dependent: :destroy
|
||||
has_many :groups, through: :category_groups
|
||||
|
||||
|
@ -405,8 +402,8 @@ SQL
|
|||
end
|
||||
|
||||
def self.query_category(slug_or_id, parent_category_id)
|
||||
self.where(slug: slug_or_id, parent_category_id: parent_category_id).includes(:featured_users).first ||
|
||||
self.where(id: slug_or_id.to_i, parent_category_id: parent_category_id).includes(:featured_users).first
|
||||
self.where(slug: slug_or_id, parent_category_id: parent_category_id).first ||
|
||||
self.where(id: slug_or_id.to_i, parent_category_id: parent_category_id).first
|
||||
end
|
||||
|
||||
def self.find_by_email(email)
|
||||
|
|
|
@ -1,63 +0,0 @@
|
|||
class CategoryFeaturedUser < ActiveRecord::Base
|
||||
belongs_to :category
|
||||
belongs_to :user
|
||||
|
||||
def self.max_featured_users
|
||||
5
|
||||
end
|
||||
|
||||
def self.feature_users_in(category_or_category_id)
|
||||
category_id =
|
||||
if category_or_category_id.is_a?(Integer)
|
||||
category_or_category_id
|
||||
else
|
||||
category_or_category_id.id
|
||||
end
|
||||
|
||||
# Figure out most recent posters in the category
|
||||
most_recent_user_ids = exec_sql "
|
||||
SELECT x.user_id
|
||||
FROM (
|
||||
SELECT DISTINCT ON (p.user_id) p.user_id AS user_id,
|
||||
p.created_at AS created_at
|
||||
FROM posts AS p
|
||||
INNER JOIN topics AS ft ON ft.id = p.topic_id
|
||||
WHERE ft.category_id = :category_id
|
||||
AND p.user_id IS NOT NULL
|
||||
ORDER BY p.user_id, p.created_at DESC
|
||||
) AS x
|
||||
ORDER BY x.created_at DESC
|
||||
LIMIT :max_featured_users;
|
||||
", category_id: category_id, max_featured_users: max_featured_users
|
||||
|
||||
user_ids = most_recent_user_ids.map { |uc| uc['user_id'].to_i }
|
||||
current = CategoryFeaturedUser.where(category_id: category_id).order(:id).pluck(:user_id)
|
||||
|
||||
return if current == user_ids
|
||||
|
||||
transaction do
|
||||
CategoryFeaturedUser.where(category_id: category_id).delete_all
|
||||
|
||||
user_ids.each do |user_id|
|
||||
create(category_id: category_id, user_id: user_id)
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
# == Schema Information
|
||||
#
|
||||
# Table name: category_featured_users
|
||||
#
|
||||
# id :integer not null, primary key
|
||||
# category_id :integer
|
||||
# user_id :integer
|
||||
# created_at :datetime not null
|
||||
# updated_at :datetime not null
|
||||
#
|
||||
# Indexes
|
||||
#
|
||||
# index_category_featured_users_on_category_id_and_user_id (category_id,user_id) UNIQUE
|
||||
#
|
|
@ -0,0 +1,10 @@
|
|||
class DropUnusedTables < ActiveRecord::Migration[5.1]
|
||||
def up
|
||||
drop_table :category_featured_users
|
||||
drop_table :versions
|
||||
end
|
||||
|
||||
def down
|
||||
raise ActiveRecord::IrreversibleMigration
|
||||
end
|
||||
end
|
|
@ -1,25 +0,0 @@
|
|||
require 'rails_helper'
|
||||
|
||||
describe CategoryFeaturedUser do
|
||||
|
||||
it { is_expected.to belong_to :category }
|
||||
it { is_expected.to belong_to :user }
|
||||
|
||||
context 'featuring users' do
|
||||
|
||||
before do
|
||||
@category = Fabricate(:category)
|
||||
CategoryFeaturedUser.feature_users_in(@category)
|
||||
end
|
||||
|
||||
it 'has a featured user' do
|
||||
expect(CategoryFeaturedUser.count).to be(1)
|
||||
end
|
||||
|
||||
it 'returns the user via the category association' do
|
||||
expect(@category.featured_users).to be_present
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
File diff suppressed because one or more lines are too long
Loading…
Reference in New Issue