Module: Arks::ClassMethods

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

Instance Method Summary collapse

Instance Method Details

#handle_delete(ids_to_delete) ⇒ Object

[View source]

79
80
81
82
83
# File 'backend/app/model/mixins/arks.rb', line 79

def handle_delete(ids_to_delete)
  ArkName.handle_delete(self, ids_to_delete)

  super
end

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

[View source]

35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
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
# File 'backend/app/model/mixins/arks.rb', line 35

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

  fk_col = ArkName.fk_for_class(self)

  rec_ids_to_arks = {}

  ark_query = ArkName.filter(fk_col => objs.map(&:id))

  unless AppConfig[:arks_allow_external_arks]
    ark_query = ark_query.filter(:is_external_url => 0)
  end

  ark_query.each do |ark_obj|
    rec_ids_to_arks[ark_obj[fk_col]] ||= []
    rec_ids_to_arks[ark_obj[fk_col]] << ark_obj
  end

  objs.zip(jsons).each do |obj, json|
    arks = rec_ids_to_arks.fetch(obj.id, [])

    unless arks.empty?
      (current, *), previous = arks.partition {|ark| ark.is_current == 1}

      json['ark_name'] = {
        'previous' => previous.map(&:value),
      }

      if current
        json['ark_name']['current'] = current.value
        json['ark_name']['current_is_external'] = (current.is_external_url == 1)

        if current.is_external_url == 1
          json['external_ark_url'] = current.ark_value
        else
          json['external_ark_url'] = nil
        end
      end
    end
  end

  jsons
end