Modularize application


#1

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


#2

On Oct 16, 10:16 am, Alfredo Garcia lopez <rails-mailing-l…@andreas-
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