Refactor: Introduce admin base action class (#37960)
This commit is contained in:
parent
1e87bd178d
commit
72406a1cd1
@ -1,11 +1,6 @@
|
|||||||
# frozen_string_literal: true
|
# frozen_string_literal: true
|
||||||
|
|
||||||
class Admin::AccountAction
|
class Admin::AccountAction < Admin::BaseAction
|
||||||
include ActiveModel::Model
|
|
||||||
include ActiveModel::Attributes
|
|
||||||
include AccountableConcern
|
|
||||||
include Authorization
|
|
||||||
|
|
||||||
TYPES = %w(
|
TYPES = %w(
|
||||||
none
|
none
|
||||||
disable
|
disable
|
||||||
@ -15,49 +10,13 @@ class Admin::AccountAction
|
|||||||
).freeze
|
).freeze
|
||||||
|
|
||||||
attr_accessor :target_account,
|
attr_accessor :target_account,
|
||||||
:current_account,
|
|
||||||
:type,
|
|
||||||
:text,
|
|
||||||
:report_id,
|
|
||||||
:warning_preset_id
|
:warning_preset_id
|
||||||
|
|
||||||
attr_reader :warning
|
|
||||||
|
|
||||||
attribute :include_statuses, :boolean, default: true
|
attribute :include_statuses, :boolean, default: true
|
||||||
attribute :send_email_notification, :boolean, default: true
|
|
||||||
|
|
||||||
alias send_email_notification? send_email_notification
|
|
||||||
alias include_statuses? include_statuses
|
alias include_statuses? include_statuses
|
||||||
|
|
||||||
validates :type, :target_account, :current_account, presence: true
|
validates :target_account, presence: true
|
||||||
validates :type, inclusion: { in: TYPES }
|
|
||||||
|
|
||||||
def save
|
|
||||||
return false unless valid?
|
|
||||||
|
|
||||||
ApplicationRecord.transaction do
|
|
||||||
process_action!
|
|
||||||
process_strike!
|
|
||||||
process_reports!
|
|
||||||
end
|
|
||||||
|
|
||||||
process_notification!
|
|
||||||
process_queue!
|
|
||||||
|
|
||||||
true
|
|
||||||
end
|
|
||||||
|
|
||||||
def save!
|
|
||||||
raise ActiveRecord::RecordInvalid, self unless save
|
|
||||||
end
|
|
||||||
|
|
||||||
def report
|
|
||||||
@report ||= Report.find(report_id) if report_id.present?
|
|
||||||
end
|
|
||||||
|
|
||||||
def with_report?
|
|
||||||
!report.nil?
|
|
||||||
end
|
|
||||||
|
|
||||||
class << self
|
class << self
|
||||||
def types_for_account(account)
|
def types_for_account(account)
|
||||||
@ -84,6 +43,17 @@ class Admin::AccountAction
|
|||||||
private
|
private
|
||||||
|
|
||||||
def process_action!
|
def process_action!
|
||||||
|
ApplicationRecord.transaction do
|
||||||
|
handle_type!
|
||||||
|
process_strike!
|
||||||
|
process_reports!
|
||||||
|
end
|
||||||
|
|
||||||
|
process_notification!
|
||||||
|
process_queue!
|
||||||
|
end
|
||||||
|
|
||||||
|
def handle_type!
|
||||||
case type
|
case type
|
||||||
when 'disable'
|
when 'disable'
|
||||||
handle_disable!
|
handle_disable!
|
||||||
|
|||||||
42
app/models/admin/base_action.rb
Normal file
42
app/models/admin/base_action.rb
Normal file
@ -0,0 +1,42 @@
|
|||||||
|
# frozen_string_literal: true
|
||||||
|
|
||||||
|
class Admin::BaseAction
|
||||||
|
include ActiveModel::Model
|
||||||
|
include ActiveModel::Attributes
|
||||||
|
include AccountableConcern
|
||||||
|
include Authorization
|
||||||
|
|
||||||
|
attr_accessor :current_account,
|
||||||
|
:type,
|
||||||
|
:text,
|
||||||
|
:report_id
|
||||||
|
|
||||||
|
attr_reader :warning
|
||||||
|
|
||||||
|
attribute :send_email_notification, :boolean, default: true
|
||||||
|
|
||||||
|
alias send_email_notification? send_email_notification
|
||||||
|
|
||||||
|
validates :type, :current_account, presence: true
|
||||||
|
validates :type, inclusion: { in: ->(a) { a.class::TYPES } }
|
||||||
|
|
||||||
|
def save
|
||||||
|
return false unless valid?
|
||||||
|
|
||||||
|
process_action!
|
||||||
|
|
||||||
|
true
|
||||||
|
end
|
||||||
|
|
||||||
|
def save!
|
||||||
|
raise ActiveRecord::RecordInvalid, self unless save
|
||||||
|
end
|
||||||
|
|
||||||
|
def report
|
||||||
|
@report ||= Report.find(report_id) if report_id.present?
|
||||||
|
end
|
||||||
|
|
||||||
|
def with_report?
|
||||||
|
!report.nil?
|
||||||
|
end
|
||||||
|
end
|
||||||
@ -1,20 +1,14 @@
|
|||||||
# frozen_string_literal: true
|
# frozen_string_literal: true
|
||||||
|
|
||||||
class Admin::StatusBatchAction
|
class Admin::StatusBatchAction < Admin::BaseAction
|
||||||
include ActiveModel::Model
|
TYPES = %w(
|
||||||
include ActiveModel::Attributes
|
delete
|
||||||
include AccountableConcern
|
mark_as_sensitive
|
||||||
include Authorization
|
report
|
||||||
|
remove_from_report
|
||||||
|
).freeze
|
||||||
|
|
||||||
attr_accessor :current_account, :type,
|
attr_accessor :status_ids
|
||||||
:status_ids, :report_id,
|
|
||||||
:text
|
|
||||||
|
|
||||||
attribute :send_email_notification, :boolean
|
|
||||||
|
|
||||||
def save!
|
|
||||||
process_action!
|
|
||||||
end
|
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
@ -117,14 +111,6 @@ class Admin::StatusBatchAction
|
|||||||
report.save!
|
report.save!
|
||||||
end
|
end
|
||||||
|
|
||||||
def report
|
|
||||||
@report ||= Report.find(report_id) if report_id.present?
|
|
||||||
end
|
|
||||||
|
|
||||||
def with_report?
|
|
||||||
!report.nil?
|
|
||||||
end
|
|
||||||
|
|
||||||
def process_notification!
|
def process_notification!
|
||||||
return unless warnable?
|
return unless warnable?
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user