From 57af2ee2e51b614ac6e2fa81b1075a55d3c0c252 Mon Sep 17 00:00:00 2001 From: Harald Eilertsen Date: Thu, 15 Oct 2015 10:11:56 +0200 Subject: Retain member and song info on errors in form. --- lib/member.rb | 14 +++++++--- lib/song.rb | 14 +++++----- registration.rb | 4 +-- spec/features/register_band_spec.rb | 13 ++++++++- spec/registration_spec.rb | 24 ++++++++--------- spec/support/band_factory.rb | 54 ++++++++++++++++++------------------- spec/support/submit_form_helper.rb | 2 ++ views/index.erb | 7 +++-- views/member.erb | 11 +++++--- views/song.erb | 17 +++++++----- 10 files changed, 94 insertions(+), 66 deletions(-) diff --git a/lib/member.rb b/lib/member.rb index 0fb69b5..598b5f4 100644 --- a/lib/member.rb +++ b/lib/member.rb @@ -1,10 +1,16 @@ class Member attr_reader :name, :instrument, :age - def initialize(params) - @name = params['name'] - @instrument = params['instrument'] - @age = params['age'].to_i + def initialize(params = nil) + if params + @name = params['name'] + @instrument = params['instrument'] + @age = params['age'].to_i + else + @name = '' + @instrument = '' + @age = 0 + end end def to_s diff --git a/lib/song.rb b/lib/song.rb index 93172f2..482834e 100644 --- a/lib/song.rb +++ b/lib/song.rb @@ -1,13 +1,13 @@ class Song attr_reader :title, :time, :isrc, :composers, :performers, :notes - def initialize(params) - @title = params['title'] - @time = params['time'] - @isrc = params['isrc'] - @composers = params['composers'].split("\n") - @performers = params['performers'].split("\n") - @notes = params['notes'] + def initialize(params = nil) + @title = params ? params['title'] : '' + @time = params ? params['time'] : '' + @isrc = params ? params['isrc'] : '' + @composers = params ? params['composers'].split("\n") : [] + @performers = params ? params['performers'].split("\n") : [] + @notes = params ? params['notes'] : '' end def to_s diff --git a/registration.rb b/registration.rb index 2888387..3aea657 100644 --- a/registration.rb +++ b/registration.rb @@ -15,11 +15,11 @@ class RegistrationApp < Sinatra::Base end get '/member/new/:num' do - erb :member, :layout => false, :locals => { :member_number => params['num'] } + erb :member, :layout => false, :locals => { :member_number => params['num'], :member => Member.new } end get '/song/new/:num' do - erb :song, :layout => false, :locals => { :song_number => params['num'] } + erb :song, :layout => false, :locals => { :song_number => params['num'], :song => Song.new } end post '/submit' do diff --git a/spec/features/register_band_spec.rb b/spec/features/register_band_spec.rb index 92bf9d3..625cb20 100644 --- a/spec/features/register_band_spec.rb +++ b/spec/features/register_band_spec.rb @@ -30,7 +30,7 @@ feature "Submit registration form" do context "with invalid data" do scenario "retains submitted data in form", js: true do - @band_params['songs'] = nil + @band_params['name'] = "" fill_in_form_with(@band_params) expect(find_field('Bandnavn:').value).to eql(@band_params['name']) @@ -48,6 +48,17 @@ feature "Submit registration form" do within '#form-members .member-info' do expect(find_field('Navn:').value).to eql(@band_params['members']['1']['name']) + expect(find_field('Instrument:').value).to eql(@band_params['members']['1']['instrument']) + expect(find_field('Alder:').value).to eql(@band_params['members']['1']['age']) + end + + within '#form-songs #song-1-info' do + expect(find_field('Tittel:').value).to eql(@band_params['songs']['1']['title']) + expect(find_field('Lengde:').value).to eql(@band_params['songs']['1']['time']) + expect(find_field('ISRC-kode:').value).to eql(@band_params['songs']['1']['isrc']) + expect(find_field('Utøvere:').value).to eql(@band_params['songs']['1']['performers']) + expect(find_field('Opphavsmenn:').value).to eql(@band_params['songs']['1']['composers']) + expect(find_field('Andre merknader:').value).to eql(@band_params['songs']['1']['notes']) end end end diff --git a/spec/registration_spec.rb b/spec/registration_spec.rb index 89e2fa5..7e4ca3a 100644 --- a/spec/registration_spec.rb +++ b/spec/registration_spec.rb @@ -53,25 +53,25 @@ describe RegistrationApp do it 'displays submitted data to user' do expect(last_response).to match(/Bandnavn\: #{band_params['name']}/) - expect(last_response).to match(/Hjemsted\: Oslo/) - expect(last_response).to match(/Webside\: http\:\/\/imbalance.no/) - expect(last_response).to match(/Plateselskap\: Calculated Imperfection/) - expect(last_response).to match(/Kort bio\: Thrash metal from Norway/) + expect(last_response).to match(/Hjemsted\: #{band_params['city']}/) + expect(last_response).to match(/Webside\: #{band_params['website']}/) + expect(last_response).to match(/Plateselskap\: #{band_params['label']}/) + expect(last_response).to match(/Kort bio\: #{band_params['shortbio']}/) expect(last_response).to match(/Kontaktperson: #{band_params['contact']['name']}/) expect(last_response).to match(/Kontaktadresse: #{band_params['contact']['addr']}/) expect(last_response).to match(/Telefon: #{band_params['contact']['phone']}/) expect(last_response).to match(/Epost: #{band_params['contact']['email']}/) - expect(last_response).to match(/Harald Eilertsen, Bass\/Vocals/) - expect(last_response).to match(/Welle, Drums/) - expect(last_response).to match(/Thormodr, Guitar/) + expect(last_response).to match(/#{band_params['members']['1']['name']}, #{band_params['members']['1']['instrument']}/) + expect(last_response).to match(/#{band_params['members']['2']['name']}, #{band_params['members']['2']['instrument']}/) + expect(last_response).to match(/#{band_params['members']['3']['name']}, #{band_params['members']['3']['instrument']}/) - expect(last_response).to match(/Bestial by Nature/) - expect(last_response).to match(/Spilletid: 02:80/) - expect(last_response).to match(/Utøvere: Harald Eilertsen, Thormod Steinert, Lars Welle/) - expect(last_response).to match(/Låtskrivere: Harald Eilertsen, Thormod Steinert/) - expect(last_response).to match(/Merknad: Rævrukkje rum kjurr!/) + expect(last_response).to match(/#{band_params['songs']['1']['title']}/) + expect(last_response).to match(/Spilletid: #{band_params['songs']['1']['time']}/) + expect(last_response).to match(/Utøvere: #{band_params['songs']['1']['performers'].split("\n").join(", ")}/) + expect(last_response).to match(/Låtskrivere: #{band_params['songs']['1']['composers'].split("\n").join(", ")}/) + expect(last_response).to match(/Merknad: #{band_params['songs']['1']['notes']}/) end it "generates a PDF file" do diff --git a/spec/support/band_factory.rb b/spec/support/band_factory.rb index 498f532..56993fa 100644 --- a/spec/support/band_factory.rb +++ b/spec/support/band_factory.rb @@ -3,46 +3,46 @@ module BandFactory opts = { :songs => 1, :name => 'Band name', + :city => 'Somewhere', + :website => 'http://bandsite.com', + :label => 'A Record Label', + :bio => 'A short bio about the band', :contact_name => 'Contact Name', :contact_addr => "Streetname 666\n1234Someplace Nice", :contact_phone => '98765432', - :contact_email => 'band@example.com' + :contact_email => 'band@example.com', + :members => [ + { :name => 'Member 1', :instrument => 'Instrument 1', :age => '24' }, + { :name => 'Member 2', :instrument => 'Instrument 2', :age => '0' }, + { :name => 'Member 3', :instrument => '', :age => '' }, + ], }.merge(options) num_songs = opts.delete(:songs) params = { 'name' => opts[:name], - 'city' => 'Oslo', - 'website' => 'http://imbalance.no', - 'label' => 'Calculated Imperfection', - 'shortbio' => 'Thrash metal from Norway', + 'city' => opts[:city], + 'website' => opts[:website], + 'label' => opts[:label], + 'shortbio' => opts[:bio], 'contact' => { 'name' => opts[:contact_name], 'addr' => opts[:contact_addr], 'phone' => opts[:contact_phone], 'email' => opts[:contact_email] }, - 'members' => { - '1' => { - 'name' => 'Harald Eilertsen', - 'instrument' => 'Bass/Vocals', - 'age' => '' - }, - '2' => { - 'name' => 'Welle', - 'instrument' => 'Drums', - 'age' => '' - }, - '3' => { - 'name' => 'Thormodr', - 'instrument' => 'Guitar', - 'age' => '' - } - }, + 'members' => {}, 'songs' => {} } + (1..opts[:members].length).each do |i| + params['members'][i.to_s] = {} + params['members'][i.to_s]['name'] = opts[:members][i-1][:name] + params['members'][i.to_s]['instrument'] = opts[:members][i-1][:instrument] + params['members'][i.to_s]['age'] = opts[:members][i-1][:age] + end + (1..num_songs).each do |i| num = i.to_s params['songs'][num] = create_song_params @@ -53,12 +53,12 @@ module BandFactory def create_song_params { - 'title' => 'Bestial by Nature', - 'time' => '02:80', + 'title' => 'A song title', + 'time' => '02:30', 'isrc' => '', - 'performers' => "Harald Eilertsen\nThormod Steinert\nLars Welle", - 'composers' => "Harald Eilertsen\nThormod Steinert", - 'notes' => 'Rævrukkje rum kjurr!' + 'performers' => "Performer 1\nPerformer 2\nPerformer 3", + 'composers' => "Composer 1\nComposer 2", + 'notes' => 'Some notes' } end end diff --git a/spec/support/submit_form_helper.rb b/spec/support/submit_form_helper.rb index acccbd6..80d06bf 100644 --- a/spec/support/submit_form_helper.rb +++ b/spec/support/submit_form_helper.rb @@ -17,6 +17,8 @@ module SubmitFormHelper within '#form-members .member-info' do fill_in 'Navn:', with: params['members']['1']['name'] + fill_in 'Instrument:', with: params['members']['1']['instrument'] + fill_in 'Alder:', with: params['members']['1']['age'] end if params['songs'] diff --git a/views/index.erb b/views/index.erb index 8565a31..f771ca5 100644 --- a/views/index.erb +++ b/views/index.erb @@ -59,8 +59,8 @@

Klikk på knappen under for å legge til medlemmene som er med i bandet. Dette er kun til informasjon for websider, promomateriale og lignende. Har bandmedlemene pseudonymer de foretrekker å bruke, så kan du bruke dem her.

- <% @band.members.each do |member| %> - <%= erb :member, layout: false, locals: { member_number: 1 } %> + <% @band.members.each do |m| %> + <%= erb :member, layout: false, locals: { member_number: 1, member: m } %> <% end %> @@ -71,6 +71,9 @@ flere låter dersom du vil, men vi kan ikke garantere at vi tar med alle låtene på utgivelsen. Vær nøye med å oppgi riktige navn (ingen pseudonymer) på utøvere og komponister, da vi sjekker dette opp imot TONO/NCB for å sikre at evt. vederlag blir betalt riktig.

+ <% @band.songs.each do |s| %> + <%= erb :song, layout: false, locals: { song_number: 1, song: s } %> + <% end %> diff --git a/views/member.erb b/views/member.erb index 1e6e78e..f618f89 100644 --- a/views/member.erb +++ b/views/member.erb @@ -7,17 +7,20 @@
+ name="band[members][<%= member_number %>][name]" + value="<%= member.name %>">
+ name="band[members][<%= member_number %>][instrument]" + value="<%= member.instrument %>">
- + size="3" pattern="[0-9]{1,2}" + value="<%= member.age %>">
diff --git a/views/song.erb b/views/song.erb index a217d97..701282b 100644 --- a/views/song.erb +++ b/views/song.erb @@ -6,7 +6,8 @@
+ name="band[songs][<%= song_number %>][title]" + value="<%= song.title %>">
@@ -14,6 +15,7 @@ title="Spilletid i min:sek (mm:ss)" id="band-song-<%= song_number %>-time" name="band[songs][<%= song_number %>][time]" + value="<%= song.time %>" size="6">
@@ -22,27 +24,28 @@ title="Dersom låten har vært utgitt før har den en ISRC-kode. Om du vet den så oppgi den her." id="band-song-<%= song_number %>-isrc" name="band[songs][<%= song_number %>][isrc]" + value="<%= song.isrc %>" size="11">
+ title="Fullt navn (ingen pseudonymer) på alle som er med på denne innspillingen." + ><%= song.performers.join("\n") %>
+ title="Fullt navn (ingen pseudonymer) på alle som har vært med på å skrive denne låten." + ><%= song.composers.join("\n") %>
+ title="Evt andre ting vi burde vite." + ><%= song.notes %>
-- cgit v1.2.3