Permalink
Browse files

User can follow other users

  • Loading branch information...
1 parent b86d04f commit c6de6d067b8b0b4daf5291984d4cf6f4c68623a1 Bartosz Pranczke committed Dec 2, 2011
@@ -0,0 +1,20 @@
+class RelationshipsController < ApplicationController
+
+ def create
+ @user = User.find(params[:relationship][:followed_id])
+ current_user.follow!(@user)
+ respond_to do |format|
+ format.html { redirect_to(:back) }
+ format.js
+ end
+ end
+
+ def destroy
+ @user = Relationship.find(params[:id]).followed
+ current_user.unfollow!(@user)
+ respond_to do |format|
+ format.html { redirect_to(:back) }
+ format.js
+ end
+ end
+end
View
@@ -0,0 +1,10 @@
+class Relationship < ActiveRecord::Base
+ attr_accessible :followed_id
+
+ belongs_to :follower, class_name: "User"
+ belongs_to :followed, class_name: "User"
+
+ validates :follower_id, presence: true
+ validates :followed_id, presence: true
+
+end
View
@@ -1,6 +1,11 @@
class User < ActiveRecord::Base
has_secure_password
- has_many :items
+ has_many :items, dependent: :destroy
+ has_many :relationships, foreign_key: "follower_id", dependent: :destroy
+ has_many :following, through: :relationships, source: :followed
+ has_many :reverse_relationships, foreign_key: "followed_id", class_name: "Relationship",
+ dependent: :destroy
+ has_many :followers, through: :reverse_relationships, source: :follower
attr_accessible :username, :fullname, :email, :password
before_create { generate_token(:auth_token) }
@@ -25,4 +30,16 @@ def self.search(search)
find(:all)
end
end
+
+ 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
end
@@ -0,0 +1 @@
+
@@ -0,0 +1,8 @@
+<%= form_for current_user.relationships.build(followed_id: @user.id) do |f| %>
+<div>
+ <%= f.hidden_field :followed_id %>
+</div>
+<div class="actions">
+ <%= f.submit "Follow" %>
+</div>
+<% end %>
@@ -0,0 +1,9 @@
+<% unless current_user?(@user) %>
+ <div id="follow_form">
+ <% if current_user.following?(@user) %>
+ <%= render 'unfollow' %>
+ <% else %>
+ <%= render 'follow' %>
+ <% end %>
+ </div>
+<% end %>
@@ -0,0 +1,6 @@
+<%= form_for current_user.relationships.find_by_followed_id(@user),
+ html: {method: :delete } do |f| %>
+<div class="actions">
+ <%= f.submit "Unfollow" %>
+</div>
+<% end %>
@@ -11,7 +11,6 @@ Szukaj
<tr>
<td class="user_row" id="<%= u.id %>a" > <%= avatar_for u %> </td>
<td class="user_row" id="<%= u.id %>b" > <%= u.username %> </td>
- <td> status </td>
</tr>
<% end %>
</tbody>
@@ -1 +1,2 @@
<%= avatar_for @user %><%= "#{@user.username} - username" %>
+<%= render 'follow_form' if signed_in? %>
View
@@ -6,7 +6,7 @@
end
end
-
+ resources :relationships, :only => [:create, :destroy]
resources :sessions, only: [:new, :create, :destroy]
match '/signup', to: 'users#new'
match '/signin', to: 'sessions#new'
@@ -0,0 +1,13 @@
+class CreateRelationships < ActiveRecord::Migration
+ def change
+ create_table :relationships do |t|
+ t.integer :follower_id
+ t.integer :followed_id
+
+ t.timestamps
+ end
+ add_index :relationships, :follower_id
+ add_index :relationships, :followed_id
+ add_index :relationships, [:follower_id, :followed_id], :unique => true
+ end
+end
View
@@ -11,7 +11,7 @@
#
# It's strongly recommended to check this file into your version control system.
-ActiveRecord::Schema.define(:version => 20111130134308) do
+ActiveRecord::Schema.define(:version => 20111202145421) do
create_table "item_relationships", :force => true do |t|
t.integer "parent_id"
@@ -36,6 +36,17 @@
t.boolean "public"
end
+ create_table "relationships", :force => true do |t|
+ t.integer "follower_id"
+ t.integer "followed_id"
+ t.datetime "created_at"
+ t.datetime "updated_at"
+ end
+
+ add_index "relationships", ["followed_id"], :name => "index_relationships_on_followed_id"
+ add_index "relationships", ["follower_id", "followed_id"], :name => "index_relationships_on_follower_id_and_followed_id", :unique => true
+ add_index "relationships", ["follower_id"], :name => "index_relationships_on_follower_id"
+
create_table "users", :force => true do |t|
t.string "username"
t.string "fullname"
@@ -0,0 +1,5 @@
+require 'spec_helper'
+
+describe Relationship do
+ pending "add some examples to (or delete) #{__FILE__}"
+end

0 comments on commit c6de6d0

Please sign in to comment.