about summary refs log tree commit diff
path: root/app/models/form/domain_block_batch.rb
diff options
context:
space:
mode:
authorClaire <claire.github-309c@sitedethib.com>2022-05-16 18:26:49 +0200
committerGitHub <noreply@github.com>2022-05-16 18:26:49 +0200
commitb91196f4b73fff91997b8077619ae25b6d04a59e (patch)
tree9d52a2fbed1170b5180540878ff4fa9292ad3ff1 /app/models/form/domain_block_batch.rb
parent3a084113067656ef9318b9fb5bcfea4fd2de6ffe (diff)
Add confirmation page when importing blocked domains (#1773)
* Move glitch-soc-specific strings to glitch-soc-specific locale files

* Add confirmation page when importing blocked domains
Diffstat (limited to 'app/models/form/domain_block_batch.rb')
-rw-r--r--app/models/form/domain_block_batch.rb35
1 files changed, 35 insertions, 0 deletions
diff --git a/app/models/form/domain_block_batch.rb b/app/models/form/domain_block_batch.rb
new file mode 100644
index 000000000..39012df51
--- /dev/null
+++ b/app/models/form/domain_block_batch.rb
@@ -0,0 +1,35 @@
+# frozen_string_literal: true
+
+class Form::DomainBlockBatch
+  include ActiveModel::Model
+  include Authorization
+  include AccountableConcern
+
+  attr_accessor :domain_blocks_attributes, :action, :current_account
+
+  def save
+    case action
+    when 'save'
+      save!
+    end
+  end
+
+  private
+
+  def domain_blocks
+    @domain_blocks ||= domain_blocks_attributes.values.filter_map do |attributes|
+      DomainBlock.new(attributes.without('enabled')) if ActiveModel::Type::Boolean.new.cast(attributes['enabled'])
+    end
+  end
+
+  def save!
+    domain_blocks.each do |domain_block|
+      authorize(domain_block, :create?)
+      next if DomainBlock.rule_for(domain_block.domain).present?
+
+      domain_block.save!
+      DomainBlockWorker.perform_async(domain_block.id)
+      log_action :create, domain_block
+    end
+  end
+end