Create new followables helper to have some methods previously on follows helper. Rename entity to followable.

This commit is contained in:
Senén Rodero Rodríguez
2017-07-19 16:48:50 +02:00
parent 3be893b0ba
commit cba497b32e
6 changed files with 64 additions and 57 deletions

View File

@@ -0,0 +1,49 @@
module FollowablesHelper
def show_follow_action?(followable)
current_user && !followed?(followable)
end
def show_unfollow_action?(followable)
current_user && followed?(followable)
end
def entity_title(title)
t("activerecord.models.#{title.underscore}.other")
end
def followable_full_name(followable)
followable.class.name.parameterize
end
def followable_title(followable)
entity = followable.class.name.underscore
t('shared.followable_title', entity: t("activerecord.models.#{entity}.one").downcase)
end
def followable_icon(followable)
{
proposals: 'Proposal',
budget: 'Budget::Investment'
}.invert[followable]
end
def render_follow(follow)
followable = follow.followable
partial = followable_class_name(followable)
locals = {followable_class_name(followable).to_sym => followable}
render partial, locals
end
def followable_class_name(followable)
followable.class.to_s.parameterize.gsub('-', '_')
end
private
def followed?(followable)
Follow.followed?(current_user, followable)
end
end

View File

@@ -1,83 +1,41 @@
module FollowsHelper
def show_follow_action?(followable)
current_user && !followed?(followable)
end
def show_unfollow_action?(followable)
current_user && followed?(followable)
end
def follow_entity_text(followable)
def follow_text(followable)
entity = followable.class.name.underscore
t('shared.follow_entity', entity: t("activerecord.models.#{entity}.one").downcase)
end
def follow_entity_title(followable)
entity = followable.class.name.underscore
t('shared.follow_entity_title', entity: t("activerecord.models.#{entity}.one").downcase)
end
def unfollow_entity_text(followable)
def unfollow_text(followable)
entity = followable.class.name.underscore
t('shared.unfollow_entity', entity: t("activerecord.models.#{entity}.one").downcase)
end
def entity_full_name(followable)
followable.class.name.parameterize
end
def follow_link_wrapper_id(followable)
"follow-expand-#{entity_full_name(followable)}-#{followable.id}"
"follow-expand-#{followable_full_name(followable)}-#{followable.id}"
end
def unfollow_link_wrapper_id(followable)
"unfollow-expand-#{entity_full_name(followable)}-#{followable.id}"
"unfollow-expand-#{followable_full_name(followable)}-#{followable.id}"
end
def follow_link_id(followable)
"follow-#{entity_full_name(followable)}-#{followable.id}"
"follow-#{followable_full_name(followable)}-#{followable.id}"
end
def unfollow_link_id(followable)
"unfollow-#{entity_full_name(followable)}-#{followable.id}"
"unfollow-#{followable_full_name(followable)}-#{followable.id}"
end
def follow_drop_id(followable)
"follow-drop-#{entity_full_name(followable)}-#{followable.id}"
"follow-drop-#{followable_full_name(followable)}-#{followable.id}"
end
def unfollow_drop_id(followable)
"unfollow-drop-#{entity_full_name(followable)}-#{followable.id}"
"unfollow-drop-#{followable_full_name(followable)}-#{followable.id}"
end
def entity_title(title)
t("activerecord.models.#{title.underscore}.other")
end
def entity_icon(entity)
{
proposals: 'Proposal',
budget: 'Budget::Investment'
}.invert[entity]
end
def entity_class_name(followable)
followable.class.to_s.parameterize.gsub('-','_')
end
def render_follow(follow)
followable = follow.followable
partial = entity_class_name(followable)
locals = {entity_class_name(followable).to_sym => followable}
render partial, locals
end
private
def followed?(followable)
Follow.followed?(current_user, followable)
end
end

View File

@@ -5,14 +5,14 @@
<%= link_to "##{follow_link_wrapper_id(followable)}",
id: follow_link_wrapper_id(followable),
title: follow_entity_text(followable),
title: follow_text(followable),
data: { toggle: follow_drop_id(followable) },
class: 'button hollow' do %>
<%= t('shared.follow') %>
<% end %>
<div class="dropdown-pane" id="<%= follow_drop_id(followable) %>"
data-dropdown data-auto-focus="true">
<%= link_to follow_entity_text(followable),
<%= link_to follow_text(followable),
follows_path(followable_id: followable.id,
followable_type: followable.class.name),
method: :post, remote: true,
@@ -26,14 +26,14 @@
<% follow = followable.follows.where(user: current_user).first %>
<%= link_to "##{unfollow_link_wrapper_id(followable)}",
id: unfollow_link_wrapper_id(followable),
title: unfollow_entity_text(followable),
title: unfollow_text(followable),
data: { toggle: unfollow_drop_id(followable) },
class: 'button hollow' 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),
<%= link_to unfollow_text(followable),
follow_path(follow),
method: :delete, remote: true,
id: unfollow_link_id(followable) %>

View File

@@ -6,7 +6,7 @@
<a href="#" class="accordion-title">
<span class="icon">
<i class="icon icon-<%= entity_icon(followable_type) %>"></i>
<i class="icon icon-<%= followable_icon(followable_type) %>"></i>
</span>
<span class="title">
<strong><%= entity_title(followable_type) %></strong>

View File

@@ -504,7 +504,7 @@ en:
flag: Flag as inappropriate
follow: "Follow"
follow_entity: "Follow %{entity}"
follow_entity_title: "Follow %{entity}: You can participate and receive notifications of any related events."
followable_title: "Follow %{entity}: You can participate and receive notifications of any related events."
hide: Hide
print:
print_button: Print this info

View File

@@ -504,7 +504,7 @@ es:
flag: Denunciar como inapropiado
follow: "Seguir"
follow_entity: "Seguir %{entity}"
follow_entity_title: "Seguir %{entity}: Podrás participar y recibir notificaciones de cualquier suceso relacionado."
followable_title: "Seguir %{entity}: Podrás participar y recibir notificaciones de cualquier suceso relacionado."
hide: Ocultar
print:
print_button: Imprimir esta información