Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add search and sort functions to hashtag admin UI #11829

Merged
merged 4 commits into from
Sep 16, 2019
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 3 additions & 12 deletions app/controllers/admin/tags_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@

module Admin
class TagsController < BaseController
before_action :set_tags, only: :index
before_action :set_tag, except: [:index, :batch, :approve_all, :reject_all]
before_action :set_usage_by_domain, except: [:index, :batch, :approve_all, :reject_all]
before_action :set_counters, except: [:index, :batch, :approve_all, :reject_all]

def index
authorize :tag, :index?

@tags = filtered_tags.page(params[:page])
@form = Form::TagBatch.new
end

Expand Down Expand Up @@ -48,10 +48,6 @@ def update

private

def set_tags
@tags = filtered_tags.page(params[:page])
end

def set_tag
@tag = Tag.find(params[:id])
end
Expand All @@ -73,16 +69,11 @@ def set_counters
end

def filtered_tags
scope = Tag
scope = scope.discoverable if filter_params[:context] == 'directory'
scope = scope.unreviewed if filter_params[:review] == 'unreviewed'
scope = scope.reviewed.order(reviewed_at: :desc) if filter_params[:review] == 'reviewed'
scope = scope.pending_review.order(requested_review_at: :desc) if filter_params[:review] == 'pending_review'
scope.order(max_score: :desc)
TagFilter.new(filter_params).results
end

def filter_params
params.slice(:context, :review, :page).permit(:context, :review, :page)
params.slice(:context, :review, :page, :name, :order).permit(:context, :review, :page, :name, :order)
end

def tag_params
Expand Down
2 changes: 1 addition & 1 deletion app/helpers/admin/filter_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ module Admin::FilterHelper
REPORT_FILTERS = %i(resolved account_id target_account_id).freeze
INVITE_FILTER = %i(available expired).freeze
CUSTOM_EMOJI_FILTERS = %i(local remote by_domain shortcode).freeze
TAGS_FILTERS = %i(context review).freeze
TAGS_FILTERS = %i(context review name order).freeze
INSTANCES_FILTERS = %i(limited by_domain).freeze
FOLLOWERS_FILTERS = %i(relationship status by_domain activity order).freeze

Expand Down
1 change: 1 addition & 0 deletions app/models/tag.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ class Tag < ApplicationRecord
scope :listable, -> { where(listable: [true, nil]) }
scope :discoverable, -> { listable.joins(:account_tag_stat).where(AccountTagStat.arel_table[:accounts_count].gt(0)).order(Arel.sql('account_tag_stats.accounts_count desc')) }
scope :most_used, ->(account) { joins(:statuses).where(statuses: { account: account }).group(:id).order(Arel.sql('count(*) desc')) }
scope :matches_name, ->(value) { where(arel_table[:name].matches("#{value}%")) }

delegate :accounts_count,
:accounts_count=,
Expand Down
62 changes: 62 additions & 0 deletions app/models/tag_filter.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
# frozen_string_literal: true

class TagFilter
attr_reader :params

def initialize(params)
@params = params
end

def results
scope = Tag.unscoped

params.each do |key, value|
next if key.to_s == 'page'

scope.merge!(scope_for(key, value.to_s.strip)) if value.present?
end

scope.order(id: :desc)
end

private

def scope_for(key, value)
case key.to_s
when 'context'
Tag.discoverable if value == 'directory'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What happens if scope.merge!(nil)?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh… thank you, I will fix it.

when 'name'
Tag.matches_name(value)
when 'order'
scope_for_order(value)
when 'review'
scope_for_review(value)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure if these method names are okay...
I'm also not sure if this is the best way to call these methods.
Is there a better idea?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems OK.

else
raise "Unknown filter: #{key}"
end
end

def scope_for_order(order)
case order
when 'popular'
Tag.order('max_score DESC NULLS LAST')
when 'active'
Tag.order('last_status_at DESC NULLS LAST')
else
raise "Unknown filter: #{order}"
end
end

def scope_for_review(review)
case review
when 'reviewed'
Tag.reviewed.order(reviewed_at: :desc)
when 'unreviewed'
Tag.unreviewed
when 'pending_review'
Tag.pending_review.order(requested_review_at: :desc)
else
raise "Unknown filter: #{review}"
end
end
end
20 changes: 20 additions & 0 deletions app/views/admin/tags/index.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,26 @@
%li= filter_link_to t('admin.tags.reviewed'), review: 'reviewed'
%li= filter_link_to safe_join([t('admin.accounts.moderation.pending'), "(#{Tag.pending_review.count})"], ' '), review: 'pending_review'

.filter-subset
%strong= t('generic.order_by')
%ul
%li= filter_link_to t('admin.tags.most_recent'), order: nil
%li= filter_link_to t('admin.tags.most_popular'), order: 'popular'
%li= filter_link_to t('admin.tags.last_active'), order: 'active'

= form_tag admin_tags_url, method: 'GET', class: 'simple_form' do
.fields-group
- Admin::FilterHelper::TAGS_FILTERS.each do |key|
= hidden_field_tag key, params[key] if params[key].present?

- %i(name).each do |key|
.input.string.optional
= text_field_tag key, params[key], class: 'string optional', placeholder: I18n.t("admin.tags.#{key}")

.actions
%button= t('admin.accounts.search')
= link_to t('admin.accounts.reset'), admin_tags_path, class: 'button negative'
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These borrowed from existing translations.
Should I have these newly prepared?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's fine, we do that for other views with the same buttons.


%hr.spacer/

= form_for(@form, url: batch_admin_tags_path) do |f|
Expand Down
4 changes: 4 additions & 0 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -521,6 +521,10 @@ en:
context: Context
directory: In directory
in_directory: "%{count} in directory"
last_active: Last active
most_popular: Most popular
most_recent: Most recent
name: Hashtag
review: Review status
reviewed: Reviewed
title: Hashtags
Expand Down
2 changes: 2 additions & 0 deletions config/locales/simple_form.en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,8 @@ en:
must_be_follower: Block notifications from non-followers
must_be_following: Block notifications from people you don't follow
must_be_following_dm: Block direct messages from people you don't follow
invite:
comment: Comment
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This translation has nothing to do with this PR.
However, this is necessary for translating invitation link comments.
I would like to include this translation in this PR if possible.

invite_request:
text: Why do you want to join?
notification_emails:
Expand Down