diff --git a/app/helpers/observation_units_helper.rb b/app/helpers/observation_units_helper.rb new file mode 100644 index 0000000000..4c1a854ba8 --- /dev/null +++ b/app/helpers/observation_units_helper.rb @@ -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 \ No newline at end of file diff --git a/app/views/samples/_associate_observation_unit.html.erb b/app/views/samples/_associate_observation_unit.html.erb new file mode 100644 index 0000000000..87d05fe363 --- /dev/null +++ b/app/views/samples/_associate_observation_unit.html.erb @@ -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 %> \ No newline at end of file diff --git a/app/views/samples/_form.html.erb b/app/views/samples/_form.html.erb index 2e4932113f..d092fd73fb 100644 --- a/app/views/samples/_form.html.erb +++ b/app/views/samples/_form.html.erb @@ -24,5 +24,7 @@ <% end %> + <%= render partial: 'associate_observation_unit', locals: {f: f, resource: @sample} if Seek::Config.observation_units_enabled %> + <%= form_submit_buttons(@sample, validate:false) %> \ No newline at end of file diff --git a/test/functional/samples_controller_test.rb b/test/functional/samples_controller_test.rb index 871e9b675d..23d5293b90 100644 --- a/test/functional/samples_controller_test.rb +++ b/test/functional/samples_controller_test.rb @@ -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 }