0

I've got this full rails engine Foo with functionality X.

I want to make another engine, engine Bar, that is pretty much the same, but override funcitonality x with y. (it basically does the same, but a few controller actions and views are differently implemented).

(I might split this later in several mountable engines, but for now, this will be the setup: project Baz, using engine Bar, which uses engine Foo)

I would like to know if there are any pitfalls. It doesn't seem like a pattern that is often used? Anybody else using this 'some sort of engine inheritance'?

Len
  • 2,093
  • 4
  • 34
  • 51

1 Answers1

1

Ruby OpenClassing is what you want to do. Eg,

# in Engine Foo
#   this code creates functionality x

Foo::SomeRubyClass
  # functionality x
  def some_method
    0.10
  end
end


# in Engine Bar
#   this code opens and reevaluates the functionality x in Foo Engine

Foo::SomeRubyClass.class_eval do
  # functionlity x method
  def some_method
    0.05
  end
end

A more thorough explanation with Rails Models/Views/Controllers. http://edgeguides.rubyonrails.org/engines.html#overriding-models-and-controllers

westonplatter
  • 1,475
  • 2
  • 19
  • 30
  • Thanks, I overlooked this simple solution. the consequence is probably, that I should not give my file the same name in Foo as in Bar? – Len Nov 19 '12 at 10:24
  • 1
    Actually, naming it the same makes sense so you can see what is Open Classing what. Open Classing is more generally known as the 'Decorator' pattern, since you are decorating some functionality with new/different features. – westonplatter Nov 19 '12 at 14:28
  • More about the decorator pattern: http://lukeredpath.co.uk/blog/decorator-pattern-with-ruby-in-8-lines.html – westonplatter Nov 19 '12 at 14:49
  • And even more about the decorator pattern: http://en.wikipedia.org/wiki/Decorator_pattern – westonplatter Nov 19 '12 at 14:50