From d5b1effe3be59d462eac8531feaebe4f9906901c Mon Sep 17 00:00:00 2001 From: Visnu Pitiyanuvath Date: Sat, 21 Aug 2010 13:22:43 -0700 Subject: [PATCH] 1-5 validation new vote with actual object --- models/models.coffee | 20 ++++++++++++++------ nodeko.coffee | 30 +++++++++++++++++++----------- views/application.js.coffee | 9 ++++++--- views/votes/new.html.jade | 10 +++++----- 4 files changed, 44 insertions(+), 25 deletions(-) diff --git a/models/models.coffee b/models/models.coffee index 7153d341..3b936d30 100644 --- a/models/models.coffee +++ b/models/models.coffee @@ -227,15 +227,15 @@ class Vote @person = options?.person @team = options?.team - @usefulness = options?.usefulness - @appearance = options?.appearance - @innovation = options?.innovation - @completeness = options?.completeness + @usefulness = parseInt options?.usefulness + @design = parseInt options?.design + @innovation = parseInt options?.innovation + @completeness = parseInt options?.completeness @comment = options?.comment - @ip_address = options?.ip_address - @user_agent = options?.user_agent + @ipAddress = options?.ipAddress + @userAgent = options?.userAgent @referer = option?.referer @requestAt = options?.requestAt @@ -244,6 +244,14 @@ class Vote @createdAt = @updatedAt = new Date() + validate: -> + errors = [] + for dimension in [ 'Usefulness', 'Design', 'Innovation', 'Completeness' ] + errors.push "#{dimension} should be between 1 and 5 stars" unless 1 <= this[dimension.toLowerCase()] <= 5 + errors + +nko.Vote = Vote + Mongo.blessAll nko Team::toParam = -> @slug diff --git a/nodeko.coffee b/nodeko.coffee index bd57b261..eb9b4aca 100644 --- a/nodeko.coffee +++ b/nodeko.coffee @@ -237,19 +237,27 @@ get '/teams/:teamId/invite/:personId', -> @redirect '/teams/' + team.toParam() # new vote -get '/votes/new', -> - # @vote = new Vote() - @render 'votes/new.html.jade', { layout: 'layout.haml' } +get '/teams/:teamId/votes/new', -> + Team.fromParam @req.param('teamId'), (error, team) => + # TODO: handle error + @team = team + @vote = new Vote() + @email = @currentPerson?.email + @render 'votes/new.html.jade', { layout: 'layout.haml' } # create vote -post '/votes', -> - @vote = new Vote @req.body - @vote.save (errors, res) => - if errors? - @errors = errors - @render 'votes/new.html.haml' - else - @redirect '/votes.json' +post '/teams/:teamId/votes', -> + Team.fromParam @req.param('teamId'), (error, team) => + # TODO: handle error + @vote = new Vote @req.body + @vote.team = @team = team + @vote.save (errors, res) => + if errors? + @errors = errors + @email = @vote.email + @render 'votes/new.html.jade', { layout: 'layout.haml' } + else + @redirect '/votes.json' Serializer = require('./models/mongo').Serializer get '/votes.json', -> diff --git a/views/application.js.coffee b/views/application.js.coffee index 455afa58..3a89bfae 100644 --- a/views/application.js.coffee +++ b/views/application.js.coffee @@ -111,14 +111,17 @@ $ -> oldVal = @input(elem).val() @input(elem).val(if newVal is oldVal then 0 else newVal) highlight: (elem, hover) -> - score = if hover then @value(elem) else @input(elem).val() - elem.parent().children().each (i, star) -> + score = parseInt(if hover then @value(elem) else @input(elem).val()) + elem.closest('.stars').children().each (i, star) -> $star = $(star) fill = $star.attr('data-value') <= score $star.find('.filled').toggle(fill) $star.find('.empty').toggle(!fill) } + $('.stars').each -> + Stars.highlight $(this) + $('.star').hover (-> Stars.highlight $(this), true), (-> Stars.highlight $(this)) - $('.star').click -> Stars.set($(this)) \ No newline at end of file + $('.star').click -> Stars.set($(this)) diff --git a/views/votes/new.html.jade b/views/votes/new.html.jade index eaa6e7a2..72032347 100644 --- a/views/votes/new.html.jade +++ b/views/votes/new.html.jade @@ -1,15 +1,15 @@ -h1.ribbon Cast Your Vote +h1.ribbon Vote for #{ctx.team.name} != partial("errors.html.haml", { locals: { ctx: ctx } }) -form( method: 'POST', action: '/votes' ) +form( method: 'POST', action: '/teams/' + ctx.team.toParam() + '/votes' ) table - - each dimension in ['Usefulness', 'Appearance', 'Innovation', 'Completeness'] + - each dimension in ['Usefulness', 'Design', 'Innovation', 'Completeness'] tr td label( for: dimension.toLowerCase() )= dimension td - input( type: 'hidden', id: dimension.toLowerCase(), name: dimension.toLowerCase(), value: 0 ) + input( type: 'hidden', id: dimension.toLowerCase(), name: dimension.toLowerCase(), value: ctx.vote[dimension.toLowerCase()] || 0 ) .stars - for (var val = 1; val <= 5; val++) span.star( 'data-value': val ) @@ -22,7 +22,7 @@ form( method: 'POST', action: '/votes' ) h2 label( for: 'email' ) Email .note Required. To confirm your vote. We don't spam. - input.email( type: 'text', name: 'email', id: 'email' ) + input.email( type: 'text', name: 'email', id: 'email', value: ctx.email || '' ) p input.button( type: 'submit', value: 'Vote!' )