-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #127 from cheshire137/add-event-voting
Add event voting
- Loading branch information
Showing
18 changed files
with
226 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
$ -> | ||
$('.vote-container').on 'ajax:success', 'form.vote-form', (event, data) -> | ||
form = event.target | ||
container = form.closest('.vote-container') | ||
container.innerHTML = data |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
.vote-header { | ||
display: inline-block; | ||
margin-right: 0.5em; | ||
} | ||
|
||
.vote-form { | ||
display: inline-block; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
class VotesController < ApplicationController | ||
before_filter :authenticate_user! | ||
before_filter :set_votable | ||
before_filter :ensure_votable | ||
|
||
def create | ||
@votable.liked_by current_user | ||
|
||
if @votable.vote_registered? | ||
render partial: 'votes/form', locals: { votable: @votable } | ||
else | ||
head :unprocessable_entity | ||
end | ||
end | ||
|
||
def destroy | ||
if current_user.voted_for?(@votable) | ||
@votable.unvote_by current_user | ||
end | ||
|
||
render partial: 'votes/form', locals: { votable: @votable } | ||
end | ||
|
||
private | ||
|
||
def set_votable | ||
model = if params[:votable_type] == "Event" | ||
Event | ||
end | ||
|
||
if model | ||
@votable = model.find_by_id(params[:votable_id]) | ||
end | ||
end | ||
|
||
def ensure_votable | ||
return head(:not_found) unless @votable | ||
|
||
# Disallow users to vote on their own items: | ||
head :forbidden if can_edit?(@votable) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
module VotesHelper | ||
def upvote_button_class(votable) | ||
if current_user.voted_up_on?(votable) | ||
"btn bg-orange" | ||
else | ||
"btn bg-grey" | ||
end | ||
end | ||
|
||
def upvote_form_method(votable) | ||
if current_user.voted_up_on?(votable) | ||
:delete | ||
else | ||
:post | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<span class="vote-header">Would you recommend this?</span> | ||
<%= form_tag votes_path, method: upvote_form_method(votable), class: "vote-form", "data-remote" => true do %> | ||
<input type="hidden" name="votable_id" value="<%= votable.id %>"> | ||
<input type="hidden" name="votable_type" value="<%= votable.class.name %>"> | ||
<button type="submit" class="<%= upvote_button_class(votable) %>"> | ||
<span class="glyphicon glyphicon-thumbs-up"></span> | ||
<% if current_user.voted_up_on?(votable) %> | ||
Recommended | ||
<% else %> | ||
Recommend | ||
<% end %> | ||
</button> | ||
<% end %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
class CreateVotes < ActiveRecord::Migration | ||
def change | ||
create_table :votes do |t| | ||
t.references :votable, polymorphic: true | ||
t.references :voter, polymorphic: true | ||
|
||
t.boolean :vote_flag | ||
t.string :vote_scope | ||
t.integer :vote_weight | ||
|
||
t.timestamps | ||
end | ||
|
||
add_index :votes, [:voter_id, :voter_type, :vote_scope] | ||
add_index :votes, [:votable_id, :votable_type, :vote_scope] | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
require 'spec_helper' | ||
|
||
describe VotesController, type: :controller do | ||
render_views | ||
let(:votable_owner) { FactoryGirl.create(:user) } | ||
let(:votable) { FactoryGirl.create(:event, user: votable_owner) } | ||
|
||
context "authenticated" do | ||
let(:user) { FactoryGirl.create(:user) } | ||
let(:my_votable) { FactoryGirl.create(:event, user: user) } | ||
|
||
before(:each) do | ||
request.env["devise.mapping"] = Devise.mappings[:user] | ||
sign_in user | ||
end | ||
|
||
describe "#create" do | ||
it "upvotes item the current user cannot edit" do | ||
expect(user.voted_up_on?(votable)).to eq(false) | ||
|
||
post :create, votable_type: votable.class.name, votable_id: votable.id | ||
|
||
expect(response).to have_http_status(:ok) | ||
expect(user.voted_up_on?(votable)).to eq(true) | ||
end | ||
|
||
it "does not upvote item the current user can edit" do | ||
expect(user.voted_up_on?(my_votable)).to eq(false) | ||
|
||
post :create, votable_type: my_votable.class.name, votable_id: my_votable.id | ||
|
||
expect(response).to have_http_status(:forbidden) | ||
expect(user.voted_up_on?(my_votable)).to eq(false) | ||
end | ||
|
||
it "404s when invalid votable type is given" do | ||
post :create, votable_type: user.class.name, votable_id: user.id | ||
|
||
expect(response).to have_http_status(:not_found) | ||
end | ||
|
||
it "404s when invalid votable ID is given" do | ||
post :create, votable_type: votable.class.name, votable_id: votable.id + 100 | ||
|
||
expect(response).to have_http_status(:not_found) | ||
end | ||
end | ||
|
||
describe "#destroy" do | ||
it "removes an upvote" do | ||
votable.liked_by user | ||
expect(user.voted_for?(votable)).to eq(true) | ||
|
||
delete :destroy, votable_type: votable.class.name, votable_id: votable.id | ||
|
||
expect(response).to have_http_status(:ok) | ||
expect(user.voted_for?(votable)).to eq(false) | ||
end | ||
end | ||
end | ||
|
||
context "unauthenticated" do | ||
describe "#create" do | ||
it "redirects" do | ||
post :create, votable_type: votable.class.name, votable_id: votable.id | ||
|
||
expect(response).to have_http_status(:redirect) | ||
end | ||
end | ||
|
||
describe "#destroy" do | ||
it "redirects" do | ||
delete :destroy, votable_type: votable.class.name, votable_id: votable.id | ||
|
||
expect(response).to have_http_status(:redirect) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters