From 01987cc0b88fdb0748d8d71cca4f5e40ea8a2604 Mon Sep 17 00:00:00 2001 From: Harald Eilertsen Date: Mon, 5 Oct 2015 20:13:33 +0200 Subject: Redirect back to form and display error if no songs specified. --- lib/band.rb | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) (limited to 'lib') diff --git a/lib/band.rb b/lib/band.rb index 22778ff..49180d8 100644 --- a/lib/band.rb +++ b/lib/band.rb @@ -4,8 +4,10 @@ require_relative 'song' class Band attr_reader :name, :city, :website, :label, :short_bio, :contact, :members, :songs + attr_reader :errors def initialize(params) + @errors = [] @name = params['name'] @city = params['city'] @website = params['website'] @@ -20,8 +22,21 @@ class Band end @songs = [] - params['songs'].each do |k, s| - @songs << Song.new(s) + if params['songs'] + params['songs'].each do |k, s| + @songs << Song.new(s) + end + end + end + + def valid? + validate! + errors.length == 0 + end + + def validate! + if @songs.length <= 0 + errors << "Du må ha med minst én låt!" end end end -- cgit v1.2.3