A code blogger once tweeted that learning a new framework is really just learning a new way of handling pagination. There is a very good pagination module for Angular JS and a very good pagination gem for Rails,but the two do not speak the same language. Instead of rolling my own solution that would not work as nicely, I decided to write a little bit of glue code to make the solutions compatible.
Within the web client, I required a pagination gem that would accept url parameters for filtering, handle the layout, be Bootstrap compatible, and have enough configuration options to handle multiple use-cases. Angular-paginate-anything clearly fit that bill. On the Rails side, our use of ActiveAdmin already brought in Kaminari. Joe Nelson, the author of the Angular module, does have a compatible Ruby gem, but I did not want to litter my code with two different pagination styles that depended on the caller.
The first step was to use
angular-paginate-anything as intended:
That was straight forward. The problem is that
angular-paginate-anything wants to paginate on ranges, but
kaminari wants to paginate on pages. The range coming from the browser code needs to be translated into a page number and vis versa for the response. I choose to do this with custom
after filters defined in the base controller of my API. My approach was inspired by the clean_pagination gem and Javier Saldana's pagination with ActiveResource blog post.
My method makes
kp_per_page (kp for KaminariPagination) available as instance variables. Values could also be put into the
ActionController needs to set the filter and use the newly scoped instance variables for paging.
Now I've got a paginated setup that works well and looks idiomatic in both Angular and Rails. I've also made this code available as a gist for easier sharing and modifications.
Did you like this? Please share:
The Lost Year: A Failed Experiment to Switch Away From Mac
Fed up with the Apple Keyboard, I bought a ThinkPad, installed Linux, and promptly decided that I hated computers.
Maker's Space, Manager's Space
The Grand Remote Work Experiment: A Retrospective
The COVID-19 pandemic has lead to an unexpected experiment in remote working. What has worked and why?