Skip to content

Commit

Permalink
Allow for injection of new tabs on edit page
Browse files Browse the repository at this point in the history
This commit adds a new code seam allowing for the modification of tab
ordering and the addition (or removal) of tabs on a form by form basis.
The default tab order is moved out of the guts4form view partial and
into a helper.

To modify the default list of tabs in an application override the `form_tabs_for` helper method and make sure it gets loaded
after Hyrax::HyraxHelperBehavoir (by default included in app/helpers/hyrax_helper.rb). For example:
```
module WorksHelper
  def form_tabs_for(form:)
    super + ["my_new_tab"]
  end
end
```

The share tab has not been included in the default tab order because it
wasn't in the view partial.  Future work is to simplify the guts4form
partial so share can be treated the same as the other tabs and included
in the default list.
  • Loading branch information
cjcolvar authored and Tom Johnson committed Jul 16, 2020
1 parent 6b9b5e3 commit 5051e81
Show file tree
Hide file tree
Showing 9 changed files with 113 additions and 14 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -81,3 +81,4 @@ _yardoc
lib/bundler/man
spec/reports
/spec/examples.txt
node_modules
1 change: 1 addition & 0 deletions app/helpers/hyrax/hyrax_helper_behavior.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ module HyraxHelperBehavior
include Hyrax::IiifHelper
include Hyrax::MembershipHelper
include Hyrax::PermissionLevelsHelper
include Hyrax::WorkFormHelper

# Which translations are available for the user to select
# @return [Hash<String,String>] locale abbreviations as keys and flags as values
Expand Down
12 changes: 12 additions & 0 deletions app/helpers/hyrax/work_form_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# frozen_string_literal: true
module Hyrax
module WorkFormHelper
def form_tabs_for(form:)
if form.instance_of? Hyrax::Forms::BatchUploadForm
%w[files metadata relationships]
else
%w[metadata files relationships]
end
end
end
end
2 changes: 1 addition & 1 deletion app/views/hyrax/base/_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
<p class="switch-upload-type"><%= t('.batch_upload_hint') %> <%= link_to t('.batch_link'), hyrax.new_batch_upload_path(payload_concern: @form.model.class) %></p>
<% end %>
<% end %>
<%= render 'hyrax/base/guts4form', f: f %>
<%= render 'hyrax/base/guts4form', f: f, tabs: form_tabs_for(form: f.object) %>
<% end %>

<script type="text/javascript">
Expand Down
1 change: 0 additions & 1 deletion app/views/hyrax/base/_guts4form.html.erb
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
<% # we will yield to content_for for each tab, e.g. :files_tab %>
<% tabs ||= %w[metadata files relationships] # default tab order %>
<div class="row">
<div class="col-xs-12 col-sm-8">
<div class="panel panel-default tabs" role="main">
Expand Down
2 changes: 1 addition & 1 deletion app/views/hyrax/batch_uploads/_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<%= link_to t("hyrax.batch_uploads.files.button_label"), [main_app, :new, Hyrax.primary_work_type.model_name.singular_route_key] %>
</p>
<% end %>
<%= render 'hyrax/base/guts4form', f: f, tabs: %w[files metadata relationships] %>
<%= render 'hyrax/base/guts4form', f: f, tabs: form_tabs_for(form: f.object) %>
<%= f.hidden_field :payload_concern, value: @form.payload_concern %>
<% end %>

Expand Down
34 changes: 34 additions & 0 deletions spec/helpers/hyrax/work_helper_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# frozen_string_literal: true

RSpec.describe Hyrax::WorkFormHelper do
describe 'form_tabs_for' do
context 'with a change set style form' do
let(:work) { build(:hyrax_work) }
let(:form) { Hyrax::Forms::ResourceForm.for(work) }

it 'returns a default tab list' do
expect(form_tabs_for(form: form)).to eq ["metadata", "files", "relationships"]
end
end

context 'with a legacy GenericWork form' do
let(:work) { stub_model(GenericWork, id: '456') }
let(:ability) { double }
let(:form) { Hyrax::GenericWorkForm.new(work, ability, controller) }

it 'returns a default tab list' do
expect(form_tabs_for(form: form)).to eq ["metadata", "files", "relationships"]
end
end

context 'with a batch upload form' do
let(:work) { stub_model(GenericWork, id: '456') }
let(:ability) { double }
let(:form) { Hyrax::Forms::BatchUploadForm.new(work, ability, controller) }

it 'returns an alternate tab ordering' do
expect(form_tabs_for(form: form)).to eq ["files", "metadata", "relationships"]
end
end
end
end
55 changes: 45 additions & 10 deletions spec/views/hyrax/base/_form.html.erb_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,17 +46,33 @@
expect(rendered).to have_selector("form[action='/concern/monographs/#{work.id}']")
end
end

context 'with non-default tabs' do
let(:tab_order) { ['metadata', 'relationships', 'newtab'] }

before do
allow(view).to receive(:form_tabs_for).with(form: form).and_return(tab_order)
allow(view).to receive(:t).and_call_original
allow(view).to receive(:t).with('hyrax.works.form.tab.newtab').and_return('New Tab')
stub_template 'hyrax/base/_form_newtab.html.erb' => 'NewTab Content'
end

it 'renders the expected tabs' do
render
expect(rendered).to have_link('Descriptions')
expect(rendered).to have_link('Relationships')
expect(rendered).to have_link('New Tab')
expect(rendered).to have_link('Sharing')
expect(rendered).not_to have_link('Files')
expect(rendered).to match(/NewTab Content/)
end
end
end

context 'with a legacy GenericWork' do
let(:work) do
stub_model(GenericWork, id: '456')
end
let(:work) { stub_model(GenericWork, id: '456') }
let(:ability) { double }

let(:form) do
Hyrax::GenericWorkForm.new(work, ability, controller)
end
let(:form) { Hyrax::GenericWorkForm.new(work, ability, controller) }

before do
stub_template('hyrax/base/_form_progress.html.erb' => 'Progress')
Expand All @@ -66,6 +82,7 @@
allow(controller).to receive(:controller_name).and_return('batch_uploads')
allow(form).to receive(:permissions).and_return([])
allow(form).to receive(:visibility).and_return('public')
allow(form).to receive(:select_files).and_return([])
stub_template 'hyrax/base/_form_files.html.erb' => 'files'
end

Expand Down Expand Up @@ -97,13 +114,10 @@
end

context "for a persisted object" do
let(:work) { stub_model(GenericWork, id: '456') }

before do
# Add an error to the work
work.errors.add :base, 'broken'
work.errors.add :visibility, 'visibility_error'
allow(form).to receive(:select_files).and_return([])
render
end

Expand All @@ -118,5 +132,26 @@
expect(rendered).to have_content("visibility_error")
end
end

context 'with non-default tabs' do
let(:tab_order) { ['metadata', 'relationships', 'newtab'] }

before do
allow(view).to receive(:form_tabs_for).with(form: form).and_return(tab_order)
allow(view).to receive(:t).and_call_original
allow(view).to receive(:t).with('hyrax.works.form.tab.newtab').and_return('New Tab')
stub_template 'hyrax/base/_form_newtab.html.erb' => 'NewTab Content'
end

it 'renders the expected tabs' do
render
expect(rendered).to have_link('Descriptions')
expect(rendered).to have_link('Relationships')
expect(rendered).to have_link('New Tab')
expect(rendered).to have_link('Sharing')
expect(rendered).not_to have_link('Files')
expect(rendered).to match(/NewTab Content/)
end
end
end
end
19 changes: 18 additions & 1 deletion spec/views/hyrax/batch_uploads/_form.html.erb_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
RSpec.describe 'hyrax/batch_uploads/_form.html.erb', type: :view do
let(:work) { GenericWork.new }
let(:ability) { double('ability', current_user: user) }
let(:controller_class) { Hyrax::BatchUploadsController }
let(:options_presenter) { double(select_options: []) }
let(:form) { Hyrax::Forms::BatchUploadForm.new(work, ability, controller) }
let(:user) { stub_model(User) }
let(:page) do
Expand All @@ -10,7 +12,13 @@
end

before do
stub_template "hyrax/base/_guts4form.html.erb" => "Form guts"
# Tell rspec where to find form_* partials
view.lookup_context.prefixes << 'hyrax/base'
allow(controller).to receive(:current_user).and_return(stub_model(User))
allow(controller).to receive(:repository).and_return(controller_class.new.repository)
allow(controller).to receive(:blacklight_config).and_return(controller_class.new.blacklight_config)
# mock the admin set options presenter to avoid hitting Solr
allow(Hyrax::AdminSetOptionsPresenter).to receive(:new).and_return(options_presenter)
assign(:form, form)
end

Expand All @@ -22,4 +30,13 @@
expect(page).not_to have_selector("input#generic_work_title")
expect(view.content_for(:files_tab)).to have_link("New Work", href: "/concern/generic_works/new")
end

describe 'tabs' do
it 'shows form tabs' do
expect(page).to have_link('Files')
expect(page).to have_link('Descriptions')
expect(page).to have_link('Relationships')
expect(page).to have_link('Sharing')
end
end
end

0 comments on commit 5051e81

Please sign in to comment.