Class: Decidim::ActionDelegator::CsvImporter
- Inherits:
-
Object
- Object
- Decidim::ActionDelegator::CsvImporter
- Includes:
- FormFactory
- Defined in:
- app/services/decidim/action_delegator/csv_importer.rb
Direct Known Subclasses
Instance Method Summary collapse
- #extract_params ⇒ Object
- #generate_info_message(mismatch_fields) ⇒ Object
- #handle_form_validity(row, details_csv, import_summary, row_number) ⇒ Object
- #handle_import_error(row, details_csv, import_summary, row_number, error_messages) ⇒ Object
- #handle_skipped_row(row, details_csv, import_summary, row_number, reason) ⇒ Object
- #headers(csv, details_csv) ⇒ Object
- #import! ⇒ Object
-
#initialize(csv_file, current_user, current_setting) ⇒ CsvImporter
constructor
A new instance of CsvImporter.
- #invalid_email?(email) ⇒ Boolean
- #process(row) ⇒ Object
Constructor Details
#initialize(csv_file, current_user, current_setting) ⇒ CsvImporter
Returns a new instance of CsvImporter.
8 9 10 11 12 |
# File 'app/services/decidim/action_delegator/csv_importer.rb', line 8 def initialize(csv_file, current_user, current_setting) @csv_file = csv_file @current_user = current_user @current_setting = current_setting end |
Instance Method Details
#extract_params ⇒ Object
81 82 83 |
# File 'app/services/decidim/action_delegator/csv_importer.rb', line 81 def extract_params raise NotImplementedError end |
#generate_info_message(mismatch_fields) ⇒ Object
62 63 64 65 |
# File 'app/services/decidim/action_delegator/csv_importer.rb', line 62 def (mismatch_fields) with_mismatched_fields = mismatch_fields.present? ? I18n.t("decidim.action_delegator.participants_csv_importer.import.with_mismatched_fields", fields: mismatch_fields) : "" I18n.t("decidim.action_delegator.participants_csv_importer.import.skip_import_info", with_mismatched_fields: with_mismatched_fields) end |
#handle_form_validity(row, details_csv, import_summary, row_number) ⇒ Object
58 59 60 |
# File 'app/services/decidim/action_delegator/csv_importer.rb', line 58 def handle_form_validity(row, details_csv, import_summary, row_number) raise NotImplementedError end |
#handle_import_error(row, details_csv, import_summary, row_number, error_messages) ⇒ Object
52 53 54 55 56 |
# File 'app/services/decidim/action_delegator/csv_importer.rb', line 52 def handle_import_error(row, details_csv, import_summary, row_number, ) import_summary[:error_rows] << { row_number: row_number - 1, error_messages: } row["reason"] = details_csv << row end |
#handle_skipped_row(row, details_csv, import_summary, row_number, reason) ⇒ Object
46 47 48 49 50 |
# File 'app/services/decidim/action_delegator/csv_importer.rb', line 46 def handle_skipped_row(row, details_csv, import_summary, row_number, reason) import_summary[:skipped_rows] << { row_number: row_number - 1 } row["reason"] = reason details_csv << row end |
#headers(csv, details_csv) ⇒ Object
67 68 69 70 71 |
# File 'app/services/decidim/action_delegator/csv_importer.rb', line 67 def headers(csv, details_csv) headers = csv.first.headers headers << I18n.t("decidim.action_delegator.participants_csv_importer.import.error_field") details_csv << headers end |
#import! ⇒ Object
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'app/services/decidim/action_delegator/csv_importer.rb', line 14 def import! import_summary = { total_rows: 0, imported_rows: 0, error_rows: [], skipped_rows: [], details_csv_path: nil } details_csv_file = File.join(File.dirname(@csv_file), "details.csv") i = 1 csv = CSV.new(@csv_file, headers: true, col_sep: ",") CSV.open(details_csv_file, "wb") do |details_csv| while (row = csv.shift).present? i += 1 params = extract_params(row) next if row&.empty? handle_form_validity(row, details_csv, import_summary, i) if process(row, params, details_csv, import_summary, i) end end import_summary[:total_rows] = i - 1 import_summary[:details_csv_path] = details_csv_file import_summary end |
#invalid_email?(email) ⇒ Boolean
73 74 75 |
# File 'app/services/decidim/action_delegator/csv_importer.rb', line 73 def invalid_email?(email) email.blank? || !email.match?(::Devise.email_regexp) end |
#process(row) ⇒ Object
77 78 79 |
# File 'app/services/decidim/action_delegator/csv_importer.rb', line 77 def process(row) raise NotImplementedError end |