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