Skip to content

Commit

Permalink
AnnotationsController - fetch action (#46)
Browse files Browse the repository at this point in the history
* AnnotationsController#fetch

Adds a fetch method which will harvest annotation data from Allmaps in real-time, should the nightly harvest data be insufficient.

* README: updates for AnnotationsController and recent enhancements
  • Loading branch information
ewlarson authored Apr 26, 2024
1 parent 8c8cb33 commit b842f44
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 11 deletions.
46 changes: 40 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,27 @@ LIGHT=blacklight bundle exec rails generate blacklight:allmaps:install
LIGHT=geoblacklight bundle exec rails generate blacklight:allmaps:install
```

## CatalogController Configuration

Configure options for local use are set into the `catalog_controller.rb` file.

### Blacklight

```ruby
# Blacklight::Allmaps Viewer
config.show.partials.insert(1, :blacklight_allmaps)
config.default_solr_unique_key = "id"
config.default_georeferenced_field = "bl_georeferenced_bsi"
config.default_iiif_manifest_field = "iiif_manifest_url_ssi"
```

### GeoBlacklight
```ruby
# Blacklight::Allmaps Viewer
config.default_solr_unique_key = "id"
config.default_georeferenced_field = "gbl_georeferenced_b"
```

## Rake Tasks

### Seed Fixtures
Expand Down Expand Up @@ -68,11 +89,8 @@ We expose the georeferenced items in the Blacklight user interface via a Georefe
![Screen shot](doc/georeferenced_facet.png)

```bash
# For Blacklight...
LIGHT=blacklight rake blacklight_allmaps:index:bl_georeferenced_facet

# For GeoBlacklight...
LIGHT=geoblacklight rake blacklight_allmaps:index:gbl_georeferenced_facet
# For Blacklight or GeoBlacklight
rake blacklight_allmaps:index:georeferenced_facet
```

## ActiveRecord Objects — Blacklight::Allmaps::Sidecar
Expand All @@ -81,7 +99,8 @@ Blacklight::Allmaps adopts the SolrDocumentSidecar "sidecar" pattern from [Spotl

We use this `document.sidecar_allmaps` object to hold the results of the Allmaps Annotation harvest task.

The Blacklight::Allmaps::Sidecar object contains:
The `Blacklight::Allmaps::Sidecar` object contains:

| Field | Value |
| --- | --- |
| id | primary key |
Expand Down Expand Up @@ -115,6 +134,21 @@ document.sidecar_allmaps =>
updated_at: Tue, 26 Mar 2024 16:39:42.427682000 UTC +00:00>
```
## Allmaps::AnnotationsController
We've added an annotations controller to the application to expose the `Blacklight::Allmaps::Sidecar` data we've harvested. This controller returns only JSON.
The `#fetch` method will query Allmaps in real time for updated annotation data, should a nightly harvest (rake) be insufficient for Solr indexing or local development needs.
```
Routes for Blacklight::Allmaps::Engine:
fetch_allmaps_annotation GET /allmaps/annotations/:id/fetch(.:format) allmaps/annotations#fetch {:format=>:json}
allmaps_annotations GET /allmaps/annotations(.:format) allmaps/annotations#index {:format=>:json}
allmaps_annotation GET /allmaps/annotations/:id(.:format) allmaps/annotations#show {:format=>:json}
PATCH /allmaps/annotations/:id(.:format) allmaps/annotations#update {:format=>:json}
PUT /allmaps/annotations/:id(.:format) allmaps/annotations#update {:format=>:json}
```
## Contributing
For Developer documentation see [doc/developer.md](./doc/development.md)
Expand Down
20 changes: 17 additions & 3 deletions app/controllers/allmaps/annotations_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class AnnotationsController < ApplicationController
# Set @annotation before show and update
before_action :set_annotation, only: %i[show update]

# GET /annotations.json
# GET /allmaps/annotations.json
def index
@annotations = Blacklight::Allmaps::Sidecar.order(:id).page params[:page]

Expand All @@ -17,14 +17,14 @@ def index
end
end

# GET /annotations/1.json
# GET /allmaps/annotations/1.json
def show
respond_to do |format|
format.all { render json: @annotation }
end
end

# PATCH/PUT /annotations/1.json
# PATCH/PUT /allmaps/annotations/1.json
def update
# Background Job to store the Allmaps Annotation
Blacklight::Allmaps::StoreSidecarAnnotation.perform_later(@annotation.solr_document_id)
Expand All @@ -34,6 +34,20 @@ def update
end
end

# GET /annotations/fetch/1.json
def fetch
# Background Job to store the Allmaps Annotation — Perform Now

Blacklight::Allmaps::StoreSidecarAnnotation.perform_now(params[:id])
set_annotation

respond_to do |format|
format.all { render json: @annotation }
end
rescue Blacklight::Exceptions::RecordNotFound
render json: {error: "Record not found"}, status: :not_found
end

private

# Find the annotation or throw a 404
Expand Down
6 changes: 5 additions & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
Blacklight::Allmaps::Engine.routes.draw do
namespace :allmaps do
resources :annotations, only: [:index, :show, :update], defaults: {format: :json}
resources :annotations, only: [:index, :show, :update], defaults: {format: :json} do
member do
get "fetch"
end
end
end
end
12 changes: 12 additions & 0 deletions spec/controllers/annotations_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,16 @@
end
end
end

describe "GET #fetch" do
it "fetches the annotation and returns http success" do
ActiveJob::Base.queue_adapter = :test

annotation = FactoryBot.create(:annotation)

# Only check for route existence in this test
# StoreSidecarAnnotation.perform_now will error b/c the annotation does not exist
get :fetch, params: {id: annotation.id}
end
end
end
1 change: 0 additions & 1 deletion spec/helpers/blacklight/allmaps/application_helper_spec.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# frozen_string_literal: true

require "spec_helper"
require "blacklight/allmaps/application_helper"

# @TODO: GeoBlacklight dependent
describe Blacklight::Allmaps::ApplicationHelper, type: :helper do
Expand Down

0 comments on commit b842f44

Please sign in to comment.