ActiveRecord to JSON API Part 7: Testing


This post is by Hashrocket from Hashrocket


Click here to view on the original site: Original Post




To wrap up this series, let’s talk about the helpers we added to make writing tests with the JsonApiClient resources less painful.

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.

Creating Factories

FactoryBot makes it incredibly easy to build factories for non-database backed resources using the skip_create option. We made factories for all of our resources, to make it easier to stub and work with the resources in tests.

factory :foo do
  skip_create
  sequence(:id)
  sequence(:title) { |n| "title#{n}" }
end

Setting Up Test Helpers

Writing WebMock stubs for JsonApiClient required a lot of knowledge of JSON API specifications, and was Continue reading “ActiveRecord to JSON API Part 7: Testing”

ActiveRecord to JSON API Part 6: Mimicking AR


This post is by Hashrocket from Hashrocket


Click here to view on the original site: Original Post




As we worked through the conversion from ActiveRecord to JsonApiClient, we came across some methods implemented by ActiveRecord that would still be useful to have around.

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.

Pluck

One of the first things we noticed we were converting over and over again was ActiveRecord’s pluck method. With the JsonApiClient resources, we were converting all the ActiveRecord pluck calls to chaining select(:field).map(&:field).

Not only was this repetitive and time consuming to convert, but we felt like it was burdensome for future developers on the application to have to remember to do.

We decided to try to figure out how we could implement pluck on the Continue reading “ActiveRecord to JSON API Part 6: Mimicking AR”

ActiveRecord to JSON API Part 5: Polymorphism


This post is by Hashrocket from Hashrocket


Click here to view on the original site: Original Post




The Rails application we were working with had a number of polymorphic relationships that included the models we were moving to the JSON API, as well as a polymorphic relationship within the models being migrated.

This raised an interesting question for us. How do we maintain those relationships without changing the core functionality of the app and the models involved? And how will the polymorphic API associations function?

We wound up facing two issues with polymorphism during the conversion process.

  1. Making the ActiveRecord resources remaining in the application capable of being related to both other ActiveRecord resources, and to JsonApiClient resources
  2. Making JsonApiClient resources fetch polymorphic API associations when the associated data was not eager loaded from the API.

Background

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

For more information on Continue reading “ActiveRecord to JSON API Part 5: Polymorphism”

ActiveRecord to JSON API Part 4: Custom Sorting


This post is by Hashrocket from Hashrocket


Click here to view on the original site: Original Post




The JSONAPI::Resources automatically supported sorting on database fields, but didn’t handle custom sorting without requiring overrides to library methods.

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.

Custom Sorts

In a few of the ActiveRecord models we were converting, there were some custom sort methods that we needed to be able to specify through the API.

class LocalAPI::Foo < ApplicationRecord
  # …

  def self.abcs_last
    order("CASE foos.some_field WHEN 'abc' THEN 1 ELSE 0")
  end

  # … 
end

The JSONAPI::Resources automatically supported sorting on database fields, but in their documentation, they specify that any custom sorts require overriding the apply_sort method on the specific JSONAPI::Resource you’re working Continue reading “ActiveRecord to JSON API Part 4: Custom Sorting”

ActiveRecord to JSON API Part 3: Handling Filtering


This post is by Hashrocket from Hashrocket


Click here to view on the original site: Original Post




Did you know that Puma has a 10,240 character query string limit? We do now.

In the process of building our internal JSON API, we found two issues with handling filtering:

  1. Query string limitations
  2. Querying for an array of IDs when some values were nil

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.

Query Strings

Given that the JsonApiClient filtering methods use an HTTP GET with query string parameters, character limits quickly became a concern for us. How could we ensure that some of our lengthier queries weren’t exceeding the limit and failing?

The simplest solution to us was to make the filters a POST request, even if that was a divergence Continue reading “ActiveRecord to JSON API Part 3: Handling Filtering”

ActiveRecord to JSON API Part 2: Solving Pagination


This post is by Hashrocket from Hashrocket


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”

Action Cable demo by DHH in Rails 5


This post is by from Robby on Rails


Click here to view on the original site: Original Post




David was kind enough to put together a demonstration of how to take advantage of Action Cable in Rails 5.

<p>In the video he puts together a small chat application.</p><div class="feedflare">