-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpolls.coffee
More file actions
88 lines (62 loc) · 2.04 KB
/
polls.coffee
File metadata and controls
88 lines (62 loc) · 2.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
if Meteor.isClient
Meteor.subscribe 'polls'
Meteor.startup ->
Session.set 'editPoll', newPoll()
Template.registerHelper 'ordinal', (i) -> i + 1
@ordinalLetter = (i) ->
if i < 26 then String.fromCharCode(i + 65) else i+1
Template.registerHelper 'ordinalLetter', ordinalLetter
Template.registerHelper 'isActiveRoute', (str) ->
str is FlowRouter.getRouteName()
Template.polls.helpers
polls : Polls.find()
mayNotCreate : ->
not Meteor.userId()
FlowRouter.route '/',
name : 'home'
action : ->
BlazeLayout.render 'layout',
content : 'polls'
FlowRouter.route '/about',
action : ->
BlazeLayout.render 'layout',
content : 'about'
FlowRouter.route '/edit/:_id',
action : ->
BlazeLayout.render 'layout',
content : 'pollEdit'
FlowRouter.route '/vote/:_id',
action : ->
BlazeLayout.render 'layout',
content : 'pollVote'
Accounts.ui.config
passwordSignupFields: "USERNAME_AND_EMAIL"
if Meteor.isServer
Meteor.publish 'polls', ->
Polls.find()
Meteor.methods
vote : (pollId, result) ->
poll = Polls.findOne(pollId)
unless this.userId
throw new Meteor.Error 'logged-out', 'must be logged in to vote'
if _.contains poll.haveVoted, this.userId
throw new Meteor.Error 'has-Voted', 'you may only vote once'
Polls.update pollId,
$inc :
"answers.#{result}.amount" : 1
$push :
haveVoted : this.userId
savePoll : (poll, callback) ->
unless this.userId
throw new Meteor.Error 'logged-out', 'must be logged in to save Polls'
if poll._id?
Polls.update poll._id, {$set : poll}, {modifier : true}, callback
else
Polls.insert poll, callback
deletePoll : (pollId) ->
poll = Polls.findOne(pollId)
unless this.userId
throw new Meteor.Error 'logged-out', 'must be logged in to delete Polls'
unless poll.creatorId is this.userId
throw new Meteor.Error 'not-the-owner', 'can only delete your own Polls'
Polls.remove pollId