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 Shared::IsData

#errors_excepting, #full_error_messages_excepting, #identical, #is_community?, #is_destroyable?, #is_editable?, #is_in_use?, #is_in_users_projects?, #metamorphosize, #similar

Methods included from Housekeeping

#has_polymorphic_relationship?

Methods inherited from ApplicationRecord

transaction_with_retry

Instance Attribute Details

#project_idInteger

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
32
33
# 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

  validates :source, presence: true
  validates :project, presence: true

  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 can not be removed, it is used in this project')
      throw(:abort)
    end
  end

end

#source_idInteger

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
32
33
# 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

  validates :source, presence: true
  validates :project, presence: true

  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 can not be removed, it is used in this project')
      throw(:abort)
    end
  end

end

Instance Method Details

#check_for_useObject (protected)



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

def check_for_use
  if source.citations.where(citations: {project: project_id}).any?
    errors.add(:base, 'Source can not be removed, it is used in this project')
    throw(:abort)
  end
end