diff --git a/lib/quo/query.rb b/lib/quo/query.rb index de12103..fc8f21d 100644 --- a/lib/quo/query.rb +++ b/lib/quo/query.rb @@ -97,9 +97,9 @@ def first(limit = nil) if transform? res = query_with_logging.first(limit) if res.is_a? Array - res.map.with_index { |r, i| transformer.call(r, i) } + res.map.with_index { |r, i| transformer&.call(r, i) } elsif !res.nil? - transformer.call(query_with_logging.first(*args)) + transformer&.call(query_with_logging.first(limit)) end else query_with_logging.first(limit) @@ -117,9 +117,9 @@ def last(limit = nil) if transform? res = query_with_logging.last(limit) if res.is_a? Array - res.map.with_index { |r, i| transformer.call(r, i) } + res.map.with_index { |r, i| transformer&.call(r, i) } elsif !res.nil? - transformer.call(query_with_logging.last(*args)) + transformer&.call(res) end else query_with_logging.last(limit) @@ -129,7 +129,7 @@ def last(limit = nil) # Convert to array def to_a arr = query_with_logging.to_a - transform? ? arr.map.with_index { |r, i| transformer.call(r, i) } : arr + transform? ? arr.map.with_index { |r, i| transformer&.call(r, i) } : arr end # Convert to EagerQuery, and load all data @@ -226,7 +226,11 @@ def transformer def offset per_page = sanitised_page_size - page = current_page&.positive? ? current_page : 1 + page = if current_page && current_page&.positive? + current_page + else + 1 + end per_page * (page - 1) end diff --git a/lib/quo/results.rb b/lib/quo/results.rb index bef3c9f..ed3e2f0 100644 --- a/lib/quo/results.rb +++ b/lib/quo/results.rb @@ -28,7 +28,7 @@ def group_by(&block) grouped = unwrapped.group_by do |*block_args| x = block_args.first transformed = transformer ? transformer.call(x) : x - block.call(transformed, *block_args[1..]) + block ? block.call(transformed, *(block_args[1..] || [])) : transformed end grouped.tap do |groups| diff --git a/lib/quo/utilities/callstack.rb b/lib/quo/utilities/callstack.rb index a17e887..e81f4b2 100644 --- a/lib/quo/utilities/callstack.rb +++ b/lib/quo/utilities/callstack.rb @@ -12,7 +12,7 @@ def debug_callstack stack = Kernel.caller.grep_v(exclude).map { |l| l.gsub(working_dir + "/", "") } stack_to_display = stack[0..callstack_size] message = "\n[Query stack]: -> #{stack_to_display&.join("\n &> ")}\n" - message += " (truncated to #{callstack_size} most recent)" if stack.size > callstack_size + message += " (truncated to #{callstack_size} most recent)" if callstack_size && stack.size > callstack_size Quo.configuration.logger&.info(message) end end