export_domain_allows_controller.rb 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. # frozen_string_literal: true
  2. require 'csv'
  3. module Admin
  4. class ExportDomainAllowsController < BaseController
  5. include AdminExportControllerConcern
  6. before_action :set_dummy_import!, only: [:new]
  7. def new
  8. authorize :domain_allow, :create?
  9. end
  10. def export
  11. authorize :instance, :index?
  12. send_export_file
  13. end
  14. def import
  15. authorize :domain_allow, :create?
  16. begin
  17. @import = Admin::Import.new(import_params)
  18. return render :new unless @import.validate
  19. @import.csv_rows.each do |row|
  20. domain = row['#domain'].strip
  21. next if DomainAllow.allowed?(domain)
  22. domain_allow = DomainAllow.new(domain: domain)
  23. log_action :create, domain_allow if domain_allow.save
  24. end
  25. flash[:notice] = I18n.t('admin.domain_allows.created_msg')
  26. rescue ActionController::ParameterMissing
  27. flash[:error] = I18n.t('admin.export_domain_allows.no_file')
  28. end
  29. redirect_to admin_instances_path
  30. end
  31. private
  32. def export_filename
  33. 'domain_allows.csv'
  34. end
  35. def export_headers
  36. %w(#domain)
  37. end
  38. def export_data
  39. CSV.generate(headers: export_headers, write_headers: true) do |content|
  40. DomainAllow.allowed_domains.each do |instance|
  41. content << [instance.domain]
  42. end
  43. end
  44. end
  45. end
  46. end