Sha256: 58b9f54707796b4f771709098f02c3346da74fa3df72405c5ddf2317ee5387e7

Contents?: true

Size: 968 Bytes

Versions: 9

Compression:

Stored size: 968 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
    true
  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
    true
  end

  def to_csv
    attributes.values
  end

  def reindex_document
    document.save
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
geoblacklight_admin-0.7.0 app/models/document_access.rb
geoblacklight_admin-0.6.3 app/models/document_access.rb
geoblacklight_admin-0.6.2 app/models/document_access.rb
geoblacklight_admin-0.6.1 app/models/document_access.rb
geoblacklight_admin-0.6.0 app/models/document_access.rb
geoblacklight_admin-0.5.1 app/models/document_access.rb
geoblacklight_admin-0.5.0 app/models/document_access.rb
geoblacklight_admin-0.4.2 app/models/document_access.rb
geoblacklight_admin-0.4.1 app/models/document_access.rb