Merge pull request #3385 from tancnle/each-key-refactor

Replace Hash#keys.each with Hash#each_key for some perf boost
This commit is contained in:
Sam 2015-04-20 06:17:27 +10:00
commit 72ab1b9714
12 changed files with 15 additions and 15 deletions

View file

@ -436,7 +436,7 @@ class PostsController < ApplicationController
result[:is_warning] = false
end
PostRevisor.tracked_topic_fields.keys.each do |f|
PostRevisor.tracked_topic_fields.each_key do |f|
params.permit(f => [])
result[f] = params[f] if params.has_key?(f)
end

View file

@ -128,7 +128,7 @@ class TopicsController < ApplicationController
guardian.ensure_can_edit!(topic)
changes = {}
PostRevisor.tracked_topic_fields.keys.each do |f|
PostRevisor.tracked_topic_fields.each_key do |f|
changes[f] = params[f] if params.has_key?(f)
end

View file

@ -19,7 +19,7 @@ class DirectoryItem < ActiveRecord::Base
def self.refresh!
ActiveRecord::Base.transaction do
exec_sql "TRUNCATE TABLE directory_items"
period_types.keys.each {|p| refresh_period!(p)}
period_types.each_key {|p| refresh_period!(p)}
end
end

View file

@ -151,7 +151,7 @@ class Group < ActiveRecord::Base
end
def self.ensure_automatic_groups!
AUTO_GROUPS.keys.each do |name|
AUTO_GROUPS.each_key do |name|
refresh_automatic_group!(name) unless lookup_group(name)
end
end

View file

@ -36,7 +36,7 @@ class IncomingLinksReport
num_clicks = link_count_per_user
num_topics = topic_count_per_user
report.data = []
num_clicks.keys.each do |username|
num_clicks.each_key do |username|
report.data << {username: username, num_clicks: num_clicks[username], num_topics: num_topics[username]}
end
report.data = report.data.sort_by {|x| x[:num_clicks]}.reverse[0,10]
@ -67,7 +67,7 @@ class IncomingLinksReport
num_clicks = link_count_per_domain
num_topics = topic_count_per_domain(num_clicks.keys)
report.data = []
num_clicks.keys.each do |domain|
num_clicks.each_key do |domain|
report.data << {domain: domain, num_clicks: num_clicks[domain], num_topics: num_topics[domain]}
end
report.data = report.data.sort_by {|x| x[:num_clicks]}.reverse[0,10]

View file

@ -221,7 +221,7 @@ class Post < ActiveRecord::Base
TopicLink.where(domain: hosts.keys, user_id: acting_user.id)
.group(:domain, :post_id)
.count.keys.each do |tuple|
.count.each_key do |tuple|
domain = tuple[0]
hosts[domain] = (hosts[domain] || 0) + 1
end

View file

@ -613,7 +613,7 @@ class Topic < ActiveRecord::Base
end
def update_action_counts
PostActionType.types.keys.each do |type|
PostActionType.types.each_key do |type|
count_field = "#{type}_count"
update_column(count_field, Post.where(topic_id: id).sum(count_field))
end

View file

@ -178,7 +178,7 @@ class PostRevisionSerializer < ApplicationSerializer
}
# Retrieve any `tracked_topic_fields`
PostRevisor.tracked_topic_fields.keys.each do |field|
PostRevisor.tracked_topic_fields.each_key do |field|
if topic.respond_to?(field)
latest_modifications[field.to_s] = [topic.send(field)]
end
@ -198,7 +198,7 @@ class PostRevisionSerializer < ApplicationSerializer
revision[:revision] = pr.number
revision[:hidden] = pr.hidden
pr.modifications.keys.each do |field|
pr.modifications.each_key do |field|
revision[field] = pr.modifications[field][0]
end
@ -210,7 +210,7 @@ class PostRevisionSerializer < ApplicationSerializer
cur = @all_revisions[r]
prev = @all_revisions[r - 1]
cur.keys.each do |field|
cur.each_key do |field|
prev[field] = prev.has_key?(field) ? prev[field] : cur[field]
end
end

View file

@ -300,7 +300,7 @@ class PostRevisor
return unless revision = PostRevision.find_by(post_id: @post.id, number: @post.version)
revision.user_id = @post.last_editor_id
modifications = post_changes.merge(@topic_changes.diff)
modifications.keys.each do |field|
modifications.each_key do |field|
if revision.modifications.has_key?(field)
old_value = revision.modifications[field][0]
new_value = modifications[field][1]

View file

@ -33,7 +33,7 @@ class ScoreCalculator
def update_posts_score(min_topic_age)
components = []
@weightings.keys.each { |k| components << "COALESCE(#{k}, 0) * :#{k}" }
@weightings.each_key { |k| components << "COALESCE(#{k}, 0) * :#{k}" }
components = components.join(" + ")
builder = SqlBuilder.new(

View file

@ -16,7 +16,7 @@ class SiteSettings::YamlLoader
def load
yaml = YAML.load_file(@file)
yaml.keys.each do |category|
yaml.each_key do |category|
yaml[category].each do |setting_name, hash|
if hash.is_a?(Hash)
# Get default value for the site setting:

View file

@ -16,7 +16,7 @@ describe "i18n integrity checks" do
end
it "needs an i18n key (notification_types) for each Notification type" do
Notification.types.keys.each do |type|
Notification.types.each_key do |type|
I18n.t("notification_types.#{type}").should_not =~ /translation missing/
end
end