Sha256: cd0b1e2e0224e18742839928c90e4f813f54a7716ac47323d792c0542991965b

Contents?: true

Size: 949 Bytes

Versions: 4

Compression:

Stored size: 949 Bytes

Contents

# frozen_string_literal: true

require "csv"

# DocumentAccess
class DocumentAccess < ApplicationRecord
  belongs_to :document, foreign_key: :friendlier_id, primary_key: :friendlier_id
  after_save :reindex_document

  # Validations
  validates :institution_code, :access_url, presence: true

  def self.import(file)
    logger.debug("CSV Import")
    ::CSV.foreach(file.path, headers: true) do |row|
      logger.debug("CSV Row: #{row.to_hash}")
      document_access = DocumentAccess.find_or_initialize_by(friendlier_id: row[0], institution_code: row[1])
      document_access.update(row.to_hash)
    end
  end

  def self.destroy_all(file)
    logger.debug("CSV Destroy")
    ::CSV.foreach(file.path, headers: true) do |row|
      logger.debug("CSV Row: #{row.to_hash}")
      DocumentAccess.destroy_by(id: row[0], friendlier_id: row[1])
    end
  end

  def to_csv
    attributes.values
  end

  def reindex_document
    document.save
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
geoblacklight_admin-0.4.0 app/models/document_access.rb
geoblacklight_admin-0.3.2 app/models/document_access.rb
geoblacklight_admin-0.3.1 app/models/document_access.rb
geoblacklight_admin-0.3.0 app/models/document_access.rb