Retrospring/app/helpers/application_helper/graph_methods.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

62 lines
2.3 KiB
Ruby
Raw Normal View History

2022-01-15 16:04:47 -08:00
# frozen_string_literal: true
2021-08-06 13:20:55 -07:00
module ApplicationHelper::GraphMethods
# Creates <meta> tags for OpenGraph properties from a hash
# @param values [Hash]
def opengraph_meta_tags(values)
safe_join(values.map { |name, content| tag.meta(property: name, content:) }, "\n")
2021-08-06 13:20:55 -07:00
end
# Creates <meta> tags from a hash
# @param values [Hash]
def meta_tags(values)
safe_join(values.map { |name, content| tag.meta(name:, content:) }, "\n")
2021-08-06 13:20:55 -07:00
end
# @param user [User]
def user_opengraph(user)
opengraph_meta_tags({
2022-01-15 16:04:47 -08:00
"og:title": user.profile.safe_name,
"og:type": "profile",
"og:image": full_profile_picture_url(user),
"og:url": user_url(user),
2022-01-15 16:04:47 -08:00
"og:description": user.profile.description,
"og:site_name": APP_CONFIG["site_name"],
"profile:username": user.screen_name,
2021-08-06 13:20:55 -07:00
})
end
# @param user [User]
def user_twitter_card(user)
meta_tags({
2022-01-15 16:04:47 -08:00
"twitter:card": "summary",
"twitter:site": "@retrospring",
"twitter:title": user.profile.motivation_header.presence || "Ask me anything!",
"twitter:description": "Ask #{user.profile.safe_name} anything on Retrospring",
"twitter:image": full_profile_picture_url(user),
2021-08-06 13:20:55 -07:00
})
end
# @param answer [Answer]
def answer_opengraph(answer)
opengraph_meta_tags({
2022-01-15 16:04:47 -08:00
"og:title": "#{answer.user.profile.safe_name} answered: #{answer.question.content}",
"og:type": "article",
"og:image": full_profile_picture_url(answer.user),
"og:url": answer_url(answer.user.screen_name, answer.id),
2022-01-15 16:04:47 -08:00
"og:description": answer.content,
"og:site_name": APP_CONFIG["site_name"],
2021-08-06 13:20:55 -07:00
})
end
def full_profile_picture_url(user)
# @type [String]
profile_picture_url = user.profile_picture.url(:large)
2022-01-15 16:04:47 -08:00
if profile_picture_url.start_with? "/"
"#{root_url}#{profile_picture_url[1..]}"
2021-08-06 13:20:55 -07:00
else
profile_picture_url
end
end
2022-01-15 16:04:47 -08:00
end