domain_block_batch.rb 822 B

1234567891011121314151617181920212223242526272829303132333435
  1. # frozen_string_literal: true
  2. class Form::DomainBlockBatch
  3. include ActiveModel::Model
  4. include Authorization
  5. include AccountableConcern
  6. attr_accessor :domain_blocks_attributes, :action, :current_account
  7. def save
  8. case action
  9. when 'save'
  10. save!
  11. end
  12. end
  13. private
  14. def domain_blocks
  15. @domain_blocks ||= domain_blocks_attributes.values.filter_map do |attributes|
  16. DomainBlock.new(attributes.without('enabled')) if ActiveModel::Type::Boolean.new.cast(attributes['enabled'])
  17. end
  18. end
  19. def save!
  20. domain_blocks.each do |domain_block|
  21. authorize(domain_block, :create?)
  22. next if DomainBlock.rule_for(domain_block.domain).present?
  23. domain_block.save!
  24. DomainBlockWorker.perform_async(domain_block.id)
  25. log_action :create, domain_block
  26. end
  27. end
  28. end