Sha256: c33652e7f5d437ad6d2f9d3b8fe69b610b07e5dab35a05967ba5d9b10bf17af8

Contents?: true

Size: 1.93 KB

Versions: 9

Compression:

Stored size: 1.93 KB

Contents

class Identifier < ActiveRecord::Base
  #attr_accessible :body, :identifier_type_id, :manifestation_id, :primary, :position
  belongs_to :identifier_type
  belongs_to :manifestation, touch: true

  validates_presence_of :body
  validates_uniqueness_of :body, scope: [:identifier_type_id, :manifestation_id]
  validate :check_identifier
  before_validation :normalize
  before_save :convert_isbn
  scope :id_type, -> type {
    where(identifier_type: IdentifierType.where(name: type).first)
  }

  acts_as_list scope: :manifestation_id
  normalize_attributes :body

  def check_identifier
    case identifier_type.try(:name)
    when 'isbn'
      unless StdNum::ISBN.valid?(body)
        errors.add(:body)
      end

    when 'issn'
      unless StdNum::ISSN.valid?(body)
        errors.add(:body)
      end

    when 'lccn'
      unless StdNum::LCCN.valid?(body)
        errors.add(:body)
      end

    when 'doi'
      if URI.parse(body).scheme
        errors.add(:body)
      end
    end
  end

  def convert_isbn
    if identifier_type.name == 'isbn'
      lisbn = Lisbn.new(body)
      if lisbn.isbn
        if lisbn.isbn.length == 10
          self.body = lisbn.isbn13
        end
      end
    end
  end

  def hyphenated_isbn
    if identifier_type.name == 'isbn'
      lisbn = Lisbn.new(body)
      lisbn.parts.join('-')
    end
  end

  def normalize
    case identifier_type.try(:name)
    when 'isbn'
      self.body = StdNum::ISBN.normalize(body)
    when 'issn'
      self.body = StdNum::ISSN.normalize(body)
    when 'lccn'
      self.body = StdNum::LCCN.normalize(body)
    end
  end
end

# == Schema Information
#
# Table name: identifiers
#
#  id                 :integer          not null, primary key
#  body               :string(255)      not null
#  identifier_type_id :integer          not null
#  manifestation_id   :integer
#  primary            :boolean
#  position           :integer
#  created_at         :datetime
#  updated_at         :datetime
#

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
enju_biblio-0.1.3 app/models/identifier.rb
enju_biblio-0.1.2 app/models/identifier.rb
enju_biblio-0.1.1 app/models/identifier.rb
enju_biblio-0.1.0 app/models/identifier.rb
enju_biblio-0.1.0.pre71 app/models/identifier.rb
enju_biblio-0.1.0.pre70 app/models/identifier.rb
enju_biblio-0.1.0.pre69 app/models/identifier.rb
enju_biblio-0.1.0.pre68 app/models/identifier.rb
enju_biblio-0.1.0.pre67 app/models/identifier.rb