Skip to content

Commit

Permalink
Dynamically load the thumbnail list
Browse files Browse the repository at this point in the history
This ensures the edit page can load quickly
  • Loading branch information
jcoyne committed May 23, 2017
1 parent 84cceac commit 3a9ef03
Show file tree
Hide file tree
Showing 8 changed files with 81 additions and 4 deletions.
22 changes: 22 additions & 0 deletions app/assets/javascripts/hyrax/admin/admin_set_controls.es6
@@ -1,5 +1,7 @@
export default class {
constructor(elem) {
this.loadThumbnailOptions(elem)

let Participants = require('hyrax/admin/admin_set/participants');
let participants = new Participants(elem.find('#participants'))
participants.setup();
Expand All @@ -8,4 +10,24 @@ export default class {
let visibilityTab = new Visibility(elem.find('#visibility'));
visibilityTab.setup();
}

// Dynamically load the file options into the "Thumbnail" select field.
loadThumbnailOptions(elem) {
let url = window.location.pathname.replace('edit', 'files')
elem.find('#admin_set_thumbnail_id').select2({
ajax: { // Use the jQuery.ajax wrapper provided by Select2
url: url,
dataType: "json",
results: function(data, page) {
return { results: data }
}
},
initSelection: function(element, callback) {
// the input tag has a value attribute preloaded that points to a preselected repository's id
// this function resolves that id attribute to an object that select2 can render
// using its formatResult renderer - that way the repository name is shown preselected
callback({ text: $(element).data('text') })
}
})
}
}
10 changes: 10 additions & 0 deletions app/controllers/hyrax/admin/admin_sets_controller.rb
Expand Up @@ -44,6 +44,16 @@ def edit
setup_form
end

# Renders a JSON response with a list of files in this admin set.
# This is used by the edit form
def files
form = form_class.new(@admin_set)
result = form.select_files.map do |label, id|
{ id: id, text: label }
end
render json: result
end

def update
if @admin_set.update(admin_set_params)
redirect_to hyrax.edit_admin_admin_set_path(@admin_set), notice: I18n.t('updated_admin_set', scope: 'hyrax.admin.admin_sets.form.permission_update_notices', name: @admin_set.title.first)
Expand Down
5 changes: 5 additions & 0 deletions app/forms/hyrax/forms/admin_set_form.rb
Expand Up @@ -22,6 +22,11 @@ def permission_template
end
end

def thumbnail_title
return unless model.thumbnail
model.thumbnail.title.first
end

class << self
# This determines whether the allowed parameters are single or multiple.
# By default it delegates to the model.
Expand Down
4 changes: 3 additions & 1 deletion app/views/hyrax/admin/admin_sets/_form.html.erb
Expand Up @@ -23,7 +23,9 @@
<%= render 'form_metadata', f: f %>
<% if f.object.persisted? && f.object.member_ids.present? %>
<%= f.input :thumbnail_id, collection: @form.select_files %>
<%# we're loading these values dynamically to speed page load %>
<%= f.input :thumbnail_id,
input_html: { data: { text: f.object.thumbnail_title } } %>
<% end %>

</div>
Expand Down
3 changes: 3 additions & 0 deletions config/routes.rb
Expand Up @@ -186,6 +186,9 @@

namespace :admin do
resources :admin_sets do
member do
get :files
end
resource :permission_template
end
resources :users, only: [:index]
Expand Down
18 changes: 16 additions & 2 deletions spec/controllers/hyrax/admin/admin_sets_controller_spec.rb
Expand Up @@ -13,8 +13,6 @@
end

describe "#new" do
let!(:admin_set) { create(:admin_set) }

it 'is unauthorized' do
get :new
expect(response).to be_redirect
Expand All @@ -32,6 +30,14 @@
end
end
end

describe "#files" do
let(:admin_set) { create(:admin_set) }
it 'is unauthorized' do
get :files, params: { id: admin_set }, format: :json
expect(response).to be_unauthorized
end
end
end

context "as an admin" do
Expand Down Expand Up @@ -111,6 +117,14 @@
end
end

describe "#files" do
let(:admin_set) { create(:admin_set, edit_users: [user]) }
it 'shows a list of member files' do
get :files, params: { id: admin_set }, format: :json
expect(response).to be_success
end
end

describe "#update" do
let(:admin_set) { create(:admin_set, edit_users: [user]) }
it 'updates a record' do
Expand Down
15 changes: 15 additions & 0 deletions spec/forms/hyrax/forms/admin_set_form_spec.rb
Expand Up @@ -9,6 +9,21 @@
end
end

describe "#thumbnail_title" do
subject { form.thumbnail_title }

context "when the admin_set has a thumbnail" do
let(:thumbnail) { stub_model(FileSet, title: ['Ulysses']) }
let(:model) { AdminSet.new(thumbnail: thumbnail) }
it { is_expected.to eq "Ulysses" }
end

context "when the admin_set has no thumbnail" do
let(:model) { AdminSet.new }
it { is_expected.to be nil }
end
end

describe "#permission_template" do
subject { form.permission_template }
context "when the PermissionTemplate doesn't exist" do
Expand Down
8 changes: 7 additions & 1 deletion spec/views/hyrax/admin/admin_sets/_form.html.erb_spec.rb
Expand Up @@ -2,13 +2,18 @@

RSpec.describe 'hyrax/admin/admin_sets/_form.html.erb', type: :view do
let(:admin_set) { stub_model(AdminSet) }
let(:form) { Hyrax::Forms::AdminSetForm.new(admin_set) }

before do
@form = Hyrax::Forms::AdminSetForm.new(admin_set)
assign(:form, form)
stub_template('hyrax/admin/admin_sets/_form_participants.html.erb' => 'participant tab')
stub_template('hyrax/admin/admin_sets/_form_visibility.html.erb' => 'visibility tab')
stub_template('hyrax/admin/admin_sets/_form_workflow.html.erb' => 'workflow tab')
allow(form).to receive(:thumbnail_title).and_return("James Joyce")
allow(admin_set).to receive(:member_ids).and_return(['123', '456'])
render
end

it "has 4 tabs" do
expect(rendered).to have_selector('#description')
expect(rendered).to have_content('participant tab')
Expand All @@ -18,6 +23,7 @@
# metadata fields
expect(rendered).to have_selector('input[type=text][name="admin_set[title]"]')
expect(rendered).to have_selector('textarea[name="admin_set[description]"]')
expect(rendered).to have_selector('input[type=text][name="admin_set[thumbnail_id]"][data-text="James Joyce"]')

# hint text
expect(rendered).to have_content("A name to aid in identifying the Administrative Set and to distinguish it from other Administrative Sets in the repository.")
Expand Down

0 comments on commit 3a9ef03

Please sign in to comment.