Sha256: 134d8e37c77bfe5f00d99c559add80a3d2c322b5804a203a2f495f21bb9681fb

Contents?: true

Size: 883 Bytes

Versions: 9

Compression:

Stored size: 883 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!
      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

9 entries across 9 versions & 1 rubygems

Version Path
simplec-0.6.0 app/models/simplec/document.rb
simplec-0.5.0 app/models/simplec/document.rb
simplec-0.4.4 app/models/simplec/document.rb
simplec-0.4.3 app/models/simplec/document.rb
simplec-0.4.2 app/models/simplec/document.rb
simplec-0.4.1 app/models/simplec/document.rb
simplec-0.4.0 app/models/simplec/document.rb
simplec-0.3.0 app/models/simplec/document.rb
simplec-0.2.0 app/models/simplec/document.rb