Permalink
Browse files

Removing most of the symbol to proc usage in Active Record

This will hopefully make Active Record run a bit more faster.
  • Loading branch information...
1 parent 1f25041 commit a7eb8d97a44d65c68df2eed61a5375a8d8da32f1 @sikachu sikachu committed with tenderlove Aug 13, 2010
@@ -35,7 +35,7 @@ def initialize(reflection)
through_reflection = reflection.through_reflection
source_reflection_names = reflection.source_reflection_names
source_associations = reflection.through_reflection.klass.reflect_on_all_associations.collect { |a| a.name.inspect }
- super("Could not find the source association(s) #{source_reflection_names.collect(&:inspect).to_sentence(:two_words_connector => ' or ', :last_word_connector => ', or ', :locale => :en)} in model #{through_reflection.klass}. Try 'has_many #{reflection.name.inspect}, :through => #{through_reflection.name.inspect}, :source => <name>'. Is it one of #{source_associations.to_sentence(:two_words_connector => ' or ', :last_word_connector => ', or ', :locale => :en)}?")
+ super("Could not find the source association(s) #{source_reflection_names.collect{ |a| a.inspect }.to_sentence(:two_words_connector => ' or ', :last_word_connector => ', or ', :locale => :en)} in model #{through_reflection.klass}. Try 'has_many #{reflection.name.inspect}, :through => #{through_reflection.name.inspect}, :source => <name>'. Is it one of #{source_associations.to_sentence(:two_words_connector => ' or ', :last_word_connector => ', or ', :locale => :en)}?")
end
end
@@ -1497,17 +1497,17 @@ def collection_reader_method(reflection, association_proxy_class)
association
end
-
+
redefine_method("#{reflection.name.to_s.singularize}_ids") do
if send(reflection.name).loaded? || reflection.options[:finder_sql]
- send(reflection.name).map(&:id)
+ send(reflection.name).map { |r| r.id }
else
if reflection.through_reflection && reflection.source_reflection.belongs_to?
through = reflection.through_reflection
primary_key = reflection.source_reflection.primary_key_name
- send(through.name).select("DISTINCT #{through.quoted_table_name}.#{primary_key}").map!(&:"#{primary_key}")
+ send(through.name).select("DISTINCT #{through.quoted_table_name}.#{primary_key}").map! { |r| r.send(:"#{primary_key}") }
@fxn

fxn Aug 13, 2010

Owner

This symbol is not necessary, #send accepts strings

@tenderlove

tenderlove Aug 13, 2010

Owner

Hum. Changing this to a string breaks the AR tests. I wonder what's going on.

@tenderlove

tenderlove Aug 13, 2010

Owner

nevermind. I'm going crazy. ;-)

else
- send(reflection.name).select("#{reflection.quoted_table_name}.#{reflection.klass.primary_key}").except(:includes).map!(&:id)
+ send(reflection.name).select("#{reflection.quoted_table_name}.#{reflection.klass.primary_key}").except(:includes).map! { |r| r.id }
end
end
end
@@ -1529,7 +1529,7 @@ def collection_accessor_methods(reflection, association_proxy_class, writer = tr
pk_column = reflection.primary_key_column
ids = (new_value || []).reject { |nid| nid.blank? }
ids.map!{ |i| pk_column.type_cast(i) }
- send("#{reflection.name}=", reflection.klass.find(ids).index_by(&:id).values_at(*ids))
+ send("#{reflection.name}=", reflection.klass.find(ids).index_by{ |r| r.id }.values_at(*ids))
end
end
end
@@ -127,7 +127,7 @@ def create_record(attributes, &block)
def record_timestamp_columns(record)
if record.record_timestamps
- record.send(:all_timestamp_attributes).map(&:to_s)
+ record.send(:all_timestamp_attributes).map { |x| x.to_s }
else
[]
end
@@ -231,7 +231,7 @@ def associated_records_to_validate_or_save(association, new_record, autosave)
def nested_records_changed_for_autosave?
self.class.reflect_on_all_autosave_associations.any? do |reflection|
association = association_instance_get(reflection.name)
- association && Array.wrap(association.target).any?(&:changed_for_autosave?)
+ association && Array.wrap(association.target).any? { |a| a.changed_for_autosave? }
end
end
@@ -519,7 +519,7 @@ def count_by_sql(sql)
# Attributes listed as readonly will be used to create a new record but update operations will
# ignore these fields.
def attr_readonly(*attributes)
- write_inheritable_attribute(:attr_readonly, Set.new(attributes.map(&:to_s)) + (readonly_attributes || []))
+ write_inheritable_attribute(:attr_readonly, Set.new(attributes.map { |a| a.to_s }) + (readonly_attributes || []))
end
# Returns an array of all the attributes that have been specified as readonly.
@@ -1286,7 +1286,7 @@ def sanitize_sql_hash_for_conditions(attrs, default_table_name = self.table_name
table = Arel::Table.new(self.table_name, :engine => arel_engine, :as => default_table_name)
builder = PredicateBuilder.new(arel_engine)
- builder.build_from_hash(attrs, table).map(&:to_sql).join(' AND ')
+ builder.build_from_hash(attrs, table).map{ |b| b.to_sql }.join(' AND ')
end
alias_method :sanitize_sql_hash, :sanitize_sql_hash_for_conditions
@@ -1737,7 +1737,7 @@ def execute_callstack_for_multiparameter_attributes(callstack)
klass = (self.class.reflect_on_aggregation(name.to_sym) || column_for_attribute(name)).klass
# in order to allow a date to be set without a year, we must keep the empty values.
# Otherwise, we wouldn't be able to distinguish it from a date with an empty day.
- values = values_with_empty_parameters.reject(&:nil?)
+ values = values_with_empty_parameters.reject { |v| v.nil? }
if values.empty?
send(name + "=", nil)
@@ -528,7 +528,7 @@ def references(*args)
# concatenated together. This string can then be prepended and appended to
# to generate the final SQL to create the table.
def to_sql
- @columns.map(&:to_sql) * ', '
+ @columns.map { |c| c.to_sql } * ', '
end
private
@@ -450,7 +450,7 @@ def assume_migrated_upto_version(version, migrations_path = ActiveRecord::Migrat
version = version.to_i
sm_table = quote_table_name(ActiveRecord::Migrator.schema_migrations_table_name)
- migrated = select_values("SELECT version FROM #{sm_table}").map(&:to_i)
+ migrated = select_values("SELECT version FROM #{sm_table}").map { |v| v.to_i }
versions = Dir["#{migrations_path}/[0-9]*_*.rb"].map do |filename|
filename.split('/').last.split('_').first.to_i
end
@@ -879,7 +879,7 @@ def distinct(columns, order_by) #:nodoc:
# Construct a clean list of column names from the ORDER BY clause, removing
# any ASC/DESC modifiers
order_columns = order_by.split(',').collect { |s| s.split.first }
- order_columns.delete_if(&:blank?)
+ order_columns.delete_if { |c| c.blank? }
order_columns = order_columns.zip((0...order_columns.size).to_a).map { |s,i| "#{s} AS alias_#{i}" }
# Return a DISTINCT ON() clause that's distinct on the columns we want but includes
@@ -40,11 +40,11 @@ class Version
include Comparable
def initialize(version_string)
- @version = version_string.split('.').map(&:to_i)
+ @version = version_string.split('.').map { |v| v.to_i }
end
def <=>(version_string)
- @version <=> version_string.split('.').map(&:to_i)
+ @version <=> version_string.split('.').map { |v| v.to_i }
end
end
@@ -345,7 +345,7 @@ def copy_table_indexes(from, to, rename = {}) #:nodoc:
name = name[5..-1]
end
- to_column_names = columns(to).map(&:name)
+ to_column_names = columns(to).map { |c| c.name }
columns = index.columns.map {|c| rename[c] || c }.select do |column|
to_column_names.include?(column)
end
@@ -690,7 +690,7 @@ def inheritance_column_name
end
def column_names
- @column_names ||= @connection.columns(@table_name).collect(&:name)
+ @column_names ||= @connection.columns(@table_name).collect { |c| c.name }
end
def read_fixture_files
@@ -908,7 +908,7 @@ def setup_fixture_accessors(table_names = nil)
def uses_transaction(*methods)
@uses_transaction = [] unless defined?(@uses_transaction)
- @uses_transaction.concat methods.map(&:to_s)
+ @uses_transaction.concat methods.map { |m| m.to_s }
end
def uses_transaction?(method)
@@ -374,7 +374,7 @@ def connection
end
def method_missing(method, *arguments, &block)
- arg_list = arguments.map(&:inspect) * ', '
+ arg_list = arguments.map{ |a| a.inspect } * ', '
say_with_time "#{method}(#{arg_list})" do
unless arguments.empty? || method == :execute
@@ -451,7 +451,7 @@ def schema_migrations_table_name
def get_all_versions
table = Arel::Table.new(schema_migrations_table_name)
- Base.connection.select_values(table.project(table['version']).to_sql).map(&:to_i).sort
+ Base.connection.select_values(table.project(table['version']).to_sql).map{ |v| v.to_i }.sort
end
def current_version
@@ -569,7 +569,7 @@ def migrations
klasses << migration
end
- migrations = migrations.sort_by(&:version)
+ migrations = migrations.sort_by { |m| m.version }
down? ? migrations.reverse : migrations
end
end
@@ -122,7 +122,7 @@ def add_observer!(klass)
end
def define_callbacks(klass)
- existing_methods = klass.instance_methods.map(&:to_sym)
+ existing_methods = klass.instance_methods.map { |m| m.to_sym }
observer = self
observer_name = observer.class.name.underscore.gsub('/', '__')
@@ -375,15 +375,15 @@ def method_missing(method, *args, &block)
def references_eager_loaded_tables?
# always convert table names to downcase as in Oracle quoted table names are in uppercase
- joined_tables = (tables_in_string(arel.joins(arel)) + [table.name, table.table_alias]).compact.map(&:downcase).uniq
+ joined_tables = (tables_in_string(arel.joins(arel)) + [table.name, table.table_alias]).compact.map{ |t| t.downcase }.uniq
(tables_in_string(to_sql) - joined_tables).any?
end
def tables_in_string(string)
return [] if string.blank?
# always convert table names to downcase as in Oracle quoted table names are in uppercase
# ignore raw_sql_ that is used by Oracle adapter as alias for limit/offset subqueries
- string.scan(/([a-zA-Z_][\.\w]+).?\./).flatten.map(&:downcase).uniq - ['raw_sql_']
+ string.scan(/([a-zA-Z_][\.\w]+).?\./).flatten.map{ |s| s.downcase }.uniq - ['raw_sql_']
end
end
@@ -348,7 +348,7 @@ def column_aliases(join_dependency)
end
def using_limitable_reflections?(reflections)
- reflections.none?(&:collection?)
+ reflections.none? { |r| r.collection? }
end
end
@@ -123,7 +123,7 @@ def table(table, stream)
end.compact
# find all migration keys used in this table
- keys = [:name, :limit, :precision, :scale, :default, :null] & column_specs.map(&:keys).flatten
+ keys = [:name, :limit, :precision, :scale, :default, :null] & column_specs.map{ |k| k.keys }.flatten
# figure out the lengths for each column based on above keys
lengths = keys.map{ |key| column_specs.map{ |spec| spec[key] ? spec[key].length + 2 : 0 }.max }
@@ -21,7 +21,7 @@ def assert_sql(*patterns_to_match)
patterns_to_match.each do |pattern|
failed_patterns << pattern unless $queries_executed.any?{ |sql| pattern === sql }
end
- assert failed_patterns.empty?, "Query pattern(s) #{failed_patterns.map(&:inspect).join(', ')} not found.#{$queries_executed.size == 0 ? '' : "\nQueries:\n#{$queries_executed.join("\n")}"}"
+ assert failed_patterns.empty?, "Query pattern(s) #{failed_patterns.map{ |p| p.inspect }.join(', ')} not found.#{$queries_executed.size == 0 ? '' : "\nQueries:\n#{$queries_executed.join("\n")}"}"
end
def assert_queries(num = 1)

0 comments on commit a7eb8d9

Please sign in to comment.