From 17cc7500412aca61caa952b2fe16cdcb76df7de5 Mon Sep 17 00:00:00 2001 From: Brenda Wallace Date: Fri, 30 Oct 2026 19:00:36 +1300 Subject: [PATCH] Fix the new seed page: a seed not yet saved has no owner The month chips look up the grower's hemisphere from the seed's owner, and a seed that is being added has none until it is saved, so the page raised. Use the member filling in the form, and add specs for the standalone new and edit pages, which were not covered. Co-Authored-By: Claude Sonnet 5.5 --- app/views/seeds/_sow_months_field.html.haml | 6 +- spec/requests/seeds_sowing_forms_spec.rb | 65 +++++++++++++++++++++ 2 files changed, 69 insertions(+), 2 deletions(-) create mode 100644 spec/requests/seeds_sowing_forms_spec.rb diff --git a/app/views/seeds/_sow_months_field.html.haml b/app/views/seeds/_sow_months_field.html.haml index 017f630bf..c15eb6cda 100644 --- a/app/views/seeds/_sow_months_field.html.haml +++ b/app/views/seeds/_sow_months_field.html.haml @@ -1,8 +1,10 @@ -# The months to sow in, as twelve chips to tick, as many as the packet says. Where the -# grower's hemisphere is known the seasons are shortcuts that tick a season's -# months at once; where it isn't there are none, rather than a guess. The prefix --# keeps the ids apart where the page has more than one of these. -- seasons = season_months(model.owner.latitude) +-# keeps the ids apart where the page has more than one of these. A seed not yet saved has +-# no owner: it is the member filling in the form who will be. +- grower = model.owner || current_member +- seasons = season_months(grower&.latitude) .sow-months{ data: { sow_months: '' } } = hidden_field_tag 'seed[sow_months][]', '', id: nil .sow-months-chips diff --git a/spec/requests/seeds_sowing_forms_spec.rb b/spec/requests/seeds_sowing_forms_spec.rb new file mode 100644 index 000000000..77d28c69a --- /dev/null +++ b/spec/requests/seeds_sowing_forms_spec.rb @@ -0,0 +1,65 @@ +# frozen_string_literal: true + +require 'rails_helper' + +# The standalone forms for a seed: the page to add one, and the page to edit one. +describe SeedsController, 'the standalone forms' do + include Devise::Test::IntegrationHelpers + + let(:member) { create_located_member('Sowing forms southern town', -41.3, 174.8) } + let(:page) { Capybara.string(response.body) } + + describe '#new' do + before do + sign_in member + get new_seed_path + end + + it('renders') { expect(response).to have_http_status(:ok) } + it('has the months to sow in, to tick') { expect(page).to have_css('.sow-months-chips input[type=checkbox]', count: 12) } + it('asks how to sow') { expect(page).to have_css('textarea[name="seed[how_to_sow]"]') } + it('asks for the germination range') { expect(page).to have_css('input[name="seed[germination_days_min]"], input[name="seed[germination_days_max]"]', count: 2) } + + it('offers the seasons for the hemisphere of the member filling it in, there being no owner yet') do + expect(page).to have_css('[data-sow-months-season="9,10,11"]', text: 'Spring') + end + end + + describe '#new for a member we do not know the whereabouts of' do + let(:member) { create(:member, location: nil) } + + before do + sign_in member + get new_seed_path + end + + it('renders') { expect(response).to have_http_status(:ok) } + it('has no season shortcuts, rather than a guess') { expect(page).to have_no_css('[data-sow-months-season]') } + end + + describe '#edit' do + let(:seed) { create(:seed, owner: member, sow_months: [10, 11]) } + + before do + sign_in member + get edit_seed_path(seed) + end + + it('renders') { expect(response).to have_http_status(:ok) } + it('has the months it says ticked') { expect(page).to have_css('.sow-months-chips input[checked][value="10"]') } + end + + describe '#edit by an admin, for someone else\'s seed' do + let(:seed) { create(:seed, owner: member) } + let(:admin) { create(:admin_member, location: nil) } + + before do + sign_in admin + get edit_seed_path(seed) + end + + it('offers the seasons for the hemisphere of the seed\'s owner') do + expect(page).to have_css('[data-sow-months-season="9,10,11"]', text: 'Spring') + end + end +end