Created
October 13, 2012 13:39
-
-
Save zkwentz/3884658 to your computer and use it in GitHub Desktop.
Model agnostic likes controller for SoPub
This file contains hidden or 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
class LikesController < ApplicationController | |
before_filter :authenticate_user! | |
def like | |
@model = get_model(params[:model_name]) | |
@item = @model.find(params[:id]) | |
#only post to Facebook if this is their first time | |
unless current_user.voted_on?(@item) | |
if session[:social_mode] | |
@graph = current_user.graph | |
@graph.put_wall_post("", { | |
:name => "#{@item.title}", | |
:link => "#{item_url(@item)}", | |
:caption => "{*actor*} liked #{@item.title} on SoPub" | |
}) | |
end | |
end | |
if current_user.vote_exclusively_for(@item) | |
respond_to do |format| | |
format.html { redirect_to item_url(@item), :success => "You liked this post." } | |
format.js | |
end | |
else | |
respond_to do |format| | |
format.html { redirect_to item_url(@item), :warning => "You have already liked this post." } | |
format.js | |
end | |
end | |
end | |
def dislike | |
@model = get_model(params[:model_name]) | |
@item = @model.find(params[:id]) | |
if current_user.vote_exclusively_against(@item) | |
respond_to do |format| | |
format.html { redirect_to @item } | |
format.js | |
end | |
end | |
end | |
private | |
def get_model(model_name) | |
model_name.classify.constantize | |
end | |
def item_url(item) | |
begin | |
@url = url_for(item) | |
rescue | |
model = get_model(item.commentable_type) | |
@parent = model.find(item.commentable_id) | |
@url = url_for(@parent) | |
end | |
@url | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment