-
Notifications
You must be signed in to change notification settings - Fork 929
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor friendships controller and model
- Loading branch information
Showing
27 changed files
with
318 additions
and
339 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
class FollowsController < ApplicationController | ||
include UserMethods | ||
|
||
layout "site" | ||
|
||
before_action :authorize_web | ||
before_action :set_locale | ||
before_action :check_database_readable | ||
|
||
authorize_resource | ||
|
||
before_action :check_database_writable | ||
before_action :lookup_friend | ||
|
||
def show | ||
@already_follows = current_user.friends_with?(@friend) | ||
end | ||
|
||
def create | ||
follow = Follow.new | ||
follow.follower = current_user | ||
follow.following = @friend | ||
if current_user.friends_with?(@friend) | ||
flash[:warning] = t ".already_followed", :name => @friend.display_name | ||
elsif current_user.follows.where(:created_at => Time.now.utc - 1.hour..).count >= current_user.max_friends_per_hour | ||
flash[:error] = t ".limit_exceeded" | ||
elsif follow.save | ||
flash[:notice] = t ".success", :name => @friend.display_name | ||
UserMailer.friendship_notification(follow).deliver_later | ||
else | ||
follow.add_error(t(".failed", :name => @friend.display_name)) | ||
end | ||
|
||
referer = safe_referer(params[:referer]) if params[:referer] | ||
|
||
redirect_to referer || user_path | ||
end | ||
|
||
def destroy | ||
if current_user.friends_with?(@friend) | ||
Follow.where(:follower => current_user, :following => @friend).delete_all | ||
flash[:notice] = t ".success", :name => @friend.display_name | ||
else | ||
flash[:error] = t ".not_followed", :name => @friend.display_name | ||
end | ||
|
||
referer = safe_referer(params[:referer]) if params[:referer] | ||
|
||
redirect_to referer || user_path | ||
end | ||
|
||
private | ||
|
||
## | ||
# ensure that there is a "friend" instance variable | ||
def lookup_friend | ||
@friend = User.active.find_by!(:display_name => params[:display_name]) | ||
rescue ActiveRecord::RecordNotFound | ||
render_unknown_user params[:display_name] | ||
end | ||
end |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<% content_for :heading do %> | ||
<h1><%= t(@already_follows ? ".unfollow.heading" : ".follow.heading", :user => @friend.display_name) %></h1> | ||
<% end %> | ||
|
||
<%= link_to t(@already_follows ? ".unfollow.button" : ".follow.button"), | ||
follow_path(:display_name => @friend.display_name, :referer => params[:referer]), | ||
:method => (@already_follows ? :delete : :post), | ||
:class => "btn btn-sm btn-primary" %> |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,11 @@ | ||
<p><%= t ".hi", :to_user => @friendship.befriendee.display_name %></p> | ||
<p><%= t ".hi", :to_user => @follow.following.display_name %></p> | ||
|
||
<p><%= t ".followed_you", :user => @friendship.befriender.display_name %></p> | ||
<p><%= t ".followed_you", :user => @follow.follower.display_name %></p> | ||
|
||
<%= message_body do %> | ||
<p><%= t ".see_their_profile_html", :userurl => link_to(@viewurl, @viewurl) %></p> | ||
|
||
<% unless @friendship.befriendee.friends_with?(@friendship.befriender) -%> | ||
<% unless @follow.following.friends_with?(@follow.follower) -%> | ||
<p><%= t ".follow_them_html", :followurl => link_to(@followurl, @followurl) %></p> | ||
<% end -%> | ||
<% end %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,9 @@ | ||
<%= t ".hi", :to_user => @friendship.befriendee.display_name %> | ||
<%= t ".hi", :to_user => @follow.following.display_name %> | ||
|
||
<%= t '.followed_you', :user => @friendship.befriender.display_name %> | ||
<%= t '.followed_you', :user => @follow.follower.display_name %> | ||
|
||
<%= t '.see_their_profile', :userurl => @viewurl %> | ||
|
||
<% unless @friendship.befriendee.friends_with?(@friendship.befriender) -%> | ||
<% unless @follow.following.friends_with?(@follow.follower) -%> | ||
<%= t '.follow_them', :followurl => @followurl %> | ||
<% end -%> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.