From b38567149629a906a485dda8e700125902ba4c09 Mon Sep 17 00:00:00 2001 From: Derek Ekins Date: Tue, 17 Aug 2021 13:22:13 +0100 Subject: [PATCH 1/9] get smtp configuration from env variables --- config/environments/production.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/config/environments/production.rb b/config/environments/production.rb index ec099ca..d51c858 100644 --- a/config/environments/production.rb +++ b/config/environments/production.rb @@ -76,10 +76,10 @@ config.i18n.fallbacks = true ActionMailer::Base.smtp_settings = { - address: 'smtp.sendgrid.net', + address: ENV['SMTP_SERVER'], port: 587, - user_name: ENV['SENDGRID_USERNAME'], - password: ENV['SENDGRID_PASSWORD'], + user_name: ENV['SMTP_USERNAME'], + password: ENV['SMTP_PASSWORD'], domain: 'carbonneutralmap.org.uk', authentication: :plain, enable_starttls_auto: true From a4dd2e97701188b81b7d20872e7dac60c49f89b4 Mon Sep 17 00:00:00 2001 From: Derek Ekins Date: Tue, 17 Aug 2021 13:27:14 +0100 Subject: [PATCH 2/9] upgrade mimemagic --- Gemfile.lock | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/Gemfile.lock b/Gemfile.lock index 3995df0..9a28ec1 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -154,7 +154,9 @@ GEM meta-tags (2.13.0) actionpack (>= 3.2.0, < 6.1) method_source (1.0.0) - mimemagic (0.3.5) + mimemagic (0.3.10) + nokogiri (~> 1) + rake mini_magick (4.9.5) mini_mime (1.0.2) mini_portile2 (2.4.0) @@ -342,4 +344,4 @@ RUBY VERSION ruby 2.6.5p114 BUNDLED WITH - 2.1.4 + 2.2.5 From f41ce4dac96dfed188ec665769244e43c224b1a1 Mon Sep 17 00:00:00 2001 From: Derek Ekins Date: Fri, 20 Aug 2021 07:50:14 +0100 Subject: [PATCH 3/9] add basic search --- .gitignore | 1 + app/helpers/suit_form_builder.rb | 12 ++--- app/models/group_website.rb | 2 +- app/models/initiative_website.rb | 2 +- app/models/public_initiative.rb | 6 ++- app/views/home/index.html.erb | 10 ++-- frontend/packs/explore_tabs.js | 23 --------- frontend/packs/home.js | 80 ++++++++++++++++++++++++++++++++ frontend/styles/base/base.css | 4 ++ 9 files changed, 100 insertions(+), 40 deletions(-) delete mode 100644 frontend/packs/explore_tabs.js create mode 100644 frontend/packs/home.js diff --git a/.gitignore b/.gitignore index 03b6b8b..0a6875c 100644 --- a/.gitignore +++ b/.gitignore @@ -6,6 +6,7 @@ # Ignore bundler config. /.bundle +vendor/bundle/**/*.* # Ignore the default SQLite database. /db/*.sqlite3* diff --git a/app/helpers/suit_form_builder.rb b/app/helpers/suit_form_builder.rb index 2ef7e1e..1ea2617 100644 --- a/app/helpers/suit_form_builder.rb +++ b/app/helpers/suit_form_builder.rb @@ -10,9 +10,9 @@ def form_field(field, options = {}, &block) css_classes << 'FormField--required' if required css_classes << 'is-error' if field_errors.any? css_classes << options[:class_names] - # rubocop:disable HelperInstanceVariable - @template.content_tag :div, class: css_classes, &block - # rubocop:enable HelperInstanceVariable + # rubocop:disable Rails/HelperInstanceVariable + @template.tag.div(class: css_classes, &block) + # rubocop:enable Rails/HelperInstanceVariable end def label(method, text = nil, options = {}) @@ -41,9 +41,9 @@ def check_box(attribute, options = {}, checked_value = '1', unchecked_value = '0 end label(attribute, class: 'FormField-check FormField-check--checkbox') do super(attribute, options.reverse_merge(class: 'FormField-checkInput'), checked_value, unchecked_value) + - # rubocop:disable HelperInstanceVariable - @template.content_tag(:span, label_text, class: 'FormField-checkLabel') - # rubocop:enable HelperInstanceVariable + # rubocop:disable Rails/HelperInstanceVariable + @template.tag.span(label_text, class: 'FormField-checkLabel') + # rubocop:enable Rails/HelperInstanceVariable end end diff --git a/app/models/group_website.rb b/app/models/group_website.rb index fe7bfcd..6c9cf14 100644 --- a/app/models/group_website.rb +++ b/app/models/group_website.rb @@ -2,7 +2,7 @@ class GroupWebsite < ApplicationRecord belongs_to :group - validates :url, format: URI.regexp(%w[http https]) + validates :url, format: URI::DEFAULT_PARSER.make_regexp(%w[http https]) include Website delegate :name, prefix: true, to: :group end diff --git a/app/models/initiative_website.rb b/app/models/initiative_website.rb index abf5472..dc1b405 100644 --- a/app/models/initiative_website.rb +++ b/app/models/initiative_website.rb @@ -1,7 +1,7 @@ # frozen_string_literal: true class InitiativeWebsite < ApplicationRecord - validates :url, format: URI.regexp(%w[http https]) + validates :url, format: URI::DEFAULT_PARSER.make_regexp(%w[http https]) include Website belongs_to :initiative diff --git a/app/models/public_initiative.rb b/app/models/public_initiative.rb index 30ab8c0..0bb89df 100644 --- a/app/models/public_initiative.rb +++ b/app/models/public_initiative.rb @@ -3,7 +3,8 @@ # The publicly available data of an initiative class PublicInitiative include ActionView::Helpers::DateHelper - delegate :name, + delegate :id, + :name, :description_what, :description_how, :description_further_information, @@ -70,7 +71,7 @@ def websites end def last_updated - time_ago_in_words(timestamp) + ' ago' + "#{time_ago_in_words(timestamp)} ago" end def href @@ -81,6 +82,7 @@ def href # rubocop:disable Metrics/AbcSize def as_json(_options = {}) { + 'id': id, 'name': name, 'description_what': description_what, 'description_how': description_how, diff --git a/app/views/home/index.html.erb b/app/views/home/index.html.erb index d857803..9644b9e 100644 --- a/app/views/home/index.html.erb +++ b/app/views/home/index.html.erb @@ -1,4 +1,4 @@ -<%= javascript_packs_with_chunks_tag 'explore_tabs', 'data-turbolinks-track': 'reload' %> +<%= javascript_packs_with_chunks_tag 'home', 'data-turbolinks-track': 'reload' %>

We share community-based projects that work towards a Carbon Neutral Stroud District by 2030 and connect groups organising them.

Share knowledge. Enable Change

@@ -16,13 +16,9 @@ Or -
+
-
@@ -75,7 +71,7 @@ <% @initiatives.each do |initiative| %> - + <%= link_to initiative.name, initiative_path(initiative) %> <%= initiative.location %> <%= initiative.status_name %> diff --git a/frontend/packs/explore_tabs.js b/frontend/packs/explore_tabs.js deleted file mode 100644 index 5ef216c..0000000 --- a/frontend/packs/explore_tabs.js +++ /dev/null @@ -1,23 +0,0 @@ -window.addEventListener("load", function() { - document.querySelectorAll(".Explore-tab").forEach(el => { - el.addEventListener("click", e => { - e.preventDefault(); - - focusTab(el); - }); - }); -}); -function focusTab(tab) { - const name = tab.getAttribute("data-content"); - document.querySelectorAll(".Explore-tab").forEach(el => { - el.classList.remove("Explore-tab_selected"); - }); - document.querySelectorAll(".Tab").forEach(el => { - el.classList.remove("Explore-tab_displayed"); - }); - tab.classList.add("Explore-tab_selected"); - document - .querySelector(`[data-content=${name}-tab]`) - .classList.add("Explore-tab_displayed"); - return false; -} diff --git a/frontend/packs/home.js b/frontend/packs/home.js new file mode 100644 index 0000000..250f9f6 --- /dev/null +++ b/frontend/packs/home.js @@ -0,0 +1,80 @@ +function focusTab(tab) { + const name = tab.getAttribute("data-content"); + document.querySelectorAll(".Explore-tab").forEach(el => { + el.classList.remove("Explore-tab_selected"); + }); + document.querySelectorAll(".Tab").forEach(el => { + el.classList.remove("Explore-tab_displayed"); + }); + tab.classList.add("Explore-tab_selected"); + document + .querySelector(`[data-content=${name}-tab]`) + .classList.add("Explore-tab_displayed"); + return false; +} + +window.addEventListener("load", function() { + document.querySelectorAll(".Explore-tab").forEach(el => { + el.addEventListener("click", e => { + e.preventDefault(); + + focusTab(el); + }); + }); +}); + +window.addEventListener("load", () => { + document + .querySelector("#search") + .addEventListener("submit", e => e.preventDefault()); + const initiatives = JSON.parse( + document.getElementById("map_data_json").innerText + ).initiatives; + const searchStrings = {}; + initiatives.forEach(initiative => { + const initiativeString = Object.values(initiative) + .map(value => { + if (value != null && typeof value == "object") { + Object.values(value) + .toString() + .toLowerCase(); + } else { + return value; + } + }) + .toString() + .toLowerCase(); + searchStrings[initiative.id] = initiativeString; + }); + + document + .querySelector("[type=search][name=q]") + .addEventListener("input", e => { + focusTab(document.querySelector("[data-content=initiatives]")); + const search = e.target.value.toLowerCase().split(" "); + const matches = []; + + for (const initiativeId in searchStrings) { + const initiativeData = searchStrings[initiativeId]; + + const partialMatch = []; + search.forEach(item => { + if (initiativeData.includes(item)) { + partialMatch.push(initiativeId); + } + }); + if (partialMatch.length == search.length) { + matches.push(initiativeId); + } + } + + document.querySelectorAll(".Initiative").forEach(initiativeEl => { + initiativeEl.classList.add("u-hidden"); + }); + matches.forEach(initiativeId => { + document + .querySelector(`[data-content=initiative_${initiativeId}]`) + .classList.remove("u-hidden"); + }); + }); +}); diff --git a/frontend/styles/base/base.css b/frontend/styles/base/base.css index 3bf1991..f6779bb 100644 --- a/frontend/styles/base/base.css +++ b/frontend/styles/base/base.css @@ -70,3 +70,7 @@ textarea { justify-content: center; width: 37px; } + +.u-hidden { + display: none; +} From cbee536313f5dd5bd970417b735afe01e4c010e2 Mon Sep 17 00:00:00 2001 From: Derek Ekins Date: Fri, 20 Aug 2021 08:59:06 +0100 Subject: [PATCH 4/9] fix up failing tests --- .exrc | 30 +++++++++++++++++++---- app/controllers/initiatives_controller.rb | 2 ++ test/models/initiative_test.rb | 1 + 3 files changed, 28 insertions(+), 5 deletions(-) diff --git a/.exrc b/.exrc index 6edac40..e1fc3b0 100644 --- a/.exrc +++ b/.exrc @@ -1,9 +1,29 @@ -let g:vigun_extra_keywords = ['test'] +let g:vigun_test_keywords = ['test', 'it', 'context', 'describe'] +let g:vjs_tags_enabled = 0 -let g:vigun_commands = [ +fun! s:watch(cmd) + return "rg --files | entr -r -c sh -c 'echo ".escape('"'.a:cmd.'"', '"')." && ".a:cmd."'" +endf + +let g:vigun_mappings = [ + \ { + \ 'pattern': 'test/.*_test.rb$', + \ 'all': 'rails test #{file}', + \ 'nearest': 'rails test #{file}:#{line}', + \ 'watch-all': s:watch('rails test #{file}'), + \ 'watch-nearest': s:watch('rails test #{file}:#{line}'), + \ }, \ { - \ 'pattern': 'test/.*_test.rb$', - \ 'normal': 'rails test', - \ 'debug': 'BACKTRACE=1 rails test', + \ 'pattern': 'test/javascript/.*_test.js$', + \ 'all': 'yarn test #{file}', + \ 'nearest': 'yarn test --fgrep #{nearest_test} #{file}', + \ 'debug-all': 'yarn test --interactive #{file}', + \ 'debug-nearest': 'yarn test --interactive --fgrep #{nearest_test} #{file}', \ } \] + +command! -nargs=0 Fixturex :cexpr system('./bin/fixturex.rb '. expand('%:t:r') .' '.shellescape(expand(''))) | copen + +" open report fixture in a split to the right +" nnoremap gj :exec "botright vnew +" test/fixtures/reports/".expand('').".json" diff --git a/app/controllers/initiatives_controller.rb b/app/controllers/initiatives_controller.rb index 5fb11a6..684b740 100644 --- a/app/controllers/initiatives_controller.rb +++ b/app/controllers/initiatives_controller.rb @@ -2,6 +2,8 @@ # rubocop:disable Metrics/ClassLength class InitiativesController < ApplicationController + include ApplicationHelper + before_action :set_initiative, only: %i[edit update] before_action :set_edit_data, only: %i[edit new create update] skip_before_action :authenticate_user!, only: %i[index show] diff --git a/test/models/initiative_test.rb b/test/models/initiative_test.rb index 6c65d2e..798d12d 100644 --- a/test/models/initiative_test.rb +++ b/test/models/initiative_test.rb @@ -64,6 +64,7 @@ class InitiativeTest < ActiveSupport::TestCase def expected_initiative_attributes [ { + id: Initiative.first.id, name: 'The Fruit Exchange', description_what: 'Connecting people with surplus food', description_how: 'Bringing food to the people', From a3a4702520a46ec360a7a7ef3edb182d6646d6a9 Mon Sep 17 00:00:00 2001 From: Derek Ekins Date: Fri, 20 Aug 2021 09:05:27 +0100 Subject: [PATCH 5/9] make lint happy --- config/environments/production.rb | 2 +- config/puma.rb | 10 +++++----- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/config/environments/production.rb b/config/environments/production.rb index ec099ca..93b7ede 100644 --- a/config/environments/production.rb +++ b/config/environments/production.rb @@ -97,7 +97,7 @@ # config.logger = ActiveSupport::TaggedLogging.new(Syslog::Logger.new 'app-name') if ENV['RAILS_LOG_TO_STDOUT'].present? - logger = ActiveSupport::Logger.new(STDOUT) + logger = ActiveSupport::Logger.new($stdout) logger.formatter = config.log_formatter config.logger = ActiveSupport::TaggedLogging.new(logger) end diff --git a/config/puma.rb b/config/puma.rb index 0402224..5a8cca2 100644 --- a/config/puma.rb +++ b/config/puma.rb @@ -6,20 +6,20 @@ # the maximum value specified for Puma. Default is set to 5 threads for minimum # and maximum; this matches the default thread size of Active Record. # -max_threads_count = ENV.fetch('RAILS_MAX_THREADS') { 5 } -min_threads_count = ENV.fetch('RAILS_MIN_THREADS') { max_threads_count } +max_threads_count = ENV.fetch('RAILS_MAX_THREADS', 5) +min_threads_count = ENV.fetch('RAILS_MIN_THREADS', max_threads_count) threads min_threads_count, max_threads_count # Specifies the `port` that Puma will listen on to receive requests; default is 3000. # -port ENV.fetch('PORT') { 3_000 } +port ENV.fetch('PORT', 3_000) # Specifies the `environment` that Puma will run in. # -environment ENV.fetch('RAILS_ENV') { 'development' } +environment ENV.fetch('RAILS_ENV', 'development') # Specifies the `pidfile` that Puma will use. -pidfile ENV.fetch('PIDFILE') { 'tmp/pids/server.pid' } +pidfile ENV.fetch('PIDFILE', 'tmp/pids/server.pid') # Specifies the number of `workers` to boot in clustered mode. # Workers are forked web server processes. If using threads and workers together From cd0bb278714f01e29d9bf1fdd51a0f4b408344f3 Mon Sep 17 00:00:00 2001 From: Derek Ekins Date: Fri, 20 Aug 2021 09:49:53 +0100 Subject: [PATCH 6/9] link to privacy policy from email address field --- app/views/initiatives/_form.html.erb | 1 + 1 file changed, 1 insertion(+) diff --git a/app/views/initiatives/_form.html.erb b/app/views/initiatives/_form.html.erb index 5e6d5bd..9a9c8b7 100644 --- a/app/views/initiatives/_form.html.erb +++ b/app/views/initiatives/_form.html.erb @@ -100,6 +100,7 @@ <%= f.form_field :contact_email, required: true do %> <%= f.label :contact_email, 'Email address to contact about this project' %> +
Find out how we use your email address - <%= link_to 'Privacy Policy', :privacy%>
<%= f.text_field :contact_email, placeholder: 'Email address' %> <% end %> From 45b5e5a503fefd112f1c07264f049c94778db7ea Mon Sep 17 00:00:00 2001 From: Derek Ekins Date: Fri, 20 Aug 2021 10:04:23 +0100 Subject: [PATCH 7/9] further privacy policy link enhancements --- app/views/initiatives/_form.html.erb | 2 +- frontend/styles/components/hint.css | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/app/views/initiatives/_form.html.erb b/app/views/initiatives/_form.html.erb index 9a9c8b7..51e02f3 100644 --- a/app/views/initiatives/_form.html.erb +++ b/app/views/initiatives/_form.html.erb @@ -100,7 +100,7 @@ <%= f.form_field :contact_email, required: true do %> <%= f.label :contact_email, 'Email address to contact about this project' %> -
Find out how we use your email address - <%= link_to 'Privacy Policy', :privacy%>
+
Find out <%= link_to 'how we use your email address', :privacy%>
<%= f.text_field :contact_email, placeholder: 'Email address' %> <% end %> diff --git a/frontend/styles/components/hint.css b/frontend/styles/components/hint.css index d2d4b4e..399dd9c 100644 --- a/frontend/styles/components/hint.css +++ b/frontend/styles/components/hint.css @@ -1,3 +1,4 @@ -.Hint { +.Hint, +.Hint a { color: #737373; } From d474eaaa2f8d72fd809fb603a91808f1e9c026ab Mon Sep 17 00:00:00 2001 From: Derek Ekins Date: Fri, 20 Aug 2021 11:09:49 +0100 Subject: [PATCH 8/9] improve website listing --- app/controllers/initiatives_controller.rb | 2 ++ app/models/initiative.rb | 4 +++ app/views/initiatives/_form.html.erb | 28 ++++++------------- .../partials/_initiative_website.html.erb | 9 ------ frontend/packs/initiative_websites.js | 18 ++++++++++++ 5 files changed, 33 insertions(+), 28 deletions(-) delete mode 100644 app/views/partials/_initiative_website.html.erb create mode 100644 frontend/packs/initiative_websites.js diff --git a/app/controllers/initiatives_controller.rb b/app/controllers/initiatives_controller.rb index 684b740..a589ac9 100644 --- a/app/controllers/initiatives_controller.rb +++ b/app/controllers/initiatives_controller.rb @@ -70,6 +70,7 @@ def update @initiative.assign_attributes initiative_params @initiative.update_location_from_postcode + @initiative.remove_empty_websites if @initiative.save(validate: publication_status != 'draft') @initiative.images.attach images if images if (params[:step] || '').empty? @@ -119,6 +120,7 @@ def update_solutions(initiative) def set_initiative @initiative = Initiative.find(params[:id]) + @initiative.websites << InitiativeWebsite.new if @initiative.websites.empty? redirect_to initiatives_url unless can_edit_initiative?(@initiative) end diff --git a/app/models/initiative.rb b/app/models/initiative.rb index 7655680..c0dcced 100644 --- a/app/models/initiative.rb +++ b/app/models/initiative.rb @@ -130,6 +130,10 @@ def carbon_saving_quantified? carbon_saving_amount&.positive? end + def remove_empty_websites + websites.delete(websites.select { |website| website.url.blank? }) + end + private def set_default_location diff --git a/app/views/initiatives/_form.html.erb b/app/views/initiatives/_form.html.erb index 51e02f3..6642c96 100644 --- a/app/views/initiatives/_form.html.erb +++ b/app/views/initiatives/_form.html.erb @@ -117,28 +117,18 @@ <%= f.check_box :consent_to_share_phone, 'Make this phone number public. (Please ensure you have permission to share it first)' %> <% end %> -

Website & Social Media

+ <%= f.label :website, 'Website(s) or social media links for further project info' %>
- - - - - - - - - - <%= f.fields_for :websites do |website_form| %> - <%= render 'partials/website_fields_row', f: website_form %> - <% end %> - -
WebsiteDelete?
-
-
- <%= f.fields_for :websites, InitiativeWebsite.new do |website_form| %> - <%= add_object_link 'Add website', 'initiative_websites', :partial => 'partials/website_fields_row', :locals => { :f => website_form } %> + <%= f.fields_for :websites do |website_form| %> + <%= website_form.form_field :url do %> + <%= website_form.text_field :url, placeholder: 'eg: https://twitter.com/carbon-map' %> + <% end %> <% end %> + <%= javascript_packs_with_chunks_tag 'initiative_websites', 'data-turbolinks-track': 'reload' %>
+ + Add another"> + <% end %> <%= initiative_step 5, @current_initiative_step, title: 'Help us understand how you got going', next_text: 'Next: project sector and notes' do %> diff --git a/app/views/partials/_initiative_website.html.erb b/app/views/partials/_initiative_website.html.erb deleted file mode 100644 index 12a0df1..0000000 --- a/app/views/partials/_initiative_website.html.erb +++ /dev/null @@ -1,9 +0,0 @@ -
- <%= f.form_field :url do %> - <%= f.label :url %> - <%= f.text_field :url, placeholder: 'e.g. https://twitter.com/carbon-map, https://facebook.com/carbon-map' %> - <% end %> - <%= f.form_field :_destroy, class_names: 'CollectionItem-destroy' do %> - <%= f.check_box :_destroy, {}, true, false %> - <% end %> -
diff --git a/frontend/packs/initiative_websites.js b/frontend/packs/initiative_websites.js new file mode 100644 index 0000000..f6d6121 --- /dev/null +++ b/frontend/packs/initiative_websites.js @@ -0,0 +1,18 @@ +window.addEventListener("load", () => { + document + .querySelector("[data-content=add_initiative_website]") + .addEventListener("click", e => { + e.preventDefault(); + let index = 0; + while ( + document.querySelector( + `#initiative_websites_attributes_${index}_url` + ) != null + ) { + index++; + } + document.querySelector( + "#initiative_websites" + ).innerHTML += `
`; + }); +}); From 7e69ff5e0af40b636d6d95beb78605ebfd14e0f3 Mon Sep 17 00:00:00 2001 From: Derek Ekins Date: Fri, 20 Aug 2021 14:19:46 +0100 Subject: [PATCH 9/9] various tweaks to layout --- app/controllers/initiatives_controller.rb | 7 +++--- app/models/initiative.rb | 23 ++++++++++--------- app/views/layouts/_base.html.erb | 3 +-- frontend/styles/components/alert.css | 7 ++++-- frontend/styles/components/footer.css | 5 ++-- frontend/styles/components/sponsors.css | 2 +- .../initiatives_controller_test.rb | 13 +++++++++++ 7 files changed, 38 insertions(+), 22 deletions(-) diff --git a/app/controllers/initiatives_controller.rb b/app/controllers/initiatives_controller.rb index a589ac9..6b313a2 100644 --- a/app/controllers/initiatives_controller.rb +++ b/app/controllers/initiatives_controller.rb @@ -37,6 +37,7 @@ def new def edit @current_initiative_step = (params[:step] || 1).to_i + @initiative.websites << InitiativeWebsite.new if @initiative.websites.empty? end # rubocop:disable Metrics/MethodLength @@ -44,7 +45,6 @@ def create @initiative = Initiative.new(initiative_params.merge(owner: current_user)) add_solutions(@initiative) find_or_create_group - @initiative.update_location_from_postcode if @initiative.save(validate: @initiative.publication_status != 'draft') redirect_to edit_initiative_step_path(@initiative, step: 2) @@ -70,8 +70,7 @@ def update @initiative.assign_attributes initiative_params @initiative.update_location_from_postcode - @initiative.remove_empty_websites - if @initiative.save(validate: publication_status != 'draft') + if @initiative.save(validate: @initiative.publication_status != 'draft') @initiative.images.attach images if images if (params[:step] || '').empty? redirect_to initiative_path(@initiative), @@ -80,6 +79,7 @@ def update redirect_to edit_initiative_step_path(@initiative, step: params[:step]) end else + @current_initiative_step = params[:step].blank? ? 1 : params[:step].to_i - 1 render :edit end end @@ -120,7 +120,6 @@ def update_solutions(initiative) def set_initiative @initiative = Initiative.find(params[:id]) - @initiative.websites << InitiativeWebsite.new if @initiative.websites.empty? redirect_to initiatives_url unless can_edit_initiative?(@initiative) end diff --git a/app/models/initiative.rb b/app/models/initiative.rb index c0dcced..8718763 100644 --- a/app/models/initiative.rb +++ b/app/models/initiative.rb @@ -6,18 +6,19 @@ class Initiative < ApplicationRecord after_initialize :set_default_location after_initialize :set_default_publication_status + before_save :remove_empty_websites belongs_to :owner, class_name: 'User' belongs_to :lead_group, class_name: 'Group' belongs_to :status, class_name: 'InitiativeStatus' belongs_to :parish - delegate :name, prefix: true, to: :status - delegate :name, prefix: true, to: :lead_group - delegate :ward, to: :parish - delegate :district, to: :ward - delegate :county, to: :district - delegate :region, to: :county + delegate :name, prefix: true, to: :status, allow_nil: true + delegate :name, prefix: true, to: :lead_group, allow_nil: true + delegate :ward, to: :parish, allow_nil: true + delegate :district, to: :ward, allow_nil: true + delegate :county, to: :district, allow_nil: true + delegate :region, to: :county, allow_nil: true has_many :solutions, class_name: 'InitiativeSolution', dependent: :destroy has_many :themes, class_name: 'InitiativeTheme', dependent: :destroy @@ -111,11 +112,11 @@ def location # rubocop:disable Metrics/MethodLength def location_attributes { - parish: parish.name, - ward: ward.name, - district: district.name, - county: county.name, - region: region.name, + parish: parish&.name, + ward: ward&.name, + district: district&.name, + county: county&.name, + region: region&.name, postcode: postcode, latlng: { # "Down to Earth Stroud, PO Box 427, Stonehouse, Gloucestershire, GL6 1JG", diff --git a/app/views/layouts/_base.html.erb b/app/views/layouts/_base.html.erb index b5a61a6..2a07851 100644 --- a/app/views/layouts/_base.html.erb +++ b/app/views/layouts/_base.html.erb @@ -115,9 +115,8 @@ set_meta_tags og: { - +