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 +++++++++++++++++-- public/styles.css | 13 ++++++++++++- registration.rb | 15 ++++++++++++--- spec/registration_spec.rb | 19 +++++++++++++++++++ views/layout.erb | 11 +++++++++++ 5 files changed, 71 insertions(+), 6 deletions(-) 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 diff --git a/public/styles.css b/public/styles.css index d0b13fc..a6c3f46 100644 --- a/public/styles.css +++ b/public/styles.css @@ -36,4 +36,15 @@ input[type=text], textarea { .song-info, .member-info { padding-left: 1em; -} \ No newline at end of file +} + +.error { + background-color: #A81B1B; + color: #D1D1D1; + padding: 2mm; +} + +.error .error-heading { + font-size: 110%; + color: whitesmoke; +} diff --git a/registration.rb b/registration.rb index e5be8f0..c6598d1 100644 --- a/registration.rb +++ b/registration.rb @@ -1,9 +1,14 @@ require 'sinatra/base' +require 'sinatra/url_for' require_relative 'lib/registration' class RegistrationApp < Sinatra::Base + helpers Sinatra::UrlForHelper + include ERB::Util get '/' do + @errors = request['errors'] + @errors = @errors.split('|') if @errors erb :index end @@ -11,9 +16,13 @@ class RegistrationApp < Sinatra::Base if request.form_data? #p request['band'] @band = Band.new(request['band']) - generate_pdf_for @band - send_registration_emails_for @band - erb :submitted + if @band.valid? + generate_pdf_for @band + send_registration_emails_for @band + erb :submitted + else + redirect to(url_for('/', :errors => @band.errors.join('|'))) + end end end diff --git a/spec/registration_spec.rb b/spec/registration_spec.rb index be4e6f7..3d7deed 100644 --- a/spec/registration_spec.rb +++ b/spec/registration_spec.rb @@ -84,5 +84,24 @@ describe RegistrationApp do expect(message.subject).to match /Registrering av band Imbalance til Norsk Urskog/ end end + + context 'with no songs' do + before :each do + @doc_spy = spy("Prawn::Document") + allow(Prawn::Document).to receive('new') { @doc_spy } + allow(@doc_spy).to receive('render_file') + + post '/submit', 'band' => create_band_params(:songs => 0) + end + + it 'should reject request and go back to the registration form' do + expect(last_response.status).to eq(302) + end + + it 'should display an error message' do + follow_redirect! + expect(last_response.body).to include "Du må ha med minst én låt" + end + end end end diff --git a/views/layout.erb b/views/layout.erb index a6f8dd8..efda293 100644 --- a/views/layout.erb +++ b/views/layout.erb @@ -8,6 +8,17 @@ Norsk Urskog 2015 - Påmeldingsskjema for band + <% if @errors %> +
+

Det er feil i skjemaet!

+

Vennligst se over følgende og prøv igjen:

+ +
+ <% end %> <%= yield %> -- cgit v1.2.3