Skip to content

Commit

Permalink
Merge pull request #1743 from MushroomObserver/nimmo-edit-thumbnail-o…
Browse files Browse the repository at this point in the history
…bs-form

Edit obs: Fix setting `thumb_image_id`
  • Loading branch information
nimmolo committed Dec 23, 2023
2 parents 5c09295 + 5678c52 commit c85e320
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 9 deletions.
7 changes: 5 additions & 2 deletions app/views/observations/form/_good_image.html.erb
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
<%# good image section of create_observation form %>

<div class="row my-4 form_image">
<div class="row my-4 good_image_<%= image.id %>">

<!-- GOOD_IMAGE -->
<div class="col-xs-1" style="max-width:30px">
<%= f.radio_button(:thumb_image_id, image.id) %>
<%= f.radio_button(
:thumb_image_id, image.id,
{ data: { action: "click->obs-form-images#setHiddenThumbField" } }
) %>
</div><!--.col-->

<div class="col-xs-11 col-sm-3">
Expand Down
13 changes: 12 additions & 1 deletion app/views/observations/form/_images.html.erb
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
<%# Images (already attached) section of create_observation form %>
<%# Images section of create_observation form
Gotcha: thumb_image_id
Although there can be radio buttons for selecting observation[thumb_image_id]
in both the good_images and the images_upload partials, they currently *do
not* set the observation thumb_image_id: their value is transferred by JS to
the hidden_field below with the same name, which takes precedence.
I believe this is because in images_upload/_template.erb, the radio buttons
do not yet have an image.id value, the image has not yet been uploaded.
%>
<%
good_image_ids_str = @good_images.map { |img| img.id }.join(" ")
%>
Expand All @@ -15,6 +23,9 @@ good_image_ids_str = @good_images.map { |img| img.id }.join(" ")
<%= hidden_field_tag(:good_images, good_image_ids_str,
data: { obs_form_images_target: "goodImages" }) %>
<%= hidden_field(:observation, :thumb_image_id,
data: { obs_form_images_target: "thumbImageId" }) %>
<%= render(partial: "observations/form/images_upload", locals: { f: f }) %>
<% end %><!--#observation_images-->
3 changes: 0 additions & 3 deletions app/views/observations/form/_images_upload.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@
<%= tag.div(class: "my-4", id: "observation_images_upload") do %>
<%= hidden_field(:observation, :thumb_image_id,
data: { obs_form_images_target: "thumbImageId" }) %>
<%= tag.p(:form_observations_upload_images.t + ":",
class: "font-weight-bold") %>
Expand Down
14 changes: 11 additions & 3 deletions test/system/observation_form_system_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -388,7 +388,10 @@ def test_post_edit_and_destroy_with_details_and_location
assert_checked_field("observation_specimen")
assert_field(other_notes_id, with: "Notes for observation")

img_ids = Image.last(2).map(&:id)
imgs = Image.last(2)
cci = imgs.find { |img| img[:original_name] == "Coprinus_comatus.jpg" }
geo = imgs.find { |img| img[:original_name] == "geotagged.jpg" }
img_ids = imgs.map(&:id)
img_ids.each do |img_id|
assert_field("good_image_#{img_id}_when_1i", with: "2010")
assert_select("good_image_#{img_id}_when_2i", text: "March")
Expand All @@ -397,6 +400,8 @@ def test_post_edit_and_destroy_with_details_and_location
with: katrina.legal_name)
assert_field("good_image_#{img_id}_notes", with: "Notes for image")
end
assert_checked_field("observation_thumb_image_id_#{cci.id}")
assert_unchecked_field("observation_thumb_image_id_#{geo.id}")

# submit_observation_form_with_changes
fill_in("observation_when_1i", with: "2011")
Expand All @@ -407,13 +412,15 @@ def test_post_edit_and_destroy_with_details_and_location
fill_in("observation_alt", with: "987m")
check("observation_is_collection_location")
fill_in(other_notes_id, with: "New notes for observation")

img_ids.each do |img_id|
fill_in("good_image_#{img_id}_when_1i", with: "2011")
select("April", from: "good_image_#{img_id}_when_2i")
select("15", from: "good_image_#{img_id}_when_3i")
fill_in("good_image_#{img_id}_notes", with: "New notes for image")
end
obs_images = find("#observation_images")
scroll_to(obs_images, align: :top)
choose("observation_thumb_image_id_#{geo.id}")
sleep(1)
within("#observation_form") { click_commit }

Expand Down Expand Up @@ -618,7 +625,8 @@ def expected_values_after_edit
is_collection_location: true,
specimen: false,
notes: "New notes for observation", # displayed in observations/show
image_notes: "New notes for image"
image_notes: "New notes for image",
thumb_image_id: Image.find_by(original_name: "geotagged.jpg").id
)
end
end

0 comments on commit c85e320

Please sign in to comment.