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
include ApplicationHelper::GraphMethods
include ApplicationHelper::TitleMethods
def inbox_count
return 0 unless user_signed_in?
count = Inbox.select("COUNT(id) AS count")
.where(new: true)
.where(user_id: current_user.id)
@ -11,31 +14,35 @@ module ApplicationHelper
.order(:count)
.first
return nil if count.nil?
return nil unless count.count > 0
return nil unless count.count.positive?
count.count
end
def notification_count
return 0 unless user_signed_in?
count = Notification.for(current_user).where(new: true)
return nil if count.nil?
return nil unless count.count > 0
return nil unless count.count.positive?
count.count
end
def privileged?(user)
((!current_user.nil?) && ((current_user == user) || current_user.mod?)) ? true : false
!current_user.nil? && ((current_user == user) || current_user.mod?)
end
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
# 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
end
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

View file

@ -1,59 +1,61 @@
# frozen_string_literal: true
module ApplicationHelper::GraphMethods
# Creates <meta> 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
safe_join(values.map { |name, content| tag.meta(property: name, content: content) }, "\n")
end
# Creates <meta> 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
safe_join(values.map { |name, content| tag.meta(name: name, content: content) }, "\n")
end
# @param user [User]
def user_opengraph(user)
opengraph_meta_tags({
'og:title': user.profile.safe_name,
'og:type': 'profile',
'og:image': full_profile_picture_url(user),
'og:url': show_user_profile_url(user.screen_name),
'og:description': user.profile.description,
'og:site_name': APP_CONFIG['site_name'],
'profile:username': user.screen_name
"og:title": user.profile.safe_name,
"og:type": "profile",
"og:image": full_profile_picture_url(user),
"og:url": show_user_profile_url(user.screen_name),
"og:description": user.profile.description,
"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.profile.motivation_header.presence || "Ask me anything!",
'twitter:description': "Ask #{user.profile.safe_name} anything on Retrospring",
'twitter:image': full_profile_picture_url(user)
"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)
})
end
# @param answer [Answer]
def answer_opengraph(answer)
opengraph_meta_tags({
'og:title': "#{answer.user.profile.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']
"og:title": "#{answer.user.profile.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]}"
if profile_picture_url.start_with? "/"
"#{root_url}#{profile_picture_url[1..]}"
else
profile_picture_url
end
end
end
end

View file

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