class Maglev::FetchCollectionItems

matching a keyword.
If the id is specified, then look for an item, it not, look for all the items
Find item(s) from a collection defined in the Maglev config file.

def build_item(original_item)

def build_item(original_item)
  return nil unless original_item
  Item.new(
    original_item.id,
    item_label(original_item),
    item_image(original_item),
    original_item
  )
end

def call

def call
  if id
    fetch_item
  else
    fetch_items
  end
end

def collection

def collection
  config.collections[collection_id.to_sym].tap do |collection|
    next if collection
    raise "[Maglev] unregistered '#{collection_id}' collection in the Maglev configuration."
  end
end

def default_fetch_original_items

def default_fetch_original_items
  model_class
    .where(keyword.present? ? model_class.arel_table[label_field].matches("%#{keyword}%") : nil)
    .limit(max_items)
    .order(label_field)
end

def fetch_item

def fetch_item
  build_item(
    id == 'any' ? fetch_original_items.first : fetch_original_items.find_by(id: id)
  )
end

def fetch_items

def fetch_items
  fetch_original_items.map do |original_item|
    build_item(original_item)
  end
end

def fetch_method_name

def fetch_method_name
  collection[:fetch_method_name]
end

def fetch_original_items

def fetch_original_items
  return default_fetch_original_items unless fetch_method_name
  model_class.public_send(fetch_method_name,
                          site: fetch_site.call,
                          keyword: keyword,
                          max_items: max_items)
end

def image_field

def image_field
  collection.dig(:fields, :image)&.to_sym
end

def item_image(item)

def item_image(item)
  image_field && item.respond_to?(image_field) ? item.public_send(image_field) : nil
end

def item_label(item)

def item_label(item)
  label_field && item.respond_to?(label_field) ? item.public_send(label_field) : nil
end

def label_field

def label_field
  collection.dig(:fields, :label)&.to_sym
end

def model_class

def model_class
  collection[:model].constantize
end