Skip to content
Permalink
Browse files

Replace map.flatten with flat_map in activerecord

  • Loading branch information...
sferik committed Mar 4, 2014
1 parent 817fe31 commit 3413b88a3d6b2b022dfb57e42565446b1e024314
@@ -530,8 +530,8 @@ def association_instance_set(name, association)
# end
#
# @firm = Firm.first
# @firm.clients.collect { |c| c.invoices }.flatten # select all invoices for all clients of the firm
# @firm.invoices # selects all invoices by going through the Client join model
# @firm.clients.flat_map { |c| c.invoices } # select all invoices for all clients of the firm
# @firm.invoices # selects all invoices by going through the Client join model
#
# Similarly you can go through a +has_one+ association on the join model:
#
@@ -23,7 +23,7 @@ def associated_records_by_owner(preloader)

reset_association owners, through_reflection.name

middle_records = through_records.map { |(_,rec)| rec }.flatten
middle_records = through_records.flat_map { |(_,rec)| rec }

preloaders = preloader.preload(middle_records,
source_reflection.name,
@@ -459,7 +459,7 @@ def create_table(table_name, options = {}) #:nodoc:
end

def bulk_change_table(table_name, operations) #:nodoc:
sqls = operations.map do |command, args|
sqls = operations.flat_map do |command, args|
table, arguments = args.shift, args
method = :"#{command}_sql"

@@ -468,7 +468,7 @@ def bulk_change_table(table_name, operations) #:nodoc:
else
raise "Unknown method called : #{method}(#{arguments.inspect})"
end
end.flatten.join(", ")
end.join(", ")

execute("ALTER TABLE #{quote_table_name(table_name)} #{sqls}")
end
@@ -159,7 +159,7 @@ def test_has_and_belongs_to_many_remove_callback_on_clear
activerecord.reload
assert activerecord.developers_with_callbacks.size == 2
end
log_array = activerecord.developers_with_callbacks.collect {|d| ["before_removing#{d.id}","after_removing#{d.id}"]}.flatten.sort
log_array = activerecord.developers_with_callbacks.flat_map {|d| ["before_removing#{d.id}","after_removing#{d.id}"]}.sort
assert activerecord.developers_with_callbacks.clear
assert_equal log_array, activerecord.developers_log.sort
end
@@ -76,9 +76,9 @@ def assert_no_difference_when_adding_callbacks_twice_for(model, association_name
end

def callbacks_for_model(model)
model.instance_variables.grep(/_callbacks$/).map do |ivar|
model.instance_variables.grep(/_callbacks$/).flat_map do |ivar|
model.instance_variable_get(ivar)
end.flatten
end
end
end

0 comments on commit 3413b88

Please sign in to comment.
You can’t perform that action at this time.