Class: Assessment
- Inherits:
-
Sequel::Model
- Object
- Sequel::Model
- Assessment
- Defined in:
- backend/app/model/assessment.rb
Defined Under Namespace
Classes: TransferRepoAttribute
Constant Summary collapse
- KEY_TO_TYPE =
{ 'ratings' => 'rating', 'formats' => 'format', 'conservation_issues' => 'conservation_issue', }
Class Method Summary collapse
-
.apply_attributes(obj, json) ⇒ Object
-
.clone_from_json(json, opts = {}) ⇒ Object
Like
create_from_json
but runs in the context where we havejson
taken from repo A and we want to create an equivalent record in the current repository. -
.create_from_json(json, opts = {}) ⇒ Object
-
.sequel_to_jsonmodel(objs, opts = {}) ⇒ Object
Instance Method Summary collapse
-
#linked_collection_uris ⇒ Object
-
#transfer_to_repository(repository, transfer_group = []) ⇒ Object
-
#update_from_json(json, opts = {}, apply_nested_records = true) ⇒ Object
Class Method Details
.apply_attributes(obj, json) ⇒ Object
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 |
# File 'backend/app/model/assessment.rb', line 124 def self.apply_attributes(obj, json) # Add the appropriate list of attributes DB.open do |db| db[:assessment_attribute].filter(:assessment_id => obj.id).delete db[:assessment_attribute_note].filter(:assessment_id => obj.id).delete valid_attribute_ids = db[:assessment_attribute_definition] .filter(:repo_id => [Repository.global_repo_id, active_repository]) .select(:id) .map {|row| row[:id]} KEY_TO_TYPE.each do |key, type| Array(json[key]).each do |attribute| next unless valid_attribute_ids.include?(attribute['definition_id']) if attribute['value'] db[:assessment_attribute].insert(:assessment_id => obj.id, :value => attribute['value'], :assessment_attribute_definition_id => attribute['definition_id']) end if attribute['note'] db[:assessment_attribute_note].insert(:assessment_id => obj.id, :note => attribute['note'], :assessment_attribute_definition_id => attribute['definition_id']) end end end # Calculate the derived "Research Value" rating (the sum of Interest and Documentation Quality) research_value_id = db[:assessment_attribute_definition].filter(:label => 'Research Value').get(:id) values = db[:assessment_attribute] .join(:assessment_attribute_definition, :id => :assessment_attribute__assessment_attribute_definition_id) .filter(:assessment_attribute_definition__label => ['Interest', 'Documentation Quality'], :assessment_attribute__assessment_id => obj.id) .select(:assessment_attribute__value) .map {|row| row[:value] ? (Integer(row[:value]) rescue nil) : nil} research_value = values.compact.reduce {|sum, n| sum + n} if research_value db[:assessment_attribute].insert(:assessment_id => obj.id, :value => research_value.to_s, :assessment_attribute_definition_id => research_value_id) end end end |
.clone_from_json(json, opts = {}) ⇒ Object
Like create_from_json
but runs in the context where we have json
taken
from repo A and we want to create an equivalent record in the current
repository.
This happens in the context of repository transfers where some (but not all)
of the records linked to an assessment are being moved to a different
repository. The situation is similar to transfer_to_repository
(see
above), but instead of transferring the assessment we’ll create a new
version in the target repository.
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 |
# File 'backend/app/model/assessment.rb', line 107 def self.clone_from_json(json, opts = {}) repo_attribute_links = [] repo_attribute_links += extract_transfer_repo_attributes(json., 'rating') repo_attribute_links += extract_transfer_repo_attributes(json.formats, 'format') repo_attribute_links += extract_transfer_repo_attributes(json.conservation_issues, 'conservation_issue') # Create as normal, which will drop any attributes that belonged to the old repository cloned_assessment = create_from_json(json, opts) # Finally, create as many of them as we can by matching against attributes # in the new repository. apply_matching_repo_attributes_for_transferred_assessment(cloned_assessment, repo_attribute_links) cloned_assessment end |
.create_from_json(json, opts = {}) ⇒ Object
30 31 32 33 34 35 |
# File 'backend/app/model/assessment.rb', line 30 def self.create_from_json(json, opts = {}) prepare_monetary_value_for_save(json, opts) obj = super apply_attributes(obj, json) obj end |
.sequel_to_jsonmodel(objs, opts = {}) ⇒ Object
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 |
# File 'backend/app/model/assessment.rb', line 171 def self.sequel_to_jsonmodel(objs, opts = {}) jsons = super prepare_monetary_value_for_jsonmodel(objs, jsons) jsons.zip(objs).each do |json, obj| json['display_string'] = obj.id.to_s json['collections'] = obj.linked_collection_uris.map {|uri| { 'ref' => uri }} end definitions_by_obj = {} # each assessment has some attributes that link to a definition DB.open do |db| db[:assessment_attribute_definition] .filter(:repo_id => [Repository.global_repo_id, active_repository]) .each do |definition| jsons.zip(objs).each do |json, obj| KEY_TO_TYPE.each do |key, type| json[key] ||= [] end key = json_key_for_type(definition[:type]) definition_json = { 'global' => definition[:repo_id] == Repository.global_repo_id, 'label' => definition[:label], 'value' => nil, 'note' => nil, 'readonly' => (definition[:readonly] == 1), 'definition_id' => definition[:id], } definitions_by_obj[obj.id] ||= {} definitions_by_obj[obj.id][definition[:id]] = definition_json json[key] << definition_json end end # Load our attribute values db[:assessment_attribute] .filter(:assessment_id => objs.map(&:id)) .each do |attribute| assessment_id = attribute[:assessment_id] definition_id = attribute[:assessment_attribute_definition_id] definition_json = definitions_by_obj.fetch(assessment_id).fetch(definition_id) definition_json['value'] = attribute[:value] end # Load our attribute notes db[:assessment_attribute_note] .filter(:assessment_id => objs.map(&:id)) .each do |attribute| assessment_id = attribute[:assessment_id] definition_id = attribute[:assessment_attribute_definition_id] definition_json = definitions_by_obj.fetch(assessment_id).fetch(definition_id) definition_json['note'] = attribute[:note] end jsons end end |
Instance Method Details
#linked_collection_uris ⇒ Object
243 244 245 246 247 248 249 250 251 252 253 |
# File 'backend/app/model/assessment.rb', line 243 def linked_collection_uris uris = self.class.find_relationship(:assessment).who_participates_with(self).map do |record| if record.is_a? Resource JSONModel(:resource).uri_for(record.id, :repo_id => record.repo_id) elsif record.is_a? ArchivalObject JSONModel(:resource).uri_for(record.root_record_id, :repo_id => record.repo_id) end end uris.compact.uniq end |
#transfer_to_repository(repository, transfer_group = []) ⇒ Object
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 |
# File 'backend/app/model/assessment.rb', line 50 def transfer_to_repository(repository, transfer_group = []) # When we transfer an assessment to another repository, it might contain # references to attribute definitions that belong to its originating # repository. # # Rather than outright breaking those links, we attempt to find a # corresponding attribute in the target repository (based on attribute # label). So the assumption here is that two attributes with the same label # are equivalent. DB.open do |db| repo_attribute_links = db[:assessment_attribute_definition] .left_join(:assessment_attribute, :assessment_attribute_definition_id => :assessment_attribute_definition__id) .left_join(:assessment_attribute_note, :assessment_attribute_definition_id => :assessment_attribute_definition__id) .filter(:assessment_attribute_definition__repo_id => self.class.active_repository) .where(Sequel.|({:assessment_attribute__assessment_id => self.id}, {:assessment_attribute_note__assessment_id => self.id})) .select(:assessment_attribute_definition__id, :assessment_attribute_definition__label, :assessment_attribute_definition__type, :assessment_attribute__value, :assessment_attribute_note__note) .map {|row| TransferRepoAttribute.new(row[:id], row[:label], row[:type], row[:value], row[:note])} # Do the transfer super # Make sure we observe the updated repo_id self.refresh unless repo_attribute_links.empty? # Unlink the repository-scoped attributes and notes that are no longer valid db[:assessment_attribute] .filter(:assessment_id => self.id, :assessment_attribute_definition_id => repo_attribute_links.map(&:definition_id)) .delete db[:assessment_attribute_note] .filter(:assessment_id => self.id, :assessment_attribute_definition_id => repo_attribute_links.map(&:definition_id)) .delete # Search for replacements based on label and link them up self.class.apply_matching_repo_attributes_for_transferred_assessment(self, repo_attribute_links) end end end |
#update_from_json(json, opts = {}, apply_nested_records = true) ⇒ Object
38 39 40 41 42 43 |
# File 'backend/app/model/assessment.rb', line 38 def update_from_json(json, opts = {}, apply_nested_records = true) self.class.prepare_monetary_value_for_save(json, opts) super self.class.apply_attributes(self, json) self end |