Skip to content

Commit

Permalink
Fix rubocop nits in helper code and specs
Browse files Browse the repository at this point in the history
  • Loading branch information
pixeldesu committed Oct 18, 2023
1 parent 29a6a66 commit 71894d6
Show file tree
Hide file tree
Showing 21 changed files with 214 additions and 188 deletions.
2 changes: 2 additions & 0 deletions app/helpers/ajax_helper.rb
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
# frozen_string_literal: true

module AjaxHelper
end
10 changes: 5 additions & 5 deletions app/helpers/application_helper/graph_methods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ 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: content) }, "\n")
safe_join(values.map { |name, content| tag.meta(property: name, content:) }, "\n")
end

# Creates <meta> tags from a hash
# @param values [Hash]
def meta_tags(values)
safe_join(values.map { |name, content| tag.meta(name: name, content: content) }, "\n")
safe_join(values.map { |name, content| tag.meta(name:, content:) }, "\n")
end

# @param user [User]
Expand All @@ -22,7 +22,7 @@ def user_opengraph(user)
"og:url": user_url(user),
"og:description": user.profile.description,
"og:site_name": APP_CONFIG["site_name"],
"profile:username": user.screen_name
"profile:username": user.screen_name,
})
end

Expand All @@ -33,7 +33,7 @@ def user_twitter_card(user)
"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:image": full_profile_picture_url(user),
})
end

Expand All @@ -45,7 +45,7 @@ def answer_opengraph(answer)
"og:image": full_profile_picture_url(answer.user),
"og:url": answer_url(answer.user.screen_name, answer.id),
"og:description": answer.content,
"og:site_name": APP_CONFIG["site_name"]
"og:site_name": APP_CONFIG["site_name"],
})
end

Expand Down
2 changes: 1 addition & 1 deletion app/helpers/application_helper/title_methods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def generate_title(name, junction = nil, content = nil, possessive = false)
def question_title(question)
context_user = question.answers&.first&.user if question.direct
name = user_screen_name question.user,
context_user: context_user,
context_user:,
author_identifier: question.author_is_anonymous ? question.author_identifier : nil,
url: false
generate_title name, "asked", question.content
Expand Down
21 changes: 13 additions & 8 deletions app/helpers/bootstrap_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,24 +25,24 @@ def nav_entry(body, path, options = {})
"#{content_tag(:i, '', class: "fa fa-#{options[:icon]}")} #{body}"
end
end
if options[:badge].present? || options.dig(:badge_attr, :data)&.has_key?(:controller)
if options[:badge].present? || options.dig(:badge_attr, :data)&.key?(:controller)
badge_class = [
"badge",
("badge-#{options[:badge_color]}" unless options[:badge_color].nil?),
("badge-pill" if options[:badge_pill])
].compact.join(" ")

body += " #{content_tag(:span, options[:badge], class: badge_class, **options[:badge_attr])}".html_safe
body += " #{content_tag(:span, options[:badge], class: badge_class, **options[:badge_attr])}".html_safe # rubocop:disable Rails/OutputSafety
end

content_tag(:li, link_to(body.html_safe, path, class: "nav-link", data: { hotkey: options[:hotkey] }), class: classes, id: options[:id])
content_tag(:li, link_to(body.html_safe, path, class: "nav-link", data: { hotkey: options[:hotkey] }), class: classes, id: options[:id]) # rubocop:disable Rails/OutputSafety
end

def list_group_item(body, path, options = {})
options = {
badge: nil,
badge_color: nil,
class: ""
class: "",
}.merge(options)

classes = [
Expand All @@ -54,13 +54,18 @@ def list_group_item(body, path, options = {})

unless options[:badge].nil? || (options[:badge]).zero?
# TODO: make this prettier?
body << " #{
content_tag(:span, options[:badge], class: "badge#{
badge = content_tag(:span, options[:badge], class: "badge#{
" badge-#{options[:badge_color]}" unless options[:badge_color].nil?
}")}"
}",)
end

content_tag(:a, body.html_safe, href: path, class: classes)
html = if badge
"#{body} #{badge}"
else
body
end

content_tag(:a, html.html_safe, href: path, class: classes) # rubocop:disable Rails/OutputSafety
end

def tooltip(body, tooltip_content, placement = "bottom")
Expand Down
2 changes: 1 addition & 1 deletion app/helpers/feedback_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ def canny_token
avatarURL: current_user.profile_picture.url(:large),
name: current_user.screen_name,
id: current_user.id,
email: current_user.email
email: current_user.email,
}

JWT.encode(user_data, APP_CONFIG.dig("canny", "sso"))
Expand Down
2 changes: 1 addition & 1 deletion app/helpers/markdown_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def question_markdown(content)
def raw_markdown(content)
renderer = Redcarpet::Render::HTML.new(**MARKDOWN_RENDERER_OPTS)
md = Redcarpet::Markdown.new(renderer, **MARKDOWN_OPTS)
raw md.render content
raw md.render content # rubocop:disable Rails/OutputSafety
end

def get_markdown(path, relative_to = Rails.root)
Expand Down
2 changes: 1 addition & 1 deletion app/helpers/social_helper/telegram_methods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def telegram_share_url(answer)
id: answer.id,
username: answer.user.screen_name,
host: APP_CONFIG["hostname"],
protocol: (APP_CONFIG["https"] ? :https : :http)
protocol: (APP_CONFIG["https"] ? :https : :http),
)

%(https://t.me/share/url?url=#{CGI.escape(url)}&text=#{CGI.escape(telegram_text(answer))})
Expand Down
18 changes: 10 additions & 8 deletions app/helpers/social_helper/tumblr_methods.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
require 'cgi'
# frozen_string_literal: true

require "cgi"

module SocialHelper::TumblrMethods
def tumblr_title(answer)
Expand All @@ -13,21 +15,21 @@ def tumblr_title(answer)

def tumblr_body(answer)
answer_url = answer_url(
id: answer.id,
username: answer.user.screen_name,
host: APP_CONFIG['hostname'],
protocol: (APP_CONFIG['https'] ? :https : :http)
id: answer.id,
username: answer.user.screen_name,
host: APP_CONFIG["hostname"],
protocol: (APP_CONFIG["https"] ? :https : :http),
)

"#{answer.content}\n\n[Smile or comment on the answer here](#{answer_url})"
end

def tumblr_share_url(answer)
answer_url = answer_url(
id: answer.id,
id: answer.id,
username: answer.user.screen_name,
host: APP_CONFIG['hostname'],
protocol: (APP_CONFIG['https'] ? :https : :http)
host: APP_CONFIG["hostname"],
protocol: (APP_CONFIG["https"] ? :https : :http),
)

"https://www.tumblr.com/widgets/share/tool?shareSource=legacy&posttype=text&title=#{CGI.escape(tumblr_title(answer))}&url=#{CGI.escape(answer_url)}&caption=&content=#{CGI.escape(tumblr_body(answer))}"
Expand Down
20 changes: 11 additions & 9 deletions app/helpers/social_helper/twitter_methods.rb
Original file line number Diff line number Diff line change
@@ -1,36 +1,38 @@
require 'cgi'
# frozen_string_literal: true

require "cgi"

module SocialHelper::TwitterMethods
include MarkdownHelper

def prepare_tweet(answer, post_tag = nil)
question_content = twitter_markdown answer.question.content.gsub(/\@(\w+)/, '\1')
question_content = twitter_markdown answer.question.content.gsub(/@(\w+)/, '\1')
original_question_length = question_content.length
answer_content = twitter_markdown answer.content
original_answer_length = answer_content.length
answer_url = answer_url(
id: answer.id,
id: answer.id,
username: answer.user.screen_name,
host: APP_CONFIG['hostname'],
protocol: (APP_CONFIG['https'] ? :https : :http)
host: APP_CONFIG["hostname"],
protocol: (APP_CONFIG["https"] ? :https : :http),
)

parsed_tweet = { :valid => false }
parsed_tweet = { valid: false }
tweet_text = ""

until parsed_tweet[:valid]
shortened_question = "#{question_content[0..122]}#{'…' if original_question_length > [123, question_content.length].min}"
shortened_answer = "#{answer_content[0..123]}#{'…' if original_answer_length > [124, answer_content.length].min}"
components = [
shortened_question,
'—',
"—",
shortened_answer,
post_tag,
answer_url
]
tweet_text = components.compact.join(' ')
tweet_text = components.compact.join(" ")

parsed_tweet = Twitter::TwitterText::Validation::parse_tweet(tweet_text)
parsed_tweet = Twitter::TwitterText::Validation.parse_tweet(tweet_text)

question_content = question_content[0..-2]
answer_content = answer_content[0..-2]
Expand Down
2 changes: 1 addition & 1 deletion app/helpers/theme_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ module ThemeHelper
"input_color" => "input-bg",
"input_text" => "input-text",
"input_placeholder" => "input-placeholder",
"muted_text" => "muted-text"
"muted_text" => "muted-text",
}.freeze

def render_theme
Expand Down
70 changes: 40 additions & 30 deletions spec/helpers/application_helper/graph_methods_spec.rb
Original file line number Diff line number Diff line change
@@ -1,76 +1,86 @@
# frozen_string_literal: true

require 'rails_helper'
require "rails_helper"

describe ApplicationHelper::GraphMethods, :type => :helper do
describe ApplicationHelper::GraphMethods, type: :helper do
describe "#user_opengraph" do
context "sample user" do
let(:user) { FactoryBot.create(:user,
profile: { display_name: 'Cunes',
description: 'A bunch of raccoons in a trenchcoat.' },
screen_name: 'raccoons') }
let(:user) do
FactoryBot.create(:user,
profile: { display_name: "Cunes",
description: "A bunch of raccoons in a trenchcoat.", },
screen_name: "raccoons",)
end

subject { user_opengraph(user) }

it 'should generate a matching OpenGraph structure for a user' do
allow(APP_CONFIG).to receive(:[]).with('site_name').and_return('pineapplespring')
expect(subject).to eq(<<~EOS.chomp)
it "should generate a matching OpenGraph structure for a user" do
allow(APP_CONFIG).to receive(:[]).with("site_name").and_return("pineapplespring")
expect(subject).to eq(<<~META.chomp)
<meta property="og:title" content="Cunes">
<meta property="og:type" content="profile">
<meta property="og:image" content="http://test.host/images/large/no_avatar.png">
<meta property="og:url" content="http://test.host/@raccoons">
<meta property="og:description" content="A bunch of raccoons in a trenchcoat.">
<meta property="og:site_name" content="pineapplespring">
<meta property="profile:username" content="raccoons">
EOS
META
end
end
end

describe "#user_twitter_card" do
context "sample user" do
let(:user) { FactoryBot.create(:user,
profile: {
display_name: '',
description: 'A bunch of raccoons in a trenchcoat.'},
screen_name: 'raccoons') }
let(:user) do
FactoryBot.create(:user,
profile: {
display_name: "",
description: "A bunch of raccoons in a trenchcoat.",
},
screen_name: "raccoons",)
end

subject { user_twitter_card(user) }
it 'should generate a matching OpenGraph structure for a user' do
expect(subject).to eq(<<~EOS.chomp)
it "should generate a matching OpenGraph structure for a user" do
expect(subject).to eq(<<~META.chomp)
<meta name="twitter:card" content="summary">
<meta name="twitter:site" content="@retrospring">
<meta name="twitter:title" content="Ask me anything!">
<meta name="twitter:description" content="Ask raccoons anything on Retrospring">
<meta name="twitter:image" content="http://test.host/images/large/no_avatar.png">
EOS
META
end
end
end

describe "#answer_opengraph" do
context "sample user and answer" do
let!(:user) { FactoryBot.create(:user,
profile: {
display_name: '',
description: 'A bunch of raccoons in a trenchcoat.'},
screen_name: 'raccoons') }
let(:answer) { FactoryBot.create(:answer,
user_id: user.id,) }
let!(:user) do
FactoryBot.create(:user,
profile: {
display_name: "",
description: "A bunch of raccoons in a trenchcoat.",
},
screen_name: "raccoons",)
end
let(:answer) do
FactoryBot.create(:answer,
user_id: user.id,)
end

subject { answer_opengraph(answer) }

it 'should generate a matching OpenGraph structure for a user' do
allow(APP_CONFIG).to receive(:[]).with('site_name').and_return('pineapplespring')
expect(subject).to eq(<<~EOS.chomp)
it "should generate a matching OpenGraph structure for a user" do
allow(APP_CONFIG).to receive(:[]).with("site_name").and_return("pineapplespring")
expect(subject).to eq(<<~META.chomp)
<meta property="og:title" content="raccoons answered: #{answer.question.content}">
<meta property="og:type" content="article">
<meta property="og:image" content="http://test.host/images/large/no_avatar.png">
<meta property="og:url" content="http://test.host/@raccoons/a/#{answer.id}">
<meta property="og:description" content="#{answer.content}">
<meta property="og:site_name" content="pineapplespring">
EOS
META
end
end
end
end
end
10 changes: 5 additions & 5 deletions spec/helpers/application_helper/title_methods_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
"anonymous_name" => "Anonymous",
"https" => true,
"items_per_page" => 5,
"sharing" => {}
})
"sharing" => {},
},)

user.profile.display_name = "Cool Man"
user.profile.save!
Expand Down Expand Up @@ -42,7 +42,7 @@

context "user has custom anonymous display name" do
before do
FactoryBot.create(:answer, question: question, user: user)
FactoryBot.create(:answer, question:, user:)
user.profile.anon_display_name = "Amogus"
user.profile.save!
end
Expand All @@ -55,9 +55,9 @@

describe "#answer_title" do
let(:answer) do
FactoryBot.create(:answer, user: user,
FactoryBot.create(:answer, user:,
content: "a",
question_content: "q")
question_content: "q",)
end

it "should generate a proper title" do
Expand Down
Loading

0 comments on commit 71894d6

Please sign in to comment.