Minor fixes for sets pages.

This commit is contained in:
Kira 2019-08-28 19:03:13 -07:00
parent 4fe2e02fa4
commit 834d837572
5 changed files with 36 additions and 13 deletions

View File

@ -7,18 +7,18 @@ class PostSetsController < ApplicationController
def index
if !params[:post_id].blank?
if CurrentUser.is_admin?
@sets = PostSet.paginate(include: :set_entries, conditions: ["set_entries.post_id = ?", params[:post_id]], page: 1, per_page: 50)
@sets = PostSet.where_has_post(params[:post_id].to_i).paginate(params[:page], limit: 50)
else
@sets = PostSet.paginate(include: :set_entries, conditions: ["(post_sets.public = TRUE OR post_sets.user_id = ?) AND set_entries.post_id = ?", CurrentUser.id || 0, params[:post_id]], page: 1, per_page: 50)
@sets = PostSet.visible(CurrentUser.user).where_has_post(params[:post_id].to_i).paginate(params[:page], limit: 50)
end
elsif !params[:maintainer_id].blank?
if CurrentUser.is_admin?
@sets = PostSet.paginate(include: :set_maintainers, conditions: ["(set_maintainers.user_id = ? AND set_maintainers.status = 'approved')", params[:maintainer_id]], page: 1, per_page: 50)
@sets = PostSet.where_has_maintainer(params[:maintainer_id].to_i).paginate(params[:page], limit: 50)
else
@sets = PostSet.paginate(include: :set_maintainers, conditions: ["(post_sets.public = TRUE OR post_sets.user_id = ?) AND (set_maintainers.user_id = ? AND set_maintainers.status = 'approved')", CurrentUser.id || 0, params[:maintainer_id]], page: 1, per_page: 50)
@sets = PostSet.visible(CurrentUser.user).where_has_maintainer(CurrentUser.id).paginate(params[:page], limit: 50)
end
else
@sets = PostSet.search(search_params).paginate(params[:page], limit: params[:limit])
@sets = PostSet.visible(CurrentUser.user).search(search_params).paginate(params[:page], limit: params[:limit])
end
respond_with(@sets)

View File

@ -16,7 +16,7 @@ class PostSet < ApplicationRecord
where(status: 'banned')
end
end
has_many :maintainers, class_name: "User", through: :post_set_maintainers
has_many :maintainers, class_name: "User", through: :post_set_maintainers, source: :user
belongs_to_creator
user_status_counter :set_count
@ -48,7 +48,8 @@ class PostSet < ApplicationRecord
end
def self.visible(user = CurrentUser)
def self.visible(user = CurrentUser.user)
return where('is_public = true') if user.nil?
return all() if user.is_admin?
where('is_public = true OR creator_id = ?', user.id)
end
@ -272,11 +273,16 @@ class PostSet < ApplicationRecord
reorder(Arel.sql("(case post_sets.id when #{current_set_id} then 0 else 1 end), post_sets.name"))
end
def self.search(prams)
q = super
def where_has_post(post_id)
where('post_ids @> ARRAY[?]::integer[]', post_id)
end
q = q.where('is_public = true')
q = q.or(where('(is_public = false AND creator_id = ?', creator_id)) if creator_id
def where_has_maintainer(user_id)
joins(:maintainers).where('(post_set_maintainers.user_id = ? AND post_set_maintainers.status = ?) OR creator_id = ?', user_id, 'active', user_id)
end
def search(params)
q = super
if params[:creator_name].present?
user = User.find_by_name(params[:username])

View File

@ -2,7 +2,7 @@
<div id="a-index">
<h1>Changes</h1>
<p>Recent updates may not have been processed yet. The most recently processed version was <%= time_ago_in_words_tagged(PostArchive.maximum(:updated_at)) %>.</p>
<p>Recent updates may not have been processed yet. The most recently processed version was <%= time_ago_in_words_tagged(PostArchive.maximum(:updated_at) || 0) %>.</p>
<% if @post_versions.length == 0 %>
<%= render "posts/blank" %>

View File

@ -0,0 +1,9 @@
class IndexSetPosts < ActiveRecord::Migration[5.2]
def up
execute "CREATE INDEX index_post_sets_on_post_ids ON post_sets USING gin (post_ids)"
end
def down
remove_index :post_sets, :post_ids
end
end

View File

@ -4491,6 +4491,13 @@ CREATE INDEX index_post_replacements_on_creator_id ON public.post_replacements U
CREATE INDEX index_post_replacements_on_post_id ON public.post_replacements USING btree (post_id);
--
-- Name: index_post_sets_on_post_ids; Type: INDEX; Schema: public; Owner: -
--
CREATE INDEX index_post_sets_on_post_ids ON public.post_sets USING gin (post_ids);
--
-- Name: index_post_versions_on_post_id; Type: INDEX; Schema: public; Owner: -
--
@ -5247,6 +5254,7 @@ INSERT INTO "schema_migrations" (version) VALUES
('20190804010156'),
('20190810064211'),
('20190815131908'),
('20190827223818');
('20190827223818'),
('20190827233008');