From cba497b32e91786828a785cb50ca224fad9d9766 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sen=C3=A9n=20Rodero=20Rodr=C3=ADguez?= Date: Wed, 19 Jul 2017 16:48:50 +0200 Subject: [PATCH] Create new followables helper to have some methods previously on follows helper. Rename entity to followable. --- app/helpers/followables_helper.rb | 49 ++++++++++++++++ app/helpers/follows_helper.rb | 58 +++---------------- app/views/follows/_followable_button.html.erb | 8 +-- app/views/users/_following.html.erb | 2 +- config/locales/en/general.yml | 2 +- config/locales/es/general.yml | 2 +- 6 files changed, 64 insertions(+), 57 deletions(-) create mode 100644 app/helpers/followables_helper.rb diff --git a/app/helpers/followables_helper.rb b/app/helpers/followables_helper.rb new file mode 100644 index 000000000..974270b0a --- /dev/null +++ b/app/helpers/followables_helper.rb @@ -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 diff --git a/app/helpers/follows_helper.rb b/app/helpers/follows_helper.rb index 2b09ccb67..c10fba678 100644 --- a/app/helpers/follows_helper.rb +++ b/app/helpers/follows_helper.rb @@ -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 diff --git a/app/views/follows/_followable_button.html.erb b/app/views/follows/_followable_button.html.erb index 527a7e029..7fc7a6ead 100644 --- a/app/views/follows/_followable_button.html.erb +++ b/app/views/follows/_followable_button.html.erb @@ -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 %>