# Converting patches to plugins

**URL:** <https://www.ruby-forum.com/t/converting-patches-to-plugins/48520>\
**Category:** Rails\
**Created:** [November 21, 2005, 8:22pm UTC](https://www.ruby-forum.com/t/converting-patches-to-plugins/48520 "2005-11-21T20:22:09Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![jeremyevans0](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/f04885/32.png) [@jeremyevans0](https://www.ruby-forum.com/u/jeremyevans0)\
**Post date:** [November 21, 2005, 8:22pm UTC](https://www.ruby-forum.com/t/converting-patches-to-plugins/48520/1 "2005-11-21T20:22:09Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![jeremyevans0](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/f04885/32.png) [@jeremyevans0](https://www.ruby-forum.com/u/jeremyevans0)\
**Post date:** [November 22, 2005, 12:36am UTC](https://www.ruby-forum.com/t/converting-patches-to-plugins/48520/2 "2005-11-22T00:36:08Z")

</div>

- Jeremy E. (removed\_email\_address@domain.invalid) [051121 14:20]:

> 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](http://www.rickbradley.com) MUPRN: 604  
| in a brutal  
random email haiku | home=20 invasion, the whole  
| city is stunned.

---

<div class="post-metadata">

**Author:** ![jeremyevans0](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/f04885/32.png) [@jeremyevans0](https://www.ruby-forum.com/u/jeremyevans0)\
**Post date:** [November 22, 2005, 12:40am UTC](https://www.ruby-forum.com/t/converting-patches-to-plugins/48520/3 "2005-11-22T00:40:09Z")

</div>

-----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-----
