-
Notifications
You must be signed in to change notification settings - Fork 118
Expand file tree
/
Copy pathresearch_output.rb
More file actions
99 lines (84 loc) · 3.43 KB
/
research_output.rb
File metadata and controls
99 lines (84 loc) · 3.43 KB
1
2
3
4
5
6
7
8
9
10
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# frozen_string_literal: true
# == Schema Information
#
# Table name: research_outputs
#
# id :bigint not null, primary key
# abbreviation :string
# access :integer default("open"), not null
# byte_size :bigint
# description :text
# display_order :integer
# is_default :boolean
# output_type :integer default("dataset"), not null
# output_type_description :string
# personal_data :boolean
# release_date :datetime
# sensitive_data :boolean
# title :string not null
# created_at :datetime not null
# updated_at :datetime not null
# license_id :bigint
# plan_id :integer
#
# Indexes
#
# index_research_outputs_on_output_type (output_type)
#
# Foreign Keys
#
# fk_rails_... (plan_id => plans.id)
# fk_rails_... (license_id => licenses.id)
#
# Object that represents a proposed output for a project
class ResearchOutput < ApplicationRecord
include Identifiable
include ValidationMessages
enum output_type: %i[audiovisual collection data_paper dataset event image
interactive_resource model_representation physical_object
service software sound text workflow other]
enum access: %i[open embargoed restricted closed]
# ================
# = Associations =
# ================
belongs_to :plan, optional: true, touch: true
belongs_to :license, optional: true
has_and_belongs_to_many :metadata_standards
has_and_belongs_to_many :repositories
# ===============
# = Validations =
# ===============
validates_presence_of :output_type, :access, :title, message: PRESENCE_MESSAGE
validates_uniqueness_of :title, { case_sensitive: false, scope: :plan_id,
message: UNIQUENESS_MESSAGE }
validates_uniqueness_of :abbreviation, { case_sensitive: false, scope: :plan_id,
allow_nil: true, allow_blank: true,
message: UNIQUENESS_MESSAGE }
validates_numericality_of :byte_size, greater_than: 0, less_than_or_equal_to: 2**63,
allow_blank: true,
message: '(Anticipated file size) is too large. Please enter a smaller value.'
# Ensure presence of the :output_type_description if the user selected 'other'
validates_presence_of :output_type_description, if: -> { other? }, message: PRESENCE_MESSAGE
# ==========
# = Scopes =
# ==========
scope :search, lambda { |term|
search_pattern = "%#{term}%"
where('lower(title) LIKE lower(?)', search_pattern)
}
# ====================
# = Instance methods =
# ====================
# Helper method to convert selected repository form params into Repository objects
def repositories_attributes=(params)
params.each_value do |repository_params|
repositories << Repository.find_by(id: repository_params[:id])
end
end
# Helper method to convert selected metadata standard form params into MetadataStandard objects
def metadata_standards_attributes=(params)
params.each_value do |metadata_standard_params|
metadata_standards << MetadataStandard.find_by(id: metadata_standard_params[:id])
end
end
end