Add server rules (#15769)
parent
dcc7c686f3
commit
8331fdf7e0
@ -0,0 +1,59 @@ |
||||
# frozen_string_literal: true |
||||
|
||||
module Admin |
||||
class RulesController < BaseController |
||||
before_action :set_rule, except: [:index, :create] |
||||
|
||||
def index |
||||
authorize :rule, :index? |
||||
|
||||
@rules = Rule.ordered |
||||
@rule = Rule.new |
||||
end |
||||
|
||||
def create |
||||
authorize :rule, :create? |
||||
|
||||
@rule = Rule.new(resource_params) |
||||
|
||||
if @rule.save |
||||
redirect_to admin_rules_path |
||||
else |
||||
@rules = Rule.ordered |
||||
render :index |
||||
end |
||||
end |
||||
|
||||
def edit |
||||
authorize @rule, :update? |
||||
end |
||||
|
||||
def update |
||||
authorize @rule, :update? |
||||
|
||||
if @rule.update(resource_params) |
||||
redirect_to admin_rules_path |
||||
else |
||||
render :edit |
||||
end |
||||
end |
||||
|
||||
def destroy |
||||
authorize @rule, :destroy? |
||||
|
||||
@rule.discard |
||||
|
||||
redirect_to admin_rules_path |
||||
end |
||||
|
||||
private |
||||
|
||||
def set_rule |
||||
@rule = Rule.find(params[:id]) |
||||
end |
||||
|
||||
def resource_params |
||||
params.require(:rule).permit(:text, :priority) |
||||
end |
||||
end |
||||
end |
@ -0,0 +1,17 @@ |
||||
# frozen_string_literal: true |
||||
|
||||
class Api::V1::Instances::RulesController < Api::BaseController |
||||
skip_before_action :require_authenticated_user!, unless: :whitelist_mode? |
||||
|
||||
before_action :set_rules |
||||
|
||||
def index |
||||
render json: @rules, each_serializer: REST::RuleSerializer |
||||
end |
||||
|
||||
private |
||||
|
||||
def set_rules |
||||
@rules = Rule.ordered |
||||
end |
||||
end |
@ -0,0 +1,22 @@ |
||||
# frozen_string_literal: true |
||||
|
||||
# == Schema Information |
||||
# |
||||
# Table name: rules |
||||
# |
||||
# id :bigint(8) not null, primary key |
||||
# priority :integer default(0), not null |
||||
# deleted_at :datetime |
||||
# text :text default(""), not null |
||||
# created_at :datetime not null |
||||
# updated_at :datetime not null |
||||
# |
||||
class Rule < ApplicationRecord |
||||
include Discard::Model |
||||
|
||||
self.discard_column = :deleted_at |
||||
|
||||
validates :text, presence: true, length: { maximum: 300 } |
||||
|
||||
scope :ordered, -> { kept.order(priority: :asc) } |
||||
end |
@ -0,0 +1,19 @@ |
||||
# frozen_string_literal: true |
||||
|
||||
class RulePolicy < ApplicationPolicy |
||||
def index? |
||||
staff? |
||||
end |
||||
|
||||
def create? |
||||
admin? |
||||
end |
||||
|
||||
def update? |
||||
admin? |
||||
end |
||||
|
||||
def destroy? |
||||
admin? |
||||
end |
||||
end |
@ -0,0 +1,9 @@ |
||||
# frozen_string_literal: true |
||||
|
||||
class REST::RuleSerializer < ActiveModel::Serializer |
||||
attributes :id, :text |
||||
|
||||
def id |
||||
object.id.to_s |
||||
end |
||||
end |
@ -0,0 +1,11 @@ |
||||
.announcements-list__item |
||||
= link_to edit_admin_rule_path(rule), class: 'announcements-list__item__title' do |
||||
= "#{rule_counter + 1}." |
||||
= truncate(rule.text) |
||||
|
||||
.announcements-list__item__action-bar |
||||
.announcements-list__item__meta |
||||
= rule.text |
||||
|
||||
%div |
||||
= table_link_to 'trash', t('admin.rules.delete'), admin_rule_path(rule), method: :delete, data: { confirm: t('admin.accounts.are_you_sure') } if can?(:destroy, rule) |
@ -0,0 +1,11 @@ |
||||
- content_for :page_title do |
||||
= t('admin.rules.edit') |
||||
|
||||
= simple_form_for @rule, url: admin_rule_path(@rule) do |f| |
||||
= render 'shared/error_messages', object: @rule |
||||
|
||||
.fields-group |
||||
= f.input :text, wrapper: :with_block_label |
||||
|
||||
.actions |
||||
= f.button :button, t('generic.save_changes'), type: :submit |
@ -0,0 +1,24 @@ |
||||
- content_for :page_title do |
||||
= t('admin.rules.title') |
||||
|
||||
.simple_form |
||||
%p.hint= t('admin.rules.description') |
||||
|
||||
- if can? :create, :rule |
||||
= simple_form_for @rule, url: admin_rules_path do |f| |
||||
= render 'shared/error_messages', object: @rule |
||||
|
||||
.fields-group |
||||
= f.input :text, wrapper: :with_block_label |
||||
|
||||
.actions |
||||
= f.button :button, t('admin.rules.add_new'), type: :submit |
||||
|
||||
%hr.spacer/ |
||||
|
||||
- if @rules.empty? |
||||
%div.muted-hint.center-text |
||||
= t 'admin.rules.empty' |
||||
- else |
||||
.announcements-list |
||||
= render partial: 'rule', collection: @rules |
@ -0,0 +1,11 @@ |
||||
class CreateRules < ActiveRecord::Migration[5.2] |
||||
def change |
||||
create_table :rules do |t| |
||||
t.integer :priority, null: false, default: 0 |
||||
t.datetime :deleted_at |
||||
t.text :text, null: false, default: '' |
||||
|
||||
t.timestamps |
||||
end |
||||
end |
||||
end |
@ -0,0 +1,5 @@ |
||||
Fabricator(:rule) do |
||||
priority "" |
||||
deleted_at "2021-02-21 05:51:09" |
||||
text "MyText" |
||||
end |
@ -0,0 +1,5 @@ |
||||
require 'rails_helper' |
||||
|
||||
RSpec.describe Rule, type: :model do |
||||
pending "add some examples to (or delete) #{__FILE__}" |
||||
end |
Loading…
Reference in new issue