Class: ProjectSource
- Inherits:
-
ApplicationRecord
- Object
- ActiveRecord::Base
- ApplicationRecord
- ProjectSource
- Includes:
- Housekeeping, Shared::IsData
- Defined in:
- app/models/project_source.rb
Overview
A ProjectSource links sources to projects.
Instance Attribute Summary collapse
-
#project_id ⇒ Integer
the project.
-
#source_id ⇒ Integer
The source.
Class Method Summary collapse
Instance Method Summary collapse
- #check_for_use ⇒ Object protected
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
Instance Attribute Details
#project_id ⇒ Integer
the project
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# 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 def self.batch_sync_to_project(params, operation, project_id = nil) return false if project_id.nil? sources = Queries::Source::Filter.new(params[:source_query]) i = 0 if sources.all.any? && !sources.params.empty? case operation.to_sym when :add begin sources.all.find_each do |s| ProjectSource.create!(source_id: s.id, project_id:) i += 1 end rescue ActiveRecord::RecordInvalid end when :remove sources.all.find_each do |s| ProjectSource.where(project_id: , source_id: s.id).delete_all i += 1 end else return false end end i end 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_id ⇒ Integer
Returns 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 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# 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 def self.batch_sync_to_project(params, operation, project_id = nil) return false if project_id.nil? sources = Queries::Source::Filter.new(params[:source_query]) i = 0 if sources.all.any? && !sources.params.empty? case operation.to_sym when :add begin sources.all.find_each do |s| ProjectSource.create!(source_id: s.id, project_id:) i += 1 end rescue ActiveRecord::RecordInvalid end when :remove sources.all.find_each do |s| ProjectSource.where(project_id: , source_id: s.id).delete_all i += 1 end else return false end end i end 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 |
Class Method Details
.batch_sync_to_project(params, operation, project_id = nil) ⇒ Object
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'app/models/project_source.rb', line 24 def self.batch_sync_to_project(params, operation, project_id = nil) return false if project_id.nil? sources = Queries::Source::Filter.new(params[:source_query]) i = 0 if sources.all.any? && !sources.params.empty? case operation.to_sym when :add begin sources.all.find_each do |s| ProjectSource.create!(source_id: s.id, project_id:) i += 1 end rescue ActiveRecord::RecordInvalid end when :remove sources.all.find_each do |s| ProjectSource.where(project_id: , source_id: s.id).delete_all i += 1 end else return false end end i end |
Instance Method Details
#check_for_use ⇒ Object (protected)
56 57 58 59 60 61 |
# File 'app/models/project_source.rb', line 56 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 |