-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #119 from curationexperts/importer
Add an Item model and update import job
- Loading branch information
Showing
33 changed files
with
703 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
module Admin | ||
# Controller for UI to manage individual Items stored in the repository | ||
class ItemsController < ApplicationController | ||
before_action :set_item, only: %i[show edit update destroy] | ||
|
||
# GET /items or /items.json | ||
def index | ||
@items = Item.all | ||
end | ||
|
||
# GET /items/1 or /items/1.json | ||
def show; end | ||
|
||
# GET /items/new | ||
def new | ||
blueprint_id = params['blueprint_id'] | ||
@item = Item.new(blueprint_id: blueprint_id) | ||
@blueprint = Blueprint.find(blueprint_id) if blueprint_id | ||
end | ||
|
||
# GET /items/1/edit | ||
def edit; end | ||
|
||
# POST /items or /items.json | ||
def create | ||
@item = Item.new(item_params) | ||
|
||
respond_to do |format| | ||
if @item.save | ||
format.html { redirect_to item_url(@item), notice: 'Item was successfully created.' } | ||
format.json { render :show, status: :created, location: @item } | ||
else | ||
format.html { render :new, status: :unprocessable_entity } | ||
format.json { render json: @item.errors, status: :unprocessable_entity } | ||
end | ||
end | ||
end | ||
|
||
# PATCH/PUT /items/1 or /items/1.json | ||
def update | ||
respond_to do |format| | ||
if @item.update(item_params) | ||
format.html { redirect_to item_url(@item), notice: 'Item was successfully updated.' } | ||
format.json { render :show, status: :ok, location: @item } | ||
else | ||
format.html { render :edit, status: :unprocessable_entity } | ||
format.json { render json: @item.errors, status: :unprocessable_entity } | ||
end | ||
end | ||
end | ||
|
||
# DELETE /items/1 or /items/1.json | ||
def destroy | ||
@item.destroy | ||
|
||
respond_to do |format| | ||
format.html { redirect_to items_url, notice: 'Item was successfully destroyed.' } | ||
format.json { head :no_content } | ||
end | ||
end | ||
|
||
private | ||
|
||
# Use callbacks to share common setup or constraints between actions. | ||
def set_item | ||
@item = Item.find(params[:id]) | ||
end | ||
|
||
# Only allow a list of trusted parameters through. | ||
def item_params | ||
params.require(:item).permit(:blueprint_id, description: {}) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
# Basic repository object, smallest unit of discovery | ||
class Item < ApplicationRecord | ||
belongs_to :blueprint | ||
|
||
after_save :update_index | ||
|
||
def to_partial_path | ||
"admin/#{super}" | ||
end | ||
|
||
def update_index | ||
save if changed? | ||
document = to_solr | ||
solr_connection.update params: {}, data: { add: { doc: document } }.to_json, | ||
headers: { 'Content-Type' => 'application/json' } | ||
solr_connection.update params: {}, data: { commit: {} }.to_json, headers: { 'Content-Type' => 'application/json' } | ||
end | ||
|
||
def to_solr | ||
doc = [] | ||
doc << ['blueprint_ssi', blueprint.name] | ||
doc << ['id', id] | ||
blueprint.fields.each do |field| | ||
label = field.solr_field_name | ||
value = description[field.display_label] | ||
doc << [label, value] | ||
end | ||
doc.to_h | ||
end | ||
|
||
private | ||
|
||
def solr_connection | ||
@solr_connection ||= CatalogController.blacklight_config.repository.connection | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<h2>Choose a Blueprint</h2> | ||
<div id='choose_blueprint'> | ||
<% Blueprint.all.each do |blueprint| %> | ||
<%= form_with(model: item) do |form| %> | ||
<%= form.hidden_field :blueprint_id, value: blueprint.id %> | ||
<%= form.submit blueprint.name %> | ||
<% end %> | ||
<% end %> | ||
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<%= form_with(model: item, id: 'item_fields') do |form| %> | ||
<% if item.errors.any? %> | ||
<div style="color: red"> | ||
<h2><%= pluralize(item.errors.count, "error") %> prohibited this item from being saved:</h2> | ||
|
||
<ul> | ||
<% item.errors.each do |error| %> | ||
<li><%= error.full_message %></li> | ||
<% end %> | ||
</ul> | ||
</div> | ||
<% end %> | ||
|
||
<div> | ||
<%= form.label :blueprint_id, style: "display: block" %> | ||
<%= form.text_field :blueprint_id %> | ||
</div> | ||
|
||
<div> | ||
<%= form.label :description, style: "display: block" %> | ||
<%= form.text_field :description %> | ||
</div> | ||
|
||
<div> | ||
<%= form.submit %> | ||
</div> | ||
<% end %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<div id="<%= dom_id item %>"> | ||
<p> | ||
<strong>Blueprint:</strong> | ||
<%= item.blueprint_id %> | ||
</p> | ||
|
||
<p> | ||
<strong>Description:</strong> | ||
<%= item.description %> | ||
</p> | ||
|
||
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
json.extract! item, :id, :blueprint_id, :description, :created_at, :updated_at | ||
json.url item_url(item, format: :json) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<h1>Editing item</h1> | ||
|
||
<%= render "form", item: @item %> | ||
|
||
<br> | ||
|
||
<div> | ||
<%= link_to "Show this item", @item %> | | ||
<%= link_to "Back to items", items_path %> | ||
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<p style="color: green"><%= notice %></p> | ||
|
||
<h1>Items</h1> | ||
|
||
<div id="items"> | ||
<% @items.each do |item| %> | ||
<%= render item %> | ||
<p> | ||
<%= link_to "Show this item", item %> | ||
</p> | ||
<% end %> | ||
</div> | ||
|
||
<%= link_to "New item", new_item_path %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
json.array! @items, partial: 'items/item', as: :item |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<h1>New item</h1> | ||
|
||
<% if @blueprint %> | ||
<%= render "form", item: @item, blueprint: @blueprint %> | ||
<% else %> | ||
<%= render "choose_blueprint", item: @item, blueprint: @blueprint %> | ||
<% end %> | ||
|
||
<br> | ||
|
||
<div> | ||
<%= link_to "Back to items", items_path %> | ||
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<p style="color: green"><%= notice %></p> | ||
|
||
<%= render @item %> | ||
|
||
<div> | ||
<%= link_to "Edit this item", edit_item_path(@item) %> | | ||
<%= link_to "Back to items", items_path %> | ||
|
||
<%= button_to "Destroy this item", @item, method: :delete %> | ||
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
json.partial! 'items/item', item: @item |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
class CreateItems < ActiveRecord::Migration[7.0] | ||
def change | ||
create_table :items do |t| | ||
t.references :blueprint, null: false, foreign_key: true | ||
t.jsonb :description | ||
|
||
t.timestamps | ||
end | ||
end | ||
end |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,16 @@ | ||
FactoryBot.define do | ||
factory :blueprint do | ||
name { Faker::Hipster.words.join(' ').gsub(/[^0-9A-Za-z\-_ ]/, '') } | ||
sequence(:name) { |n| "blank_blueprint_#{n}" } | ||
end | ||
|
||
factory :blueprint_with_basic_fields, class: 'Blueprint' do | ||
sequence(:name) { |n| "basic_blueprint_#{n}" } | ||
fields do | ||
[ | ||
build(:field_config, display_label: 'title', solr_suffix: '*_tesi'), | ||
build(:field_config, display_label: 'author', solr_suffix: '*_tesim'), | ||
build(:field_config, display_label: 'date', solr_suffix: '*_isi') | ||
] | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
FactoryBot.define do | ||
factory :item do | ||
blueprint | ||
description { '' } | ||
end | ||
|
||
factory :populated_item, class: 'Item' do | ||
blueprint factory: %i[blueprint_with_basic_fields] | ||
description do | ||
{ | ||
'title' => 'One Hundred Years of Solitute', | ||
'author' => 'Márquez, Gabriel García', | ||
'date' => '1967' | ||
} | ||
end | ||
end | ||
end |
Oops, something went wrong.