From 313b78978d543aac07c53d41848b5f6f7d0df24e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sen=C3=A9n=20Rodero=20Rodr=C3=ADguez?= Date: Fri, 30 Jun 2017 20:21:38 +0200 Subject: [PATCH] Add followable JS module. Refactor follow_helper and follow buttons partial. --- app/assets/javascripts/application.js | 4 ++- app/assets/javascripts/followable.js.coffee | 5 +++ app/helpers/follows_helper.rb | 31 ++++++++++++---- app/views/follows/_followable_button.html.erb | 36 +++++++++++++------ 4 files changed, 59 insertions(+), 17 deletions(-) create mode 100644 app/assets/javascripts/followable.js.coffee diff --git a/app/assets/javascripts/application.js b/app/assets/javascripts/application.js index d736be793..7595f9555 100644 --- a/app/assets/javascripts/application.js +++ b/app/assets/javascripts/application.js @@ -58,6 +58,7 @@ //= require legislation_allegations //= require legislation_annotatable //= require watch_form_changes +//= require followable //= require tree_navigator //= require custom @@ -93,6 +94,7 @@ var initialize_modules = function() { App.LegislationAnnotatable.initialize(); App.WatchFormChanges.initialize(); App.TreeNavigator.initialize(); + App.Followable.initialize(); }; $(function(){ @@ -100,5 +102,5 @@ $(function(){ $(document).ready(initialize_modules); $(document).on('page:load', initialize_modules); - $(document).on('ajax:complete', initialize_modules); + $(document).on('ajaxComplete', initialize_modules); }); diff --git a/app/assets/javascripts/followable.js.coffee b/app/assets/javascripts/followable.js.coffee new file mode 100644 index 000000000..4aa69fd94 --- /dev/null +++ b/app/assets/javascripts/followable.js.coffee @@ -0,0 +1,5 @@ +App.Followable = + + initialize: -> + $('.followable-content a[data-toggle]').on 'click', (event) -> + event.preventDefault() diff --git a/app/helpers/follows_helper.rb b/app/helpers/follows_helper.rb index cc01e9fc6..49767d3e2 100644 --- a/app/helpers/follows_helper.rb +++ b/app/helpers/follows_helper.rb @@ -18,14 +18,33 @@ module FollowsHelper t('shared.unfollow_entity', entity: t("activerecord.models.#{entity}.one").downcase) end - def entity_name(followable) - entity_name = followable.class.name.split('::').last - entity_name.downcase + def entity_full_name(followable) + name = followable.class.name + name.downcase.gsub("::", "-") end - def entity_full_name(followable) - entity_name = followable.class.name - entity_name.downcase.gsub("::", "-") + def follow_link_wrapper_id(followable) + "follow-expand-#{entity_full_name(followable)}-#{followable.id}" + end + + def unfollow_link_wrapper_id(followable) + "unfollow-expand-#{entity_full_name(followable)}-#{followable.id}" + end + + def follow_link_id(followable) + "follow-#{entity_full_name(followable)}-#{followable.id}" + end + + def unfollow_link_id(followable) + "unfollow-#{entity_full_name(followable)}-#{followable.id}" + end + + def follow_drop_id(followable) + "follow-drop-#{entity_full_name(followable)}-#{followable.id}" + end + + def unfollow_drop_id(followable) + "unfollow-drop-#{entity_full_name(followable)}-#{followable.id}" end private diff --git a/app/views/follows/_followable_button.html.erb b/app/views/follows/_followable_button.html.erb index cd6bae621..e54d524c1 100644 --- a/app/views/follows/_followable_button.html.erb +++ b/app/views/follows/_followable_button.html.erb @@ -1,21 +1,37 @@ -<% entity = entity_name(followable) %> + <% if show_follow_action? followable %> - + <%= link_to "##{follow_link_wrapper_id(followable)}", + id: follow_link_wrapper_id(followable), + title: follow_entity_text(followable), + data: { toggle: follow_drop_id(followable) } do %> <%= t('shared.follow') %> - -