Skip to content

Commit

Permalink
More caching
Browse files Browse the repository at this point in the history
Initializing models immediately.
  • Loading branch information
psguazz committed Mar 7, 2024
1 parent 7cf0d84 commit 3374d82
Showing 1 changed file with 8 additions and 6 deletions.
14 changes: 8 additions & 6 deletions lib/dato_cms_graphql/model_iterator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ module DatoCmsGraphql
class ModelIterator
include Enumerable

delegate :each, :[], to: :localized_results

def initialize(model)
@model = model
@query = model::INDEX
Expand All @@ -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
Expand Down

0 comments on commit 3374d82

Please sign in to comment.