Class: Term

Inherits:
Sequel::Model
  • Object
show all
Includes:
ASModel
Defined in:
backend/app/model/term.rb

Constant Summary

Constants included from JSONModel

JSONModel::REFERENCE_KEY_REGEX

Class Method Summary collapse

Instance Method Summary collapse

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, check_valid_refs, 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_publish_flags!, set_repository, strict_mode, strict_mode?, validate_schema, with_repository

Class Method Details

.broadcast_changesObject



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

def self.broadcast_changes
  Notifications.notify("VOCABULARY_CHANGED")
end

.create_from_json(json, opts = {}) ⇒ Object



21
22
23
24
25
26
27
# File 'backend/app/model/term.rb', line 21

def self.create_from_json(json, opts = {})
  set_vocabulary(json, opts)
  obj = super

  broadcast_changes
  obj
end

.ensure_exists(json, referrer) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'backend/app/model/term.rb', line 40

def self.ensure_exists(json, referrer)
  DB.attempt {
    self.create_from_json(json)
  }.and_if_constraint_fails {|exception|
    term_type_id = BackendEnumSource.id_for_value("subject_term_type", json.term_type)

    term = Term.find(:vocab_id => JSONModel(:vocabulary).id_for(json.vocabulary),
                     :term => json.term,
                     :term_type_id => term_type_id)

    if !term
      # The term 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("Term '#{json.term}' seems to have been created by a currently running transaction.  Restarting this one.")
      sleep 5
      raise RetryTransaction.new
    end

    term
  }
end

.sequel_to_jsonmodel(objs, opts = {}) ⇒ Object



29
30
31
32
33
34
35
36
37
# File 'backend/app/model/term.rb', line 29

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

.set_vocabulary(json, opts) ⇒ Object



13
14
15
16
17
18
19
# File 'backend/app/model/term.rb', line 13

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

#validateObject



7
8
9
10
11
# File 'backend/app/model/term.rb', line 7

def validate
  super
  validates_unique([:vocab_id, :term, :term_type_id], :message => "Term must be unique")
  map_validation_to_json_property([:vocab_id, :term, :term_type_id], :term)
end