Sha256: 11db4ccf2cd8b3898dfcbb7c0212dc890d1bf42cb49b072bde529046d7926b82

Contents?: true

Size: 1.06 KB

Versions: 1

Compression:

Stored size: 1.06 KB

Contents

# frozen_string_literal: true

require "csv"

class DocumentDataDictionary < ApplicationRecord
  include ActiveModel::Validations

  # Callbacks (keep at top)
  after_save :parse_csv_file

  # Associations
  has_one_attached :csv_file
  belongs_to :document, foreign_key: :friendlier_id, primary_key: :friendlier_id
  has_many :document_data_dictionary_entries, -> { order(position: :asc) }, dependent: :destroy

  # Validations
  validates :name, presence: true
  validates :csv_file, attached: true, content_type: {in: "text/csv", message: "is not a CSV file"}

  validates_with DocumentDataDictionary::CsvHeaderValidator

  def parse_csv_file
    if csv_file.attached?
      csv_data = CSV.parse(csv_file.download, headers: true)
      csv_data.each do |row|
        document_data_dictionary_entries.create!(row.to_h)
      end
    end
  end

  def self.sort_entries(id_array)
    transaction do
      logger.debug { id_array.inspect }
      id_array.each_with_index do |entry_id, i|
        DocumentDataDictionaryEntry.update(entry_id, position: i)
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
geoblacklight_admin-0.7.0 app/models/document_data_dictionary.rb