Class: IDLookup

Inherits:
Object
  • Object
show all
Defined in:
backend/app/model/idlookup.rb

Instance Method Summary collapse

Instance Method Details

#find_by_ids(model, params) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'backend/app/model/idlookup.rb', line 3

def find_by_ids(model, params)
  filters = {}

  params.each do |key, values|
    key = key.intern
    unless key == :repo_id || key == :resolve
      # The identifier for a resource needs to be massaged to match the db
      if !Array(values).empty? && key == :identifier
        identifiers = Array(values).map {|identifier|
          parsed = ASUtils.json_parse(identifier)
          if parsed.is_a?(Array) && parsed.length > 0 && parsed.length <= 4
            padded = (parsed + ([nil] * 3)).take(4)
            ASUtils.to_json(padded)
          end
        }.compact
        filters[key] = Array(identifiers)
      # For cases other than resources
      elsif !Array(values).empty?
        filters[key] = Array(values)
      end
    end
  end

  return [] if filters.empty?

  model.this_repo.where(filters).select(:id).map {|record|
    {'ref' => record.uri}
  }
end