1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Removing most of the symbol to proc usage in Active Record

This will hopefully make Active Record run a bit more faster.
This commit is contained in:
Prem Sichanugrist 2010-08-14 05:59:15 +07:00 committed by Aaron Patterson
parent 1f250415fe
commit a7eb8d97a4
15 changed files with 28 additions and 28 deletions

View file

@ -35,7 +35,7 @@ module ActiveRecord
through_reflection = reflection.through_reflection through_reflection = reflection.through_reflection
source_reflection_names = reflection.source_reflection_names source_reflection_names = reflection.source_reflection_names
source_associations = reflection.through_reflection.klass.reflect_on_all_associations.collect { |a| a.name.inspect } 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
end end
@ -1500,14 +1500,14 @@ module ActiveRecord
redefine_method("#{reflection.name.to_s.singularize}_ids") do redefine_method("#{reflection.name.to_s.singularize}_ids") do
if send(reflection.name).loaded? || reflection.options[:finder_sql] if send(reflection.name).loaded? || reflection.options[:finder_sql]
send(reflection.name).map(&:id) send(reflection.name).map { |r| r.id }
else else
if reflection.through_reflection && reflection.source_reflection.belongs_to? if reflection.through_reflection && reflection.source_reflection.belongs_to?
through = reflection.through_reflection through = reflection.through_reflection
primary_key = reflection.source_reflection.primary_key_name 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}") }
else 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 end
end end
@ -1529,7 +1529,7 @@ module ActiveRecord
pk_column = reflection.primary_key_column pk_column = reflection.primary_key_column
ids = (new_value || []).reject { |nid| nid.blank? } ids = (new_value || []).reject { |nid| nid.blank? }
ids.map!{ |i| pk_column.type_cast(i) } 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 end
end end

View file

@ -127,7 +127,7 @@ module ActiveRecord
def record_timestamp_columns(record) def record_timestamp_columns(record)
if record.record_timestamps if record.record_timestamps
record.send(:all_timestamp_attributes).map(&:to_s) record.send(:all_timestamp_attributes).map { |x| x.to_s }
else else
[] []
end end

View file

@ -231,7 +231,7 @@ module ActiveRecord
def nested_records_changed_for_autosave? def nested_records_changed_for_autosave?
self.class.reflect_on_all_autosave_associations.any? do |reflection| self.class.reflect_on_all_autosave_associations.any? do |reflection|
association = association_instance_get(reflection.name) 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
end end

View file

@ -519,7 +519,7 @@ module ActiveRecord #:nodoc:
# Attributes listed as readonly will be used to create a new record but update operations will # Attributes listed as readonly will be used to create a new record but update operations will
# ignore these fields. # ignore these fields.
def attr_readonly(*attributes) 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 end
# Returns an array of all the attributes that have been specified as readonly. # Returns an array of all the attributes that have been specified as readonly.
@ -1286,7 +1286,7 @@ MSG
table = Arel::Table.new(self.table_name, :engine => arel_engine, :as => default_table_name) table = Arel::Table.new(self.table_name, :engine => arel_engine, :as => default_table_name)
builder = PredicateBuilder.new(arel_engine) 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 end
alias_method :sanitize_sql_hash, :sanitize_sql_hash_for_conditions alias_method :sanitize_sql_hash, :sanitize_sql_hash_for_conditions
@ -1737,7 +1737,7 @@ MSG
klass = (self.class.reflect_on_aggregation(name.to_sym) || column_for_attribute(name)).klass 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. # 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. # 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? if values.empty?
send(name + "=", nil) send(name + "=", nil)

View file

@ -528,7 +528,7 @@ module ActiveRecord
# concatenated together. This string can then be prepended and appended to # concatenated together. This string can then be prepended and appended to
# to generate the final SQL to create the table. # to generate the final SQL to create the table.
def to_sql def to_sql
@columns.map(&:to_sql) * ', ' @columns.map { |c| c.to_sql } * ', '
end end
private private

View file

@ -450,7 +450,7 @@ module ActiveRecord
version = version.to_i version = version.to_i
sm_table = quote_table_name(ActiveRecord::Migrator.schema_migrations_table_name) 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| versions = Dir["#{migrations_path}/[0-9]*_*.rb"].map do |filename|
filename.split('/').last.split('_').first.to_i filename.split('/').last.split('_').first.to_i
end end

View file

@ -879,7 +879,7 @@ module ActiveRecord
# Construct a clean list of column names from the ORDER BY clause, removing # Construct a clean list of column names from the ORDER BY clause, removing
# any ASC/DESC modifiers # any ASC/DESC modifiers
order_columns = order_by.split(',').collect { |s| s.split.first } 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}" } 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 # Return a DISTINCT ON() clause that's distinct on the columns we want but includes

View file

@ -40,11 +40,11 @@ module ActiveRecord
include Comparable include Comparable
def initialize(version_string) def initialize(version_string)
@version = version_string.split('.').map(&:to_i) @version = version_string.split('.').map { |v| v.to_i }
end end
def <=>(version_string) def <=>(version_string)
@version <=> version_string.split('.').map(&:to_i) @version <=> version_string.split('.').map { |v| v.to_i }
end end
end end
@ -345,7 +345,7 @@ module ActiveRecord
name = name[5..-1] name = name[5..-1]
end 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| columns = index.columns.map {|c| rename[c] || c }.select do |column|
to_column_names.include?(column) to_column_names.include?(column)
end end

View file

@ -690,7 +690,7 @@ class Fixtures < (RUBY_VERSION < '1.9' ? YAML::Omap : Hash)
end end
def column_names def column_names
@column_names ||= @connection.columns(@table_name).collect(&:name) @column_names ||= @connection.columns(@table_name).collect { |c| c.name }
end end
def read_fixture_files def read_fixture_files
@ -908,7 +908,7 @@ module ActiveRecord
def uses_transaction(*methods) def uses_transaction(*methods)
@uses_transaction = [] unless defined?(@uses_transaction) @uses_transaction = [] unless defined?(@uses_transaction)
@uses_transaction.concat methods.map(&:to_s) @uses_transaction.concat methods.map { |m| m.to_s }
end end
def uses_transaction?(method) def uses_transaction?(method)

View file

@ -374,7 +374,7 @@ module ActiveRecord
end end
def method_missing(method, *arguments, &block) 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 say_with_time "#{method}(#{arg_list})" do
unless arguments.empty? || method == :execute unless arguments.empty? || method == :execute
@ -451,7 +451,7 @@ module ActiveRecord
def get_all_versions def get_all_versions
table = Arel::Table.new(schema_migrations_table_name) 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 end
def current_version def current_version
@ -569,7 +569,7 @@ module ActiveRecord
klasses << migration klasses << migration
end end
migrations = migrations.sort_by(&:version) migrations = migrations.sort_by { |m| m.version }
down? ? migrations.reverse : migrations down? ? migrations.reverse : migrations
end end
end end

View file

@ -122,7 +122,7 @@ module ActiveRecord
end end
def define_callbacks(klass) 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 = self
observer_name = observer.class.name.underscore.gsub('/', '__') observer_name = observer.class.name.underscore.gsub('/', '__')

View file

@ -375,7 +375,7 @@ module ActiveRecord
def references_eager_loaded_tables? def references_eager_loaded_tables?
# always convert table names to downcase as in Oracle quoted table names are in uppercase # 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? (tables_in_string(to_sql) - joined_tables).any?
end end
@ -383,7 +383,7 @@ module ActiveRecord
return [] if string.blank? return [] if string.blank?
# always convert table names to downcase as in Oracle quoted table names are in uppercase # 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 # 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
end end

View file

@ -348,7 +348,7 @@ module ActiveRecord
end end
def using_limitable_reflections?(reflections) def using_limitable_reflections?(reflections)
reflections.none?(&:collection?) reflections.none? { |r| r.collection? }
end end
end end

View file

@ -123,7 +123,7 @@ HEADER
end.compact end.compact
# find all migration keys used in this table # 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 # 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 } lengths = keys.map{ |key| column_specs.map{ |spec| spec[key] ? spec[key].length + 2 : 0 }.max }

View file

@ -21,7 +21,7 @@ module ActiveRecord
patterns_to_match.each do |pattern| patterns_to_match.each do |pattern|
failed_patterns << pattern unless $queries_executed.any?{ |sql| pattern === sql } failed_patterns << pattern unless $queries_executed.any?{ |sql| pattern === sql }
end 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 end
def assert_queries(num = 1) def assert_queries(num = 1)