Skip to content

Commit

Permalink
fix TypeError on pagination
Browse files Browse the repository at this point in the history
`FlexSearch.some_query :page => 1, :size => 10` used to raise
"TypeError: nil can't be coerced into Fixnum".

That's because the `:from` computation used to be of the form
`(page - 1) * vars[:params][:size] || vars[:size] || 10`, which gets
operator precedence wrong: it's parsed the same as
`((page - 1) * vars[:params][:size]) || vars[:size] || 10`. So, if
`vars[:page]` is specified but `vars[:params][:size]` isn't, we
multiply a number by a nil, which raises an error.

We *could* just apply better grouping, but a new variable
declaration is more readable, anyway.
  • Loading branch information
matchu committed Jan 18, 2013
1 parent 2c2cc67 commit 13d45e0
Showing 1 changed file with 2 additions and 1 deletion.
3 changes: 2 additions & 1 deletion lib/flex/template/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ def process_vars(vars)
vars[:params] ||= {}
page = vars[:page].to_i
page = 1 unless page > 0
vars[:params][:from] = ((page - 1) * vars[:params][:size] || vars[:size] || 10).ceil
size = vars[:params][:size] || vars[:size] || 10
vars[:params][:from] = ((page - 1) * size).ceil
end
vars
end
Expand Down

0 comments on commit 13d45e0

Please sign in to comment.