From 119a15af993b95ab10eca0f6face75fe3e812eab Mon Sep 17 00:00:00 2001 From: Jason Gessner Date: Sat, 9 May 2026 22:15:00 +0000 Subject: [PATCH] Add resource and test for latest PublishedDatabase. --- .../published_databases_controller.rb | 14 +++++++++++ app/resources/published_database_resource.rb | 20 ++++++++++++++++ config/routes.rb | 1 + .../published_database_resource_reads_spec.rb | 23 +++++++++++++++++++ 4 files changed, 58 insertions(+) create mode 100644 app/controllers/published_databases_controller.rb create mode 100644 app/resources/published_database_resource.rb create mode 100644 spec/resources/published_database_resource_reads_spec.rb diff --git a/app/controllers/published_databases_controller.rb b/app/controllers/published_databases_controller.rb new file mode 100644 index 00000000..0c8826c4 --- /dev/null +++ b/app/controllers/published_databases_controller.rb @@ -0,0 +1,14 @@ +# frozen_string_literal: true + +# Controller for the PublishedDatabase resource. +class PublishedDatabasesController < ApplicationController + def index + add_total_stat(params) + published_databases = PublishedDatabaseResource.all(params) + respond_with(published_databases) + end + + def add_total_stat(params) + # don't return stats + end +end diff --git a/app/resources/published_database_resource.rb b/app/resources/published_database_resource.rb new file mode 100644 index 00000000..3bff3713 --- /dev/null +++ b/app/resources/published_database_resource.rb @@ -0,0 +1,20 @@ +# frozen_string_literal: true + +# Public resource for the PublishedDatabase object. +class PublishedDatabaseResource < ApplicationResource + primary_endpoint '/published_databases', [:index] + + attribute :url, :string + attribute :updated_at, :datetime + attribute :created_at, :datetime + + def base_scope + latest_id = PublishedDatabase.order(created_at: :desc).select(:id).limit(1) + PublishedDatabase.where(id: latest_id) + end + + # Only return the latest database. + def default_page_size + 1 + end +end diff --git a/config/routes.rb b/config/routes.rb index 55be6a30..02cd692f 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -18,6 +18,7 @@ resources :formats, only: %i[index show] resources :illustrators, only: %i[index show] resources :printings, only: %i[index show] + resources :published_databases, only: [:index] resources :restrictions, only: %i[index show] resources :reviews, only: %i[index show] resources :rulings, only: %i[index show] diff --git a/spec/resources/published_database_resource_reads_spec.rb b/spec/resources/published_database_resource_reads_spec.rb new file mode 100644 index 00000000..b92ab317 --- /dev/null +++ b/spec/resources/published_database_resource_reads_spec.rb @@ -0,0 +1,23 @@ +# frozen_string_literal: true + +require 'rails_helper' + +RSpec.describe PublishedDatabaseResource, type: :resource do + describe 'serialization' do + fixtures :published_databases + + let(:new_db) { published_databases(:latest_db) } + + it 'works and only returns the latest database' do + render + data = jsonapi_data + + expect(data.length).to eq(1) + + record = data[0] + expect(record.url).to eq(new_db.url) + expect(Time.parse(record.created_at).to_i).to eq(new_db.created_at.to_i) + expect(record.jsonapi_type).to eq('published_databases') + end + end +end