From 5f7d211636850d31782a3ebfb6c07343f6ec7cde Mon Sep 17 00:00:00 2001 From: aaaaargZombies Date: Wed, 3 Aug 2022 10:24:45 +0100 Subject: [PATCH] Refactor conditional rendering for primary neighbourhood in site admin (#1426) --- app/javascript/src/behaviors/all_behaviors.js | 1 - .../src/behaviors/behaviors.site.js | 12 --------- .../_sites_neighbourhood_fields.html.erb | 25 ++++++++++--------- test/system/admin/site_test.rb | 13 ++++++++++ 4 files changed, 26 insertions(+), 25 deletions(-) delete mode 100644 app/javascript/src/behaviors/behaviors.site.js diff --git a/app/javascript/src/behaviors/all_behaviors.js b/app/javascript/src/behaviors/all_behaviors.js index 547459cbf..a73d9a71b 100644 --- a/app/javascript/src/behaviors/all_behaviors.js +++ b/app/javascript/src/behaviors/all_behaviors.js @@ -2,4 +2,3 @@ import { Behaviors } from './behaviors.base.js' import './behaviors.collection.js' import './behaviors.partner.js' import './behaviors.place.js' -import './behaviors.site.js' diff --git a/app/javascript/src/behaviors/behaviors.site.js b/app/javascript/src/behaviors/behaviors.site.js deleted file mode 100644 index eff16abe9..000000000 --- a/app/javascript/src/behaviors/behaviors.site.js +++ /dev/null @@ -1,12 +0,0 @@ -jQuery.extend(Behaviors, { - site: { - form: { - init: function() { - // If the sites neighbourhood's relation_type is primary, it generates a label - // with the class 'cocoon_delete-this'. This allows us to remove it from the form - // so it does not submit a primary relation as being a secondary one or remove it - $('.cocoon_delete-this').parents('.nested-fields').remove(); - } - } - } -}); diff --git a/app/views/admin/sites/_sites_neighbourhood_fields.html.erb b/app/views/admin/sites/_sites_neighbourhood_fields.html.erb index 21521c3b9..de8ece5c9 100644 --- a/app/views/admin/sites/_sites_neighbourhood_fields.html.erb +++ b/app/views/admin/sites/_sites_neighbourhood_fields.html.erb @@ -1,16 +1,17 @@
<%# The style width setting here is applied to dynamically created elements, we do not know why. %> <%# TODO: Fix the dynamic styling so that width does not get applied, so we can remove this smell %> - <%= f.input :neighbourhood_id, collection: options_for_sites_neighbourhoods, include_blank: false, - value_method: ->(obj) { obj[:id] }, - label_method: ->(obj) { obj[:name] }, - input_html: { class: 'form-control', data: { controller: "select2" }, style: "width: 599.8px;" }, - label: '', label_html: { hidden: true } %> - - <%# This label causes behaviours.site.js to remove the association from the form %> - <%= f.label :ignore, class: 'cocoon_delete-this' if f.object.relation_type == 'Primary' %> -
- <%= f.hidden_field :relation_type, value: 'Secondary' %> - <%= link_to_remove_association 'Remove', f, class: "pl-2 pt-1 text-danger" %> -
+ <% if f.object.relation_type != 'Primary' %> + <%# If the sites neighbourhood's relation_type is primary we do not render it %> + <%# so it does not submit a primary relation as being a secondary one or remove it %> + <%= f.input :neighbourhood_id, collection: options_for_sites_neighbourhoods, include_blank: false, + value_method: ->(obj) { obj[:id] }, + label_method: ->(obj) { obj[:name] }, + input_html: { class: 'form-control', data: { controller: "select2" }, style: "width: 599.8px;" }, + label: '', label_html: { hidden: true } %> +
+ <%= f.hidden_field :relation_type, value: 'Secondary' %> + <%= link_to_remove_association 'Remove', f, class: "pl-2 pt-1 text-danger" %> +
+ <% end %>
diff --git a/test/system/admin/site_test.rb b/test/system/admin/site_test.rb index d91f2007c..cda1e3f19 100644 --- a/test/system/admin/site_test.rb +++ b/test/system/admin/site_test.rb @@ -16,6 +16,10 @@ class AdminSiteTest < ApplicationSystemTestCase @neighbourhood_one = neighbourhoods[1].to_s.gsub('w', 'W') @neighbourhood_two = neighbourhoods[2].to_s.gsub('w', 'W') + @sites_neighbourhood = create(:sites_neighbourhood, + site: @site, + neighbourhood: neighbourhoods[1]) + # logging in as root user visit '/users/sign_in' fill_in 'Email', with: 'root@lvh.me' @@ -70,4 +74,13 @@ class AdminSiteTest < ApplicationSystemTestCase tags = select2_node 'site_tags' assert_select2_multiple [@tag.name, @tag_pub.name], tags end + test 'primary neighbourhood not rendering on other neighbourhoods section' do + click_sidebar 'sites' + await_datatables + click_link @site.name + await_select2 + service_areas = all_cocoon_select2_nodes 'sites_neighbourhoods' + msg = '@site should only have a primary neighbourhood, if this fails either this is now rendering where it should\'t or another neighborhood has been added at setup and the test should be adjusted' + assert service_areas.length.zero?, msg + end end