Skip to content

Commit

Permalink
1-5 validation
Browse files Browse the repository at this point in the history
new vote with actual object
  • Loading branch information
Visnu Pitiyanuvath committed Aug 21, 2010
1 parent 97237d4 commit d5b1eff
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 25 deletions.
20 changes: 14 additions & 6 deletions models/models.coffee
Expand Up @@ -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
Expand All @@ -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
Expand Down
30 changes: 19 additions & 11 deletions nodeko.coffee
Expand Up @@ -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', ->
Expand Down
9 changes: 6 additions & 3 deletions views/application.js.coffee
Expand Up @@ -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))
$('.star').click -> Stars.set($(this))
10 changes: 5 additions & 5 deletions 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 )
Expand All @@ -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!' )
Expand Down

0 comments on commit d5b1eff

Please sign in to comment.