Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

83 custom success message for your first friend #116

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions app/controllers/friendships_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,18 @@ def create
# PATCH/PUT /friendships/1 or /friendships/1.json
def update
respond_to do |format|
if Friendship.where(buddy_id: current_user.id, status: :accepted).length == 0
if @friendship.update(friendship_params)
format.html do
redirect_to @friendship, notice: "It's dangerous to go alone, take this Friend."
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we just swap out the message instead?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Length === 1?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought that but testing it in the browser it only actually works when length === 0 🤔

Why do you think that it should be length 1? My thought process was since it's for the first friendship and the update is happening just after in the if @friendship.update

The message can be swapped out no problem, I agree it will make it cleaner.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well this PR was a while ago.... I was thinking we'd check the length inside the format.html and swap between two strings there. But we could also use a variable from outside and check against 0.

end
format.json { render :show, status: :ok, location: @friendship }
else
format.html { render :edit, status: :unprocessable_entity }
format.json { render json: @friendship.errors, status: :unprocessable_entity }
end
end

if @friendship.update(friendship_params)
format.html { redirect_to @friendship, notice: "Friendship was successfully updated." } # friend made = buddy up
format.json { render :show, status: :ok, location: @friendship }
Expand Down
7 changes: 7 additions & 0 deletions spec/requests/friendships_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,13 @@
patch_update
friendship.reload
expect(friendship.status).to eq("accepted")

# When you accept your first friendship, send a custom message. "It's dangerous to go alone, take this Friend."
if Friendship.where(buddy_id: user.id, status: :accepted).length == 0
expect(flash[:notice]).to match(/It's dangerous to go alone, take this Friend.*/)
else
expect(flash[:notice]).to match(/Friendship was successfully updated.*/)
end
end

it "redirects to the friendship" do
Expand Down