Skip to content
15 changes: 13 additions & 2 deletions app/controllers/sdb/sample_manifests_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ def index
def show
@study_id = @sample_manifest.study_id
@samples = @sample_manifest.samples.paginate(page: params[:page])
@barcode_types = Rails.application.config.tube_manifest_barcode_config[:barcode_type_labels].values.sort
@asset_type = @sample_manifest.asset_type
end

def new
Expand All @@ -57,12 +59,14 @@ def create # rubocop:todo Metrics/AbcSize
end
end

def print_labels # rubocop:todo Metrics/MethodLength
def print_labels # rubocop:todo Metrics/MethodLength, Metrics/AbcSize
print_job =
LabelPrinter::PrintJob.new(
params[:printer],
LabelPrinter::Label::SampleManifestRedirect,
sample_manifest: @sample_manifest
sample_manifest: @sample_manifest,
label_template_name: label_template_for_2d_barcodes(@sample_manifest.asset_type),
barcode_type: params[:barcode_type]
)
if print_job.execute
flash[:notice] = print_job.success
Expand Down Expand Up @@ -112,4 +116,11 @@ def validate_type
redirect_to sample_manifests_path
end
end

def label_template_for_2d_barcodes(asset_type)
if params[:barcode_type] == Rails.application.config.tube_manifest_barcode_config[:barcode_type_labels]['2d'] &&
SampleManifest.tube_asset_types.include?(asset_type)
Rails.application.config.tube_manifest_barcode_config[:two_dimensional_label_template]
end
end
end
9 changes: 8 additions & 1 deletion app/views/sdb/sample_manifests/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,14 @@
<%= label_tag :barcode_printer, 'Barcode printer' %>
<%= render partial: "shared/printer_list" %>
</div>


<% if SampleManifest.tube_asset_types.include?(@asset_type) %>
<div class="form-group">
<%= label_tag :barcode_printer_type_id, 'Barcode type' %>
<%= select_tag :barcode_type, options_for_select(@barcode_types),class: "form-control select2" %>
</div>
<% end %>

<div class="form-group mb-0">
<%= f.submit "Reprint all #{@sample_manifest.barcodes&.size || ''} labels", class: 'btn btn-success' %>
</div>
Expand Down
5 changes: 3 additions & 2 deletions app/views/shared/_printer_list.html.erb
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@

<div><label for="barcode_printer_list" style="display:none">Barcode Printer</label></div>
<select name="printer" id="barcode_printer_list" class="select2">
<% printer_list = BarcodePrinter.alphabetical.includes(:barcode_printer_type).all.group_by {|printer| "#{printer.barcode_printer_type.name} (#{printer.active? ? 'Active' : 'Inactive'})" } %>
<% printer_list.each do |group, printers| %>
<% printer_list = BarcodePrinter.alphabetical.includes(:barcode_printer_type).all.group_by {|printer| "#{printer.barcode_printer_type.name} (#{printer.active? ? 'Active' : 'Inactive'})" } %>
<% printer_list.each do |group, printers| %>
<optgroup label="<%= group %>">
<% printers.each do |printer| %>
<option value="<%= printer.name %>"><%= printer.name %></option>
<% end %>
</optgroup>
<% end %>
</select>

67 changes: 67 additions & 0 deletions spec/controllers/sample_manifest_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,73 @@
end
end

describe 'POST #print_labels' do
let!(:user) { create(:user, swipecard_code: '123456') }

before do
user.grant_administrator
session[:user] = user.id
request.env['HTTP_REFERER'] = '/'
end

context 'when printing labels for a plate manifest' do
let!(:sample_manifest) { create(:sample_manifest, asset_type: 'plate') }
let(:print_job) { instance_double(LabelPrinter::PrintJob) }

before do
allow(LabelPrinter::PrintJob).to receive(:new).and_return(print_job)
allow(print_job).to receive_messages(execute: true, success: 'Printed')
end

it 'prints successfully' do
post :print_labels,
params: {
id: sample_manifest.id,
printer: 'printer1'
}

expect(flash[:notice]).to eq('Printed')
end
end

context 'when printing labels for a tube manifest with 2D barcodes' do
subject(:make_request) do
post :print_labels,
params: {
id: sample_manifest.id,
printer: 'printer1',
barcode_type: '2D Barcode'
}
end

let!(:sample_manifest) { create(:sample_manifest, asset_type: '1dtube') }
let(:print_job) { instance_double(LabelPrinter::PrintJob) }

before do
allow(controller).to receive(:label_template_for_2d_barcodes).and_return('2d_template')
allow(LabelPrinter::PrintJob).to receive(:new).and_return(print_job)
allow(print_job).to receive_messages(execute: true, success: 'Printed')
make_request
end

it 'passes the 2D label template to the print job' do
expect(LabelPrinter::PrintJob).to have_received(:new).with(
'printer1',
LabelPrinter::Label::SampleManifestRedirect,
hash_including(
sample_manifest: sample_manifest,
label_template_name: '2d_template',
barcode_type: '2D Barcode'
)
)
end

it 'prints successfully' do
expect(flash[:notice]).to eq('Printed')
end
end
end

def expect_correct_attributes(sample_manifest, study, supplier, purpose) # rubocop:todo Metrics/AbcSize
expect(sample_manifest.study_id).to eq(study.id)
expect(sample_manifest.supplier_id).to eq(supplier.id)
Expand Down
Loading