Class: GlobalID::Locator::BaseLocator

Inherits:
Object
  • Object
show all
Defined in:
lib/global_id/locator.rb

Direct Known Subclasses

UnscopedLocator

Instance Method Summary collapse

Instance Method Details

#locate(gid, options = {}) ⇒ Object



157
158
159
160
161
162
163
# File 'lib/global_id/locator.rb', line 157

def locate(gid, options = {})
  return unless model_id_is_valid?(gid)
  model_class = gid.model_class
  model_class = model_class.includes(options[:includes]) if options[:includes]

  model_class.find gid.model_id
end

#locate_many(gids, options = {}) ⇒ Object



165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
# File 'lib/global_id/locator.rb', line 165

def locate_many(gids, options = {})
  ids_by_model = Hash.new { |hash, key| hash[key] = [] }

  gids.each do |gid|
    next unless model_id_is_valid?(gid)
    ids_by_model[gid.model_class] << gid.model_id
  end

  records_by_model_name_and_id = {}

  ids_by_model.each do |model, ids|
    records = find_records(model, ids, ignore_missing: options[:ignore_missing], includes: options[:includes])

    records_by_id = records.index_by do |record|
      record.id.is_a?(Array) ? record.id.map(&:to_s) : record.id.to_s
    end

    records_by_model_name_and_id[model.name] = records_by_id
  end

  gids.filter_map { |gid| records_by_model_name_and_id[gid.model_name][gid.model_id] }
end