Skip to content
This repository has been archived by the owner on May 12, 2018. It is now read-only.

Commit

Permalink
Added friendship model and confirmed_friends and requested_friends
Browse files Browse the repository at this point in the history
  • Loading branch information
jwang47 committed Mar 8, 2013
1 parent 9ba7bcb commit 2eab346
Show file tree
Hide file tree
Showing 10 changed files with 169 additions and 4 deletions.
18 changes: 18 additions & 0 deletions app/controllers/friendships_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 1,18 @@
class FriendshipsController < ApplicationController

def create
@friendship = current_user.friendships.build(:friend_id => params[:friend_id])
if @friendship.save
flash[:notice] = "Added friend."
redirect_to root_url
else
flash[:notice] = "Unable to add friend."
redirect_to root_url
end
end

def index
@friendships = current_user.friendships
end

end
7 changes: 7 additions & 0 deletions app/controllers/users_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 1,7 @@
class UsersController < ApplicationController

def index
@users = User.all
end

end
8 changes: 8 additions & 0 deletions app/models/friendship.rb
Original file line number Diff line number Diff line change
@@ -0,0 1,8 @@
class Friendship < ActiveRecord::Base
belongs_to :user
belongs_to :friend, :class_name => 'User'
attr_accessible :friend, :user
attr_accessible :friend_id, :user_id

validates_presence_of :user, :friend
end
25 changes: 25 additions & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
@@ -1,4 1,11 @@
class User < ActiveRecord::Base

has_many :friendships
has_many :friends, :through => :friendships

has_many :inverse_friendships, :class_name => "Friendship", :foreign_key => "friend_id"
has_many :inverse_friends, :through => :inverse_friendships, :source => :user

# Include default devise modules. Others available are:
# :confirmable,
# :lockable, :timeoutable and :omniauthable
Expand All @@ -9,4 16,22 @@ class User < ActiveRecord::Base
# Setup accessible (or protected) attributes for your model
attr_accessible :email, :password, :password_confirmation, :remember_me
# attr_accessible :title, :body

# friendships that are not reciprocated
def requested_friends
logger.info "inverse: #{inverse_friends.map{|f| f.id}}"
logger.info "forward #{friends.map{|f| f.id}}"
logger.info "requested #{inverse_friends.map{|f| f.id} - friends.map{|f| f.id}}"
request_ids = inverse_friends.map{|f| f.id} - friends.map{|f| f.id}
User.where(:id => request_ids)
end

# friendships that are reciprocated
def confirmed_friends
logger.info "inverse: #{inverse_friendships.map{|f| f.id}}"
logger.info "forward #{friendships.map{|f| f.id}}"
logger.info "confirmed #{inverse_friendships.map{|f| f.id} & friendships.map{|f| f.id}}"
friend_ids = inverse_friends.map{|f| f.id} & friends.map{|f| f.id}
User.where(:id => friend_ids)
end
end
65 changes: 65 additions & 0 deletions app/views/users/index.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 1,65 @@


<h1>users</h1>

<table class="table table-condensed">
<thead>
<tr>
<td>id</td>
<td>email</td>
</tr>
</thead>

<% @users.each do |user| %>
<tr>
<td><%= user.id %></td>
<td><%= user.email %></td>
<td><%= link_to "Friend", friendships_path(:friend_id => user), :method => :post %></td>
</tr>
<% end %>
</table>

<% if current_user.confirmed_friends.empty? %>
No friends
<% else %>
<% if user_signed_in? %>
<table class="table table-condensed">
<caption>Friends</caption>
<thead>
<tr>
<td>friend id</td>
<td>friend email</td>
</tr>
</thead>

<% current_user.confirmed_friends.each do |friend| %>
<tr>
<td><%= friend.id %></td>
<td><%= friend.email %></td>
</tr>
<% end %>
</table>
<% end %>

<% if current_user.requested_friends.empty? %>
No friend requests
<% else %>
<table class="table table-condensed">
<caption>Friend requests</caption>
<thead>
<tr>
<td>requestor id</td>
<td>requestor email</td>
</tr>
</thead>

<% current_user.requested_friends.each do |requestor| %>
<tr>
<td><%= requestor.id %></td>
<td><%= requestor.email %></td>
</tr>
<% end %>
</table>
<% end %>

<% end %>
10 changes: 7 additions & 3 deletions config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,16 1,20 @@
CoPlatform::Application.routes.draw do

root :to => 'users#index'

post 'friendships' => 'friendships#create'
get 'friendships' => 'friendships#index'

get 'users/:id/friends' => 'users#friends'

devise_for :users

devise_scope :users do
namespace :api do
namespace :v1 do
resources :tokens,:only => [:create, :destroy]
end
end
end

root :to => 'game_server#index'

get 'game_server/:id/heartbeat' => 'game_server#heartbeat'
get 'game_server/check' => 'game_server#check_servers'
Expand Down
8 changes: 8 additions & 0 deletions db/migrate/20130308044548_create_friendships.rb
Original file line number Diff line number Diff line change
@@ -0,0 1,8 @@
class CreateFriendships < ActiveRecord::Migration
def change
create_table :friendships do |t|
t.integer :user_id
t.integer :friend_id
end
end
end
16 changes: 15 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 11,21 @@
#
# It's strongly recommended to check this file into your version control system.

ActiveRecord::Schema.define(:version => 20130308025511) do
ActiveRecord::Schema.define(:version => 20130308044548) do

create_table "friend_requests", :force => true do |t|
t.integer "requestor_id"
t.integer "target_id"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
end

create_table "friendships", :force => true do |t|
t.integer "user_id"
t.integer "friend_id"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
end

create_table "game_servers", :force => true do |t|
t.string "ip_address"
Expand Down
9 changes: 9 additions & 0 deletions test/fixtures/friendships.yml
Original file line number Diff line number Diff line change
@@ -0,0 1,9 @@
# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/Fixtures.html

one:
user_id: 1
friend_id: 1

two:
user_id: 1
friend_id: 1
7 changes: 7 additions & 0 deletions test/unit/friendship_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 1,7 @@
require 'test_helper'

class FriendshipTest < ActiveSupport::TestCase
# test "the truth" do
# assert true
# end
end

0 comments on commit 2eab346

Please sign in to comment.