Sha256: 20e7b053465fc7ca029b4bb887c12705cad43cd1675b54d44b70ef0f812835d6

Contents?: true

Size: 1.74 KB

Versions: 1

Compression:

Stored size: 1.74 KB

Contents

require 'importable_attachments/importers'

class Library < ActiveRecord::Base
  include SmarterDates if ::Configuration.for('smarter_dates').enabled

  if ::Configuration.for('versioning').enabled
    has_paper_trail ignore: [:updated_at]
  end

  RECORD_HEADERS = ['title', 'author', 'last checkout at']

  if ::Configuration.for('attachments').enabled
    extend ImportableAttachments::Base::ClassMethods
    has_importable_attachment spreadsheet_columns: RECORD_HEADERS,
      import_into: :books
    include ImportableAttachments::Importers::Csv
    validates_with ImportableAttachments::CsvValidator, if: Proc.new { |model| model.attachment.present? && model.attachment.persisted? }
  end

  # --------------------------------------------------------------------------
  # define: attributes and relationships
  has_many :books, dependent: :destroy, inverse_of: :library

  attr_accessor :converted_headers

  # --------------------------------------------------------------------------
  # define: DTD i.e. validations
  validates :name, alpha_numeric: {punctuation: :limited}, presence: true
  validates :address, alpha_numeric: {punctuation: :limited}, presence: true

  # --------------------------------------------------------------------------
  # define: scopes
  attr_accessible :name, :address

  # --------------------------------------------------------------------------
  # define: behaviors

  # : call-seq:
  # import_attachment
  #
  # imports an attachment of a given mime-type (data-stream to ruby),
  # calls import_rows with a ruby data-store

  def import_attachment
    import_csv
  end

  protected

  def sanitize_data_callback(headers, sheet) # :nodoc:
    new_sheet = sheet.map {|row| row + [id]}
    [headers + ['library_id'], new_sheet]
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
importable_attachments-0.0.13 spec/dummy/app/models/library.rb