Class: ClassificationTerm

Inherits:
Sequel::Model
  • Object
show all
Includes:
ASModel, ClassificationIndexing, Publishable, Relationships, TreeNodes
Defined in:
backend/app/model/classification_term.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 ClassificationIndexing

#reindex_children

Methods included from TreeNodes

#absolute_position, #children, #has_children?, included, #order_siblings, #set_position_in_list, #set_root, #siblings, #transfer_to_repository, #trigger_index_of_child_nodes

Methods included from Relationships

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

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 = {})



23
24
25
26
27
28
# File 'backend/app/model/classification_term.rb', line 23

def self.create_from_json(json, opts = {})
  self.set_path_from_root(json)
  obj = super(json, :title_sha1 => Digest::SHA1.hexdigest(json.title))
  obj.reindex_children
  obj
end

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



56
57
58
59
60
61
62
63
64
# File 'backend/app/model/classification_term.rb', line 56

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

  jsons.each do |json|
    self.set_path_from_root(json)
  end

  jsons
end

+ (Object) set_path_from_root(json)



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'backend/app/model/classification_term.rb', line 39

def self.set_path_from_root(json)
  path = [{'title' => json.title, 'identifier' => json.identifier}]
  parent_id = json.parent ? self.parse_reference(json.parent['ref'], {})[:id] : nil

  while parent_id
    node = ClassificationTerm[parent_id]
    path << {'title' => node.title, 'identifier' => node.identifier}
    parent_id = node.parent_id
  end

  root = Classification[self.parse_reference(json.classification['ref'], {})[:id]]
  path << {'title' => root.title, 'identifier' => root.identifier}

  json['path_from_root'] = path.reverse
end

Instance Method Details

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



31
32
33
34
35
36
# File 'backend/app/model/classification_term.rb', line 31

def update_from_json(json, opts = {}, apply_nested_records = true)
  self.class.set_path_from_root(json)
  obj = super(json, {:title_sha1 => Digest::SHA1.hexdigest(json.title)}, apply_nested_records)
  obj.reindex_children
  obj
end

- (Object) update_position_only(parent_id, position)



67
68
69
70
# File 'backend/app/model/classification_term.rb', line 67

def update_position_only(parent_id, position)
  super
  self.reindex_children
end

- (Object) validate



73
74
75
76
77
78
79
80
81
82
83
84
# File 'backend/app/model/classification_term.rb', line 73

def validate
  validates_unique([:parent_name, :title_sha1],
                   :message => "must be unique to its level in the tree")

  validates_unique([:parent_name, :identifier],
                   :message => "must be unique to its level in the tree")

  map_validation_to_json_property([:parent_name, :title_sha1], :title)
  map_validation_to_json_property([:parent_name, :identifier], :identifier)

  super
end