I'm trying to fully understand what this method in Ruby does. Basically
it passes a collection of records from the database (using the Rails
find method) into the argument list as a local variable called
resources. It assigns the local variable to an instance variable called
@all_resources. Then it checks the query string in the url to see if the
limit key has a value and if so grab it(e.g. ?limit=10), otherwise
default to 25. Then it checks the query string for a page key and if
none is there, it defaults to 1 and assigns it to instance variable
@page. Then we subtract 1 from @page, presumably because if we are on
page 1, then 1 -1 = 0, so 0 is assigned to @page_index. This means when
we multiply page_index by 25 (limit), we get 0, and so when we slice the
resources we slice the limit (25) by offset (0), and hence we return the
first 25 records, making sense since we want to display first 35 records
on page 1. However, if $page is equal to 2 and limit 25, then
page_index is equal to 1, and offset equal to 25. But I dont see how we
are getting the next 25 records on page 2.
I think what's confusing me is this line of code:
((@all_resources.size - 1) / @limit).to_i + 1
What does the size method do and why add one at the end?
I'm trying to fully understand what this method in Ruby does. Basically
it passes a collection of records from the database (using the Rails
find method) into the argument list as a local variable called
resources. It assigns the local variable to an instance variable called
@all_resources. Then it checks the query string in the url to see if the
limit key has a value and if so grab it(e.g. ?limit=10), otherwise
default to 25. Then it checks the query string for a page key and if
none is there, it defaults to 1 and assigns it to instance variable
@page. Then we subtract 1 from @page, presumably because if we are on
page 1, then 1 -1 = 0, so 0 is assigned to @page_index. This means when
we multiply page_index by 25 (limit), we get 0, and so when we slice the
resources we slice the limit (25) by offset (0), and hence we return the
first 25 records, making sense since we want to display first 35 records
on page 1. However, if $page is equal to 2 and limit 25, then
page_index is equal to 1, and offset equal to 25. But I dont see how we
are getting the next 25 records on page 2.
Have a look at the Rails Guide on debugging and find out how to use
ruby-debug to break into the code and inspect data. Then break in at
the appropriate points and see what is happening. By a combination of
examining variables, entering expressions for evaluation to see what
the results are and stepping through the code you should see how (if)
it works.
I think what's confusing me is this line of code:
((@all_resources.size - 1) / @limit).to_i + 1
What does the size method do and why add one at the end?
The size method is Array#size and returns the number of elements in
the array, ie the number of records in @all_resources in this case.
BTW, if you found this in production code, I'd highly recommend
KILLING IT WITH FIRE and using something like will_paginate if at all
possible - this is the quite-ancient original pagination code included
with Rails (stripped out to the classic_pagination plugin in 1.2.4!)
and is immensely inefficient. The Rails version of your code really
doesn't matter; I just got will_paginate up and running on an old
1.2.3 app without any major issues.