# Modularize application

**URL:** <https://www.ruby-forum.com/t/modularize-application/151093>\
**Category:** Rails\
**Created:** [October 16, 2008, 7:16pm UTC](https://www.ruby-forum.com/t/modularize-application/151093 "2008-10-16T19:16:39Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![alfredogz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/dbc845/32.png) [@alfredogz](https://www.ruby-forum.com/u/alfredogz)\
**Post date:** [October 16, 2008, 7:16pm UTC](https://www.ruby-forum.com/t/modularize-application/151093/1 "2008-10-16T19:16:39Z")

</div>

Hello.  
WhatÂ´s the best way to extend a rails base application?.  
I explain myself.  
I have been writting an e-commerce application and while in production  
extending it to client needings.

I have rewritten the code adding the new features and controlling all  
these changes with subversion.

Now I would wish to modularize the application starting with the  
simplest possible e-commerce application and having all the “extra”  
features isolated.

I have been taking a look at engines but IÂ´m not sure if I need this  
level of complexity.

I also know generators but I donÂ´t feel confortable with this approach.

WhatÂ´s the best way to isolate a base application and keep growing it  
with new features?.

Needings are:  
\*add,overwrite models,controllers,helpers methods  
\*add,overwrite views with possibility to render the overwritten view  
from the new view.This is the most complex part because several modules  
could overwrite the same view.  
\*for css,js and images IÂ´m not sure if i would use generators or perhaps  
the posibility to define several public directories with preference  
order.

Any suggestions?. Thanks  
Excuse my poor english.  
Alfredo Garcia - Spain

---

<div class="post-metadata">

**Author:** ![alfredogz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/dbc845/32.png) [@alfredogz](https://www.ruby-forum.com/u/alfredogz)\
**Post date:** [October 17, 2008, 9:25pm UTC](https://www.ruby-forum.com/t/modularize-application/151093/2 "2008-10-17T21:25:21Z")

</div>

On Oct 16, 10:16 am, Alfredo Garcia lopez \<rails-mailing-l…@andreas-  
[s.net](http://s.net)\> wrote:

> Needings are:  
> \*add,overwrite models,controllers,helpers methods  
> \*add,overwrite views with possibility to render the overwritten view  
> from the new view.This is the most complex part because several modules  
> could overwrite the same view.  
> \*for css,js and images I´m not sure if i would use generators or perhaps  
> the posibility to define several public directories with preference  
> order.

It sounds like these are some ambitious goals! I’d recommend starting  
with the simplest components and extracting more and more as necessary  
to keep it manageable. For the simple functionality extracting it into  
Modules in /lib and including them in your Models and Controllers can  
help to keep things DRY and cohesive.

Example:  
class Commerce::CommerceController \< ApplicationController  
include CustomerLoader  
layout ‘commerce’  
…  
end

module CustomerLoader

# Makes cart and customer available as ActionView helper method.

def self.included(base)  
base.send :helper\_method, :current\_customer, :cart if  
base.respond\_to? :helper\_method  
end

protected  
def current\_customer  
…  
end

def cart  
…  
end  
end

After you get some Modules separated it may make sense to group those  
together and bundle it as a gem. I don’t have much experience with  
that but it is an excellent tool for code sharing and reuse,  
especially with Rails 2.1 config.gem shizzy.

Additionally you can use inheritance to isolate the places that need  
to change. Be sure that you’re dealing with is-a relationships though.

Example:  
module Commerce  
class ProductsController \< CommerceController  
…  
end  
end

For your CSS and .js I think that having different layouts that  
include the correct files would be the best way. You could even choose  
the layout dynamically.

For some more tips look around the code of some of your favorite  
plugins and see how they’ve handled these issues. The ways I’ve  
suggested are just a few and won’t get you all the way there, but I  
hope that they’ve been a good starting point and will open up the  
discussion to some more techniques.

Hope to have helped,  
Dimo  
[http://strd6.com/blog](http://strd6.com/blog)
