Module: AgentNames::ClassMethods

Defined in:
backend/app/model/mixins/agent_names.rb

Instance Method Summary collapse

Instance Method Details

#apply_authority_id(obj, json) ⇒ Object



54
55
56
57
58
59
60
61
62
63
# File 'backend/app/model/mixins/agent_names.rb', line 54

def apply_authority_id(obj, json)
  obj.name_authority_id_dataset.delete

  if json['authority_id']
    obj.name_authority_id = NameAuthorityId.create(:authority_id => json['authority_id'],
                                                   :lock_version => 0)
  end

  obj
end

#assemble_hash_fields(json) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'backend/app/model/mixins/agent_names.rb', line 85

def assemble_hash_fields(json)
  name = my_jsonmodel.from_hash(json)
  hash_fields = []
  name_fields = %w(dates qualifier source rules) + type_specific_hash_fields

  name['use_dates'].each do |date|
    hash_fields << [:date_type_structured,
                    :date_label].map {|property|
      date[property.to_s] || ' '
    }.join('_')
  end


  hash_fields << name_fields.uniq.map {|property|
    if !name[property]
      ' '
    elsif name.class.schema["properties"][property]["dynamic_enum"]
      enum = name.class.schema["properties"][property]["dynamic_enum"]
      BackendEnumSource.id_for_value(enum, name[property])
    else
      name[property.to_s]
    end
  }.join('_')

  hash_fields
end

#associations_to_eagerly_loadObject



50
51
52
# File 'backend/app/model/mixins/agent_names.rb', line 50

def associations_to_eagerly_load
  super + [:name_authority_id]
end

#calculate_object_graph(object_graph, opts = {}) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
# File 'backend/app/model/mixins/agent_names.rb', line 37

def calculate_object_graph(object_graph, opts = {})
  super

  # Add the rows for authority IDs too
  column = "#{self.table_name}_id".intern

  ids = NameAuthorityId.filter(column => object_graph.ids_for(self)).
                        map {|row| row[:id]}

  object_graph.add_objects(NameAuthorityId, ids)
end

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



66
67
68
69
# File 'backend/app/model/mixins/agent_names.rb', line 66

def create_from_json(json, opts = {})
  obj = super
  apply_authority_id(obj, json)
end

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



72
73
74
75
76
77
78
79
80
81
82
# File 'backend/app/model/mixins/agent_names.rb', line 72

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

  jsons.zip(objs).each do |json, obj|
    if obj.name_authority_id
      json['authority_id'] = obj.name_authority_id.authority_id
    end
  end

  jsons
end