Skip to content

Commit

Permalink
make solution required
Browse files Browse the repository at this point in the history
  • Loading branch information
dereke committed Dec 22, 2019
1 parent 1fbbad4 commit e44c42b
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 13 deletions.
6 changes: 6 additions & 0 deletions app/models/initiative.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ class Initiative < ApplicationRecord
:lead_group,
presence: true

validate :at_least_one_solution_or_theme

def at_least_one_solution_or_theme
errors.add(:solution, 'at least one equired') if solutions.empty? && themes.empty?
end

def set_default_location
self.latitude ||= 51.742
self.longitude ||= -2.222
Expand Down
2 changes: 1 addition & 1 deletion app/views/initiatives/_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@

<h2>Solutions</h2>

<%= f.form_field :choose_solution, required: true do %>
<%= f.form_field :solution, required: true do %>
<input type="hidden" id="initial_themes" value="<%=themes_as_json(initiative)%>">
<input type="hidden" id="initial_solutions" value="<%=solutions_as_json(initiative)%>">
<div id="solution_picker"></div>
Expand Down
14 changes: 12 additions & 2 deletions test/controllers/initiatives_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ def header_image
@initiative.images.attach(header_image)
images = [avenue_image]
patch initiative_url(@initiative),
params: update_params(@initiative, images)
params: update_params(@initiative, images: images)
assert_equal 2, @initiative.reload.images.size
websites = @initiative.websites
assert_equal 2, websites.size
Expand All @@ -124,6 +124,7 @@ def header_image

# rubocop:disable Metrics/MethodLength
def create_params(initiative, lead_group: nil, images: nil, solutions: nil)
solutions ||= default_solutions
{
initiative: {
anticipated_carbon_saving: initiative.anticipated_carbon_saving,
Expand All @@ -149,7 +150,8 @@ def create_params(initiative, lead_group: nil, images: nil, solutions: nil)
}
end

def update_params(initiative, images)
def update_params(initiative, images: nil, solutions: nil)
solutions ||= default_solutions
{
initiative: {
anticipated_carbon_saving: initiative.anticipated_carbon_saving,
Expand All @@ -165,6 +167,7 @@ def update_params(initiative, images)
summary: initiative.summary,
images: images,
consent_to_share: true,
solutions_attributes: solutions,
websites_attributes: [
{ website: 'http://one' },
{ website: 'http://two' }
Expand All @@ -173,4 +176,11 @@ def update_params(initiative, images)
}
end
# rubocop:enable Metrics/MethodLength

def default_solutions
solution_class = SolutionSolutionClass.last.solution_class
solution = SolutionSolutionClass.last.solution

{ '0': { solution_class_id: solution_class.id, solution_id: solution.id } }
end
end
13 changes: 4 additions & 9 deletions test/fixtures/initiative_solutions.yml
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
# Read about fixtures at https://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html

one:
solution: one
solution_class: one
initiative: one

two:
solution: two
solution_class: two
initiative: two
fruit_exchange_ashp:
solution: ashp
solution_class: heat_pumps
initiative: fruit_exchange
9 changes: 8 additions & 1 deletion test/models/initiative_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,14 @@ def expected_initiative_attributes
summary:
'The Fruit Exchange connects food outlets with people who have surplus fruit or veg.',
status: 'Operational',
solutions: [],
solutions: [
{
sector: 'Energy',
theme: 'Heating',
class: 'Heat pumps',
solution: 'ASHP'
}
],
themes: [],
websites: [],
images: []
Expand Down

0 comments on commit e44c42b

Please sign in to comment.