Class: Subject

Inherits:
Sequel::Model
  • Object
show all
Includes:
ASModel, AutoGenerator, ExternalDocuments, ExternalIDs, ImpliedPublication, Publishable, Relationships
Defined in:
backend/app/model/subject.rb

Instance Attribute Summary

Attributes included from Relationships

#cached_relationships

Class Method Summary (collapse)

Instance Method Summary (collapse)

Methods included from Publishable

db_value_for, included

Methods included from ImpliedPublication

included, #is_published_by_implication?, relevant_relationships

Methods included from Relationships

#assimilate, #cache_relationships, included, #my_relationships, #related_records, #transfer_to_repository, #trigger_reindex_of_dependants

Methods included from AutoGenerator

included

Methods included from ExternalIDs

included

Methods included from ExternalDocuments

included

Methods included from ASModel

all_models, included, update_publish_flag, update_suppressed_flag

Methods included from JSONModel

JSONModel, #JSONModel, add_error_handler, all, allow_unmapped_enum_value, backend_url, client_mode?, custom_validations, destroy_model, enum_default_value, enum_values, handle_error, init, load_schema, #models, models, parse_jsonmodel_ref, parse_reference, repository, repository_for, schema_src, set_repository, strict_mode, strict_mode?, with_repository

Class Method Details

+ (Object) create_from_json(json, opts = {})



63
64
65
66
# File 'backend/app/model/subject.rb', line 63

def self.create_from_json(json, opts = {})
  set_vocabulary(json, opts)
  super(json, opts.merge(:terms_sha1 => generate_terms_sha1(json)))
end

+ (Object) ensure_exists(json, referrer)



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'backend/app/model/subject.rb', line 69

def self.ensure_exists(json, referrer)
  DB.attempt {
    self.create_from_json(json)
  }.and_if_constraint_fails {|exception|
    subject = find_matching(json)

    if !subject
      # The subject exists but we can't find it.  This could mean it was
      # created in a currently running transaction.  Abort this one to trigger
      # a retry.
      Log.info("Subject '#{json.terms}' seems to have been created by a currently running transaction.  Restarting this one.")
      sleep 5
      raise RetryTransaction.new
    end

    subject
  }
end

+ (Object) find_matching(json)



89
90
91
92
93
94
95
# File 'backend/app/model/subject.rb', line 89

def self.find_matching(json)
  source_id = BackendEnumSource.id_for_value("subject_source", json.source)

  Subject.find(:vocab_id => JSONModel(:vocabulary).id_for(json.vocabulary),
               :terms_sha1 => generate_terms_sha1(json),
               :source_id => source_id)
end

+ (Object) generate_terms_sha1(json)



56
57
58
59
60
# File 'backend/app/model/subject.rb', line 56

def self.generate_terms_sha1(json)
  return nil if json.terms.empty?

  Digest::SHA1.hexdigest(json.terms.map {|term| [term['term'], term['term_type']]}.inspect)
end

+ (Object) handle_delete(ids_to_delete)



116
117
118
119
120
# File 'backend/app/model/subject.rb', line 116

def self.handle_delete(ids_to_delete)
  self.db[:subject_term].filter(:subject_id => ids_to_delete).delete

  super
end

+ (Object) sequel_to_jsonmodel(objs, opts = {})



105
106
107
108
109
110
111
112
113
# File 'backend/app/model/subject.rb', line 105

def self.sequel_to_jsonmodel(objs, opts = {})
  jsons = super

  jsons.zip(objs).each do |json, obj|
    json.vocabulary = uri_for(:vocabulary, obj.vocab_id)
  end

  jsons
end

+ (Object) set_vocabulary(json, opts)



47
48
49
50
51
52
53
# File 'backend/app/model/subject.rb', line 47

def self.set_vocabulary(json, opts)
  opts["vocab_id"] = nil

  if json.vocabulary
    opts["vocab_id"] = parse_reference(json.vocabulary, opts)[:id]
  end
end

Instance Method Details

- (Object) update_from_json(json, opts = {}, apply_nested_records = true)



98
99
100
101
102
# File 'backend/app/model/subject.rb', line 98

def update_from_json(json, opts = {}, apply_nested_records = true)
  self.class.set_vocabulary(json, opts)
  self[:terms_sha1] = self.class.generate_terms_sha1(json) # add a terms sha1 hash to allow for uniqueness test
  super
end

- (Object) validate



123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'backend/app/model/subject.rb', line 123

def validate
  super

  if self[:source_id]
    validates_unique([:vocab_id, :source_id, :terms_sha1], :message => "Subject must be unique")
  else
    validates_unique([:vocab_id, :terms_sha1], :message => "Subject must be unique")
  end

  validates_unique([:vocab_id, :source_id, :authority_id], :message => "Subject heading identifier must be unique within source")
  map_validation_to_json_property([:vocab_id, :source_id, :authority_id], :authority_id)
  map_validation_to_json_property([:vocab_id, :terms_sha1], :terms)
  map_validation_to_json_property([:vocab_id, :source_id, :terms_sha1], :terms)
end