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: { - +