I’ve attached a small ruby program that will take a patch file and
convert it to something suitable to use in a plugin. For example, it
will take a patch like:
Index: activerecord/lib/active_record/associations.rb
— activerecord/lib/active_record/associations.rb (revision 3072)
+++ activerecord/lib/active_record/associations.rb (working copy)
@@ -902,13 +915,14 @@
primary_key_lookup_table[table_name] =
schema_abbreviations.find { |cn, tc| tc == [ table_name,
primary_key ] }.first
-
reflections.collect do |reflection|
-
primary_key_lookup_table[reflection.klass.table_name] =
schema_abbreviations.find { |cn, tc|
-
tc == [ reflection.klass.table_name,
reflection.klass.primary_key ]
-
reflections.each do |reflection|
-
alias_name = "#{table_name}_#{reflection.name}".to_sym
-
primary_key_lookup_table[alias_name] =
schema_abbreviations.find { |cn, tc|
-
tc == [ alias_name, reflection.klass.primary_key ] }.first end
-
return primary_key_lookup_table
-
primary_key_lookup_table end
@@ -920,12 +934,13 @@
end
def construct_finder_sql_with_included_associations(options,
schema_abbreviations, reflections)
-
sql = "SELECT #{column_aliases(schema_abbreviations)} FROM
#{table_name} "
-
sql << reflections.collect { |reflection|
association_join(reflection) }.to_s
-
sql = "SELECT "
-
reflections.each { |reflection| sql << "
join_#{table_name}_#{reflection.name}.*, " if reflection.macro ==
:has_and_belongs_to_many }
-
sql << " #{column_aliases(schema_abbreviations)} FROM
#{table_name} "
-
reflections.each { |reflection| sql <<
association_join(reflection) }
sql << "#{options[:joins]} " if options[:joins]
add_conditions!(sql, options[:conditions])
-
add_sti_conditions!(sql, reflections) add_limited_ids_condition!(sql, options) if
!using_limitable_reflections?(reflections) && options[:limit]
sql << "ORDER BY #{options[:order]} " if options[:order]
and convert it to something usable in a plugin:
From: activerecord/lib/active_record/associations.rb, lines 917-924
module ActiveRecord
module Associations # :nodoc:
module ClassMethods
private
def generate_primary_key_table(reflections,
schema_abbreviations)
primary_key_lookup_table = {}
primary_key_lookup_table[table_name] =
schema_abbreviations.find { |cn, tc| tc == [ table_name,
primary_key ] }.first
reflections.each do |reflection|
alias_name = “#{table_name}_#{reflection.name}”.to_sym
primary_key_lookup_table[alias_name] =
schema_abbreviations.find { |cn, tc|
tc == [ alias_name, reflection.klass.primary_key ]
}.first
end
primary_key_lookup_table
end
end
end
end
From: activerecord/lib/active_record/associations.rb, lines 936-942
module ActiveRecord
module Associations # :nodoc:
module ClassMethods
private
def construct_finder_sql_with_included_associations(options,
schema_abbreviations, reflections)
sql = "SELECT "
reflections.each { |reflection| sql << "
join_#{table_name}_#{reflection.name}.*, " if reflection.macro ==
:has_and_belongs_to_many }
sql << " #{column_aliases(schema_abbreviations)} FROM
#{table_name} "
reflections.each { |reflection| sql <<
association_join(reflection) }
sql << "#{options[:joins]} " if options[:joins]
add_conditions!(sql, options[:conditions])
add_limited_ids_condition!(sql, options) if
!using_limitable_reflections?(reflections) && options[:limit]
sql << "ORDER BY #{options[:order]} " if options[:order]
add_limit!(sql, options) if
using_limitable_reflections?(reflections)
return sanitize_sql(sql)
end
end
end
end
The program relies on indentation levels, which generally works well
with the Rails source. I’ve used it to convert a 21kb patch to a
plugin (the latest patch in ticket 1562), but it certainly has some
caveats that are noted in the program’s comments. Hopefully someone
else on the list can benefit from it.