Skip to content

Commit

Permalink
Persist hidden proposals
Browse files Browse the repository at this point in the history
After reading through lots of the proposals and pretending like I was
voting, it became clear that persisting the hidden proposals would be
useful incase you needed to leave the page.
  • Loading branch information
beerlington committed Mar 18, 2014
1 parent 6239ece commit cc80514
Show file tree
Hide file tree
Showing 15 changed files with 135 additions and 8 deletions.
15 changes: 13 additions & 2 deletions app/assets/javascripts/controllers/proposal_controller.js.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,19 @@ Proposals.ProposalController = Ember.ObjectController.extend

false

toggleHide: ->
@toggleProperty('model.visible')
toggleHide: (hidden_vote, proposal)->
if hidden_vote
hidden_vote.deleteRecord()
else
hidden_vote = @store.createRecord('hidden_vote',
proposal: proposal
)

hidden_vote.save().then(=>
@toggleProperty('visible')
).catch((error) ->
alert(error.responseText)
)
false

showAbstract: ->
Expand Down
3 changes: 3 additions & 0 deletions app/assets/javascripts/models/hidden_vote.js.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Proposals.HiddenVote = DS.Model.extend
proposal: DS.belongsTo('proposal')
round: DS.attr('string')
3 changes: 2 additions & 1 deletion app/assets/javascripts/models/proposal.js.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ Proposals.Proposal = DS.Model.extend
twitter: DS.attr('string')
github: DS.attr('string')
selected: DS.attr('boolean')
visible: DS.attr('boolean')
vote: DS.belongsTo('vote')
visible: true
hiddenVote: DS.belongsTo('hidden_vote')
hidden: Ember.computed.equal('visible', false)
4 changes: 2 additions & 2 deletions app/assets/javascripts/templates/proposal.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@

{{#unless selected}}
{{#if hidden}}
<span class="hide" {{action 'toggleHide'}}>Show</span>
<span class="hide" {{action 'toggleHide' hiddenVote model}}>Show</span>
{{else}}
<span class="hide" {{action 'toggleHide'}}><i class="fi-dislike"></i> Hide</span>
<span class="hide" {{action 'toggleHide' hiddenVote model}}><i class="fi-dislike"></i> Hide</span>
{{/if}}
{{/unless}}
</dd>
Expand Down
20 changes: 20 additions & 0 deletions app/controllers/api/hidden_votes_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
module Api
class HiddenVotesController < ApplicationController
respond_to :json
before_filter :require_voter!

def create
hidden_votes_in_current_round = current_user.hidden_votes.where(round: current_round)

proposal = Proposal.find(params[:hidden_vote].fetch(:proposal_id))
vote = hidden_votes_in_current_round.create(proposal: proposal)
render json: {hidden_vote: vote}
end

def destroy
vote = current_user.hidden_votes.where(round: current_round).find(params[:id])
vote.destroy
render json: {}
end
end
end
6 changes: 5 additions & 1 deletion app/controllers/api/proposals_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,13 @@ def index
current_votes = current_user.votes.where(round: current_round).
includes(:proposal)

current_hidden_votes = current_user.hidden_votes.where(round: current_round).
includes(:proposal)

respond_with current_round.proposals,
each_serializer: ProposalSerializer,
current_votes: current_votes
current_votes: current_votes,
current_hidden_votes: current_hidden_votes
end
end
end
6 changes: 6 additions & 0 deletions app/models/hidden_vote.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class HiddenVote < ActiveRecord::Base
belongs_to :user
belongs_to :proposal

classy_enum_attr :round
end
1 change: 1 addition & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ class User < ActiveRecord::Base

has_many :proposals
has_many :votes
has_many :hidden_votes

validates :name, presence: true
validates :email, presence: true, on: :update
Expand Down
5 changes: 5 additions & 0 deletions app/serializers/hidden_vote_serializer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class HiddenVoteSerializer < ActiveModel::Serializer
embed :ids
attributes :id, :proposal_id
has_one :proposal
end
13 changes: 12 additions & 1 deletion app/serializers/proposal_serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,31 @@ class ProposalSerializer < ActiveModel::Serializer
embed :ids, include: true

attributes :id, :title, :abstract, :notes, :pitch, :user_name, :twitter,
:github, :selected
:github, :selected, :visible

has_one :vote
has_one :hidden_vote

def vote
vote = options[:current_votes].detect { |vote| vote.proposal_id == object.id }
return if vote.nil?
VoteSerializer.new(vote)
end

def hidden_vote
vote = options[:current_hidden_votes].detect { |vote| vote.proposal_id == object.id }
return if vote.nil?
HiddenVoteSerializer.new(vote)
end

def selected
vote.present?
end

def visible
hidden_vote.nil?
end

def pitch
object.pitch unless anonymous_round?
end
Expand Down
2 changes: 2 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
get '/votes' => 'votes#index'
post '/votes' => 'votes#create'
delete '/votes/:id' => 'votes#destroy'
post '/hidden_votes' => 'hidden_votes#create'
delete '/hidden_votes/:id' => 'hidden_votes#destroy'
end

# Ember app root
Expand Down
11 changes: 11 additions & 0 deletions db/migrate/20140318015232_create_hidden_votes.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class CreateHiddenVotes < ActiveRecord::Migration
def change
create_table :hidden_votes do |t|
t.references :user, index: true
t.references :proposal, index: true
t.string :round, index: true

t.timestamps
end
end
end
13 changes: 12 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,22 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 20140217010430) do
ActiveRecord::Schema.define(version: 20140318015232) do

# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"

create_table "hidden_votes", force: true do |t|
t.integer "user_id"
t.integer "proposal_id"
t.string "round"
t.datetime "created_at"
t.datetime "updated_at"
end

add_index "hidden_votes", ["proposal_id"], name: "index_hidden_votes_on_proposal_id", using: :btree
add_index "hidden_votes", ["user_id"], name: "index_hidden_votes_on_user_id", using: :btree

create_table "proposals", force: true do |t|
t.string "title"
t.text "abstract"
Expand Down
34 changes: 34 additions & 0 deletions test/integration/voting_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,38 @@ class VotingTest < ActionDispatch::IntegrationTest
find('li', text: title)
end
end

test 'Voter hides a proposal in round 1' do
visit root_path
click_link 'Sign In w/ GitHub'
click_link 'Voting'
click_link 'Cast your vote'

title = "PHP Forever"

proposal = Proposal.find_by(title: title)

within '.proposal', text: title do
find('span.hide').click
end

# wait until AJAX request completes
find('.filters', text: 'Hidden (1)')

within '.filters' do
click_link 'Hidden (1)'
end

assert_equal 1, @user.hidden_votes.where(round: :one).count

# Show proposal
within '.proposal', text: title do
find('span.hide').click
end

# wait until AJAX request completes
find('.filters', text: 'Hidden (0)')

assert_equal 0, @user.hidden_votes.where(round: :one).count
end
end
7 changes: 7 additions & 0 deletions test/models/hidden_vote_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
require 'test_helper'

class HiddenVoteTest < ActiveSupport::TestCase
# test "the truth" do
# assert true
# end
end

0 comments on commit cc80514

Please sign in to comment.