Class: Resource

Inherits:
ApplicationRecord show all
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

Class Method Details

.latest_timestampActiveSupport::TimeWithZone?

Returns:

  • (ActiveSupport::TimeWithZone)

    if one more resources exist, this is the created_at time for the most recently-created resource

  • (nil)

    if no resources exist



85
86
87
# File 'app/models/resource.rb', line 85

def self.latest_timestamp
  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

Returns:

  • (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

Returns:

  • (Boolean)


167
168
169
# File 'app/models/resource.rb', line 167

def assigned?
  !unassigned?
end

#best_representationObject



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

Returns:

  • (Boolean)


143
144
145
146
# File 'app/models/resource.rb', line 143

def complete?
  return @complete if defined? @complete
  @complete = representations.count >= organization.meta.count
end

#generate_canonical_idObject



171
172
173
# File 'app/models/resource.rb', line 171

def generate_canonical_id
  self.canonical_id = SecureRandom.uuid
end

#host_uris=(value) ⇒ Object

Parameters:

  • value (String)

    a newline-delimited list of host URIs to store for this Resource



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

#labelString

Returns a human-friendly means of identifying this resource in titles and select boxes

Returns:

  • (String)

    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

Returns:

  • (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

Returns each triplet represents a resource that is linked to this resource, with verbs given from this resource's perspective

Returns:

  • (Array<Symbol, ResourceLink, Resource>)

    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 related_resources
  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

Returns:

  • (Boolean)


158
159
160
# File 'app/models/resource.rb', line 158

def represented?
  !unrepresented?
end

#set_canonical_idObject



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_identifierObject



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

#statusesObject

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

Returns:

  • (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

Returns:

  • (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

Returns:

  • (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