Contrary to popular (?) belief, your app folder’s content is not restricted to models, controllers, helpers and views. You can through some other stuff in there as well!

If you have used sweepers in Rails, then you probably would have also realised that you can put other folders that make sense into your app folder. If you haven’t used sweepers, then you probably aren’t using caching, and if you haven’t used caching, you need to go and watch this video from the Railscasts on Rails Caching now.

Another good example of this, is Rails Presenters.

Now, using, or not using presenters is beside the point, the point IS that you can use your app directory for GASP YOUR application code!

Why do I bring this up?

Well, it is because of the often misused (in my opinion) $RAILS_ROOT/lib folder.

In my opinion, the $RAILS_ROOT/lib folder, should be for Ruby code that comes from an external source, or something that your application uses as an external action. Ideally, you would have in the $RAILS_ROOT/lib folder, code that someone else has written and tested as working somewhere else. Off course, it could also be code YOU have written and tested elsewhere, but it remains that it is code that is not strictly part of your app, but your app uses it.

Then however, you can have a problem, where would you put code that does not specifically relate to a model, but DOES relate to your app and is a critical part of your app that you need to use and refer to from your models? What sort of stuff would this be?

Well, in my app, I generate PDFs, these are pre made PDFs that are for a very specific and unique task. To make this sane, I make a generate_#{name}_pdf.rb file for each one.

Where do I put these?

Well, in the $RAILS_ROOT/app/lib/ folder! It doesn’t exist? Fine, add it!

Once you add it though, you need to tell rails about it so that the files in there get included in the load path. This is quite easy, whack open your environment.rb file and put in:

1
2
3
Rails::Initializer.run do |config|
  config.load_paths << "#{RAILS_ROOT}/app/lib"
end

(Obviously, don’t delete what is already in the Initializer block, I just put the block there for context, just add the line).

Then, once you have done that, you can put your application specific non rails ruby files into the lib directory where they belong.

As an added bonus, when you are in one of these files you can Ctrl-Shift Down Arrow and you will jump to your Rspec spec for the file which will be in specs/lib/filename_spec.rb and this will all work as well.

blogLater

Mikel

3 Responses to “Tip #18 - Take Back Your App Folder!”

  1. Luca G.Soave Says:

    Shouldn’t be placed into ../component dir ? In case you say no, what should be the difference between the two places in term of architectural purpose ?

  2. Mikel Lindsaar Says:

    Heya Luca,

    Well, in the later versions of Rails, the ./component dir is no more, it has been deprecated from the Rails stack, so no, putting it there won’t work.

    From an architectural point of view, I personally think that anything to do with your application should live in your app folder.

    The RAILS_ROOT/lib folder is for stuff that you write that isn’t really part of your application, and RAILS_ROOT/vendor is for 3rd party parts that your application depends on.

    For example, if you wrote some code that does authentication to an LDAP server, it could live in ./lib as you probably use it elsewhere in other rails apps, but a bit of code that converts a PDF document from your rails app and serves it back to your rails app, probably in app/lib as it is an integral part of your application.

    Regards

    Mikel

  3. Luca G.S. Says:

    Many thanks for your precious sharing! bye luca

Leave a Reply