This repository has been archived by the owner on May 24, 2021. It is now read-only.
forked from peteonrails/vote_fu
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Changed this file to markdown format for GitHub goodness Added a commented out unique index in the migration generator for "one person, one vote" Removed votes_controller.rb from lib/ and moved to examples
- Loading branch information
1 parent
0bb0846
commit 9ce041c
Showing
13 changed files
with
380 additions
and
104 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
2008-07-15 | ||
========== | ||
* Added examples directory | ||
* Changed this file to markdown format for GitHub goodness | ||
* Added a commented out unique index in the migration generator for "one person, one vote" | ||
* Removed votes\_controller.rb from lib/ and moved to examples | ||
|
||
2008-07-10 | ||
========== | ||
|
||
* Added a generator class for the migration. | ||
* Implemented rails/init.rb | ||
* Implemented capability to use any model as the initiator of votes. | ||
* Implemented acts\_as\_voter methods. |
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,7 @@ | ||
|
||
map.resources :users do |user| | ||
user.resources :votes | ||
user.resources :voteable do |mv| | ||
mv.resources :votes | ||
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,76 @@ | ||
# I usually use the user class from restful_authentication as my principle voter class | ||
# There are generally no changes required to support voting in this controller. | ||
|
||
class UsersController < ApplicationController | ||
# Be sure to include AuthenticationSystem in Application Controller instead | ||
include AuthenticatedSystem | ||
|
||
# Protect these actions behind an admin login | ||
before_filter :admin_required, :only => [:suspend, :unsuspend, :destroy, :purge] | ||
before_filter :find_user, :only => [:suspend, :unsuspend, :destroy, :purge, :show] | ||
|
||
before_filter :login_required, :only => [:index] | ||
|
||
# render new.html.erb | ||
def new | ||
end | ||
|
||
# GET /users/:id | ||
def show | ||
end | ||
|
||
|
||
def create | ||
cookies.delete :auth_token | ||
@user = User.new(params[:user]) | ||
@user.register! if @user.valid? | ||
if @user.errors.empty? | ||
self.current_user.forget_me if logged_in? | ||
cookies.delete :auth_token | ||
reset_session | ||
flash[:notice] = "Thanks for signing up!" | ||
else | ||
render :action => 'new' | ||
end | ||
end | ||
|
||
def activate | ||
unless params[:activation_code].blank? | ||
self.current_user = User.find_by_activation_code(params[:activation_code]) | ||
if logged_in? && !current_user.active? | ||
current_user.activate! | ||
flash[:notice] = "Signup complete!" | ||
redirect_back_or_default('/') | ||
else | ||
flash[:error] = "Sorry, we couldn't find that activation code. Please cut and paste your activation code into the space at left." | ||
end | ||
end | ||
# render activate.html.erb | ||
end | ||
|
||
def suspend | ||
@user.suspend! | ||
redirect_to users_path | ||
end | ||
|
||
def unsuspend | ||
@user.unsuspend! | ||
redirect_to users_path | ||
end | ||
|
||
def destroy | ||
@user.delete! | ||
redirect_to users_path | ||
end | ||
|
||
def purge | ||
@user.destroy | ||
redirect_to users_path | ||
end | ||
|
||
protected | ||
def find_user | ||
@user = User.find(params[:id]) | ||
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,8 @@ | ||
<div id="voteable_<%= @voteable.id %>"> | ||
|
||
..... Show some fields ..... | ||
|
||
<div id="votes_<%= @voteable.id %>"> | ||
<%= render :partial => "votes/voteable_vote", :locals => {:voteable => @voteable} %> | ||
</div> | ||
</div> |
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,10 @@ | ||
class Voteable < ActiveRecord::Base | ||
|
||
belongs_to :user | ||
|
||
acts_as_voteable | ||
|
||
named_scope :descending, :order => "created_at DESC" | ||
|
||
|
||
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,117 @@ | ||
# This example controller assumes you are using the User class from restful_authentication | ||
# and a nested voteable resource. See routes.rb | ||
|
||
|
||
class VoteablesController < ApplicationController | ||
|
||
before_filter :find_user | ||
before_filter :login_required, :only => [:new, :edit, :destroy, :create, :update] | ||
before_filter :must_own_voteable, :only => [:edit, :destroy, :update] | ||
|
||
# GET /users/:id/voteables | ||
# GET /users/:id/voteables.xml | ||
def index | ||
@voteable = Voteable.descending | ||
|
||
respond_to do |format| | ||
format.html # index.html.erb | ||
format.xml { render :xml => @voteables } | ||
end | ||
end | ||
|
||
# GET /users/:id/voteables/1 | ||
# GET /users/:id/voteables/1.xml | ||
def show | ||
@voteable = Voteable.find(params[:id]) | ||
|
||
respond_to do |format| | ||
format.html # show.html.erb | ||
format.xml { render :xml => @voteable } | ||
end | ||
end | ||
|
||
# GET /users/:id/voteables/new | ||
# GET /users/:id/voteables/new.xml | ||
def new | ||
@voteable = Voteable.new | ||
|
||
respond_to do |format| | ||
format.html # new.html.erb | ||
format.xml { render :xml => @voteable } | ||
end | ||
end | ||
|
||
# GET /users/:id/voteables/1/edit | ||
def edit | ||
@voteable ||= Voteable.find(params[:id]) | ||
end | ||
|
||
# POST /users/:id/voteables | ||
# POST /users/:id/voteables.xml | ||
def create | ||
@voteable = Voteable.new(params[:voteable]) | ||
@voteable.user = current_user | ||
|
||
respond_to do |format| | ||
if @voteable.save | ||
flash[:notice] = 'Voteable was successfully saved.' | ||
format.html { redirect_to([@user, @voteable]) } | ||
format.xml { render :xml => @voteable, :status => :created, :location => @voteable } | ||
else | ||
format.html { render :action => "new" } | ||
format.xml { render :xml => @voteable.errors, :status => :unprocessable_entity } | ||
end | ||
end | ||
end | ||
|
||
# PUT /users/:id/voteable/1 | ||
# PUT /users/:id/voteable/1.xml | ||
def update | ||
@voteable = Voteable.find(params[:id]) | ||
|
||
respond_to do |format| | ||
if @quote.update_attributes(params[:voteable]) | ||
flash[:notice] = 'Voteable was successfully updated.' | ||
format.html { redirect_to([@user, @voteable]) } | ||
format.xml { head :ok } | ||
else | ||
format.html { render :action => "edit" } | ||
format.xml { render :xml => @voteable.errors, :status => :unprocessable_entity } | ||
end | ||
end | ||
end | ||
|
||
# DELETE /users/:id/voteable/1 | ||
# DELETE /users/:id/voteable/1.xml | ||
def destroy | ||
@voteable = Voteable.find(params[:id]) | ||
@voteable.destroy | ||
|
||
respond_to do |format| | ||
format.html { redirect_to(user_voteables_url) } | ||
format.xml { head :ok } | ||
end | ||
end | ||
|
||
private | ||
def find_user | ||
@user = User.find(params[:user_id]) | ||
end | ||
|
||
def must_own_voteable | ||
@voteable ||= Voteable.find(params[:id]) | ||
@voteable.user == current_user || ownership_violation | ||
end | ||
|
||
def ownership_violation | ||
respond_to do |format| | ||
flash[:notice] = 'You cannot edit or delete voteable that you do not own!' | ||
format.html do | ||
redirect_to user_path(current_user) | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<% | ||
# You can't vote if it is your quote, | ||
# you are not logged in, | ||
# or you have already voted on this item | ||
|
||
unless quote.user == current_user || | ||
!logged_in? || | ||
current_user.voted_on?(@voteable) | ||
%> | ||
<%= link_to_remote "Up", | ||
:url => user_voteable_votes_path(voteable.user, voteable, :vote => :true, :format => :rjs), | ||
:method => :post | ||
%> | ||
/ | ||
<%= link_to_remote "Down", | ||
:url => user_voteable_votes_path(voteable.user, voteable, :vote => :false, :format => :rjs), | ||
:method => :post | ||
%> | ||
<% end %> | ||
|
||
Votes: <%= voteable.votes_for - voteable.votes_against %> |
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 @@ | ||
page.replace_html "votes_#{@voteable.id}", :partial => "voteable_vote", :locals => {:voteable => @voteable} |
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,110 @@ | ||
# An example controller for "votes" that are nested resources under users. See examples/routes.rb | ||
|
||
class VotesController < ApplicationController | ||
|
||
# First, figure out our nested scope. User or Voteable? | ||
before_filter :find_votes_for_my_scope, :only => [:index] | ||
|
||
before_filter :login_required, :only => [:new, :edit, :destroy, :create, :update] | ||
before_filter :must_own_vote, :only => [:edit, :destroy, :update] | ||
before_filter :not_allowed, :only => [:edit, :update, :new] | ||
|
||
# GET /users/:user_id/votes/ | ||
# GET /users/:user_id/votes.xml | ||
# GET /users/:user_id/voteables/:voteable_id/votes/ | ||
# GET /users/:user_id/voteables/:voteable_id/votes.xml | ||
def index | ||
respond_to do |format| | ||
format.html # index.html.erb | ||
format.xml { render :xml => @votes } | ||
end | ||
end | ||
|
||
# GET /users/:user_id/votes/1 | ||
# GET /users/:user_id/votes/1.xml | ||
# GET /users/:user_id/voteables/:voteable_id/votes/1 | ||
# GET /users/:user_id/voteables/:voteable_id/1.xml | ||
def show | ||
@voteable = Vote.find(params[:id]) | ||
|
||
respond_to do |format| | ||
format.html # show.html.erb | ||
format.xml { render :xml => @vote } | ||
end | ||
end | ||
|
||
# GET /users/:id/votes/new | ||
# GET /users/:id/votes/new.xml | ||
# GET /users/:id/votes/new | ||
# GET /users/:id/votes/new.xml | ||
def new | ||
# Not generally used. Most people want to vote via AJAX calls. | ||
end | ||
|
||
# GET /users/:id/votes/1/edit | ||
def edit | ||
# Not generally used. Most people don't want to allow editing of votes. | ||
end | ||
|
||
# POST /users/:user_id/voteables/:voteable_id/votes | ||
# POST /users/:user_id/voteables/:voteable_id/votes.xml | ||
def create | ||
@voteable = Voteable.find(params[:quote_id]) | ||
|
||
respond_to do |format| | ||
if current_user.vote(@voteable, params[:vote]) | ||
format.rjs { render :action => "create", :vote => @vote } | ||
format.html { redirect_to([@voteable.user, @voteable]) } | ||
format.xml { render :xml => @voteable, :status => :created, :location => @voteable } | ||
else | ||
format.rjs { render :action => "error" } | ||
format.html { render :action => "new" } | ||
format.xml { render :xml => @vote.errors, :status => :unprocessable_entity } | ||
end | ||
end | ||
end | ||
|
||
# PUT /users/:id/votes/1 | ||
# PUT /users/:id/votes/1.xml | ||
def update | ||
# Not generally used | ||
end | ||
|
||
# DELETE /users/:id/votes/1 | ||
# DELETE /users/:id/votes/1.xml | ||
def destroy | ||
@vote = Vote.find(params[:id]) | ||
@vote.destroy | ||
|
||
respond_to do |format| | ||
format.html { redirect_to(user_votes_url) } | ||
format.xml { head :ok } | ||
end | ||
end | ||
|
||
private | ||
def find_votes_for_my_scope | ||
if params[:voteable_id] | ||
@votes = Vote.for_voteable(Voteable.find(params[:voteable_id])).descending | ||
elsif params[:user_id] | ||
@votes = Vote.for_voter(User.find(params[:user_id])).descending | ||
else | ||
@votes = [] | ||
end | ||
end | ||
|
||
def must_own_vote | ||
@vote ||= Vote.find(params[:id]) | ||
@vote.user == current_user || ownership_violation | ||
end | ||
|
||
def ownership_violation | ||
respond_to do |format| | ||
flash[:notice] = 'You cannot edit or delete votes that you do not own!' | ||
format.html do | ||
redirect_to user_path(current_user) | ||
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
Oops, something went wrong.