-
-
Notifications
You must be signed in to change notification settings - Fork 6.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
cba2897
commit fc66e86
Showing
30 changed files
with
590 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
# frozen_string_literal: true | ||
|
||
module Admin | ||
class TextBlocksController < BaseController | ||
before_action :set_new_text_block, only: [:index, :create] | ||
before_action :find_and_set_text_block, only: [:show, :update, :destroy] | ||
before_action :authorize_text_block | ||
|
||
def index | ||
page = params[:page] | ||
@text_block = TextBlock.new if page.nil? | ||
@text_blocks = TextBlock.recent.page(page) | ||
end | ||
|
||
def show; end | ||
|
||
def create | ||
if @text_block.save | ||
log_action :create, @text_block | ||
redirect_to({ action: :index }, notice: I18n.t('admin.text_blocks.created')) | ||
else | ||
@text_blocks = TextBlock.page(0) | ||
flash.now[:alert] = @text_block.errors.full_messages.first | ||
render :index | ||
end | ||
end | ||
|
||
def update | ||
if @text_block.update text_block_params | ||
log_action :update, @text_block | ||
redirect_to({ action: :index }, notice: I18n.t('admin.text_blocks.updated')) | ||
else | ||
flash.now[:alert] = @text_block.errors.full_messages.first | ||
render :show | ||
end | ||
end | ||
|
||
def destroy | ||
if @text_block.destroy | ||
log_action :destroy, @text_block | ||
flash[:notice] = I18n.t('admin.text_blocks.destroyed') | ||
else | ||
flash[:alert] = @text_block.errors.full_messages.first | ||
end | ||
|
||
redirect_to action: :index | ||
end | ||
|
||
private | ||
|
||
def authorize_text_block | ||
authorize @text_block | ||
end | ||
|
||
def set_new_text_block | ||
@text_block = TextBlock.new(text_block_params) | ||
end | ||
|
||
def find_and_set_text_block | ||
@text_block = TextBlock.find(params.require(:id)) | ||
end | ||
|
||
def text_block_params | ||
params[:text_block]&.permit(:text, :severity) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
# frozen_string_literal: true | ||
# == Schema Information | ||
# | ||
# Table name: text_blocks | ||
# | ||
# id :integer not null, primary key | ||
# text :string not null | ||
# severity :integer not null | ||
# created_at :datetime not null | ||
# updated_at :datetime not null | ||
# | ||
|
||
class TextBlock < ApplicationRecord | ||
after_commit :uncache | ||
|
||
enum severity: [:silence, :reject] | ||
|
||
scope :recent, -> { reorder(id: :desc) } | ||
|
||
def self.silence?(object) | ||
(object.is_a?(Status) && (silence?(object.account) || object.media_attachments.any? { |attachment| silence? attachment })) || | ||
[:description, :display_name, :name, :note, :spoiler_text, :text].any? do |key| | ||
object.respond_to?(key) && silenced_texts.any? do |text| | ||
object.public_send(key)&.include? text | ||
end | ||
end | ||
end | ||
|
||
def self.rejected_texts | ||
Rails.cache.fetch :rejected_texts do | ||
where(severity: :reject).pluck(:text) | ||
end | ||
end | ||
|
||
def self.silenced_texts | ||
Rails.cache.fetch :silenced_texts do | ||
where(severity: :silence).pluck(:text) | ||
end | ||
end | ||
|
||
private | ||
|
||
def uncache | ||
Rails.cache.delete :rejected_texts | ||
Rails.cache.delete :silenced_texts | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
# frozen_string_literal: true | ||
|
||
class TextBlockPolicy < ApplicationPolicy | ||
def index? | ||
true | ||
end | ||
|
||
def show? | ||
true | ||
end | ||
|
||
def create? | ||
admin? | ||
end | ||
|
||
def update? | ||
admin? | ||
end | ||
|
||
def destroy? | ||
admin? | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
# frozen_string_literal: true | ||
|
||
class TextBlocksValidator < ActiveModel::EachValidator | ||
def validate_each(record, attribute, value) | ||
return if value.nil? | ||
|
||
rejected = TextBlock.rejected_texts.find { |text| value.include? text } | ||
record.errors.add attribute, I18n.t('rejected_text', text: rejected) if rejected.present? | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
= simple_form_for text_block, url: text_block.persisted? ? admin_text_block_path(text_block) : admin_text_blocks_path do |f| | ||
= render 'shared/error_messages', object: text_block | ||
|
||
= f.input :text, placeholder: t('admin.text_blocks.text'), disabled: text_block.persisted?, readonly: text_block.persisted?, required: true | ||
|
||
= f.input :severity, collection: TextBlock.severities.keys, wrapper: :with_label, include_blank: false, label_method: ->(type) { t(".severities.#{type}") } | ||
|
||
%p.hint= t('admin.text_blocks.severity.desc_html') | ||
|
||
.actions | ||
= f.button :button, text_block.persisted? ? t('admin.text_blocks.save') : t('admin.text_blocks.create') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
- content_for :page_title do | ||
= t('admin.text_blocks.title') | ||
|
||
.table-wrapper | ||
%table.table | ||
%thead | ||
%tr | ||
%th= t('admin.text_blocks.text') | ||
%th= t('admin.text_blocks.severity.title') | ||
%th | ||
%th | ||
%tbody | ||
- @text_blocks.each do |text_block| | ||
%tr | ||
%td= link_to text_block.text, admin_text_block_path(text_block) | ||
%td= t("admin.text_blocks.severities.#{text_block.severity}") | ||
%td | ||
= table_link_to 'trash', t('admin.text_blocks.destroy'), admin_text_block_path(text_block), method: :delete | ||
|
||
= paginate @text_blocks | ||
|
||
- if @text_block.present? | ||
%hr | ||
|
||
%h3= t('admin.text_blocks.new') | ||
|
||
= render @text_block |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
- content_for :page_title do | ||
= t('.title') | ||
|
||
= render @text_block | ||
|
||
%hr | ||
|
||
%h3= t('admin.text_blocks.deletion') | ||
|
||
.simple_form | ||
= link_to t('admin.text_blocks.destroy'), admin_text_block_path(@text_block), class: 'block-button', method: :delete |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
class CreateTextBlocks < ActiveRecord::Migration[5.1] | ||
def change | ||
create_table :text_blocks do |t| | ||
t.string :text, index: { unique: true }, null: false | ||
t.integer :severity, null: false | ||
t.timestamps | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.