users/show.html.erb

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<table class="profile" summary="Profile information">
  <tr>
  <td class="main">
    <h1><%= @user.name %></h1>
    <%= render 'follow_form' if user_signed_in? %>
      <% unless @user.posts.empty? %>
        <table class="posts" summary="User posts">
       <%= render @posts %>     # I heard that this passes the object to /posts/_post which 
            </table>         # allows the local var in post.content (its no defined anywhere else)
      <%= will_paginate @posts %>
    <% end %>
  </td>
  <td class="sidebar round">
    <%= link_to avatar_for(@user), @user.avatar.url %><br />
    <strong>Name</strong> <%= @user.name %><br />
    <strong>URL</strong>  <%= link_to user_path(@user), user_path(@user) %>
    <strong>Posts</strong> <%= @user.posts.count %>
    <%= render 'shared/stats' %>
  </td>  
  </tr>  
</table>

posts/_post.html.erb

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
<tr>
  <td class="post">
     <span class="title"><strong><%= link_to post.title, post %></strong></span><br />
   <span class="image"><strong>Image: </strong><%= image_tag post.image_url.to_s %></span><br />
   <span class="a_name"><strong>Artist: </strong><%= post.a_name %></span><br /> 
   <span class="item_name"><strong>Title: </strong><%= post.item_name %></span><br />
   <span class="a_twitter"><strong>Twitter: </strong><%= post.a_twitter %></span><br />
   <span class="g_from"><strong>Gotten From: </strong><%= post.g_from %></span><br />
   <span class="content"><strong>Plot: </strong><%= post.content %></span><br />

<%= render(:partial => 'users/like_form') if user_signed_in? %>

     <span class="timestamp">
      Posted <%= time_ago_in_words(post.created_at) %> ago. </span>
        <a href="<%= likers_post_path(@post) %>">Likers</a><span id="likers"><br />
    </span>

  </td>

  <% if current_user?(post.user)%>
    <td>
      <%= link_to "delete", post, :method => :delete,
                                         :confirm => "You sure?",
                                         :title => post.content %>
    </td>
  <%end%>
</tr>

users/_like_form

1
2
3
4
5
6
7
8
9
10
<% unless current_user?(@user) %>
  <div id="like_form">
    <% if current_user.likes?(@post) %>
    <%=render(:partial => 'users/unlike')  %>
  <% else %>
    <%=render(:partial => 'users/unlike')  %>
  <% end %>
  </div>
<% end %>

users/_unlike

1
2
3
4
5
6
<%= form_for(current_user.appreciations.find_by_liked_id(@user),
                                   :html => { :method => :delete },
                             :remote => true) do |f| %>
  <div class="actions"><%= f.submit "Unlike" %></div>
<% end %>

users/_like

1
2
3
4
5
6
7
<%= form_for(current_user.appreciations.
                          build(:liked_id => @post.id),
              :remote => true) do |f| %>
 <div><%= f.hidden_field :liked_id %></div>
 <div class="actions"><%= f.submit "Like" %></div>
<% end %>

users controller

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
class UsersController < ApplicationController
  before_filter :authenticate_user!, :except => [:create, :show, :new]

  def show
    @user = User.find(params[:id])
    @posts = @user.posts.paginate(:per_page => "10",:page => params[:page])
    @title = @user.name
  end

  def following
    @title = "Following"
    @user = User.find(params[:id])
    @users = @user.following.paginate(:page => params[:page])
    render 'show_follow' 
  end

  def followers
    @title = "Followers"
    @user = User.find(params[:id])
    @users = @user.followers.paginate(:page => params[:page])
    render 'show_follow'
  end

  def likes
    @title = "Likes"
    @user = User.find(params[:id])
    @liked = @user.likes.paginate(:page => params[:page])
    render 'show_likes' 
  end

  def likers
    @title = "Likers"
    @user = User.find(params[:id])
    @likers = @user.likers.paginate(:page => params[:page])
    render 'show_likers'
  end


end

posts controller

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
class PostsController < ApplicationController

  before_filter :authenticate_user!, :only => [:create, :edit, :update, :destroy]
  before_filter :authorized_user, :only => [:destroy, :edit, :update]

  def create
    @user = current_user
    @post  = current_user.posts.build(params[:post])
    if @post.save
      flash[:success] = "Post created!"
      redirect_to root_path
    else
       @feed_items = current_user.feed.paginate(:per_page => "10", :page => params[:page])
      render 'pages/home'
    end
  end

  def index
    @posts = Post.paginate(:page => params[:page])
  end

  def show
    @post = Post.find(params[:id])
  end

  def destroy
    @post.destroy
    redirect_to root_path
  end

private
  def authorized_user
    @post = Post.find(params[:id])
    redirect_to root_path unless current_user?(@post.user)
  end
end

appreciations controller

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class AppreciationsController < ApplicationController
  before_filter :authenticate_user!

  def create
    @post = Post.find(params[:appreciation][:liked_id])
    current_user.like!(@post)
    redirect_to @post
  end

  def destroy
    @post = Appreciation.find(params[:id]).liked
    current_user.unlike!(@post)
    redirect_to @post
  end

end

user model

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
class User < ActiveRecord::Base

  has_many :appreciations, :dependent => :destroy,
                           :foreign_key => "liker_id"

  has_many :likes, :through => :appreciations, :source => :liked


  def likes?(liked)
    appreciations.find_by_liked_id(liked)
  end

  def like!(liked)
    appreciations.create!(:liked_id => liked.id)
  end

  def unlike!(liked)
    appreciations.find_by_liked_id(liked).destroy


  #following


  has_many :relationships, :dependent => :destroy,
                           :foreign_key => "follower_id"

  has_many :reverse_relationships, :dependent => :destroy,
                                   :foreign_key => "followed_id",
                                   :class_name => "Relationship"

  has_many :following, :through => :relationships, :source => :followed

  has_many :followers, :through => :reverse_relationships, :source => :follower

 # following and followers
  def following?(followed)
    relationships.find_by_followed_id(followed)
  end

  def follow!(followed)
    relationships.create!(:followed_id => followed.id)
  end

  def unfollow!(followed)
    relationships.find_by_followed_id(followed).destroy
  end

post model

1
2
3
4
5
6
7
8
9
  belongs_to :user


  has_many :reverse_relationships, :dependent => :destroy,
                                   :foreign_key => "liked_id",
                                   :class_name => "Appreciations"

  has_many :likers, :through => :reverse_relationships, :source => :liker

relationships model

1
2
3
4
5
6
7
8
9
10
11

class Relationship < ActiveRecord::Base
  attr_accessible :followed_id

  belongs_to :follower, :foreign_key => "follower_id", :class_name => "User"
  belongs_to :followed, :foreign_key => "followed_id", :class_name => "User"

  validates :follower_id, :presence => true
  validates :followed_id, :presence => true
end

routes.rb

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
Gp2011::Application.routes.draw do

  match 'auth/:provider/callback' => 'authentications#create'
  resources :authentications

  devise_for :users, :controllers => {:registrations => 'registrations'}

  resources :posts do
    member do
      get :likers
    end
  end  

  resources :relationships, :only => [:create, :destroy]
  resources :appreciations, :only => [:create, :destroy]

  root :to => "pages#home"

  match '/contact', :to => 'pages#contact'
  match '/about',   :to => 'pages#about'
  match '/help',    :to => 'pages#help'
  match '/blog',    :to => 'pages#blog'

  resources :users do
     member do
     get :following, :followers, :likes
     end
 end

appreciations migration

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

class CreateAppreciations < ActiveRecord::Migration
  def self.up
    create_table :appreciations do |t|
      t.integer :liker_id
      t.integer :liked_id

      t.timestamps
    end
    add_index :appreciations, :liker_id
    add_index :appreciations, :liked_id
  end

  def self.down
    drop_table :appreciations
  end
end