Improve admin UI for custom emojis, add copy/disable/enable (#5231)

This commit is contained in:
Eugen Rochko
2017-10-05 23:42:05 +02:00
committed by GitHub
parent b9c76e2edb
commit 49cc0eb3e7
13 changed files with 330 additions and 15 deletions

View File

@ -2,8 +2,10 @@
module Admin
class CustomEmojisController < BaseController
before_action :set_custom_emoji, except: [:index, :new, :create]
def index
@custom_emojis = CustomEmoji.local
@custom_emojis = filtered_custom_emojis.page(params[:page])
end
def new
@ -21,14 +23,50 @@ module Admin
end
def destroy
CustomEmoji.find(params[:id]).destroy
@custom_emoji.destroy
redirect_to admin_custom_emojis_path, notice: I18n.t('admin.custom_emojis.destroyed_msg')
end
def copy
emoji = @custom_emoji.dup
emoji.domain = nil
if emoji.save
redirect_to admin_custom_emojis_path, notice: I18n.t('admin.custom_emojis.copied_msg')
else
redirect_to admin_custom_emojis_path, alert: I18n.t('admin.custom_emojis.copy_failed_msg')
end
end
def enable
@custom_emoji.update!(disabled: false)
redirect_to admin_custom_emojis_path, notice: I18n.t('admin.custom_emojis.enabled_msg')
end
def disable
@custom_emoji.update!(disabled: true)
redirect_to admin_custom_emojis_path, notice: I18n.t('admin.custom_emojis.disabled_msg')
end
private
def set_custom_emoji
@custom_emoji = CustomEmoji.find(params[:id])
end
def resource_params
params.require(:custom_emoji).permit(:shortcode, :image)
end
def filtered_custom_emojis
CustomEmojiFilter.new(filter_params).results
end
def filter_params
params.permit(
:local,
:remote
)
end
end
end

View File

@ -4,6 +4,6 @@ class Api::V1::CustomEmojisController < Api::BaseController
respond_to :json
def index
render json: CustomEmoji.local, each_serializer: REST::CustomEmojiSerializer
render json: CustomEmoji.local.where(disabled: false), each_serializer: REST::CustomEmojiSerializer
end
end

View File

@ -9,9 +9,11 @@ class AccountFilter
def results
scope = Account.alphabetic
params.each do |key, value|
scope.merge!(scope_for(key, value)) if value.present?
end
scope
end

View File

@ -12,6 +12,7 @@
# image_updated_at :datetime
# created_at :datetime not null
# updated_at :datetime not null
# disabled :boolean default(FALSE), not null
#
class CustomEmoji < ApplicationRecord
@ -26,10 +27,16 @@ class CustomEmoji < ApplicationRecord
validates_attachment :image, content_type: { content_type: 'image/png' }, presence: true, size: { in: 0..50.kilobytes }
validates :shortcode, uniqueness: { scope: :domain }, format: { with: /\A#{SHORTCODE_RE_FRAGMENT}\z/ }, length: { minimum: 2 }
scope :local, -> { where(domain: nil) }
scope :local, -> { where(domain: nil) }
scope :remote, -> { where.not(domain: nil) }
scope :alphabetic, -> { order(domain: :asc, shortcode: :asc) }
include Remotable
def local?
domain.nil?
end
class << self
def from_text(text, domain)
return [] if text.blank?
@ -38,7 +45,7 @@ class CustomEmoji < ApplicationRecord
return [] if shortcodes.empty?
where(shortcode: shortcodes, domain: domain)
where(shortcode: shortcodes, domain: domain, disabled: false)
end
end
end

View File

@ -0,0 +1,34 @@
# frozen_string_literal: true
class CustomEmojiFilter
attr_reader :params
def initialize(params)
@params = params
end
def results
scope = CustomEmoji.alphabetic
params.each do |key, value|
scope.merge!(scope_for(key, value)) if value.present?
end
scope
end
private
def scope_for(key, value)
case key.to_s
when 'local'
CustomEmoji.local
when 'remote'
CustomEmoji.remote
when 'by_domain'
CustomEmoji.where(domain: value)
else
raise "Unknown filter: #{key}"
end
end
end

View File

@ -3,5 +3,18 @@
= image_tag custom_emoji.image.url, class: 'emojione', alt: ":#{custom_emoji.shortcode}:"
%td
%samp= ":#{custom_emoji.shortcode}:"
%td
- if custom_emoji.local?
= t('admin.accounts.location.local')
- else
= custom_emoji.domain
%td
- unless custom_emoji.local?
= table_link_to 'copy', t('admin.custom_emojis.copy'), copy_admin_custom_emoji_path(custom_emoji), method: :post
%td
- if custom_emoji.disabled?
= table_link_to 'power-off', t('admin.custom_emojis.enable'), enable_admin_custom_emoji_path(custom_emoji), method: :post, data: { confirm: t('admin.accounts.are_you_sure') }
- else
= table_link_to 'power-off', t('admin.custom_emojis.disable'), disable_admin_custom_emoji_path(custom_emoji), method: :post, data: { confirm: t('admin.accounts.are_you_sure') }
%td
= table_link_to 'times', t('admin.custom_emojis.delete'), admin_custom_emoji_path(custom_emoji), method: :delete, data: { confirm: t('admin.accounts.are_you_sure') }

View File

@ -1,14 +1,34 @@
- content_for :page_title do
= t('admin.custom_emojis.title')
.filters
.filter-subset
%strong= t('admin.accounts.location.title')
%ul
%li= filter_link_to t('admin.accounts.location.all'), local: nil, remote: nil
%li
- if selected? local: '1', remote: nil
= filter_link_to t('admin.accounts.location.local'), {local: nil, remote: nil}, {local: '1', remote: nil}
- else
= filter_link_to t('admin.accounts.location.local'), local: '1', remote: nil
%li
- if selected? remote: '1', local: nil
= filter_link_to t('admin.accounts.location.remote'), {remote: nil, local: nil}, {remote: '1', local: nil}
- else
= filter_link_to t('admin.accounts.location.remote'), remote: '1', local: nil
.table-wrapper
%table.table
%thead
%tr
%th= t('admin.custom_emojis.emoji')
%th= t('admin.custom_emojis.shortcode')
%th= t('admin.accounts.domain')
%th
%th
%th
%tbody
= render @custom_emojis
= paginate @custom_emojis
= link_to t('admin.custom_emojis.upload'), new_admin_custom_emoji_path, class: 'button'