ActiveRecord to JSON API Part 2: Solving Pagination


This post is by Mary Lee from Hashrocket Articles


Click here to view on the original site: Original Post




While building an internal JSON API for a Rails application, we came across a few issues with pagination.

  • Making Kaminari work
  • Resolving problems with pagination link parameters
  • Enabling pagination while still allowing users to get everything

Background

We were building an internal JSON API using the following libraries:

For more information on our goals and set up for the internal API, see part 1 of this series.

Kaminari

The application we were working on was using Kaminari for pagination, and one of the first things we noticed during our switch from ActiveRecord to JsonApiClient::Resources was that pagination no longer worked, but only when the page number param was not present.

Upon investigating the source code for JsonApiClient, we noticed that when the page number param was blank during pagination, the page was resolved to 0,

Continue reading “ActiveRecord to JSON API Part 2: Solving Pagination”

ActiveRecord to JSON API Part 1: Our Approach


This post is by Mary Lee from Hashrocket Articles


Click here to view on the original site: Original Post




In a recent Rails project, we were tasked with the migration of a group of ActiveRecord models to a microservice that had not yet been built.

This raised an interesting question for us: how do we migrate data out of an application when we don’t have anything to replace it with?

Reasoning

The originally proposed solution was to build a layer between the controllers and models that would delegate out to the ActiveRecord models for now, and then when the new microservice was done, would delegate out to an ActiveResource object. This solution raised some concerns for us.

  • How would we update the models and jobs that interacted with the models being extracted, when the layer was only present in the controllers?
  • How do we know the performance impact of moving from a database record to fetching data from an API?
  • How do we know we’ve properly converted everything when

    Continue reading “ActiveRecord to JSON API Part 1: Our Approach”

Integrating with Google Calendar as a Service App


This post is by Mary Lee from Hashrocket Articles


Click here to view on the original site: Original Post




Recently, I was working on a project where the goal was to have a shared calendar for the application that all the users would have access to, but no individual user would have ownership of. This seemed like the perfect use case to have a service app owned calendar that gave access to users based on email addresses.

I started by going to the Google developers console, creating a new project, creating service app account credentials, and enabling the Google Calendar API.

I used two libraries for setting up the integration:

Basic App Set Up

I was working with users that had access to many calendars, and those calendars had many events.

class User < ApplicationRecord
  has_many :calendar_users
  has_many :calendars, through: :calendar_users
end

class Calendar < ApplicationRecord
  has_many :calendar_users
  has_many :users, through: :calendar_users
  has_many 
Continue reading "Integrating with Google Calendar as a Service App"

Exploring the Default Postgres Template Databases


This post is by Mary Lee from Hashrocket Articles


Click here to view on the original site: Original Post




I was recently poking around the psql terminal, and noticed something that I had never had to deal with before: the Postgres template databases. I decided to seize the moment and launched myself into an exploration of what the template databases are meant to do, and why the heck there are two of them.

My first plan of attack was to just list them and see if I could find a difference right away, but everything looked exactly the same.

postgres=# \l
                                   List of databases
   Name   |   Owner  | Encoding |   Collate   |    Ctype    |   Access privileges
----------+----------+----------+-------------+-------------+-----------------------
postgres  | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 |
template0 | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/postgres + postgres=CTc/postgres
template1 | postgres | UTF8     | en_US.UTF
Continue reading "Exploring the Default Postgres Template Databases"