Permalink
Browse files

facebook theme and likable implementation

  • Loading branch information...
timothyf committed Jul 15, 2010
1 parent 2144f4c commit ce05b8ce466d2a61476d30b6b9743fa12228499d
@@ -80,6 +80,7 @@ def index
else
@section = 'MAIN'
@page = Page.find_by_name('home')
+ @user = current_user
@photos = Photo.find(:all, :limit=>6,
:select=>'id, parent_id, filename',
:order => Photo.connection.adapter_name == 'PostgreSQL' ? 'RANDOM()' : 'RAND()',
@@ -0,0 +1,45 @@
+class LikesController < ApplicationController
+
+ include ActivityFeedHelper
+
+ def create
+ user_id = params[:user_id]
+ likable_id = params[:likable_id]
+ likable_type = params[:likable_type]
+
+ # prevent a user from liking something twice
+ like = Like.find_by_user_id_and_likable_id_and_likable_type(user_id, likable_id, likable_type)
+ if like == nil || like.size == 0
+ @like = Like.create(:user_id => user_id, :likable_id => likable_id, :likable_type => likable_type)
+ end
+
+ respond_to do |format|
+ #if @like.save
+ format.xml { render :xml => @like, :status => :created, :location => @like }
+ format.json {render :json => @like, :status => :created, :location => @like }
+ #else
+ # format.xml { render :xml => @link.errors, :status => :unprocessable_entity }
+ # format.json { render :json => @link.errors.to_json, :status => :unprocessable_entity }
+ #end
+ end
+ end
+
+
+ def like_text
+ activity = Activity.find(params[:activity_id])
+ render :text=>display_like_text(activity), :layout => false
+ end
+
+
+ def destroy
+ @like = Like.find(params[:id])
+ @like.destroy
+ respond_to do |format|
+ format.html { redirect_to(likes_url) }
+ format.xml { head :ok }
+ format.json { head :ok }
+ end
+ end
+
+
+end
@@ -13,6 +13,26 @@
# limitations under the License.
module ActivityFeedHelper
+ def display_like_text(activity)
+ if current_user.like_this?(activity) == true
+ if activity.likes.size > 1
+ text = "You and #{(activity.likes.size - 1).to_s} other #{activity.likes.size > 2 ? 'people':'person'} like this"
+ else
+ text = "You like this"
+ end
+ elsif activity.likes && activity.likes.size > 0
+ if activity.likes.size > 1
+ text = "#{(activity.likes.size).to_s} people like this"
+ else
+ text = "1 person likes this"
+ end
+ else
+ text = "Be the first to like this"
+ end
+ return text
+ end
+
+
def user_link(user)
"#{link_to(user.name, user_url(user))}"
end
@@ -41,14 +61,14 @@ def activity_feed_message(activity)
end
when "Photo"
if activity.action == 'destroy'
- %(#{user_link(user)} deleted a photo. #{activity_date(activity)})
+ %(<span class="activity_text"><span class="activity_user_link">#{user_link(user)}</span> deleted a photo.</span>#{activity_date(activity)})
else
if activity.item
photo = Photo.find(activity.item.id, :select=>"id, user_id, filename, parent_id, created_at")
- %(#{user_link(user)} uploaded a photo - <a href="/photos/#{photo.id}">#{image_tag(photo.public_filename(:small))}</a>. #{activity_date(photo)})
+ %(<span class="activity_text"><span class="activity_user_link">#{user_link(user)}</span> uploaded a photo - <a href="/photos/#{photo.id}">#{image_tag(photo.public_filename(:small))}</a>.</span>#{activity_date(photo)})
else
# photo no longer exists, but still need to display upload event for history
- %(#{user_link(user)} uploaded a photo. #{activity_date(activity)})
+ %(<span class="activity_text"><span class="activity_user_link">#{user_link(user)}</span> uploaded a photo.</span>#{activity_date(activity)})
end
end
when "Group"
@@ -71,7 +91,7 @@ def activity_feed_message(activity)
end
when "Membership"
membership = activity.item
- %(#{user_link(user)} joined the group, <a href="/groups/#{membership.group.id}">#{membership.group.name}</a>. #{activity_date(membership)})
+ %(<span class="activity_text"><span class="activity_user_link">#{user_link(user)}</span> joined the group, <a href="/groups/#{membership.group.id}">#{membership.group.name}</a>. </span>#{activity_date(membership)})
when "ForumPost"
forum_post = activity.item
%(#{user_link(user)} posted a new message to the forum, <a href="/forum_posts/#{forum_post.id}">#{forum_post.title}</a>. #{activity_date(forum_post)})
@@ -88,7 +108,7 @@ def activity_feed_message(activity)
%(#{user.name} posted a new review for #{book_review.name}. #{activity_date(book_review)})
when "StatusPost"
status_post = activity.item
- %(#{user_link(user)} #{EngineyUtil.linkify(status_post.body)} #{activity_date(status_post)})
+ %(<span class="activity_text"><span class="activity_user_link">#{user_link(user)}</span> #{EngineyUtil.linkify(status_post.body)}</span>#{activity_date(status_post)})
when "Link"
link = activity.item
if link
@@ -0,0 +1,2 @@
+module LikesHelper
+end
View
@@ -31,6 +31,7 @@ class Activity < ActiveRecord::Base
belongs_to :user
belongs_to :item, :polymorphic => true
has_many :feeds
+ acts_as_likable
GLOBAL_FEED_SIZE = 10
View
@@ -26,6 +26,8 @@ class User < ActiveRecord::Base
has_many :memberships, :dependent => :destroy
has_many :groups, :through => :memberships
+ has_many :likes
+
# TODO:
# Add a condition to only get the attendances set in the future.
# Do not get attendances for past events
@@ -111,6 +113,25 @@ def current_status
end
+ # Return true if the user likes the activity passed in, otherwise false
+ def like_this?(activity)
+ @likes = Like.find_likes_cast_by_user(self)
+ if @likes
+ results = @likes.select{|like| (like.likable_id == activity.id && like.likable_type == 'Activity') }
+ if results && results.size > 0
+ return true
+ end
+ end
+ return false
+ end
+
+
+ # Returns the Like object associated with the Activity passed in for this user, if this user 'likes' the activity
+ def get_like(activity)
+ Like.find_by_user_id_and_likable_id_and_likable_type(self.id, activity.id, 'Activity')
+ end
+
+
# Returns an array of users who have an API key
def self.with_api_key
User.find(:all, :conditions => "api_key != ''")
@@ -18,7 +18,7 @@ limitations under the License.
<div class="notice"><%= flash[:notice] %></div>
<% end %>
<div id="login_form">
- <h5>Sign in with your RubyMI login and password</h5>
+ <h5>Sign in with your login and password</h5>
<% form_tag session_path do -%>
<div class="form_field">
<label for="login">Login:</label>
View
@@ -1,9 +1,11 @@
embed_assets: on
javascripts:
-
+ workspace:
+ - public/javascripts/*.js
stylesheets:
+ common:
+ - public/stylesheets/*.css
+ - public/stylesheets/themes/**/*.css
-
-templates:
View
@@ -1,13 +1,10 @@
ActionController::Routing::Routes.draw do |map|
map.resources :nav_items
-
map.resources :ey_modules
-
map.resources :modules
Jammit::Routes.draw(map)
-
map.resources :follows
map.resources :contents
map.resources :facebook_posts
@@ -54,6 +51,7 @@
map.connect 'users/authenticate.:format', :controller=>'users', :action=>'authenticate'
map.connect 'blog_post_topics/ajax_save', :controller=>'blog_post_topics', :action=>'ajax_save'
map.connect 'blog_post_topics/fetch_topic_list', :controller=>'blog_post_topics', :action=>'fetch_topic_list'
+ map.connect 'likes/like_text', :controller=>'likes', :action=>'like_text'
map.resources :blog_post_topics
map.resources :videos
@@ -66,6 +64,7 @@
map.resources :users
map.resources :memberships
map.resource :session
+ map.resources :likes
map.activate '/activate/:activation_code', :controller => 'users', :action => 'activate', :activation_code => nil
View
@@ -9,7 +9,7 @@
#
# It's strongly recommended to check this file into your version control system.
-ActiveRecord::Schema.define(:version => 20100628192855) do
+ActiveRecord::Schema.define(:version => 20100707160333) do
create_table "activities", :force => true do |t|
t.integer "user_id"
@@ -274,6 +274,17 @@
t.datetime "updated_at"
end
+ create_table "likes", :force => true do |t|
+ t.integer "user_id", :null => false
+ t.integer "likable_id", :null => false
+ t.string "likable_type", :null => false
+ t.datetime "created_at"
+ t.datetime "updated_at"
+ end
+
+ add_index "likes", ["likable_type", "likable_id"], :name => "index_likable_type"
+ add_index "likes", ["user_id"], :name => "fk_likes_user"
+
create_table "links", :force => true do |t|
t.integer "user_id"
t.string "title"
Oops, something went wrong.

0 comments on commit ce05b8c

Please sign in to comment.