Forum: Ruby on Rails Modularize application

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.
Ae4b91b75b0d2ce20b0240b0fb91be1f?d=identicon&s=25 Alfredo Garcia lopez (fredini)
on 2008-10-16 19:16
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
33117162fff8a9cf50544a604f60c045?d=identicon&s=25 Daniel X Moore (yahivin)
on 2008-10-17 21:25
(Received via mailing list)
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
This topic is locked and can not be replied to.