# Where do I put code for my own types?

**URL:** <https://www.ruby-forum.com/t/where-do-i-put-code-for-my-own-types/109896>\
**Category:** Rails\
**Created:** [July 20, 2007, 12:04am UTC](https://www.ruby-forum.com/t/where-do-i-put-code-for-my-own-types/109896 "2007-07-20T00:04:40Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![dnstevenson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/8dc957/32.png) [@dnstevenson](https://www.ruby-forum.com/u/dnstevenson)\
**Post date:** [July 20, 2007, 12:04am UTC](https://www.ruby-forum.com/t/where-do-i-put-code-for-my-own-types/109896/1 "2007-07-20T00:04:40Z")

</div>

Sorry for all the questions today…

Beyond the obvious things–models, controllers, helpers, and other  
framework-related types–where should I put the code for my own types?  
For example, I was thinking of writing my own AJAX-friendly paginator,  
but I don’t know where to put the code for that sort of thing.

I would prefer to keep things object-oriented, so I would rather do a  
lot of work with classes (even if their lifespan is short during a  
request) than use helper functions for everything.

Does it belong in the /vendor directory?

---

<div class="post-metadata">

**Author:** ![dnstevenson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/8dc957/32.png) [@dnstevenson](https://www.ruby-forum.com/u/dnstevenson)\
**Post date:** [July 20, 2007, 12:12am UTC](https://www.ruby-forum.com/t/where-do-i-put-code-for-my-own-types/109896/2 "2007-07-20T00:12:11Z")

</div>

Dave S. wrote:

> Sorry for all the questions today…

Bring’em on.

> Beyond the obvious things–models, controllers, helpers, and other  
> framework-related types–where should I put the code for my own types?  
> For example, I was thinking of writing my own AJAX-friendly paginator,  
> but I don’t know where to put the code for that sort of thing.

The /lib folder is for stuff that doesn’t occupy the MVC triad.

> I would prefer to keep things object-oriented, so I would rather do a  
> lot of work with classes (even if their lifespan is short during a  
> request) than use helper functions for everything.

These should become nested classes inside their classic MVC classes.  
Use the lib folder for (roughly speaking) anything that could work  
alone, without any Rails around it.

> Does it belong in the /vendor directory?

Only if it’s a plugin or otherwise from someone else.

–  
Phlip  
[http://www.oreilly.com/catalog/9780596510657/](http://www.oreilly.com/catalog/9780596510657/)  
“Test Driven Ajax (on Rails)”  
assert\_xpath, assert\_javascript, & assert\_ajax

---

<div class="post-metadata">

**Author:** ![dnstevenson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/8dc957/32.png) [@dnstevenson](https://www.ruby-forum.com/u/dnstevenson)\
**Post date:** [July 20, 2007, 12:42am UTC](https://www.ruby-forum.com/t/where-do-i-put-code-for-my-own-types/109896/3 "2007-07-20T00:42:14Z")

</div>

On Jul 19, 2007, at 17:04 , Dave S. wrote:

> Beyond the obvious things–models, controllers, helpers, and other  
> framework-related types–where should I put the code for my own types?

> Does it belong in the /vendor directory?

If you’ve written them as plugins (which is a nice, portable idea).  
Otherwise in lib/

Michael G.  
grzm seespotcode net

---

<div class="post-metadata">

**Author:** ![dnstevenson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/8dc957/32.png) [@dnstevenson](https://www.ruby-forum.com/u/dnstevenson)\
**Post date:** [July 20, 2007, 5:30am UTC](https://www.ruby-forum.com/t/where-do-i-put-code-for-my-own-types/109896/4 "2007-07-20T05:30:25Z")

</div>

Thanks guys… I’m not sure how I missed the ‘lib’ directory.

Is everything namespaced based on its folder in there? So something in  
‘lib/foo/bar.rb’ will need a “require ‘foo’” at the top of any file that  
uses it?

---

<div class="post-metadata">

**Author:** ![dnstevenson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/8dc957/32.png) [@dnstevenson](https://www.ruby-forum.com/u/dnstevenson)\
**Post date:** [July 20, 2007, 8:02am UTC](https://www.ruby-forum.com/t/where-do-i-put-code-for-my-own-types/109896/5 "2007-07-20T08:02:50Z")

</div>

On 7/19/07, Dave S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Sorry for all the questions today…
> 
> Beyond the obvious things–models, controllers, helpers, and other  
> framework-related types–where should I put the code for my own types?  
> For example, I was thinking of writing my own AJAX-friendly paginator,  
> but I don’t know where to put the code for that sort of thing.

if you need an ajax friendly paginator, I’d suggest using the  
will\_paginate  
plugin ([http://errtheblog.com/post/4791](http://errtheblog.com/post/4791)) and then apply the following  
patch:

[http://pastie.caboo.se/80535](http://pastie.caboo.se/80535)

you can then paginate your collection of objects as follows:

\<%= will\_paginate @products, :ajax\_method =\> ‘my\_ajax\_callback’ %\>

and of course you’d replace @products and ‘my\_ajax\_callback’ with the  
name  
of your objects and ajax pagination method.

Adam

---

<div class="post-metadata">

**Author:** ![dnstevenson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/8dc957/32.png) [@dnstevenson](https://www.ruby-forum.com/u/dnstevenson)\
**Post date:** [July 20, 2007, 4:55pm UTC](https://www.ruby-forum.com/t/where-do-i-put-code-for-my-own-types/109896/6 "2007-07-20T16:55:35Z")

</div>

Thanks, I’ll try that! I’m a bit new to this… how do I “apply the  
patch”?

---

<div class="post-metadata">

**Author:** ![dnstevenson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/8dc957/32.png) [@dnstevenson](https://www.ruby-forum.com/u/dnstevenson)\
**Post date:** [July 20, 2007, 7:03pm UTC](https://www.ruby-forum.com/t/where-do-i-put-code-for-my-own-types/109896/7 "2007-07-20T19:03:47Z")

</div>

On 7/20/07, Dave S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Thanks, I’ll try that! I’m a bit new to this… how do I “apply the  
> patch”?

copy the contents of the patch into a new textfile, call it something  
like “view\_helpers\_ajax.patch” and place it in the same directory as  
your view\_helpers.rb file (it’ll be in  
#{RAILS\_ROOT}/vendor/plugins/will\_paginate/lib/). Then just enter the  
following at the command line:

patch -p0 \<view\_helpers\_ajax.patch

You should see the following output:

## Hmm… Looks like a unified diff to me… The text leading up to this was:

|— view\_helpers.rb Fri Jul 20 01:55:04 2007

| +++ view\_helpers\_ajax.rb Fri Jul 20 01:54:26 2007 |
| --- |
| Patching file view\_helpers.rb using Plan A… |
| Hunk #1 succeeded at 12. |
| Hunk #2 succeeded at 32. |
| Hunk #3 succeeded at 46. |
| Hunk #4 succeeded at 65. |
| Hunk #5 succeeded at 74. |
| Hunk #6 succeeded at 83. |
| done |

if you get the above, it means that the patch was applied  
successfully. You’ll also find a new file in the same directory named  
“view\_helpers.rb.orig” which is the original unpatched view\_helpers.rb  
file (in case everything fails horribly and you want to go back to the  
original file). Restart your web server and you should be good to go.

Adam

---

<div class="post-metadata">

**Author:** ![dnstevenson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/8dc957/32.png) [@dnstevenson](https://www.ruby-forum.com/u/dnstevenson)\
**Post date:** [July 20, 2007, 7:39pm UTC](https://www.ruby-forum.com/t/where-do-i-put-code-for-my-own-types/109896/8 "2007-07-20T19:39:50Z")

</div>

I’m doing this in windows. I have the Gnuwin32 version of patch, but it  
isn’t running correctly. It crashes with a message:

patching file view\_helpers.rb  
Assertion failed: hunk, file …/patch-2.5.9-src/patch.c, line 354

This application has request the Runtime to terminate it… blah blah

I may just hold off. I’m sure you guys don’t want to deal with this.  
🙂

---

<div class="post-metadata">

**Author:** ![dnstevenson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/8dc957/32.png) [@dnstevenson](https://www.ruby-forum.com/u/dnstevenson)\
**Post date:** [July 20, 2007, 10:52pm UTC](https://www.ruby-forum.com/t/where-do-i-put-code-for-my-own-types/109896/9 "2007-07-20T22:52:16Z")

</div>

Here’s my modified view\_helpers.rb file. Just copy and paste it from  
here:

[http://pastie.caboo.se/80752](http://pastie.caboo.se/80752)

Adam
