adding friends

git-svn-id: http://svn.barleysodas.com/barleysodas/trunk@116 0f7b21a7-9e3a-4941-bbeb-ce5c7c368fa7
master
andrew 2008-02-01 07:43:40 +00:00
parent 0055fd85d2
commit ee38d3393a
16 changed files with 201 additions and 5 deletions

View File

@ -0,0 +1,49 @@
class FriendsController < ApplicationController
append_before_filter :fetch_people, :only => [ :show, :destroy ]
# GET /friends/1
# GET /friends/1.xml
def show
@content_title = "Friends of #{@people.title}"
@friends = @people.actual_friends
respond_to do |format|
format.html # show.rhtml
format.xml { render :xml => @friends.to_xml }
end
end
# POST /friends
# POST /friends.xml
def create
@friend = Friend.new(params[:friend])
respond_to do |format|
if @friend.save
flash[:notice] = 'Successfully added the new friend'
format.html { redirect_to people_path(@people.page.title_for_url) }
else
format.html { render :action => "new" }
end
end
end
# DELETE /friends/1
# DELETE /friends/1.xml
def destroy
@friend = Friend.find :first,
:conditions => [ 'source_id = ? AND destination_id = ?',
params[:d].to_i, @people.id ]
@friend.destroy if @friend
respond_to do |format|
flash[:notice] = 'Removed the friend'
format.html { redirect_to people_path(@people.page.title_for_url) }
end
end
protected
def fetch_people
@people = People.find_by_title(Page.title_from_url(params[:id]))
raise ActiveRecord::RecordNotFound.new if @people.nil?
@page = @people.page
end
end

View File

@ -0,0 +1,3 @@
module FriendsHelper
include PeoplesHelper
end

View File

@ -12,4 +12,21 @@ module PeoplesHelper
link_to 'Edit People', edit_people_path(people.page.title_for_url), link_to 'Edit People', edit_people_path(people.page.title_for_url),
{ :title => "Edit #{people.title}" } { :title => "Edit #{people.title}" }
end end
def show_friends_link(people)
link_to "#{people.title}'s Friends (#{people.actual_friends.size})",
friend_path(people.page.title_for_url)
end
def add_friend_link(people)
link_to "#{image_tag('list-add.png')} Friend",
friends_path('friend[source_id]' => session[:people_id],
'friend[destination_id]' => people.id), :method => :post
end
def remove_friend_link(people)
link_to "#{image_tag('list-remove.png')} Friend",
friend_url(:id => people.page.title_for_url, :d => session[:people_id]),
:method => :delete
end
end end

16
app/models/friend.rb Normal file
View File

@ -0,0 +1,16 @@
##
# Represents a relationship of association between two People models.
#
class Friend < ActiveRecord::Base
belongs_to :source, :class_name => 'People', :foreign_key => :source_id
belongs_to :destination, :class_name => 'People',
:foreign_key => :destination_id
validates_presence_of :source_id, :destination_id
validates_uniqueness_of :destination_id, :scope => :source_id
validates_each :destination_id do |record, attr, value|
if value == record.source_id
record.errors.add(attr, 'cannot be the same as the source')
end
end
end

View File

@ -10,11 +10,21 @@ class People < ActiveRecord::Base
has_many :images, :dependent => :destroy has_many :images, :dependent => :destroy
has_many_tagged_images has_many_tagged_images
validates_uniqueness_of :title validates_uniqueness_of :title
has_many :friends, :foreign_key => :source_id, :dependent => :destroy
has_many :actual_friends, :through => :friends, :source => :destination
make_authenticatable make_authenticatable
validates_length_of :password, :minimum => 8, :if => :password_required?, validates_length_of :password, :minimum => 8, :if => :password_required?,
:message => 'must be at least 8 characters in length' :message => 'must be at least 8 characters in length'
##
# Used to quickly determine if the particular id of another Person is a
# Friend of this instance.
#
def friend_of?(people_id)
Friend.count([ 'source_id = ? AND destination_id = ?', people_id, id ]) > 0
end
## ##
# Finds me. # Finds me.
# #

View File

@ -0,0 +1,7 @@
<% for friend in @friends do -%>
<div class="friend_box"><%= link_to friend.title, people_path(friend.page.title_for_url) -%></div>
<% end -%>
<% content_for :sidebar do -%>
<%= show_people_link(@people) -%><br />
<% end -%>

View File

@ -7,4 +7,7 @@
<%= edit_people_link(@people) %><br /> <%= edit_people_link(@people) %><br />
<%= link_to 'Destroy', people_path(@people.page.title_for_url), :confirm => 'Are you sure?', :method => :delete %><br /> <%= link_to 'Destroy', people_path(@people.page.title_for_url), :confirm => 'Are you sure?', :method => :delete %><br />
<% unless @people.tagged_images.empty? -%><%= tagged_image_browser_link -%><br /><% end -%> <% unless @people.tagged_images.empty? -%><%= tagged_image_browser_link -%><br /><% end -%>
<%= show_friends_link(@people) -%><br />
<% unless @people.id == session[:people_id] or @people.friend_of?(session[:people_id]) -%><%= add_friend_link(@people) -%><% end -%>
<% if @people.friend_of?(session[:people_id]) -%><%= remove_friend_link(@people) -%><% end -%>
<% end -%> <% end -%>

View File

@ -1,8 +1,6 @@
ActionController::Routing::Routes.draw do |map| ActionController::Routing::Routes.draw do |map|
map.resources :tag_images
map.resources :beers, :breweries, :pages, :discussions, :peoples, :roles, map.resources :beers, :breweries, :pages, :discussions, :peoples, :roles,
:sessions, :styles, :galleries, :tag_images :sessions, :styles, :galleries, :tag_images, :friends
map.connect ':controller/:action/:id.:format' map.connect ':controller/:action/:id.:format'
map.connect ':controller/:action/:id' map.connect ':controller/:action/:id'

View File

@ -0,0 +1,16 @@
class CreateFriends < ActiveRecord::Migration
def self.up
create_table :friends do |t|
t.column :source_id, :integer
t.column :destination_id, :integer
t.column :rank, :integer
t.column :created_at, :timestamp
end
add_index :friends, :source_id
add_index :friends, :destination_id
end
def self.down
drop_table :friends
end
end

View File

@ -5,7 +5,7 @@ base_actions = ApplicationController.action_methods
# rather than defining them here. # rather than defining them here.
controllers = [ PagesController, DiscussionsController, StylesController, controllers = [ PagesController, DiscussionsController, StylesController,
PeoplesController, BeersController, BreweriesController, RolesController, PeoplesController, BeersController, BreweriesController, RolesController,
GalleriesController, TagImagesController ] GalleriesController, TagImagesController, FriendsController ]
controllers.each do |c| controllers.each do |c|
actions = c.action_methods - base_actions actions = c.action_methods - base_actions
cname = c.controller_name cname = c.controller_name
@ -33,6 +33,11 @@ Permission.find(:all,
next if [ 'show', 'create', 'destroy', 'taggable_search' ].include?(p.action) next if [ 'show', 'create', 'destroy', 'taggable_search' ].include?(p.action)
r.permissions << p r.permissions << p
end end
Permission.find(:all,
:conditions => [ 'controller = ?', 'friends' ]).each do |p|
next if [ 'edit', 'create', 'destroy' ].include?(p.action)
r.permissions << p
end
r2 = Role.admin_role r2 = Role.admin_role
Permission.find(:all).each do |p| Permission.find(:all).each do |p|

BIN
public/images/list-add.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 323 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 247 B

View File

@ -256,7 +256,7 @@
} }
/* People uploaded Image styling */ /* People uploaded Image styling */
#content .people_image { #content .people_image, #content .friend_box {
border: 1px solid #DDD; border: 1px solid #DDD;
background-color: #F2F2F2; background-color: #F2F2F2;
float: left; float: left;

5
test/fixtures/friends.yml vendored Normal file
View File

@ -0,0 +1,5 @@
# Read about fixtures at http://ar.rubyonrails.org/classes/Fixtures.html
one:
id: 1
two:
id: 2

View File

@ -0,0 +1,57 @@
require File.dirname(__FILE__) + '/../test_helper'
require 'friends_controller'
# Re-raise errors caught by the controller.
class FriendsController; def rescue_action(e) raise e end; end
class FriendsControllerTest < Test::Unit::TestCase
fixtures :friends
def setup
@controller = FriendsController.new
@request = ActionController::TestRequest.new
@response = ActionController::TestResponse.new
end
def test_should_get_index
get :index
assert_response :success
assert assigns(:friends)
end
def test_should_get_new
get :new
assert_response :success
end
def test_should_create_friend
old_count = Friend.count
post :create, :friend => { }
assert_equal old_count+1, Friend.count
assert_redirected_to friend_path(assigns(:friend))
end
def test_should_show_friend
get :show, :id => 1
assert_response :success
end
def test_should_get_edit
get :edit, :id => 1
assert_response :success
end
def test_should_update_friend
put :update, :id => 1, :friend => { }
assert_redirected_to friend_path(assigns(:friend))
end
def test_should_destroy_friend
old_count = Friend.count
delete :destroy, :id => 1
assert_equal old_count-1, Friend.count
assert_redirected_to friends_path
end
end

10
test/unit/friend_test.rb Normal file
View File

@ -0,0 +1,10 @@
require File.dirname(__FILE__) + '/../test_helper'
class FriendTest < Test::Unit::TestCase
fixtures :friends
# Replace this with your real tests.
def test_truth
assert true
end
end