Skip to content

Commit

Permalink
obs unit selector for sample form #2020
Browse files Browse the repository at this point in the history
use objects_input, grouped by study
  • Loading branch information
stuzart committed Oct 25, 2024
1 parent 63ab585 commit 6b430d3
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 1 deletion.
24 changes: 24 additions & 0 deletions app/helpers/observation_units_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
module ObservationUnitsHelper

def observation_unit_selector(name, selected = nil, opts = {})
opts[:data] ||= {}
opts[:multiple] = false
grouped_options = grouped_observation_unit_options
opts[:select_options] = grouped_options_for_select(grouped_options, selected)

objects_input(name, [selected&.id], opts)
end

private

def grouped_observation_unit_options
obs_units = authorised_assets(ObservationUnit, nil, :edit)
grouped = obs_units.group_by(&:study)
grouped.keys.collect do |study|
title = study&.can_view? ? study.title : "Hidden #{t('study')}"
[title, grouped[study].collect{|obs_unit| [obs_unit.title, obs_unit.id]}]
end

end

end
3 changes: 3 additions & 0 deletions app/views/samples/_associate_observation_unit.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<%= folding_panel(t('observation_unit'), resource.observation_unit.present?) do %>
<%= observation_unit_selector('sample[observation_unit_id]', resource.observation_unit) %>
<% end %>
2 changes: 2 additions & 0 deletions app/views/samples/_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,7 @@
<% end %>
</div>

<%= render partial: 'associate_observation_unit', locals: {f: f, resource: @sample} if Seek::Config.observation_units_enabled %>
<%= form_submit_buttons(@sample, validate:false) %>
</div>
4 changes: 3 additions & 1 deletion test/functional/samples_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,9 @@ def rdf_test_object
end

test 'edit' do
login_as(FactoryBot.create(:person))
person = FactoryBot.create(:person)
login_as(person)
FactoryBot.create(:observation_unit, contributor: person)

get :edit, params: { id: populated_patient_sample.id }

Expand Down

0 comments on commit 6b430d3

Please sign in to comment.