Skip to content

Commit

Permalink
Add JSON endpoint for CitationsController#index
Browse files Browse the repository at this point in the history
  • Loading branch information
gbp committed Oct 4, 2024
1 parent b89e691 commit a914b8a
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
7 changes: 7 additions & 0 deletions app/controllers/citations_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
16 changes: 16 additions & 0 deletions app/models/citation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

0 comments on commit a914b8a

Please sign in to comment.