Skip to content

Commit 2d71a68

Browse files
committed
ActiveRecord & ruby optimizations
- select(:foo).map(:foo) -> pluck(:foo) - select(:foo).collect(:foo) -> pluck(:foo) - map { |foo| foo.bar } -> map(&:bar) - map { … }.flatten -> flat_map { … } - collect { … }.flatten -> flat_map { … } - gsub() -> sub() - gsub() -> tr()
1 parent 30c39fa commit 2d71a68

21 files changed

+46
-46
lines changed

app/controllers/networks_controller.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ def tag
7878
@topics = tags_array
7979
@topic = tags_array.join(' + ')
8080
@topic_user = nil
81-
@networks = tags_array.map { |tag| Network.networks_for_tag(tag) }.flatten.uniq if @networks.nil?
81+
@networks = tags_array.flat_map { |tag| Network.networks_for_tag(tag) }.uniq if @networks.nil?
8282
end
8383

8484
def mayor

app/controllers/opportunities_controller.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ def index
8888

8989
chosen_location = 'Worldwide' if chosen_location.nil?
9090
@locations = Rails.cache.fetch("job_locations_#{params[:location]}_#{params[:skill]}", expires_in: 1.hour) do
91-
Opportunity.by_tag(tag).map(&:locations).flatten.reject { |loc| loc == "Worldwide" }.push("Worldwide").uniq.compact
91+
Opportunity.by_tag(tag).flat_map(&:locations).reject { |loc| loc == "Worldwide" }.push("Worldwide").uniq.compact
9292
end
9393
@locations.delete(chosen_location) unless @locations.frozen?
9494
params[:location] = chosen_location
@@ -147,11 +147,11 @@ def stringify_location
147147
end
148148

149149
def all_job_locations
150-
Rails.cache.fetch('job_locations', expires_in: 23.hours) { Opportunity.all.map(&:locations).flatten.push("Worldwide").uniq.compact }
150+
Rails.cache.fetch('job_locations', expires_in: 23.hours) { Opportunity.all.flat_map(&:locations).push("Worldwide").uniq.compact }
151151
end
152152

153153
def all_job_skills
154-
Rails.cache.fetch('job_skills', expires_in: 23.hours) { Opportunity.all.map(&:tags).flatten.uniq.compact }
154+
Rails.cache.fetch('job_skills', expires_in: 23.hours) { Opportunity.all.flat_map(&:tags).uniq.compact }
155155
end
156156

157157
def closest_to_user(user)

app/controllers/protips_controller.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -525,7 +525,7 @@ def suggested_networks
525525
@protips.facets['suggested-networks']['terms'].map { |h| h['term'] }
526526
else
527527
#gets top 10 tags for the protips and picks up associated networks
528-
Network.tagged_with(@protips.map(&:tags).flatten.reduce(Hash.new(0)) { |h, t| h[t] += 1; h }.sort_by { |k, v| -v }.first(10).flatten.values_at(*(0..20).step(2))).select(:slug).limit(4).map(&:slug)
528+
Network.tagged_with(@protips.flat_map(&:tags).reduce(Hash.new(0)) { |h, t| h[t] += 1; h }.sort_by { |k, v| -v }.first(10).flatten.values_at(*(0..20).step(2))).limit(4).pluck(:slug)
529529
end
530530
end
531531
@@ -545,7 +545,7 @@ def get_topics_from_protips(protips)
545545
topics = protips.facets['top-tags']['terms'].map { |h| h['term'] }
546546
end
547547
548-
topics = protips.map(&:tags).flatten.uniq.first(8) if topics.blank? && protips.present?
548+
topics = protips.flat_map(&:tags).uniq.first(8) if topics.blank? && protips.present?
549549
topics
550550
end
551551

app/controllers/teams_controller.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,7 @@ def team_from_params(opts)
290290
end
291291

292292
def replace_section(section_name)
293-
section_name = section_name.gsub('-', '_')
293+
section_name = section_name.tr('-', '_')
294294
"$('##{section_name}').replaceWith('#{escape_javascript(render(:partial => section_name))}');"
295295
end
296296

@@ -313,7 +313,7 @@ def page_based_on_rank(rank)
313313

314314
def job_public_ids
315315
Opportunity
316-
Rails.cache.fetch('all-jobs-public-ids', :expires_in => 1.hour) { Opportunity.select(:public_id).group('team_id, created_at, public_id').map(&:public_id) }
316+
Rails.cache.fetch('all-jobs-public-ids', :expires_in => 1.hour) { Opportunity.group('team_id, created_at, public_id').pluck(:public_id) }
317317
end
318318

319319
def next_job(job)

app/helpers/badges_helper.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ def share_coderwall_on_twitter
88
end
99

1010
def dom_tag(tag)
11-
sanitize_dom_id(tag).gsub(' ', '-').gsub('+', 'plus').gsub('#', 'sharp')
11+
sanitize_dom_id(tag).tr(' ', '-').gsub('+', 'plus').gsub('#', 'sharp')
1212
end
1313

1414
def dom_for_badge(badge)
@@ -27,4 +27,4 @@ def unlocked_badge_title
2727
"#{@user.short_name} leveled up and unlocked the #{@badge.display_name} on Coderwall"
2828
end
2929

30-
end
30+
end

app/helpers/premium_helper.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ def panel_form_for_section(section_id, title = nil, show_save_button = true, &bl
7070
end
7171

7272
def partialify_html_section_id(section_id)
73-
section_id.to_s.gsub("-", "_").gsub('#', '')
73+
section_id.to_s.tr("-", "_").gsub('#', '')
7474
end
7575

7676
def add_active_class_to_first_member

app/helpers/protips_helper.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,7 @@ def display_scope_class
285285
end
286286

287287
def current_user_upvotes
288-
@upvoted_protip_ids ||= current_user.upvoted_protips.select(:public_id).map(&:public_id)
288+
@upvoted_protip_ids ||= current_user.upvoted_protips.pluck(:public_id)
289289
end
290290

291291
def user_upvoted?(protip)

app/jobs/create_network_job.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ class CreateNetworkJob
55

66
def perform(tag)
77
top_tags = Protip.trending_topics
8-
sub_tags = Protip.tagged_with([tag], on: :topics).collect(&:topics).flatten
8+
sub_tags = Protip.tagged_with([tag], on: :topics).flat_map(&:topics)
99
sub_tags.delete_if { |sub_tag| top_tags.include? sub_tag }
1010
unless sub_tags.blank?
1111
sub_tag_frequency = sub_tags.inject(Hash.new(0)) { |h, sub_tag| h[sub_tag] += 1; h }

app/models/badges/node_knockout.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ def github_for(path)
115115
begin
116116
res = Servant.get("http://nodeknockout.com#{path}")
117117
doc = Nokogiri::HTML(res.to_s)
118-
username = doc.css("a.github").first[:href].gsub(/https?:\/\/github.com\//, '')
118+
username = doc.css("a.github").first[:href].sub(/https?:\/\/github.com\//, '')
119119
role = doc.css(".role").first.text
120120
Rails.logger.info "Found node knockout #{role}: #{username}" if ENV['DEBUG']
121121
return [role, username]
@@ -129,7 +129,7 @@ def twitter_for(path)
129129
begin
130130
res = Servant.get("http://nodeknockout.com#{path}")
131131
doc = Nokogiri::HTML(res.to_s)
132-
username = doc.css("a.twitter").first[:href].gsub("http://twitter.com/", '').strip
132+
username = doc.css("a.twitter").first[:href].sub("http://twitter.com/", '').strip
133133
role = doc.css(".role").first.text
134134
Rails.logger.info "Found node knockout #{role}: #{username}"
135135
return [role, username]

app/models/badges/polygamous.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,14 @@ class Polygamous < BadgeBase
99
def reasons
1010
@reasons ||= begin
1111
facts = user.facts.select { |fact| fact.tagged?('personal', 'repo', 'original') }
12-
facts.collect do |fact|
12+
facts.flat_map do |fact|
1313
fact.metadata[:languages]
14-
end.flatten.uniq
14+
end.uniq
1515
end
1616
end
1717

1818
def award?
1919
reasons.size >= 4
2020
end
2121

22-
end
22+
end

0 commit comments

Comments
 (0)