if Settings.status != "database_offline"
can [:index, :new, :create, :show, :edit, :update, :destroy], ClientApplication
can [:new, :create, :edit, :update, :comment, :subscribe, :unsubscribe], DiaryEntry
+ can [:make_friend, :remove_friend], Friendship
can [:new, :create, :reply, :show, :inbox, :outbox, :mark, :destroy], Message
can [:close, :reopen], Note
can [:new, :create], Report
can [:mine, :new, :create, :edit, :update, :destroy], Trace
- can [:account, :go_public, :make_friend, :remove_friend], User
+ can [:account, :go_public], User
if user.moderator?
can [:hide, :hidecomment], DiaryEntry
--- /dev/null
+class FriendshipsController < ApplicationController
+ layout "site"
+
+ before_action :authorize_web
+ before_action :set_locale
+ before_action :check_database_readable
+
+ authorize_resource
+
+ before_action :check_database_writable, :only => [:make_friend, :remove_friend]
+
+ def make_friend
+ @new_friend = User.find_by(:display_name => params[:display_name])
+
+ if @new_friend
+ if request.post?
+ friendship = Friendship.new
+ friendship.befriender = current_user
+ friendship.befriendee = @new_friend
+ if current_user.is_friends_with?(@new_friend)
+ flash[:warning] = t "friendships.make_friend.already_a_friend", :name => @new_friend.display_name
+ elsif friendship.save
+ flash[:notice] = t "friendships.make_friend.success", :name => @new_friend.display_name
+ Notifier.friend_notification(friendship).deliver_later
+ else
+ friendship.add_error(t("friendships.make_friend.failed", :name => @new_friend.display_name))
+ end
+
+ if params[:referer]
+ redirect_to params[:referer]
+ else
+ redirect_to user_path
+ end
+ end
+ else
+ render_unknown_user params[:display_name]
+ end
+ end
+
+ def remove_friend
+ @friend = User.find_by(:display_name => params[:display_name])
+
+ if @friend
+ if request.post?
+ if current_user.is_friends_with?(@friend)
+ Friendship.where(:befriender => current_user, :befriendee => @friend).delete_all
+ flash[:notice] = t "friendships.remove_friend.success", :name => @friend.display_name
+ else
+ flash[:error] = t "friendships.remove_friend.not_a_friend", :name => @friend.display_name
+ end
+
+ if params[:referer]
+ redirect_to params[:referer]
+ else
+ redirect_to user_path
+ end
+ end
+ else
+ render_unknown_user params[:display_name]
+ end
+ end
+end
authorize_resource
before_action :require_self, :only => [:account]
- before_action :check_database_writable, :only => [:new, :account, :confirm, :confirm_email, :lost_password, :reset_password, :go_public, :make_friend, :remove_friend]
+ before_action :check_database_writable, :only => [:new, :account, :confirm, :confirm_email, :lost_password, :reset_password, :go_public]
before_action :require_cookies, :only => [:new, :login, :confirm]
before_action :lookup_user_by_name, :only => [:set_status, :delete]
before_action :allow_thirdparty_images, :only => [:show, :account]
end
end
- def make_friend
- @new_friend = User.find_by(:display_name => params[:display_name])
-
- if @new_friend
- if request.post?
- friendship = Friendship.new
- friendship.befriender = current_user
- friendship.befriendee = @new_friend
- if current_user.is_friends_with?(@new_friend)
- flash[:warning] = t "users.make_friend.already_a_friend", :name => @new_friend.display_name
- elsif friendship.save
- flash[:notice] = t "users.make_friend.success", :name => @new_friend.display_name
- Notifier.friend_notification(friendship).deliver_later
- else
- friendship.add_error(t("users.make_friend.failed", :name => @new_friend.display_name))
- end
-
- if params[:referer]
- redirect_to params[:referer]
- else
- redirect_to :action => "show"
- end
- end
- else
- render_unknown_user params[:display_name]
- end
- end
-
- def remove_friend
- @friend = User.find_by(:display_name => params[:display_name])
-
- if @friend
- if request.post?
- if current_user.is_friends_with?(@friend)
- Friendship.where(:befriender => current_user, :befriendee => @friend).delete_all
- flash[:notice] = t "users.remove_friend.success", :name => @friend.display_name
- else
- flash[:error] = t "users.remove_friend.not_a_friend", :name => @friend.display_name
- end
-
- if params[:referer]
- redirect_to params[:referer]
- else
- redirect_to :action => "show"
- end
- end
- else
- render_unknown_user params[:display_name]
- end
- end
-
##
# sets a user's status
def set_status
with_recipient_locale friend.befriendee do
@friend = friend
@viewurl = user_url(@friend.befriender)
- @friendurl = url_for(:controller => "users", :action => "make_friend",
- :display_name => @friend.befriender.display_name)
+ @friendurl = make_friend_url(@friend.befriender)
@author = @friend.befriender.display_name
attach_user_avatar(@friend.befriender)
comment: Comment
newer_comments: "Newer Comments"
older_comments: "Older Comments"
+ friendships:
+ make_friend:
+ heading: "Add %{user} as a friend?"
+ button: "Add as friend"
+ success: "%{name} is now your friend!"
+ failed: "Sorry, failed to add %{name} as a friend."
+ already_a_friend: "You are already friends with %{name}."
+ remove_friend:
+ heading: "Unfriend %{user}?"
+ button: "Unfriend"
+ success: "%{name} was removed from your friends."
+ not_a_friend: "%{name} is not one of your friends."
geocoder:
search:
title:
flash success: "Home location saved successfully"
go_public:
flash success: "All your edits are now public, and you are now allowed to edit."
- make_friend:
- heading: "Add %{user} as a friend?"
- button: "Add as friend"
- success: "%{name} is now your friend!"
- failed: "Sorry, failed to add %{name} as a friend."
- already_a_friend: "You are already friends with %{name}."
- remove_friend:
- heading: "Unfriend %{user}?"
- button: "Unfriend"
- success: "%{name} was removed from your friends."
- not_a_friend: "%{name} is not one of your friends."
index:
title: Users
heading: Users
# user pages
get "/user/:display_name" => "users#show", :as => "user"
- match "/user/:display_name/make_friend" => "users#make_friend", :via => [:get, :post], :as => "make_friend"
- match "/user/:display_name/remove_friend" => "users#remove_friend", :via => [:get, :post], :as => "remove_friend"
match "/user/:display_name/account" => "users#account", :via => [:get, :post], :as => "user_account"
get "/user/:display_name/set_status" => "users#set_status", :as => :set_status_user
get "/user/:display_name/delete" => "users#delete", :as => :delete_user
+ # friendships
+ match "/user/:display_name/make_friend" => "friendships#make_friend", :via => [:get, :post], :as => "make_friend"
+ match "/user/:display_name/remove_friend" => "friendships#remove_friend", :via => [:get, :post], :as => "remove_friend"
+
# user lists
match "/users" => "users#index", :via => [:get, :post]
match "/users/:status" => "users#index", :via => [:get, :post]
def test_routes
assert_routing(
{ :path => "/user/username/make_friend", :method => :get },
- { :controller => "users", :action => "make_friend", :display_name => "username" }
+ { :controller => "friendships", :action => "make_friend", :display_name => "username" }
)
assert_routing(
{ :path => "/user/username/make_friend", :method => :post },
- { :controller => "users", :action => "make_friend", :display_name => "username" }
+ { :controller => "friendships", :action => "make_friend", :display_name => "username" }
)
assert_routing(
{ :path => "/user/username/remove_friend", :method => :get },
- { :controller => "users", :action => "remove_friend", :display_name => "username" }
+ { :controller => "friendships", :action => "remove_friend", :display_name => "username" }
)
assert_routing(
{ :path => "/user/username/remove_friend", :method => :post },
- { :controller => "users", :action => "remove_friend", :display_name => "username" }
+ { :controller => "friendships", :action => "remove_friend", :display_name => "username" }
)
end
# When not logged in a GET should ask us to login
get make_friend_path(friend)
- assert_redirected_to :action => "login", :referer => make_friend_path(:display_name => friend.display_name)
+ assert_redirected_to login_path(:referer => make_friend_path(:display_name => friend.display_name))
# When not logged in a POST should error
post make_friend_path(friend)
# When not logged in a GET should ask us to login
get remove_friend_path(friend)
- assert_redirected_to :action => "login", :referer => remove_friend_path(:display_name => friend.display_name)
+ assert_redirected_to login_path(:referer => remove_friend_path(:display_name => friend.display_name))
# When not logged in a POST should error
post remove_friend_path, :params => { :display_name => friend.display_name }