Add followable JS module. Refactor follow_helper and follow buttons partial.

This commit is contained in:
Senén Rodero Rodríguez
2017-06-30 20:21:38 +02:00
parent 1f22286e29
commit 313b78978d
4 changed files with 59 additions and 17 deletions

View File

@@ -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);
});

View File

@@ -0,0 +1,5 @@
App.Followable =
initialize: ->
$('.followable-content a[data-toggle]').on 'click', (event) ->
event.preventDefault()

View File

@@ -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

View File

@@ -1,21 +1,37 @@
<% entity = entity_name(followable) %>
<span class="followable-content">
<% if show_follow_action? followable %>
<a href="#follow-drop-<%= entity_full_name(followable) %>-<%= followable.id %>" id="follow-expand-<%= entity_full_name(followable) %>-<%= followable.id %>" data-toggle="follow-drop-<%= entity_full_name(followable) %>-<%= followable.id %>" title="<%= follow_entity_text(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') %>
</a>
<div class="dropdown-pane" id="follow-drop-<%= entity_full_name(followable) %>-<%= followable.id %>" data-dropdown data-auto-focus="true">
<%= link_to follow_entity_text(followable), follows_path(followable_id: followable.id, followable_type: followable.class.name), method: :post, remote: true, id: "follow-#{entity_full_name(followable)}-#{ followable.id }" %>
<% end %>
<div class="dropdown-pane" id="<%= follow_drop_id(followable) %>"
data-dropdown data-auto-focus="true">
<%= link_to follow_entity_text(followable),
follows_path(followable_id: followable.id,
followable_type: followable.class.name),
method: :post, remote: true,
id: follow_link_id(followable) %>
</div>
<% end %>
<% if show_unfollow_action? followable %>
<% follow = followable.follows.where(user: current_user).first %>
<a href="#unfollow-drop-<%= entity_full_name(followable) %>-<%= followable.id %>" id="unfollow-expand-<%= entity_full_name(followable) %>-<%= followable.id %>" data-toggle="unfollow-drop-<%= entity_full_name(followable) %>-<%= followable.id %>" title="<%= unfollow_entity_text(followable) %>">
<%= t('shared.unfollow') %>
</a>
<div class="dropdown-pane" id="unfollow-drop-<%= entity_full_name(followable) %>-<%= followable.id %>" data-dropdown data-auto-focus="true">
<%= link_to unfollow_entity_text(followable), follow_path(follow), method: :delete, remote: true, id: "unfollow-#{entity_full_name(followable)}-#{ followable.id }" %>
<%= link_to "##{unfollow_link_wrapper_id(followable)}",
id: unfollow_link_wrapper_id(followable),
title: unfollow_entity_text(followable),
data: { toggle: unfollow_drop_id(followable) } do %>
<%= t('shared.unfollow') %>
<% end %>
<div class="dropdown-pane" id="<%= unfollow_drop_id(followable) %>"
data-dropdown data-auto-focus="true">
<%= link_to unfollow_entity_text(followable),
follow_path(follow),
method: :delete, remote: true,
id: unfollow_link_id(followable) %>
</div>
<% end %>
</span>