# frozen_string_literal: true module CsvExportable extend ActiveSupport::Concern COMMON_HEADERS = %i[approved notes tags signed_at].freeze COMMON_VALUES = %w[approved? clean_notes clean_tags signed_on].freeze included do class << self def custom_csv_exportable_headers [] end def csv_headers headers = custom_csv_exportable_headers + COMMON_HEADERS headers.map do |header| I18n.t("#{model_name.plural}.index.table_headers.#{header}") end end end def to_csv_row (self.class.custom_csv_exportable_headers + COMMON_VALUES).map do |function| send(function) end end private def owner_info compact_contact_info(name: person_name, address: person_address, phone: person_phone, email: person_email) end def contact_info owner_info end def compact_contact_info(name: nil, address: nil, phone: nil, email: nil) contact_info = '' contact_info += "#{name}; " if name.present? contact_info += "#{address}; " if address.present? contact_info += "P: #{phone}; " if phone.present? contact_info += "E: #{email}" if email.present? contact_info.delete_suffix '; ' end def clean_notes notes = '' self.notes.order_by_recent.each do |note| notes += "#{note.content}(#{note.email}), " end notes.delete_suffix ', ' end def clean_tags tags = '' self.tags.each do |tag| tags += "#{tag.name}, " end tags.delete_suffix ', ' end end end