Skip to content

Commit

Permalink
DEV: stop freezing frozen strings
Browse files Browse the repository at this point in the history
We have the `# frozen_string_literal: true` comment on all our
files. This means all string literals are frozen. There is no need
to call #freeze on any literals.

For files with `# frozen_string_literal: true`

```
puts %w{a b}[0].frozen?
=> true

puts "hi".frozen?
=> true

puts "a #{1} b".frozen?
=> true

puts ("a " + "b").frozen?
=> false

puts (-("a " + "b")).frozen?
=> true
```

For more details see: https://samsaffron.com/archive/2018/02/16/reducing-string-duplication-in-ruby
  • Loading branch information
SamSaffron committed Apr 30, 2020
1 parent 02ef880 commit d0d5a13
Show file tree
Hide file tree
Showing 51 changed files with 98 additions and 98 deletions.
14 changes: 7 additions & 7 deletions app/controllers/categories_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def index
parent_category = Category.find_by_slug(params[:parent_category_id]) || Category.find_by(id: params[:parent_category_id].to_i)

category_options = {
is_homepage: current_homepage == "categories".freeze,
is_homepage: current_homepage == "categories",
parent_category_id: params[:parent_category_id],
include_topics: include_topics(parent_category)
}
Expand Down Expand Up @@ -53,10 +53,10 @@ def index
no_definitions: true
}

if style == "categories_and_latest_topics".freeze
if style == "categories_and_latest_topics"
@topic_list = TopicQuery.new(current_user, topic_options).list_latest
@topic_list.more_topics_url = url_for(public_send("latest_path"))
elsif style == "categories_and_top_topics".freeze
elsif style == "categories_and_top_topics"
@topic_list = TopicQuery.new(nil, topic_options).list_top_for(SiteSetting.top_page_default_timeframe.to_sym)
@topic_list.more_topics_url = url_for(public_send("top_path"))
end
Expand Down Expand Up @@ -245,7 +245,7 @@ def categories_and_topics(topics_filter)
discourse_expires_in 1.minute

category_options = {
is_homepage: current_homepage == "categories".freeze,
is_homepage: current_homepage == "categories",
parent_category_id: params[:parent_category_id],
include_topics: false
}
Expand Down Expand Up @@ -354,8 +354,8 @@ def include_topics(parent_category = nil)
view_context.mobile_view? ||
params[:include_topics] ||
(parent_category && parent_category.subcategory_list_includes_topics?) ||
style == "categories_with_featured_topics".freeze ||
style == "categories_boxes_with_topics".freeze ||
style == "categories_with_top_topics".freeze
style == "categories_with_featured_topics" ||
style == "categories_boxes_with_topics" ||
style == "categories_with_top_topics"
end
end
16 changes: 8 additions & 8 deletions app/controllers/webhooks_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@ def sendgrid
events.each do |event|
message_id = (event["smtp-id"] || "").tr("<>", "")
to_address = event["email"]
if event["event"] == "bounce".freeze
if event["event"] == "bounce"
if event["status"]["4."]
process_bounce(message_id, to_address, SiteSetting.soft_bounce_score)
else
process_bounce(message_id, to_address, SiteSetting.hard_bounce_score)
end
elsif event["event"] == "dropped".freeze
elsif event["event"] == "dropped"
process_bounce(message_id, to_address, SiteSetting.hard_bounce_score)
end
end
Expand All @@ -34,7 +34,7 @@ def mailjet
events.each do |event|
message_id = event["CustomID"]
to_address = event["email"]
if event["event"] == "bounce".freeze
if event["event"] == "bounce"
if event["hard_bounce"]
process_bounce(message_id, to_address, SiteSetting.hard_bounce_score)
else
Expand Down Expand Up @@ -156,9 +156,9 @@ def handle_mailgun_legacy(params)
# only handle soft bounces, because hard bounces are also handled
# by the "dropped" event and we don't want to increase bounce score twice
# for the same message
if event == "bounced".freeze && params["error"]["4."]
if event == "bounced" && params["error"]["4."]
process_bounce(message_id, to_address, SiteSetting.soft_bounce_score)
elsif event == "dropped".freeze
elsif event == "dropped"
process_bounce(message_id, to_address, SiteSetting.hard_bounce_score)
end

Expand All @@ -174,10 +174,10 @@ def handle_mailgun_new(params)
to_address = data["recipient"]
severity = data["severity"]

if data["event"] == "failed".freeze
if severity == "temporary".freeze
if data["event"] == "failed"
if severity == "temporary"
process_bounce(message_id, to_address, SiteSetting.soft_bounce_score)
elsif severity == "permanent".freeze
elsif severity == "permanent"
process_bounce(message_id, to_address, SiteSetting.hard_bounce_score)
end
end
Expand Down
4 changes: 2 additions & 2 deletions app/helpers/application_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def google_tag_manager_json
end

def shared_session_key
if SiteSetting.long_polling_base_url != '/'.freeze && current_user
if SiteSetting.long_polling_base_url != '/' && current_user
sk = "shared_session_key"
return request.env[sk] if request.env[sk]

Expand Down Expand Up @@ -282,7 +282,7 @@ def render_sitelinks_search_tag
'query-input' => 'required name=search_term_string',
}
}
content_tag(:script, MultiJson.dump(json).html_safe, type: 'application/ld+json'.freeze)
content_tag(:script, MultiJson.dump(json).html_safe, type: 'application/ld+json')
end

def gsub_emoji_to_unicode(str)
Expand Down
4 changes: 2 additions & 2 deletions app/jobs/regular/bulk_user_title_update.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

module Jobs
class BulkUserTitleUpdate < ::Jobs::Base
UPDATE_ACTION = 'update'.freeze
RESET_ACTION = 'reset'.freeze
UPDATE_ACTION = 'update'
RESET_ACTION = 'reset'

def execute(args)
new_title = args[:new_title]
Expand Down
4 changes: 2 additions & 2 deletions app/jobs/regular/emit_web_hook_event.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

module Jobs
class EmitWebHookEvent < ::Jobs::Base
PING_EVENT = 'ping'.freeze
MAX_RETRY_COUNT = 4.freeze
PING_EVENT = 'ping'
MAX_RETRY_COUNT = 4
RETRY_BACKOFF = 5

def execute(args)
Expand Down
2 changes: 1 addition & 1 deletion app/jobs/scheduled/bookmark_reminder_notifications.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ module Jobs
# Any leftovers will be caught in the next run, because the reminder_at column
# is set to NULL once a reminder has been sent.
class BookmarkReminderNotifications < ::Jobs::Scheduled
JOB_RUN_NUMBER_KEY ||= 'jobs_bookmark_reminder_notifications_job_run_num'.freeze
JOB_RUN_NUMBER_KEY ||= 'jobs_bookmark_reminder_notifications_job_run_num'
AT_DESKTOP_CONSISTENCY_RUN_NUMBER ||= 6

every 5.minutes
Expand Down
2 changes: 1 addition & 1 deletion app/jobs/scheduled/pending_queued_posts_reminder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def last_notified_id=(arg)
end

def self.last_notified_key
"last_notified_queued_post_id".freeze
"last_notified_queued_post_id"
end
end
end
2 changes: 1 addition & 1 deletion app/jobs/scheduled/pending_reviewables_reminder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def self.last_notified_id=(arg)
end

def self.last_notified_key
"last_notified_reviewable_id".freeze
"last_notified_reviewable_id"
end

def self.clear_key
Expand Down
2 changes: 1 addition & 1 deletion app/jobs/scheduled/pending_users_reminder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ def previous_newest_username=(username)
end

def previous_newest_username_cache_key
"pending-users-reminder:newest-username".freeze
"pending-users-reminder:newest-username"
end

end
Expand Down
4 changes: 2 additions & 2 deletions app/jobs/scheduled/poll_mailbox.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def process_popmail(popmail)
Email::Processor.process!(popmail.pop)
end

POLL_MAILBOX_TIMEOUT_ERROR_KEY ||= "poll_mailbox_timeout_error_key".freeze
POLL_MAILBOX_TIMEOUT_ERROR_KEY ||= "poll_mailbox_timeout_error_key"

def poll_pop3
pop3 = Net::POP3.new(SiteSetting.pop3_polling_host, SiteSetting.pop3_polling_port)
Expand Down Expand Up @@ -62,7 +62,7 @@ def poll_pop3
Discourse.handle_job_exception(e, error_context(@args, "Signing in to poll incoming emails."))
end

POLL_MAILBOX_ERRORS_KEY ||= "poll_mailbox_errors".freeze
POLL_MAILBOX_ERRORS_KEY ||= "poll_mailbox_errors"

def self.errors_in_past_24_hours
Discourse.redis.zremrangebyscore(POLL_MAILBOX_ERRORS_KEY, 0, 24.hours.ago.to_i)
Expand Down
2 changes: 1 addition & 1 deletion app/models/category_featured_topic.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ class CategoryFeaturedTopic < ActiveRecord::Base
belongs_to :category
belongs_to :topic

NEXT_CATEGORY_ID_KEY = 'category-featured-topic:next-category-id'.freeze
NEXT_CATEGORY_ID_KEY = 'category-featured-topic:next-category-id'
DEFAULT_BATCH_SIZE = 100

# Populates the category featured topics.
Expand Down
2 changes: 1 addition & 1 deletion app/models/category_list.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def initialize(guardian = nil, options = {})
end

def preload_key
"categories_list".freeze
"categories_list"
end

def self.order_categories(categories)
Expand Down
2 changes: 1 addition & 1 deletion app/models/concerns/category_hashtag.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
module CategoryHashtag
extend ActiveSupport::Concern

SEPARATOR = ":".freeze
SEPARATOR = ":"

class_methods do
def query_from_hashtag_slug(category_slug)
Expand Down
6 changes: 3 additions & 3 deletions app/models/emoji.rb
Original file line number Diff line number Diff line change
Expand Up @@ -140,9 +140,9 @@ def self.base_url

def self.replacement_code(code)
code
.split('-'.freeze)
.split('-')
.map!(&:hex)
.pack("U*".freeze)
.pack("U*")
end

def self.unicode_replacements
Expand All @@ -166,7 +166,7 @@ def self.unicode_replacements
replacements[code] = name
if is_tonable_emojis.include?(name)
fitzpatrick_scales.each_with_index do |scale, index|
toned_code = code.codepoints.insert(1, scale).pack("U*".freeze)
toned_code = code.codepoints.insert(1, scale).pack("U*")
replacements[toned_code] = "#{name}:t#{index + 2}"
end
end
Expand Down
2 changes: 1 addition & 1 deletion app/models/topic.rb
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ def recover!(recovered_by = nil)
end

after_save do
banner = "banner".freeze
banner = "banner"

if archetype_before_last_save == banner || archetype == banner
ApplicationController.banner_json_cache.clear
Expand Down
2 changes: 1 addition & 1 deletion app/models/topic_link.rb
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ def self.extract_from(post)
uri = UrlHelper.relaxed_parse(u.url)
[u, uri]
end
.reject { |_, p| p.nil? || "mailto".freeze == p.scheme }
.reject { |_, p| p.nil? || "mailto" == p.scheme }
.uniq { |_, p| p }
.each do |link, parsed|

Expand Down
6 changes: 3 additions & 3 deletions app/models/topic_tracking_state.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ class TopicTrackingState
include ActiveModel::SerializerSupport

CHANNEL = "/user-tracking"
UNREAD_MESSAGE_TYPE = "unread".freeze
LATEST_MESSAGE_TYPE = "latest".freeze
MUTED_MESSAGE_TYPE = "muted".freeze
UNREAD_MESSAGE_TYPE = "unread"
LATEST_MESSAGE_TYPE = "latest"
MUTED_MESSAGE_TYPE = "muted"

attr_accessor :user_id,
:topic_id,
Expand Down
2 changes: 1 addition & 1 deletion app/models/upload.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class Upload < ActiveRecord::Base
SHA1_LENGTH = 40
SEEDED_ID_THRESHOLD = 0
URL_REGEX ||= /(\/original\/\dX[\/\.\w]*\/([a-zA-Z0-9]+)[\.\w]*)/
SECURE_MEDIA_ROUTE = "secure-media-uploads".freeze
SECURE_MEDIA_ROUTE = "secure-media-uploads"

belongs_to :user
belongs_to :access_control_post, class_name: 'Post'
Expand Down
4 changes: 2 additions & 2 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,7 @@ def effective_locale
end

EMAIL = %r{([^@]+)@([^\.]+)}
FROM_STAGED = "from_staged".freeze
FROM_STAGED = "from_staged"

def self.new_from_params(params)
user = User.new
Expand Down Expand Up @@ -598,7 +598,7 @@ def publish_notifications_state
notification = notifications.visible.order('notifications.created_at desc').first
json = NotificationSerializer.new(notification).as_json if notification

sql = (<<~SQL).freeze
sql = (<<~SQL)
SELECT * FROM (
SELECT n.id, n.read FROM notifications n
LEFT JOIN topics t ON n.topic_id = t.id
Expand Down
2 changes: 1 addition & 1 deletion app/serializers/incoming_email_details_serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def initialize(incoming_email, opts)
@mail = Mail.new(incoming_email.raw)
end

EMAIL_RECEIVER_ERROR_PREFIX = "Email::Receiver::".freeze
EMAIL_RECEIVER_ERROR_PREFIX = "Email::Receiver::"

def error
@error_string.presence || I18n.t("emails.incoming.unrecognized_error")
Expand Down
2 changes: 1 addition & 1 deletion app/services/badge_granter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ def self.find_by_type(type)
end

def self.queue_key
"badge_queue".freeze
"badge_queue"
end

# Options:
Expand Down
12 changes: 6 additions & 6 deletions config/initializers/100-silence_logger.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# frozen_string_literal: true

class SilenceLogger < Rails::Rack::Logger
PATH_INFO = 'PATH_INFO'.freeze
HTTP_X_SILENCE_LOGGER = 'HTTP_X_SILENCE_LOGGER'.freeze
PATH_INFO = 'PATH_INFO'
HTTP_X_SILENCE_LOGGER = 'HTTP_X_SILENCE_LOGGER'

def initialize(app, opts = {})
@app = app
Expand Down Expand Up @@ -36,9 +36,9 @@ def call(env)
end

silenced = [
"/mini-profiler-resources/results".freeze,
"/mini-profiler-resources/includes.js".freeze,
"/mini-profiler-resources/includes.css".freeze,
"/mini-profiler-resources/jquery.tmpl.js".freeze
"/mini-profiler-resources/results",
"/mini-profiler-resources/includes.js",
"/mini-profiler-resources/includes.css",
"/mini-profiler-resources/jquery.tmpl.js"
]
Rails.configuration.middleware.swap Rails::Rack::Logger, SilenceLogger, silenced: silenced
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class PostgreSQLFallbackHandler
attr_reader :masters_down
attr_accessor :initialized

DATABASE_DOWN_CHANNEL = '/global/database_down'.freeze
DATABASE_DOWN_CHANNEL = '/global/database_down'

def initialize
@masters_down = DistributedCache.new('masters_down', namespace: false)
Expand Down
2 changes: 1 addition & 1 deletion lib/badge_posts_view_manager.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# frozen_string_literal: true

class BadgePostsViewManager
VIEW_NAME = "badge_posts".freeze
VIEW_NAME = "badge_posts"

def self.create!
sql = <<~SQL
Expand Down
2 changes: 1 addition & 1 deletion lib/base62.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# Modified version of: https://github.com/steventen/base62-rb

module Base62
KEYS ||= "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ".freeze
KEYS ||= "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
KEYS_HASH ||= KEYS.each_char.with_index.to_h
BASE ||= KEYS.length

Expand Down
2 changes: 1 addition & 1 deletion lib/bookmark_reminder_notification_handler.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# frozen_string_literal: true

class BookmarkReminderNotificationHandler
PENDING_AT_DESKTOP_KEY_PREFIX ||= 'pending_at_desktop_bookmark_reminder_user_'.freeze
PENDING_AT_DESKTOP_KEY_PREFIX ||= 'pending_at_desktop_bookmark_reminder_user_'
PENDING_AT_DESKTOP_EXPIRY_DAYS ||= 20

def self.send_notification(bookmark)
Expand Down
4 changes: 2 additions & 2 deletions lib/discourse.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def self.execute_command(*command, **args)
end

def self.pretty_logs(logs)
logs.join("\n".freeze)
logs.join("\n")
end

def self.atomic_write_file(destination, contents)
Expand Down Expand Up @@ -811,7 +811,7 @@ def self.deprecate(warning, drop_from: nil, since: nil, raise_error: false, outp
warning
end

SIDEKIQ_NAMESPACE ||= 'sidekiq'.freeze
SIDEKIQ_NAMESPACE ||= 'sidekiq'

def self.sidekiq_redis_config
conf = GlobalSetting.redis_config.dup
Expand Down
Loading

0 comments on commit d0d5a13

Please sign in to comment.