diff --git a/app/assets/javascripts/followable.js.coffee b/app/assets/javascripts/followable.js.coffee index 20ff96f3f..9fa9aa319 100644 --- a/app/assets/javascripts/followable.js.coffee +++ b/app/assets/javascripts/followable.js.coffee @@ -1,4 +1,8 @@ App.Followable = - update: (followable_id, button) -> + update: (followable_id, button, notice) -> $("#" + followable_id + " .js-follow").html(button) + if ($('[data-alert]').length > 0) + $('[data-alert]').replaceWith(notice) + else + $("body").append(notice) diff --git a/app/controllers/follows_controller.rb b/app/controllers/follows_controller.rb index 596aa657f..3c3300507 100644 --- a/app/controllers/follows_controller.rb +++ b/app/controllers/follows_controller.rb @@ -5,12 +5,14 @@ class FollowsController < ApplicationController def create followable = find_followable @follow = Follow.create(user: current_user, followable: followable) + flash.now[:notice] = t("shared.followable.#{followable_translation_key(@follow.followable)}.create.notice_html") render :refresh_follow_button end def destroy @follow = Follow.find(params[:id]) @follow.destroy + flash.now[:notice] = t("shared.followable.#{followable_translation_key(@follow.followable)}.destroy.notice_html") render :refresh_follow_button end @@ -20,4 +22,8 @@ class FollowsController < ApplicationController params[:followable_type].constantize.find(params[:followable_id]) end + def followable_translation_key(followable) + followable.class.name.parameterize.gsub("-", "_") + end + end diff --git a/app/views/follows/refresh_follow_button.js.erb b/app/views/follows/refresh_follow_button.js.erb index 1a5b58903..ad7a7d4f1 100644 --- a/app/views/follows/refresh_follow_button.js.erb +++ b/app/views/follows/refresh_follow_button.js.erb @@ -1,2 +1,3 @@ App.Followable.update("<%= dom_id(@follow.followable) %>", - "<%= j render('follow_button', follow: @follow) %>") + "<%= j render('follow_button', follow: @follow) %>", + "<%= j render('layouts/flash') %>") diff --git a/config/locales/en/general.yml b/config/locales/en/general.yml index bbd8350d3..dd4bcee3d 100644 --- a/config/locales/en/general.yml +++ b/config/locales/en/general.yml @@ -504,7 +504,17 @@ en: flag: Flag as inappropriate follow: "Follow" follow_entity: "Follow %{entity}" - followable_title: "Follow %{entity}: You can participate and receive notifications of any related events." + followable: + budget_investment: + create: + notice_html: "You are now following this investment project!
We will notify you of changes as they occur so that you are up-to-date." + destroy: + notice_html: "You have stopped following this investment project!
You will no longer receive notifications related to this project." + proposal: + create: + notice_html: "Now you are following this citizen proposal!
We will notify you of changes as they occur so that you are up-to-date." + destroy: + notice_html: "You have stopped following this citizen proposal!
You will no longer receive notifications related to this proposal." hide: Hide print: print_button: Print this info diff --git a/config/locales/es/general.yml b/config/locales/es/general.yml index e913a28a1..76dde0bce 100644 --- a/config/locales/es/general.yml +++ b/config/locales/es/general.yml @@ -504,7 +504,17 @@ es: flag: Denunciar como inapropiado follow: "Seguir" follow_entity: "Seguir %{entity}" - followable_title: "Seguir %{entity}: Podrás participar y recibir notificaciones de cualquier suceso relacionado." + followable: + budget_investment: + create: + notice_html: "¡Ahora estás siguiendo este proyecto de inversión!
Te notificaremos los cambios a medida que se produzcan para que estés al día." + destroy: + notice_html: "¡Has dejado de seguir este proyecto de inverisión!
Ya no recibirás más notificaciones relacionadas con este proyecto." + proposal: + create: + notice_html: "¡Ahora estás siguiendo esta propuesta ciudadana!
Te notificaremos los cambios a medida que se produzcan para que estés al día." + destroy: + notice_html: "¡Has dejado de seguir esta propuesta ciudadana!
Ya no recibirás más notificaciones relacionadas con esta propuesta." hide: Ocultar print: print_button: Imprimir esta información diff --git a/spec/shared/features/followable.rb b/spec/shared/features/followable.rb index 7c738c8a3..31afc38d3 100644 --- a/spec/shared/features/followable.rb +++ b/spec/shared/features/followable.rb @@ -49,10 +49,23 @@ shared_examples "followable" do |followable_class_name, followable_path, followa within "##{dom_id(followable)}" do click_link "Follow" + expect(page).not_to have_link "Follow" expect(page).to have_link "Unfollow" end end + scenario "Should display new follower notice after user clicks on follow button", :js do + user = create(:user) + login_as(user) + + visit send(followable_path, arguments) + within "##{dom_id(followable)}" do + click_link "Follow" + end + + expect(page).to have_content strip_tags(t("shared.followable.#{followable_class_name}.create.notice_html")) + end + scenario "Display unfollow button when user already following" do user = create(:user) follow = create(:follow, user: user, followable: followable) @@ -63,7 +76,7 @@ shared_examples "followable" do |followable_class_name, followable_path, followa expect(page).to have_link("Unfollow") end - scenario "Should display follow button after user clicks on unfollow button", :js do + scenario "Should update follow button and show destroy notice after user clicks on unfollow button", :js do user = create(:user) follow = create(:follow, user: user, followable: followable) login_as(user) @@ -72,10 +85,24 @@ shared_examples "followable" do |followable_class_name, followable_path, followa within "##{dom_id(followable)}" do click_link "Unfollow" + expect(page).not_to have_link "Unfollow" expect(page).to have_link "Follow" end end + scenario "Should display destroy follower notice after user clicks on unfollow button", :js do + user = create(:user) + follow = create(:follow, user: user, followable: followable) + login_as(user) + + visit send(followable_path, arguments) + within "##{dom_id(followable)}" do + click_link "Unfollow" + end + + expect(page).to have_content strip_tags(t("shared.followable.#{followable_class_name}.destroy.notice_html")) + end + end end