Class: Resource
- Inherits:
-
ApplicationRecord
- Object
- ActiveRecord::Base
- ApplicationRecord
- Resource
- Defined in:
- app/models/resource.rb
Overview
We use the Dublin Core meaning for what a Resource represents: “…a resource is anything that has identity. Familiar examples include an electronic document, an image, a service (e.g., ”today's weather report for Los Angeles“), and a collection of other resources. Not all resources are network ”retrievable“; e.g., human beings, corporations, and bound books in a library can also be considered resources.”
Class Method Summary collapse
Instance Method Summary collapse
- #approved? ⇒ Boolean
- #assigned? ⇒ Boolean
- #best_representation ⇒ Object
- #complete? ⇒ Boolean
- #generate_canonical_id ⇒ Object
- #host_uris=(value) ⇒ Object
-
#label ⇒ String
A human-friendly means of identifying this resource in titles and select boxes.
- #partially_complete? ⇒ Boolean
-
#related_resources ⇒ Array<Symbol, ResourceLink, Resource>
Each triplet represents a resource that is linked to this resource, with verbs given from this resource's perspective.
- #represented? ⇒ Boolean
- #set_canonical_id ⇒ Object
- #set_identifier ⇒ Object
-
#statuses ⇒ Object
TODO: Should maybe move this stuff to a presenter / decorator Status identifiers.
- #unassigned? ⇒ Boolean
- #unrepresented? ⇒ Boolean
- #viewable? ⇒ Boolean
Class Method Details
.latest_timestamp ⇒ ActiveSupport::TimeWithZone?
85 86 87 |
# File 'app/models/resource.rb', line 85 def self. order(:created_at).last.try(:created_at) end |
.ransackable_scopes(_ = nil) ⇒ Object
79 80 81 |
# File 'app/models/resource.rb', line 79 def self.ransackable_scopes(_ = nil) %i[order_by_priority_and_date represented assigned unassigned unrepresented assigned_unrepresented unassigned_unrepresented with_approved_representations] end |
Instance Method Details
#approved? ⇒ Boolean
138 139 140 141 |
# File 'app/models/resource.rb', line 138 def approved? return @approved if defined? @approved @approved = complete? && representations.all?(&:approved?) end |
#assigned? ⇒ Boolean
167 168 169 |
# File 'app/models/resource.rb', line 167 def assigned? !unassigned? end |
#best_representation ⇒ Object
133 134 135 136 |
# File 'app/models/resource.rb', line 133 def best_representation return @best_representation if defined? @best_representation @best_representation = representations.by_status.by_title_length.first end |
#complete? ⇒ Boolean
143 144 145 146 |
# File 'app/models/resource.rb', line 143 def complete? return @complete if defined? @complete @complete = representations.count >= organization..count end |
#generate_canonical_id ⇒ Object
171 172 173 |
# File 'app/models/resource.rb', line 171 def generate_canonical_id self.canonical_id = SecureRandom.uuid end |
#host_uris=(value) ⇒ Object
90 91 92 |
# File 'app/models/resource.rb', line 90 def host_uris=(value) write_attribute(:host_uris, value.to_s.split(/[\r\n]+/)) end |
#label ⇒ String
Returns a human-friendly means of identifying this resource in titles and select boxes
99 100 101 |
# File 'app/models/resource.rb', line 99 def label "#{title} (#{identifier})" end |
#partially_complete? ⇒ Boolean
148 149 150 151 |
# File 'app/models/resource.rb', line 148 def partially_complete? return @partially_complete if defined? @partially_complete @partially_complete = represented? && !complete? end |
#related_resources ⇒ Array<Symbol, ResourceLink, Resource>
Returns each triplet represents a resource that is linked to this resource, with verbs given from this resource's perspective
106 107 108 109 110 111 112 113 114 115 116 117 118 |
# File 'app/models/resource.rb', line 106 def result = [] subject_resource_links.each do |link| result << [link.verb, link, link.object_resource] end object_resource_links.each do |link| result << [link.reverse_verb, link, link.subject_resource] end result end |
#represented? ⇒ Boolean
158 159 160 |
# File 'app/models/resource.rb', line 158 def represented? !unrepresented? end |
#set_canonical_id ⇒ Object
175 176 177 178 |
# File 'app/models/resource.rb', line 175 def set_canonical_id return unless canonical_id.blank? next while Resource.where(canonical_id: generate_canonical_id).where.not(id: id).any? end |
#set_identifier ⇒ Object
180 181 182 183 184 185 186 187 188 189 190 |
# File 'app/models/resource.rb', line 180 def set_identifier return unless identifier.blank? root_identifier = title.parameterize identifier = root_identifier i = 1 while Resource.where(identifier: identifier).where.not(id: id).any? i += 1 identifier = "#{root_identifier}-#{i}" end self.identifier = identifier end |
#statuses ⇒ Object
TODO: Should maybe move this stuff to a presenter / decorator Status identifiers
122 123 124 125 126 127 128 129 130 131 |
# File 'app/models/resource.rb', line 122 def statuses @statuses ||= [].tap do |statuses| statuses.push(:urgent) if priority_flag? statuses.push(:unrepresented) if unrepresented? statuses.push(:represented) if represented? statuses.push(:unassigned) if unassigned? statuses.push(:assigned) if assigned? statuses.push(:partially_complete) if partially_complete? end end |
#unassigned? ⇒ Boolean
162 163 164 165 |
# File 'app/models/resource.rb', line 162 def unassigned? return @unassigned if defined? @unassigned @unassigned = assignments.none? end |
#unrepresented? ⇒ Boolean
153 154 155 156 |
# File 'app/models/resource.rb', line 153 def unrepresented? return @unrepresented if defined? @unrepresented @unrepresented = representations.none? end |
#viewable? ⇒ Boolean
94 95 96 |
# File 'app/models/resource.rb', line 94 def viewable? (source_uri.present? || uploaded_resource.attached?) && Coyote::Resource::IMAGE_LIKE_TYPES.include?(resource_type.to_sym) end |