Fix code style in ApplicationHelper

This commit is contained in:
Andreas Nedbal 2022-01-16 01:04:47 +01:00 committed by Andreas Nedbal
parent bdaf2f7fef
commit a438e414c9
3 changed files with 51 additions and 44 deletions

View File

@ -1,9 +1,12 @@
# frozen_string_literal: true
module ApplicationHelper module ApplicationHelper
include ApplicationHelper::GraphMethods include ApplicationHelper::GraphMethods
include ApplicationHelper::TitleMethods include ApplicationHelper::TitleMethods
def inbox_count def inbox_count
return 0 unless user_signed_in? return 0 unless user_signed_in?
count = Inbox.select("COUNT(id) AS count") count = Inbox.select("COUNT(id) AS count")
.where(new: true) .where(new: true)
.where(user_id: current_user.id) .where(user_id: current_user.id)
@ -11,31 +14,35 @@ module ApplicationHelper
.order(:count) .order(:count)
.first .first
return nil if count.nil? return nil if count.nil?
return nil unless count.count > 0 return nil unless count.count.positive?
count.count count.count
end end
def notification_count def notification_count
return 0 unless user_signed_in? return 0 unless user_signed_in?
count = Notification.for(current_user).where(new: true) count = Notification.for(current_user).where(new: true)
return nil if count.nil? return nil if count.nil?
return nil unless count.count > 0 return nil unless count.count.positive?
count.count count.count
end end
def privileged?(user) def privileged?(user)
((!current_user.nil?) && ((current_user == user) || current_user.mod?)) ? true : false !current_user.nil? && ((current_user == user) || current_user.mod?)
end end
def ios_web_app? def ios_web_app?
user_agent = request.env['HTTP_USER_AGENT'] || 'Mozilla/5.0' user_agent = request.env["HTTP_USER_AGENT"] || "Mozilla/5.0"
# normal MobileSafari.app UA: Mozilla/5.0 (iPhone; CPU iPhone OS 8_1_1 like Mac OS X) AppleWebKit/600.1.4 (KHTML, like Gecko) Version/8.0 Mobile/12B435 Safari/600.1.4 # normal MobileSafari.app UA: Mozilla/5.0 (iPhone; CPU iPhone OS 8_1_1 like Mac OS X) AppleWebKit/600.1.4 (KHTML, like Gecko) Version/8.0 Mobile/12B435 Safari/600.1.4
# webapp UA: Mozilla/5.0 (iPhone; CPU iPhone OS 8_1_1 like Mac OS X) AppleWebKit/600.1.4 (KHTML, like Gecko) Mobile/12B435 # webapp UA: Mozilla/5.0 (iPhone; CPU iPhone OS 8_1_1 like Mac OS X) AppleWebKit/600.1.4 (KHTML, like Gecko) Mobile/12B435
return true if user_agent.match /^Mozilla\/\d+\.\d+ \(i(?:Phone|Pad|Pod); CPU(?:.*) like Mac OS X\)(?:.*) Mobile(?:\S*)$/ return true if user_agent.match(/^Mozilla\/\d+\.\d+ \(i(?:Phone|Pad|Pod); CPU(?:.*) like Mac OS X\)(?:.*) Mobile(?:\S*)$/)
false false
end end
def rails_admin_path_for_resource(resource) def rails_admin_path_for_resource(resource)
[rails_admin_path, resource.model_name.param_key, resource.id].join('/') [rails_admin_path, resource.model_name.param_key, resource.id].join("/")
end end
end end

View File

@ -1,59 +1,61 @@
# frozen_string_literal: true
module ApplicationHelper::GraphMethods module ApplicationHelper::GraphMethods
# Creates <meta> tags for OpenGraph properties from a hash # Creates <meta> tags for OpenGraph properties from a hash
# @param values [Hash] # @param values [Hash]
def opengraph_meta_tags(values) def opengraph_meta_tags(values)
values.map { |name, content| tag(:meta, property: name, content: content) }.join("\n").html_safe safe_join(values.map { |name, content| tag.meta(property: name, content: content) }, "\n")
end end
# Creates <meta> tags from a hash # Creates <meta> tags from a hash
# @param values [Hash] # @param values [Hash]
def meta_tags(values) def meta_tags(values)
values.map { |name, content| tag(:meta, name: name, content: content) }.join("\n").html_safe safe_join(values.map { |name, content| tag.meta(name: name, content: content) }, "\n")
end end
# @param user [User] # @param user [User]
def user_opengraph(user) def user_opengraph(user)
opengraph_meta_tags({ opengraph_meta_tags({
'og:title': user.profile.safe_name, "og:title": user.profile.safe_name,
'og:type': 'profile', "og:type": "profile",
'og:image': full_profile_picture_url(user), "og:image": full_profile_picture_url(user),
'og:url': show_user_profile_url(user.screen_name), "og:url": show_user_profile_url(user.screen_name),
'og:description': user.profile.description, "og:description": user.profile.description,
'og:site_name': APP_CONFIG['site_name'], "og:site_name": APP_CONFIG["site_name"],
'profile:username': user.screen_name "profile:username": user.screen_name
}) })
end end
# @param user [User] # @param user [User]
def user_twitter_card(user) def user_twitter_card(user)
meta_tags({ meta_tags({
'twitter:card': 'summary', "twitter:card": "summary",
'twitter:site': '@retrospring', "twitter:site": "@retrospring",
'twitter:title': user.profile.motivation_header.presence || "Ask me anything!", "twitter:title": user.profile.motivation_header.presence || "Ask me anything!",
'twitter:description': "Ask #{user.profile.safe_name} anything on Retrospring", "twitter:description": "Ask #{user.profile.safe_name} anything on Retrospring",
'twitter:image': full_profile_picture_url(user) "twitter:image": full_profile_picture_url(user)
}) })
end end
# @param answer [Answer] # @param answer [Answer]
def answer_opengraph(answer) def answer_opengraph(answer)
opengraph_meta_tags({ opengraph_meta_tags({
'og:title': "#{answer.user.profile.safe_name} answered: #{answer.question.content}", "og:title": "#{answer.user.profile.safe_name} answered: #{answer.question.content}",
'og:type': 'article', "og:type": "article",
'og:image': full_profile_picture_url(answer.user), "og:image": full_profile_picture_url(answer.user),
'og:url': show_user_answer_url(answer.user.screen_name, answer.id), "og:url": show_user_answer_url(answer.user.screen_name, answer.id),
'og:description': answer.content, "og:description": answer.content,
'og:site_name': APP_CONFIG['site_name'] "og:site_name": APP_CONFIG["site_name"]
}) })
end end
def full_profile_picture_url(user) def full_profile_picture_url(user)
# @type [String] # @type [String]
profile_picture_url = user.profile_picture.url(:large) profile_picture_url = user.profile_picture.url(:large)
if profile_picture_url.start_with? '/' if profile_picture_url.start_with? "/"
"#{root_url}#{profile_picture_url[1..-1]}" "#{root_url}#{profile_picture_url[1..]}"
else else
profile_picture_url profile_picture_url
end end
end end
end end

View File

@ -1,28 +1,26 @@
# frozen_string_literal: true
module ApplicationHelper::TitleMethods module ApplicationHelper::TitleMethods
include MarkdownHelper include MarkdownHelper
include UserHelper include UserHelper
def generate_title(name, junction = nil, content = nil, s = false) def generate_title(name, junction = nil, content = nil, possesive = false)
if s if possesive
if name[-1].downcase != "s" name = if name[-1].downcase == "s"
name = name + "'s" "#{name}'"
else else
name = name + "'" "#{name}'s"
end end
end end
list = [name] list = [name, junction].compact
list.push junction unless junction.nil?
unless content.nil? unless content.nil?
content = strip_markdown(content) content = strip_markdown(content)
if content.length > 45 content = "#{content[0..42]}" if content.length > 45
content = content[0..42] + ""
end
list.push content list.push content
end end
list.push "|", APP_CONFIG['site_name'] list.push "|", APP_CONFIG["site_name"]
list.join " " list.join " "
end end
@ -61,4 +59,4 @@ module ApplicationHelper::TitleMethods
def list_title(list) def list_title(list)
generate_title list.name generate_title list.name
end end
end end