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_allegations
//= require legislation_annotatable //= require legislation_annotatable
//= require watch_form_changes //= require watch_form_changes
//= require followable
//= require tree_navigator //= require tree_navigator
//= require custom //= require custom
@@ -93,6 +94,7 @@ var initialize_modules = function() {
App.LegislationAnnotatable.initialize(); App.LegislationAnnotatable.initialize();
App.WatchFormChanges.initialize(); App.WatchFormChanges.initialize();
App.TreeNavigator.initialize(); App.TreeNavigator.initialize();
App.Followable.initialize();
}; };
$(function(){ $(function(){
@@ -100,5 +102,5 @@ $(function(){
$(document).ready(initialize_modules); $(document).ready(initialize_modules);
$(document).on('page:load', 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) t('shared.unfollow_entity', entity: t("activerecord.models.#{entity}.one").downcase)
end end
def entity_name(followable) def entity_full_name(followable)
entity_name = followable.class.name.split('::').last name = followable.class.name
entity_name.downcase name.downcase.gsub("::", "-")
end end
def entity_full_name(followable) def follow_link_wrapper_id(followable)
entity_name = followable.class.name "follow-expand-#{entity_full_name(followable)}-#{followable.id}"
entity_name.downcase.gsub("::", "-") 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 end
private private

View File

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