|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +# == Schema Information |
| 4 | +# |
| 5 | +# Table name: research_outputs |
| 6 | +# |
| 7 | +# id :bigint not null, primary key |
| 8 | +# abbreviation :string |
| 9 | +# access :integer default(0), not null |
| 10 | +# byte_size :bigint |
| 11 | +# coverage_end :datetime |
| 12 | +# coverage_region :string |
| 13 | +# coverage_start :datetime |
| 14 | +# description :text |
| 15 | +# display_order :integer |
| 16 | +# is_default :boolean default("false") |
| 17 | +# mandatory_attribution :text |
| 18 | +# output_type :integer default(3), not null |
| 19 | +# output_type_description :string |
| 20 | +# personal_data :boolean |
| 21 | +# release_date :datetime |
| 22 | +# sensitive_data :boolean |
| 23 | +# title :string not null |
| 24 | +# created_at :datetime not null |
| 25 | +# updated_at :datetime not null |
| 26 | +# mime_type_id :integer |
| 27 | +# plan_id :integer |
| 28 | +# |
| 29 | +# Indexes |
| 30 | +# |
| 31 | +# index_research_outputs_on_output_type (output_type) |
| 32 | +# index_research_outputs_on_plan_id (plan_id) |
| 33 | +# |
| 34 | +class ResearchOutput < ApplicationRecord |
| 35 | + |
| 36 | + include Identifiable |
| 37 | + include ValidationMessages |
| 38 | + |
| 39 | + enum output_type: %i[audiovisual collection data_paper dataset event image |
| 40 | + interactive_resource model_representation physical_object |
| 41 | + service software sound text workflow other] |
| 42 | + |
| 43 | + enum access: %i[open embargoed restricted closed] |
| 44 | + |
| 45 | + # ================ |
| 46 | + # = Associations = |
| 47 | + # ================ |
| 48 | + |
| 49 | + belongs_to :plan, optional: true |
| 50 | + belongs_to :mime_type, optional: true |
| 51 | + |
| 52 | + # =============== |
| 53 | + # = Validations = |
| 54 | + # =============== |
| 55 | + |
| 56 | + validates_presence_of :output_type, :access, :title, message: PRESENCE_MESSAGE |
| 57 | + validates_uniqueness_of :title, :abbreviation, scope: :plan_id |
| 58 | + |
| 59 | + # Ensure presence of the :output_type_description if the user selected 'other' |
| 60 | + validates_presence_of :output_type_description, if: -> { other? }, message: PRESENCE_MESSAGE |
| 61 | + # Ensure that :coverage_start comes before :coverage_end |
| 62 | + validate :end_date_after_start_date |
| 63 | + |
| 64 | + # ==================== |
| 65 | + # = Instance methods = |
| 66 | + # ==================== |
| 67 | + |
| 68 | + # :mime_type is only applicable for certain :output_types |
| 69 | + # This method returns the applicable :mime_types |
| 70 | + def available_mime_types |
| 71 | + cat = %w[audio video] if audiovisual? || sound? |
| 72 | + cat = %w[image] if image? |
| 73 | + cat = %w[model] if model_representation? |
| 74 | + cat = %w[text] if data_paper? || dataset? || text? |
| 75 | + |
| 76 | + cat.present? ? MimeType.where(category: cat).order(:description) : [] |
| 77 | + end |
| 78 | + |
| 79 | + # TODO: placeholders for once the License, Repository, Metadata Standard and |
| 80 | + # Resource Type Lookups feature is built. |
| 81 | + # |
| 82 | + # Be sure to add the scheme in the appropriate upgrade task (and to the |
| 83 | + # seed.rb as well) |
| 84 | + def licenses |
| 85 | + # scheme = IdentifierScheme.find_by(name: '[name of license scheme]') |
| 86 | + # return [] unless scheme.present? |
| 87 | + # identifiers.select { |id| id.identifier_scheme = scheme } |
| 88 | + [] |
| 89 | + end |
| 90 | + |
| 91 | + def repositories |
| 92 | + # scheme = IdentifierScheme.find_by(name: '[name of repository scheme]') |
| 93 | + # return [] unless scheme.present? |
| 94 | + # identifiers.select { |id| id.identifier_scheme = scheme } |
| 95 | + [] |
| 96 | + end |
| 97 | + |
| 98 | + def metadata_standards |
| 99 | + # scheme = IdentifierScheme.find_by(name: '[name of openaire scheme]') |
| 100 | + # return [] unless scheme.present? |
| 101 | + # identifiers.select { |id| id.identifier_scheme = scheme } |
| 102 | + [] |
| 103 | + end |
| 104 | + |
| 105 | + def resource_types |
| 106 | + # scheme = IdentifierScheme.find_by(name: '[name of resource_type scheme]') |
| 107 | + # return [] unless scheme.present? |
| 108 | + # identifiers.select { |id| id.identifier_scheme = scheme } |
| 109 | + [] |
| 110 | + end |
| 111 | + |
| 112 | + private |
| 113 | + |
| 114 | + # Validation to prevent end date from coming before the start date |
| 115 | + def end_date_after_start_date |
| 116 | + # allow nil values |
| 117 | + return true if coverage_end.blank? || coverage_start.blank? |
| 118 | + |
| 119 | + errors.add(:coverage_end, _("must be after the start date")) if coverage_end < coverage_start |
| 120 | + end |
| 121 | + |
| 122 | +end |
0 commit comments