Sha256: 493d01bbdb7362854c2821619e0e3000c725c4cbaff21a23408d053c6db7c924

Contents?: true

Size: 951 Bytes

Versions: 9

Compression:

Stored size: 951 Bytes

Contents

# frozen_string_literal: true

require "csv"

module Decidim
  module Admin
    # A form object used to upload CSV to batch participatory space private users.
    #
    class ParticipatorySpacePrivateUserCsvImportForm < Form
      include Decidim::HasUploadValidations
      include Decidim::ProcessesFileLocally

      attribute :file, Decidim::Attributes::Blob
      attribute :user_name, String
      attribute :email, String

      validates :file, presence: true, file_content_type: { allow: ["text/csv"] }
      validate :validate_csv

      def validate_csv
        return if file.blank?

        process_file_locally(file) do |file_path|
          CSV.foreach(file_path, encoding: "BOM|UTF-8") do |_email, user_name|
            errors.add(:user_name, :invalid) unless user_name.match?(UserBaseEntity::REGEXP_NAME)
          end
        end
      rescue CSV::MalformedCSVError
        errors.add(:file, :malformed)
      end
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
decidim-admin-0.27.9 app/forms/decidim/admin/participatory_space_private_user_csv_import_form.rb
decidim-admin-0.27.8 app/forms/decidim/admin/participatory_space_private_user_csv_import_form.rb
decidim-admin-0.27.7 app/forms/decidim/admin/participatory_space_private_user_csv_import_form.rb
decidim-admin-0.27.6 app/forms/decidim/admin/participatory_space_private_user_csv_import_form.rb
decidim-admin-0.27.5 app/forms/decidim/admin/participatory_space_private_user_csv_import_form.rb
decidim-admin-0.27.4 app/forms/decidim/admin/participatory_space_private_user_csv_import_form.rb
decidim-admin-0.27.3 app/forms/decidim/admin/participatory_space_private_user_csv_import_form.rb
decidim-admin-0.27.2 app/forms/decidim/admin/participatory_space_private_user_csv_import_form.rb
decidim-admin-0.27.1 app/forms/decidim/admin/participatory_space_private_user_csv_import_form.rb