Converting patches to plugins

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.

I’ve attached a small ruby program that will take a patch file and
convert it to something suitable to use in a plugin.

I haven’t played yet with this, but it looks pretty cool.
Thanks!

Rick

http://www.rickbradley.com MUPRN: 604
| in a brutal
random email haiku | home=20 invasion, the whole
| city is stunned.

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On Nov 21, 2005, at 11:18 AM, Jeremy E. wrote:

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:

Totally cool. Nice hack.

jeremy
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2 (Darwin)

iD8DBQFDglqwAQHALep9HFYRAv/OAKCPZHqsQnPBiK4qG7YfQCe9t9z6fwCg16rz
KE1xNosP1vrY7O98yBQAvCQ=
=ai++
-----END PGP SIGNATURE-----