Skip to content

Commit

Permalink
Merge pull request #30 from crystal-garage/fix-collection-each
Browse files Browse the repository at this point in the history
fix Collection#each method
  • Loading branch information
mamantoha authored Jan 13, 2025
2 parents c4ac908 + 09d8ce9 commit b6fc53e
Showing 1 changed file with 9 additions and 9 deletions.
18 changes: 9 additions & 9 deletions src/clear/model/collection.cr
Original file line number Diff line number Diff line change
Expand Up @@ -319,26 +319,26 @@ module Clear::Model

# Build the SQL, send the query then iterate through each models
# gathered by the request.
def each(fetch_columns = false, &block : T ->)
cr = @cached_result
def each(fetch_columns = false, & : T ->) : Nil
result = @cached_result

if cr
cr.each(&block)
else
o = [] of T
unless result
result = [] of T

if @polymorphic
fetch(fetch_all: false) do |hash|
type = hash[@polymorphic_key].as(String)
o << Clear::Model::Factory.build(type, hash, persisted: true, fetch_columns: fetch_columns, cache: @cache).as(T)
result << Clear::Model::Factory.build(type, hash, persisted: true, fetch_columns: fetch_columns, cache: @cache).as(T)
end
else
fetch(fetch_all: false) do |hash|
o << Clear::Model::Factory.build(T, hash, persisted: true, fetch_columns: fetch_columns, cache: @cache)
result << Clear::Model::Factory.build(T, hash, persisted: true, fetch_columns: fetch_columns, cache: @cache)
end
end
end

o.each(&block)
result.each do |value|
yield value
end
end

Expand Down

0 comments on commit b6fc53e

Please sign in to comment.