diff options
author | Eugen Rochko <eugen@zeonfederated.com> | 2018-11-16 15:02:18 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-11-16 15:02:18 +0100 |
commit | 8069fd636ba14059524303ce76abe2173c4f3d01 (patch) | |
tree | 55dd4f8ecd39813080cb5ddedd834f93a7ffc919 /app/models | |
parent | 367ad2199509e9008c04c08f3476e236b27b52a1 (diff) |
Remove intermediary arrays when creating hash maps from results (#9291)
Diffstat (limited to 'app/models')
-rw-r--r-- | app/models/concerns/account_interactions.rb | 4 | ||||
-rw-r--r-- | app/models/notification.rb | 2 | ||||
-rw-r--r-- | app/models/setting.rb | 2 | ||||
-rw-r--r-- | app/models/status.rb | 10 | ||||
-rw-r--r-- | app/models/trending_tags.rb | 2 |
5 files changed, 10 insertions, 10 deletions
diff --git a/app/models/concerns/account_interactions.rb b/app/models/concerns/account_interactions.rb index f5f833446..ad2909d91 100644 --- a/app/models/concerns/account_interactions.rb +++ b/app/models/concerns/account_interactions.rb @@ -45,9 +45,9 @@ module AccountInteractions end def domain_blocking_map(target_account_ids, account_id) - accounts_map = Account.where(id: target_account_ids).select('id, domain').map { |a| [a.id, a.domain] }.to_h + accounts_map = Account.where(id: target_account_ids).select('id, domain').each_with_object({}) { |a, h| h[a.id] = a.domain } blocked_domains = domain_blocking_map_by_domain(accounts_map.values.compact, account_id) - accounts_map.map { |id, domain| [id, blocked_domains[domain]] }.to_h + accounts_map.reduce({}) { |h, (id, domain)| h.merge(id => blocked_domains[domain]) } end def domain_blocking_map_by_domain(target_domains, account_id) diff --git a/app/models/notification.rb b/app/models/notification.rb index 78b180301..4233532d0 100644 --- a/app/models/notification.rb +++ b/app/models/notification.rb @@ -75,7 +75,7 @@ class Notification < ApplicationRecord return if account_ids.empty? - accounts = Account.where(id: account_ids).map { |a| [a.id, a] }.to_h + accounts = Account.where(id: account_ids).each_with_object({}) { |a, h| h[a.id] = a } cached_items.each do |item| item.from_account = accounts[item.from_account_id] diff --git a/app/models/setting.rb b/app/models/setting.rb index 033d09fd5..a5878e96a 100644 --- a/app/models/setting.rb +++ b/app/models/setting.rb @@ -40,7 +40,7 @@ class Setting < RailsSettings::Base def all_as_records vars = thing_scoped - records = vars.map { |r| [r.var, r] }.to_h + records = vars.each_with_object({}) { |r, h| h[r.var] = r } default_settings.each do |key, default_value| next if records.key?(key) || default_value.is_a?(Hash) diff --git a/app/models/status.rb b/app/models/status.rb index 11a75f876..680081724 100644 --- a/app/models/status.rb +++ b/app/models/status.rb @@ -321,19 +321,19 @@ class Status < ApplicationRecord end def favourites_map(status_ids, account_id) - Favourite.select('status_id').where(status_id: status_ids).where(account_id: account_id).map { |f| [f.status_id, true] }.to_h + Favourite.select('status_id').where(status_id: status_ids).where(account_id: account_id).each_with_object({}) { |f, h| h[f.status_id] = true } end def reblogs_map(status_ids, account_id) - select('reblog_of_id').where(reblog_of_id: status_ids).where(account_id: account_id).reorder(nil).map { |s| [s.reblog_of_id, true] }.to_h + select('reblog_of_id').where(reblog_of_id: status_ids).where(account_id: account_id).reorder(nil).each_with_object({}) { |s, h| h[s.reblog_of_id] = true } end def mutes_map(conversation_ids, account_id) - ConversationMute.select('conversation_id').where(conversation_id: conversation_ids).where(account_id: account_id).map { |m| [m.conversation_id, true] }.to_h + ConversationMute.select('conversation_id').where(conversation_id: conversation_ids).where(account_id: account_id).each_with_object({}) { |m, h| h[m.conversation_id] = true } end def pins_map(status_ids, account_id) - StatusPin.select('status_id').where(status_id: status_ids).where(account_id: account_id).map { |p| [p.status_id, true] }.to_h + StatusPin.select('status_id').where(status_id: status_ids).where(account_id: account_id).each_with_object({}) { |p, h| h[p.status_id] = true } end def reload_stale_associations!(cached_items) @@ -348,7 +348,7 @@ class Status < ApplicationRecord return if account_ids.empty? - accounts = Account.where(id: account_ids).map { |a| [a.id, a] }.to_h + accounts = Account.where(id: account_ids).each_with_object({}) { |a, h| h[a.id] = a } cached_items.each do |item| item.account = accounts[item.account_id] diff --git a/app/models/trending_tags.rb b/app/models/trending_tags.rb index c559651c6..3a8be2164 100644 --- a/app/models/trending_tags.rb +++ b/app/models/trending_tags.rb @@ -18,7 +18,7 @@ class TrendingTags def get(limit) key = "#{KEY}:#{Time.now.utc.beginning_of_day.to_i}" tag_ids = redis.zrevrange(key, 0, limit - 1).map(&:to_i) - tags = Tag.where(id: tag_ids).to_a.map { |tag| [tag.id, tag] }.to_h + tags = Tag.where(id: tag_ids).to_a.each_with_object({}) { |tag, h| h[tag.id] = tag } tag_ids.map { |tag_id| tags[tag_id] }.compact end |