Class: ProjectSource

Inherits:
ApplicationRecord show all
Includes:
Housekeeping, Shared::IsData
Defined in:
app/models/project_source.rb

Overview

A ProjectSource links sources to projects.

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

Methods included from Housekeeping

#has_polymorphic_relationship?

Methods included from ActiverecordUtilities

#trim_attributes

Instance Attribute Details

- (Integer) project_id

the project

Returns:

  • (Integer)


11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'app/models/project_source.rb', line 11

class ProjectSource < ApplicationRecord
  include Housekeeping
  include Shared::IsData

  belongs_to :project, inverse_of: :project_sources
  belongs_to :source, inverse_of: :project_sources

  # source presence validation is handled in PG, as per accepts_nested_attributes constraints
  #
  validates_uniqueness_of :source_id, scope: [:project_id]
  before_destroy :check_for_use

  protected

  def check_for_use
    if source.citations.where(citations: {project: project_id}).any?
      errors.add(:base, 'source is still used in project')
      throw(:abort)
    end
  end
end

- (Integer) source_id

Returns the source

Returns:

  • (Integer)

    the source



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'app/models/project_source.rb', line 11

class ProjectSource < ApplicationRecord
  include Housekeeping
  include Shared::IsData

  belongs_to :project, inverse_of: :project_sources
  belongs_to :source, inverse_of: :project_sources

  # source presence validation is handled in PG, as per accepts_nested_attributes constraints
  #
  validates_uniqueness_of :source_id, scope: [:project_id]
  before_destroy :check_for_use

  protected

  def check_for_use
    if source.citations.where(citations: {project: project_id}).any?
      errors.add(:base, 'source is still used in project')
      throw(:abort)
    end
  end
end

Instance Method Details

- (Object) check_for_use (protected)



25
26
27
28
29
30
# File 'app/models/project_source.rb', line 25

def check_for_use
  if source.citations.where(citations: {project: project_id}).any?
    errors.add(:base, 'source is still used in project')
    throw(:abort)
  end
end