Forum: Ruby on Rails Where do I put code for my own types?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Dave S. (Guest)
on 2007-07-20 02:04
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?
Phlip (Guest)
on 2007-07-20 02:12
(Received via mailing list)
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/
 "Test Driven Ajax (on Rails)"
 assert_xpath, assert_javascript, & assert_ajax
Michael G. (Guest)
on 2007-07-20 02:42
(Received via mailing list)
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
Dave S. (Guest)
on 2007-07-20 07:30
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?
Adam C. (Guest)
on 2007-07-20 10:02
(Received via mailing list)
On 7/19/07, Dave S. <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) and then apply the following
patch:

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
Dave S. (Guest)
on 2007-07-20 18:55
Thanks, I'll try that!  I'm a  bit new to this... how do I "apply the
patch"?
Adam C. (Guest)
on 2007-07-20 21:03
(Received via mailing list)
On 7/20/07, Dave S. <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
Dave S. (Guest)
on 2007-07-20 21:39
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.
:-)
Adam C. (Guest)
on 2007-07-21 00:52
(Received via mailing list)
Here's my modified view_helpers.rb file. Just copy and paste it from
here:

http://pastie.caboo.se/80752

Adam
This topic is locked and can not be replied to.