Skip to content
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
14 changes: 8 additions & 6 deletions app/controllers/kudos_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,7 @@ def create
respond_to do |format|
format.html do
flash[:kudos_notice] = t(".success")
redirect_path = url_from(request.referer) || polymorphic_path(@kudo.commentable)
redirect_to "#{redirect_path}#kudos_message" and return
redirect_to kudos_redirect_path(@kudo.commentable) and return
end

format.js do
Expand All @@ -59,8 +58,7 @@ def create
return if check_user_status

flash[:kudos_error] = error_message
redirect_path = url_from(request.referer) || polymorphic_path(@kudo.commentable || root_path)
redirect_to "#{redirect_path}#kudos_message" and return
redirect_to kudos_redirect_path(@kudo.commentable || root_path) and return
end

format.js do
Expand All @@ -78,8 +76,7 @@ def create
respond_to do |format|
format.html do
flash[:kudos_error] = error_message
redirect_path = url_from(request.referer) || polymorphic_path(@kudo&.commentable || root_path)
redirect_to "#{redirect_path}#kudos_message" and return
redirect_to kudos_redirect_path(@kudo&.commentable || root_path) and return
end

format.js do
Expand All @@ -90,6 +87,11 @@ def create

private

def kudos_redirect_path(commentable)
redirect_path = url_from(request.referer) || polymorphic_path(commentable)
"#{redirect_path.split('#', 2).first}#kudos_message"
end

def kudo_params
params.require(:kudo).permit(:commentable_id, :commentable_type)
end
Expand Down
6 changes: 6 additions & 0 deletions spec/controllers/kudos_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@
it_redirects_to_with_kudos_notice(referer, "Thank you for leaving kudos!")
end

it "redirects without a duplicate anchor when the referer already has an anchor" do
request.headers["HTTP_REFERER"] = work_path(work, anchor: "bookmark-form")
post :create, params: { kudo: { commentable_id: work.id, commentable_type: "Work" } }
it_redirects_to_with_kudos_notice(work_path(work, anchor: "kudos_message"), "Thank you for leaving kudos!")
end

it "does not save user on kudos" do
post :create, params: { kudo: { commentable_id: work.id, commentable_type: "Work" } }
expect(assigns(:kudo)).to be_persisted
Expand Down
Loading