replace Array#once_per extension with #uniq

Change-Id: I49111316d6cbc1f4be111567ed7f5e670d1d948c
Reviewed-on: https://gerrit.instructure.com/30023
Tested-by: Jenkins <jenkins@instructure.com>
Reviewed-by: Zach Pendleton <zachp@instructure.com>
Product-Review: Zach Pendleton <zachp@instructure.com>
QA-Review: Stephan Hagemann <stephan@pivotallabs.com>
This commit is contained in:
Raphael Weiner 2014-02-12 08:57:25 -07:00 committed by Stephan Hagemann
parent 46a63670e0
commit 5f52dce2d4
10 changed files with 13 additions and 24 deletions

View File

@ -62,7 +62,7 @@ class FacebookController < ApplicationController
@messages = [] @messages = []
if @user if @user
@messages = @user.messages.to_facebook.to_a @messages = @user.messages.to_facebook.to_a
@domains = @user.pseudonyms.includes(:account).to_a.once_per(&:account_id).map{|p| HostUrl.context_host(p.account) }.uniq @domains = @user.pseudonyms.includes(:account).to_a.uniq(&:account_id).map{|p| HostUrl.context_host(p.account) }.uniq
end end
respond_to do |format| respond_to do |format|
format.html { render :action => 'index', :layout => 'facebook' } format.html { render :action => 'index', :layout => 'facebook' }

View File

@ -26,7 +26,7 @@ class RubricsController < ApplicationController
return unless authorized_action(@context, @current_user, :manage) return unless authorized_action(@context, @current_user, :manage)
js_env :ROOT_OUTCOME_GROUP => get_root_outcome js_env :ROOT_OUTCOME_GROUP => get_root_outcome
@rubric_associations = @context.rubric_associations.bookmarked.include_rubric.to_a @rubric_associations = @context.rubric_associations.bookmarked.include_rubric.to_a
@rubric_associations = Canvas::ICU.collate_by(@rubric_associations.select(&:rubric_id).once_per(&:rubric_id)) { |r| r.rubric.title } @rubric_associations = Canvas::ICU.collate_by(@rubric_associations.select(&:rubric_id).uniq(&:rubric_id)) { |r| r.rubric.title }
@rubrics = @rubric_associations.map(&:rubric) @rubrics = @rubric_associations.map(&:rubric)
@context.is_a?(User) ? render(:action => 'user_index') : render @context.is_a?(User) ? render(:action => 'user_index') : render
end end

View File

@ -117,7 +117,7 @@ module Context
def sorted_rubrics(user, context) def sorted_rubrics(user, context)
associations = RubricAssociation.bookmarked.for_context_codes(context.asset_string).include_rubric associations = RubricAssociation.bookmarked.for_context_codes(context.asset_string).include_rubric
Canvas::ICU.collate_by(associations.to_a.once_per(&:rubric_id).select{|r| r.rubric }) { |r| r.rubric.title || SortLast } Canvas::ICU.collate_by(associations.to_a.uniq(&:rubric_id).select{|r| r.rubric }) { |r| r.rubric.title || SortLast }
end end
def rubric_contexts(user) def rubric_contexts(user)
@ -131,7 +131,7 @@ module Context
codes_order = {} codes_order = {}
context_codes.each_with_index{|c, idx| codes_order[c] = idx } context_codes.each_with_index{|c, idx| codes_order[c] = idx }
associations = RubricAssociation.bookmarked.for_context_codes(context_codes).include_rubric associations = RubricAssociation.bookmarked.for_context_codes(context_codes).include_rubric
associations = associations.to_a.select{|a| a.rubric }.once_per{|a| [a.rubric_id, a.context_code] } associations = associations.to_a.select{|a| a.rubric }.uniq{|a| [a.rubric_id, a.context_code] }
contexts = associations.group_by{|a| a.context_code }.map do |code, associations| contexts = associations.group_by{|a| a.context_code }.map do |code, associations|
context_name = associations.first.context_name context_name = associations.first.context_name
res = { res = {

View File

@ -969,7 +969,7 @@ class DiscussionTopic < ActiveRecord::Base
end end
media_objects = media_object_ids.empty? ? [] : MediaObject.find_all_by_media_id(media_object_ids) media_objects = media_object_ids.empty? ? [] : MediaObject.find_all_by_media_id(media_object_ids)
media_objects += media_object_ids.map{|id| MediaObject.new(:media_id => id) } media_objects += media_object_ids.map{|id| MediaObject.new(:media_id => id) }
media_objects = media_objects.once_per(&:media_id) media_objects = media_objects.uniq(&:media_id)
media_objects = media_objects.map do |media_object| media_objects = media_objects.map do |media_object|
if media_object.new_record? if media_object.new_record?
media_object.context = context media_object.context = context

View File

@ -44,7 +44,7 @@ class ReportSnapshot < ActiveRecord::Base
items << [stamp*1000, week[key]] items << [stamp*1000, week[key]]
end end
end end
items.sort_by(&:first).once_per(&:first) items.sort_by(&:first).uniq(&:first)
end end
def report_value_over_time(*args) def report_value_over_time(*args)

View File

@ -91,7 +91,7 @@
<h3 class="service_type"><%= image_tag "#{service_type}_icon.png", :alt => "" %><%= service_type.titleize %></h3> <h3 class="service_type"><%= image_tag "#{service_type}_icon.png", :alt => "" %><%= service_type.titleize %></h3>
<div class="short_description"><%= UserService.short_description(service_type) %></div> <div class="short_description"><%= UserService.short_description(service_type) %></div>
<ul class="user_list"> <ul class="user_list">
<% services.once_per(&:user_id).each do |service| %> <% services.uniq(&:user_id).each do |service| %>
<li class="<%= 'current_user' if @current_user && service.user_id == @current_user.id %>"> <li class="<%= 'current_user' if @current_user && service.user_id == @current_user.id %>">
<% if service.has_profile_link? %> <% if service.has_profile_link? %>
<a href="<%= service.service_user_link %>"> <a href="<%= service.service_user_link %>">

View File

@ -23,7 +23,7 @@
<% <%
rubric_associations = @context.rubric_associations.bookmarked.before(@range_end) rubric_associations = @context.rubric_associations.bookmarked.before(@range_end)
rubric_associations = rubric_associations.select(&:rubric_id).once_per(&:rubric_id) rubric_associations = rubric_associations.select(&:rubric_id).uniq(&:rubric_id)
%> %>
<div id="reports-tabs" style="display:none;"> <div id="reports-tabs" style="display:none;">
<ul> <ul>

View File

@ -46,7 +46,7 @@
<% else %> <% else %>
<select name="question_<%= hash_get(question, :id) %>_answer_<%= hash_get(answer, :id) %>" id="question_<%= hash_get(question, :id) %>_answer_<%= hash_get(answer, :id) %>" class="question_input" style="max-width: 90%;"> <select name="question_<%= hash_get(question, :id) %>_answer_<%= hash_get(answer, :id) %>" id="question_<%= hash_get(question, :id) %>_answer_<%= hash_get(answer, :id) %>" class="question_input" style="max-width: 90%;">
<option value="" <%= "selected" unless @stored_params["question_#{hash_get(question, :id)}_answer_#{hash_get(answer, :id)}"] %>>[ <%= t :choose_answer, "Choose" %> ]</option> <option value="" <%= "selected" unless @stored_params["question_#{hash_get(question, :id)}_answer_#{hash_get(answer, :id)}"] %>>[ <%= t :choose_answer, "Choose" %> ]</option>
<% right_answers.once_per{|a| hash_get(a, :right) || (hash_get(question, :matches, []).find{|aa| hash_get(aa, :match_id).to_i == hash_get(a, :match_id).to_i}[:text] rescue '') }.each do |a| %> <% right_answers.uniq{|a| hash_get(a, :right) || (hash_get(question, :matches, []).find{|aa| hash_get(aa, :match_id).to_i == hash_get(a, :match_id).to_i}[:text] rescue '') }.each do |a| %>
<option value="<%= hash_get(a, :match_id) %>" <%= "selected" if @stored_params["question_#{hash_get(question, :id)}_answer_#{hash_get(answer, :id)}"].to_s == hash_get(a, :match_id).to_s %>><%= (hash_get(a, :right) || (hash_get(question, :matches).find{|aa| hash_get(aa, :match_id).to_i == hash_get(a, :match_id).to_i}[:text] rescue '')) %></option> <option value="<%= hash_get(a, :match_id) %>" <%= "selected" if @stored_params["question_#{hash_get(question, :id)}_answer_#{hash_get(answer, :id)}"].to_s == hash_get(a, :match_id).to_s %>><%= (hash_get(a, :right) || (hash_get(question, :matches).find{|aa| hash_get(aa, :match_id).to_i == hash_get(a, :match_id).to_i}[:text] rescue '')) %></option>
<% end %> <% end %>
</select> </select>

View File

@ -43,17 +43,6 @@ class Array
end end
end end
def once_per(&block)
finds = {}
self.inject([]) do |array, item|
mapped = block.call(item)
found = finds[mapped]
finds[mapped] = true
array << item unless found
array
end
end
def clump_per(&block) def clump_per(&block)
self.inject({}) do |hash, item| self.inject({}) do |hash, item|
mapped = block.call(item) mapped = block.call(item)

View File

@ -482,11 +482,11 @@ describe RubricsController do
@rubric = Rubric.create!(:user => @user, :context => @course) @rubric = Rubric.create!(:user => @user, :context => @course)
RubricAssociation.create!(:rubric => @rubric, :context => @course, :purpose => :bookmark, :association_object => @course) RubricAssociation.create!(:rubric => @rubric, :context => @course, :purpose => :bookmark, :association_object => @course)
@course.rubric_associations.bookmarked.include_rubric.to_a.select(&:rubric_id).once_per(&:rubric_id).sort_by{|a| a.rubric.title }.map(&:rubric).should == [@rubric] @course.rubric_associations.bookmarked.include_rubric.to_a.select(&:rubric_id).uniq(&:rubric_id).sort_by{|a| a.rubric.title }.map(&:rubric).should == [@rubric]
delete 'destroy', :course_id => @course.id, :id => @rubric.id delete 'destroy', :course_id => @course.id, :id => @rubric.id
response.should be_success response.should be_success
@course.rubric_associations.bookmarked.include_rubric.to_a.select(&:rubric_id).once_per(&:rubric_id).sort_by{|a| a.rubric.title }.map(&:rubric).should == [] @course.rubric_associations.bookmarked.include_rubric.to_a.select(&:rubric_id).uniq(&:rubric_id).sort_by{|a| a.rubric.title }.map(&:rubric).should == []
@rubric.reload @rubric.reload
@rubric.deleted?.should be_true @rubric.deleted?.should be_true
end end
@ -499,11 +499,11 @@ describe RubricsController do
@rubric = Rubric.create!(:user => @user, :context => Account.default) @rubric = Rubric.create!(:user => @user, :context => Account.default)
RubricAssociation.create!(:rubric => @rubric, :context => @course, :purpose => :bookmark, :association_object => @course) RubricAssociation.create!(:rubric => @rubric, :context => @course, :purpose => :bookmark, :association_object => @course)
RubricAssociation.create!(:rubric => @rubric, :context => Account.default, :purpose => :bookmark, :association_object => @course) RubricAssociation.create!(:rubric => @rubric, :context => Account.default, :purpose => :bookmark, :association_object => @course)
@course.rubric_associations.bookmarked.include_rubric.to_a.select(&:rubric_id).once_per(&:rubric_id).sort_by{|a| a.rubric.title }.map(&:rubric).should == [@rubric] @course.rubric_associations.bookmarked.include_rubric.to_a.select(&:rubric_id).uniq(&:rubric_id).sort_by{|a| a.rubric.title }.map(&:rubric).should == [@rubric]
delete 'destroy', :course_id => @course.id, :id => @rubric.id delete 'destroy', :course_id => @course.id, :id => @rubric.id
response.should be_success response.should be_success
@course.rubric_associations.bookmarked.include_rubric.to_a.select(&:rubric_id).once_per(&:rubric_id).sort_by{|a| a.rubric.title }.map(&:rubric).should == [] @course.rubric_associations.bookmarked.include_rubric.to_a.select(&:rubric_id).uniq(&:rubric_id).sort_by{|a| a.rubric.title }.map(&:rubric).should == []
@rubric.reload @rubric.reload
@rubric.deleted?.should be_false @rubric.deleted?.should be_false
end end