From 3374d8294df4262c588aeeb7b2ecc3c00cc3a85b Mon Sep 17 00:00:00 2001 From: psguazz Date: Thu, 7 Mar 2024 22:52:15 +0100 Subject: [PATCH] More caching Initializing models immediately. --- lib/dato_cms_graphql/model_iterator.rb | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/lib/dato_cms_graphql/model_iterator.rb b/lib/dato_cms_graphql/model_iterator.rb index a823dbc..4fac3c3 100644 --- a/lib/dato_cms_graphql/model_iterator.rb +++ b/lib/dato_cms_graphql/model_iterator.rb @@ -2,6 +2,8 @@ module DatoCmsGraphql class ModelIterator include Enumerable + delegate :each, :[], to: :localized_results + def initialize(model) @model = model @query = model::INDEX @@ -11,19 +13,19 @@ def initialize(model) @pages = @count / @page_size + ((@count % @page_size).positive? ? 1 : 0) end - def each - results["#{I18n.locale}_items"].each do |element| - yield @model.new(element) - end - end - private + def localized_results + results["#{I18n.locale}_items"] + end + def results @results ||= (0..@pages).each_with_object({}) do |page, rs| res_page = DatoCmsGraphql.query(@query, variables: {skip: @page_size * page}) res_page.each do |k, v| + v = v.map { |m| @model.new(m) } + rs[k] ||= [] rs[k].concat(v) end