From a914b8a18a9ec6b5747064944fc5e53d1e6cb39a Mon Sep 17 00:00:00 2001 From: Graeme Porteous Date: Fri, 4 Oct 2024 16:13:33 +0100 Subject: [PATCH] Add JSON endpoint for CitationsController#index --- app/controllers/citations_controller.rb | 7 +++++++ app/models/citation.rb | 16 ++++++++++++++++ 2 files changed, 23 insertions(+) diff --git a/app/controllers/citations_controller.rb b/app/controllers/citations_controller.rb index 70b6380782..1be2c5bef5 100644 --- a/app/controllers/citations_controller.rb +++ b/app/controllers/citations_controller.rb @@ -6,10 +6,17 @@ class CitationsController < ApplicationController before_action :load_resource_and_authorise, except: :index before_action :set_in_pro_area, except: :index + skip_before_action :html_response, only: :index + def index per_page = 10 page = get_search_page_from_params @citations = Citation.not_embargoed.paginate(page: page, per_page: per_page) + + respond_to do |format| + format.html { @has_json = true } + format.json { render json: @citations } + end end def new diff --git a/app/models/citation.rb b/app/models/citation.rb index e06dd6fcd6..e6bf9a91ee 100644 --- a/app/models/citation.rb +++ b/app/models/citation.rb @@ -17,6 +17,9 @@ # A Citation of an InfoRequest or InfoRequestBatch # class Citation < ApplicationRecord + include Rails.application.routes.url_helpers + include LinkToHelper + self.inheritance_column = nil belongs_to :user, inverse_of: :citations @@ -61,4 +64,17 @@ class Citation < ApplicationRecord def applies_to_batch_request? citable.is_a?(InfoRequestBatch) end + + def as_json(_options) + citable_path = case citable + when InfoRequest + request_path(citable) + when InfoRequestBatch + info_request_batch_path(citable) + end + + attributes. + except('user_id', 'citable_id', 'citable_type'). + merge(citable_path: citable_path) + end end