Sha256: da12634658b3fb39af9eb30f85843fa3008a00337e1fd06fb4ae15723030c290

Contents?: true

Size: 914 Bytes

Versions: 3

Compression:

Stored size: 914 Bytes

Contents

module Simplec
  class Document < ApplicationRecord

    belongs_to :document_set,
      optional: true
    has_and_belongs_to_many :subdomains

    dragonfly_accessor :file

    validates :name,
      presence: true
    validate :validate_subdomain_or_set!
    validate :validate_slug!

    def path
      self.file.url
    end

    private

    def validate_subdomain_or_set!
      return if self.subdomains.length > 0
      return if self.document_set
      errors.add :subdomain_ids, 'either a document set or at least one subdomain is required'
    end

    def validate_slug!
      return if self.slug.nil?
      similar = self.class.where(slug: self.slug).where.not(id: self.id)
      return if similar.size == 0
      return if (
        similar.map(&:subdomain_ids).flatten & self.subdomain_ids
      ).length == 0
      errors.add :slug, 'slug must be unique across linked subdomains'
    end

  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
simplec-0.8.0 app/models/simplec/document.rb
simplec-0.7.1 app/models/simplec/document.rb
simplec-0.7.0 app/models/simplec/document.rb