Skip to content

Commit

Permalink
Extract a DestinationSelectorComponent
Browse files Browse the repository at this point in the history
  • Loading branch information
jcoyne committed Aug 16, 2023
1 parent 93ac349 commit 3b32b61
Show file tree
Hide file tree
Showing 13 changed files with 105 additions and 170 deletions.
24 changes: 24 additions & 0 deletions app/components/destination_selector_component.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<div data-scheduler-lookup-url='<%= paging_schedule_path(origin: form.object.paging_origin_library) %>'>
<% if pickup_destinations.one? %>
<div class='form-group'>
<div class='<%= helpers.label_column_class %> control-label'>
Will be delivered to
</div>
<div class='<%= helpers.content_column_class %> input-like-text'>
<%= destination_label(pickup_destinations.first) %>
</div>
<%= form.hidden_field :destination, value: pickup_destinations.first %>
</div>
<% elsif pickup_destinations.many? %>
<%= form.label :destination, 'Deliver to' %>
<%= form.select :destination,
pickup_destinations_array(pickup_destinations),
{
selected: request.destination || request.default_pickup_destination
},
aria: { controls: 'scheduler-text' },
data: { 'paging-schedule-updater' => 'true', 'text-selector' => "[data-text-object='#{request.object_id}']" }
%>
<% end %>
<%= render 'requests/scheduler_text', f: form, current_request: form.object %>
</div>
28 changes: 28 additions & 0 deletions app/components/destination_selector_component.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# frozen_string_literal: true

# Draws the select a destionation widget
class DestinationSelectorComponent < ViewComponent::Base
def initialize(form:)
@form = form
end

attr_reader :form

def request
form.object
end

delegate :pickup_destinations, to: :request

# Get the label, if it exists, for the pickup destination
def destination_label(pickup_destination)
Settings.ils.pickup_destination_class.constantize.new(pickup_destination).display_label || pickup_destination
end

# Return the array of destinations for the dropdown
def pickup_destinations_array(pickup_destinations)
pickup_destinations.map do |k|
[destination_label(k), k]
end.sort
end
end
65 changes: 0 additions & 65 deletions app/helpers/pickup_libraries_helper.rb

This file was deleted.

6 changes: 0 additions & 6 deletions app/helpers/requests_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
# Helper module for requests related markup
###
module RequestsHelper
include PickupLibrariesHelper

def status_text_for_item(item)
status_text_for_errored_item(item) || i18n_status_text(item)
end
Expand Down Expand Up @@ -86,10 +84,6 @@ def status_text_for_errored_item(item)
item.request_status.user_error_text || item.request_status.text
end

def format_date(date)
Time.zone.parse(date.to_s).strftime('%Y-%m-%d %I:%M%P')
end

def searchworks_link(item_id, item_title, html_options = {})
link_to item_title, "#{Settings.searchworks_link}/#{item_id}", html_options
end
Expand Down
2 changes: 1 addition & 1 deletion app/views/requests/_destination_and_needed_date.html.erb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<div class="alert alert-warning destination-note-callout">
<%= render partial: 'destination_selector', locals: { f: f } %>
<%= render DestinationSelectorComponent.new(form: f) %>

<%= render partial: 'needed_date', locals: { f: f } %>
</div>
4 changes: 0 additions & 4 deletions app/views/requests/_destination_selector.html.erb

This file was deleted.

2 changes: 1 addition & 1 deletion app/views/shared/_request_status_information.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<% destination_name = LibraryLocation.library_name_by_code(current_request.destination) %>
<% if destination_name.present? %>
<dt>
<%= label_for_pickup_destinations_dropdown(current_request.pickup_destinations) %>
Will be delivered to
</dt>
<dd>
<%= destination_name %>
Expand Down
47 changes: 47 additions & 0 deletions spec/components/destination_selector_component_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# frozen_string_literal: true

require 'rails_helper'

RSpec.describe DestinationSelectorComponent, type: :component do
let(:component) { described_class.new(form:) }
let(:rendered) { render_inline(component) }
let(:form) { ActionView::Helpers::FormBuilder.new(nil, request, vc_test_controller.view_context, {}) }

describe 'single library' do
let(:request) { build(:request, origin: 'SAL3', origin_location: 'PAGE-EN', bib_data:) }
let(:item) do
build(:item,
barcode: '3610512345678',
callnumber: 'ABC 123',
effective_location: build(:page_en_location))
end

let(:bib_data) { double(:bib_data, title: 'Test title', request_holdings: [item]) }

it 'returns library text and a hidden input w/ the destination library' do
expect(rendered).to have_css('.form-group .control-label', text: 'Will be delivered to')
expect(rendered).to have_css('.form-group .input-like-text', text: 'Engineering Library (Terman)')
expect(rendered).to have_css('input[type="hidden"][value="ENG"]', visible: :hidden)
end
end

describe 'multiple libraries' do
let(:request) { create(:request, origin: 'SAL3', origin_location: 'PAGE-HP') }

it 'creates a select list' do
expect(rendered).to have_select 'Deliver to'
end

context 'with a destination' do
let(:request) { create(:request, origin: 'SAL3', destination: 'ART', origin_location: 'PAGE-HP') }

before do
allow(request).to receive(:pickup_destinations).and_return(['GREEN', 'HOPKINS', 'ART'])
end

it 'defaults to the destination library' do
expect(rendered).to have_select 'Deliver to', selected: 'Art & Architecture Library (Bowes)'
end
end
end
end
32 changes: 0 additions & 32 deletions spec/helpers/pickup_libraries_helper_spec.rb

This file was deleted.

57 changes: 0 additions & 57 deletions spec/helpers/requests_helper_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,63 +5,6 @@
RSpec.describe RequestsHelper do
include ApplicationHelper

describe '#select_for_pickup_destinations' do
let(:form) { double('form') }

before do
allow(form).to receive_messages(object: request)
end

describe 'single library' do
let(:request) { build(:request, origin: 'SAL3', origin_location: 'PAGE-EN', bib_data:) }
let(:item) do
build(:item,
barcode: '3610512345678',
callnumber: 'ABC 123',
effective_location: build(:page_en_location))
end

let(:bib_data) { double(:bib_data, title: 'Test title', request_holdings: [item]) }

it 'returns library text and a hidden input w/ the destination library' do
expect(form).to receive(:hidden_field).with(:destination, value: 'ENG').and_return('<hidden_field>')
markup = Capybara.string(select_for_pickup_destinations(form))
expect(markup).to have_css('.form-group .control-label', text: 'Will be delivered to')
expect(markup).to have_css('.form-group .input-like-text', text: 'Engineering Library (Terman)')
expect(markup).to have_css('hidden_field')
end
end

describe 'multiple libraries' do
let(:request) { create(:request, origin: 'SAL3', origin_location: 'PAGE-HP') }

it 'attempts to create a select list' do
expect(form).to receive(:select).with(any_args).and_return('<select>')
expect(select_for_pickup_destinations(form)).to eq '<select>'
end

context 'with a destination' do
let(:request) { create(:request, origin: 'SAL3', destination: 'ART', origin_location: 'PAGE-HP') }

it 'defaults to the destination library' do
expect(form).to receive(:select).with(anything, anything, hash_including(selected: 'ART'), anything)
.and_return('<select>')
expect(select_for_pickup_destinations(form)).to eq '<select>'
end
end
end
end

describe '#label_for_pickup_destinations_dropdown' do
it 'is "Deliver to" when if there are mutliple possiblities' do
expect(label_for_pickup_destinations_dropdown(%w(GREEN MUSIC))).to eq 'Deliver to'
end

it 'is "Will be delivered to" when there is only one possibility' do
expect(label_for_pickup_destinations_dropdown(['GREEN'])).to eq 'Will be delivered to'
end
end

describe 'searchworks link' do
it 'constructs a searchworks link including the passed in html_options' do
result = '<a data-elt-opt="somebehavior" href="http://searchworks.stanford.edu/view/234">A title</a>'
Expand Down
2 changes: 1 addition & 1 deletion spec/views/requests/status.html.erb_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
it 'has the destination library' do
request.destination = 'GREEN'
render
expect(rendered).to have_css('dt', text: 'Deliver to')
expect(rendered).to have_css('dt', text: 'Will be delivered to')
expect(rendered).to have_css('dd', text: 'Green Library')
end

Expand Down
2 changes: 1 addition & 1 deletion spec/views/requests/success.html.erb_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
it 'has the destination library' do
request.destination = 'GREEN'
render
expect(rendered).to have_css('dt', text: 'Deliver to')
expect(rendered).to have_css('dt', text: 'Will be delivered to')
expect(rendered).to have_css('dd', text: 'Green Library')
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
context 'when there is no delivery destination (e.g. for a scan)' do
let(:request) { build_stubbed(:scan, :without_validations, :with_item_title, user:) }

it "doesn't display the 'Deliver to' field" do
it "doesn't display the 'Will be delivered to' field" do
render
expect(rendered).not_to have_content('Will be delivered to')
end
Expand All @@ -22,7 +22,7 @@
context 'when there is a delivery destination' do
let(:request) { build_stubbed(:page_mp_mediated_page, user:, barcodes: ['12345678']) }

it "displays the 'Deliver to' field" do
it "displays the 'Will be delivered to' field" do
render
expect(rendered).to have_content('Will be delivered to')
end
Expand Down

0 comments on commit 3b32b61

Please sign in to comment.