From d08d0156dc77fb4df8acb7b834d25a4b7910af66 Mon Sep 17 00:00:00 2001 From: Karina Kwiatek Date: Fri, 6 Aug 2021 22:20:55 +0200 Subject: [PATCH] Move graph helpers to separate module --- app/helpers/application_helper.rb | 60 +------------------ .../application_helper/graph_methods.rb | 59 ++++++++++++++++++ spec/helpers/application_helper_spec.rb | 3 +- 3 files changed, 62 insertions(+), 60 deletions(-) create mode 100644 app/helpers/application_helper/graph_methods.rb diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index 7d5fcff9..296f1aa8 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -1,4 +1,6 @@ module ApplicationHelper + include ApplicationHelper::GraphMethods + def nav_entry(body, path, options = {}) options = { badge: nil, @@ -169,62 +171,4 @@ module ApplicationHelper def list_title(list) generate_title list.name end - - # Creates tags for OpenGraph properties from a hash - # @param values [Hash] - def opengraph_meta_tags(values) - values.map { |name, content| tag(:meta, property: name, content: content) }.join("\n").html_safe - end - - # Creates tags from a hash - # @param values [Hash] - def meta_tags(values) - values.map { |name, content| tag(:meta, name: name, content: content) }.join("\n").html_safe - end - - # @param user [User] - def user_opengraph(user) - opengraph_meta_tags({ - 'og:title': user.safe_name, - 'og:type': 'profile', - 'og:image': full_profile_picture_url(user), - 'og:url': show_user_profile_url(user.screen_name), - 'og:description': user.bio, - 'og:site_name': APP_CONFIG['site_name'], - 'profile:username': user.screen_name - }) - end - - # @param user [User] - def user_twitter_card(user) - meta_tags({ - 'twitter:card': 'summary', - 'twitter:site': '@retrospring', - 'twitter:title': user.motivation_header.presence || "Ask me anything!", - 'twitter:description': "Ask #{user.safe_name} anything on Retrospring", - 'twitter:image': full_profile_picture_url(user) - }) - end - - # @param answer [Answer] - def answer_opengraph(answer) - opengraph_meta_tags({ - 'og:title': "#{answer.user.safe_name} answered: #{answer.question.content}", - 'og:type': 'article', - 'og:image': full_profile_picture_url(answer.user), - 'og:url': show_user_answer_url(answer.user.screen_name, answer.id), - 'og:description': answer.content, - 'og:site_name': APP_CONFIG['site_name'] - }) - end - - def full_profile_picture_url(user) - # @type [String] - profile_picture_url = user.profile_picture.url(:large) - if profile_picture_url.start_with? '/' - "#{root_url}#{profile_picture_url[1..-1]}" - else - profile_picture_url - end - end end diff --git a/app/helpers/application_helper/graph_methods.rb b/app/helpers/application_helper/graph_methods.rb new file mode 100644 index 00000000..137bcf73 --- /dev/null +++ b/app/helpers/application_helper/graph_methods.rb @@ -0,0 +1,59 @@ +module ApplicationHelper::GraphMethods + # Creates tags for OpenGraph properties from a hash + # @param values [Hash] + def opengraph_meta_tags(values) + values.map { |name, content| tag(:meta, property: name, content: content) }.join("\n").html_safe + end + + # Creates tags from a hash + # @param values [Hash] + def meta_tags(values) + values.map { |name, content| tag(:meta, name: name, content: content) }.join("\n").html_safe + end + + # @param user [User] + def user_opengraph(user) + opengraph_meta_tags({ + 'og:title': user.safe_name, + 'og:type': 'profile', + 'og:image': full_profile_picture_url(user), + 'og:url': show_user_profile_url(user.screen_name), + 'og:description': user.bio, + 'og:site_name': APP_CONFIG['site_name'], + 'profile:username': user.screen_name + }) + end + + # @param user [User] + def user_twitter_card(user) + meta_tags({ + 'twitter:card': 'summary', + 'twitter:site': '@retrospring', + 'twitter:title': user.motivation_header.presence || "Ask me anything!", + 'twitter:description': "Ask #{user.safe_name} anything on Retrospring", + 'twitter:image': full_profile_picture_url(user) + }) + end + + # @param answer [Answer] + def answer_opengraph(answer) + opengraph_meta_tags({ + 'og:title': "#{answer.user.safe_name} answered: #{answer.question.content}", + 'og:type': 'article', + 'og:image': full_profile_picture_url(answer.user), + 'og:url': show_user_answer_url(answer.user.screen_name, answer.id), + 'og:description': answer.content, + 'og:site_name': APP_CONFIG['site_name'] + }) + end + + def full_profile_picture_url(user) + # @type [String] + profile_picture_url = user.profile_picture.url(:large) + if profile_picture_url.start_with? '/' + "#{root_url}#{profile_picture_url[1..-1]}" + else + profile_picture_url + end + end +end \ No newline at end of file diff --git a/spec/helpers/application_helper_spec.rb b/spec/helpers/application_helper_spec.rb index 6129bbb3..58a2ff69 100644 --- a/spec/helpers/application_helper_spec.rb +++ b/spec/helpers/application_helper_spec.rb @@ -87,5 +87,4 @@ EOS end end end - -end \ No newline at end of file +end