Class: FriendshipsController

Inherits:
BaseController show all
Defined in:
app/controllers/friendships_controller.rb

Instance Method Summary collapse

Methods inherited from BaseController

#advertise, #cache_action?, #css_help, #footer_content, #homepage_features, #plaxo, #rss_site_index, #site_index

Methods included from LocalizedApplication

#get_matching_ui_locale, #get_sorted_langs_from_accept_header, #get_valid_lang_from_accept_header, #set_locale

Methods included from AuthenticatedSystem

#login_by_token, #update_last_seen_at

Instance Method Details

#acceptObject



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'app/controllers/friendships_controller.rb', line 33

def accept
  @user = User.find(params[:user_id])    
  @friendship = @user.friendships_not_initiated_by_me.find(params[:id])
 
  respond_to do |format|
    if @friendship.update_attributes(:friendship_status => FriendshipStatus[:accepted]) && @friendship.reverse.update_attributes(:friendship_status => FriendshipStatus[:accepted])
      flash[:notice] = :the_friendship_was_accepted.l
      format.html { 
        redirect_to accepted_user_friendships_path(@user) 
      }
    else
      format.html { render :action => "edit" }
    end
  end
end

#acceptedObject



59
60
61
62
63
64
65
66
67
68
69
# File 'app/controllers/friendships_controller.rb', line 59

def accepted
  @user = User.find(params[:user_id])    
  @friend_count = @user.accepted_friendships.count
  @pending_friendships_count = @user.pending_friendships.count
        
  @friendships = @user.friendships.accepted.page(params[:page]).per(12)
  
  respond_to do |format|
    format.html
  end
end

#createObject



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'app/controllers/friendships_controller.rb', line 90

def create
  @user = User.find(params[:user_id])
  @friendship = Friendship.new(:user_id => params[:user_id], :friend_id => params[:friend_id], :initiator => true )
  @friendship.friendship_status_id = FriendshipStatus[:pending].id    
  reverse_friendship = Friendship.new(params[:friendship])
  reverse_friendship.friendship_status_id = FriendshipStatus[:pending].id 
  reverse_friendship.user_id, reverse_friendship.friend_id = @friendship.friend_id, @friendship.user_id
  
  respond_to do |format|
    if @friendship.save && reverse_friendship.save
      UserNotifier.friendship_request(@friendship).deliver if @friendship.friend.notify_friend_requests?
      format.html {
        flash[:notice] = :friendship_requested.l_with_args(:friend => @friendship.friend.) 
        redirect_to accepted_user_friendships_path(@user)
      }
      format.js { render( :inline => :requested_friendship_with.l+" #{@friendship.friend.}." ) }        
    else
      flash.now[:error] = :friendship_could_not_be_created.l
      format.html { redirect_to user_friendships_path(@user) }
      format.js { render( :inline => "Friendship request failed." ) }                
    end
  end
end

#deniedObject



49
50
51
52
53
54
55
56
# File 'app/controllers/friendships_controller.rb', line 49

def denied
  @user = User.find(params[:user_id])    
  @friendships = @user.friendships.where("friendship_status_id = ?", FriendshipStatus[:denied].id).page(params[:page])
  
  respond_to do |format|
    format.html
  end
end

#denyObject



19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'app/controllers/friendships_controller.rb', line 19

def deny
  @user = User.find(params[:user_id])    
  @friendship = @user.friendships.find(params[:id])
 
  respond_to do |format|
    if @friendship.update_attributes(:friendship_status => FriendshipStatus[:denied]) && @friendship.reverse.update_attributes(:friendship_status => FriendshipStatus[:denied])
      flash[:notice] = :the_friendship_was_denied.l
      format.html { redirect_to denied_user_friendships_path(@user) }
    else
      format.html { render :action => "edit" }
    end
  end    
end

#destroyObject



114
115
116
117
118
119
120
121
122
123
124
# File 'app/controllers/friendships_controller.rb', line 114

def destroy
  @user = User.find(params[:user_id])    
  @friendship = Friendship.find(params[:id])
  Friendship.transaction do 
    @friendship.destroy
    @friendship.reverse.destroy
  end
  respond_to do |format|
    format.html { redirect_to accepted_user_friendships_path(@user) }
  end
end

#indexObject



6
7
8
9
10
11
12
13
14
15
16
17
# File 'app/controllers/friendships_controller.rb', line 6

def index
  @body_class = 'friendships-browser'
  
  @user = (params[:id] ||params[:user_id]) ? User.find((params[:id] || params[:user_id] )): Friendship.find(:first).user
  @friendships = Friendship.find(:all, :conditions => ['user_id = ? OR friend_id = ?', @user.id, @user.id], :limit => 40)
  @users = User.find(:all, :conditions => ['users.id in (?)', @friendships.collect{|f| f.friend_id }])    
  
  respond_to do |format|
    format.html 
    format.xml { render :action => 'index.rxml', :layout => false}    
  end
end

#pendingObject



71
72
73
74
75
76
77
78
# File 'app/controllers/friendships_controller.rb', line 71

def pending
  @user = User.find(params[:user_id])    
  @friendships = @user.friendships.find(:all, :conditions => ["initiator = ? AND friendship_status_id = ?", false, FriendshipStatus[:pending].id])
  
  respond_to do |format|
    format.html
  end
end

#showObject



80
81
82
83
84
85
86
87
# File 'app/controllers/friendships_controller.rb', line 80

def show
  @friendship = Friendship.find(params[:id])
  @user = @friendship.user
  
  respond_to do |format|
    format.html
  end
end