1

I want to be able to override models and controllers of my rails 3 engine in the base app.

Inspecting $LOAD_PATH, I found engine's 'app/{models,controllers}' there, but I can't explicitly require engine's model or controller file: require 'engine_name/model_name' fails with "no such file" (tried with both namespaced(app/controllers/enginename/*) and plain engine).

So, what's the best way to extend engine's models/controllers in rails 3 without copying them to base app?


Basically, it's a load order problem. So, if I explicitly require model from engine, everything's ok, but I hope there is a better way.

Kev
  • 118,037
  • 53
  • 300
  • 385
vitus
  • 101
  • 1
  • 3
  • what do you mean by "users to be able to override models and controllers? – Deepak N Oct 24 '10 at 19:12
  • I mean other developers, that use my engine. Users of engine) – vitus Oct 24 '10 at 19:44
  • Have you tried the following solution? http://stackoverflow.com/questions/2964050/rails-engines-extending-functionality/2990539#2990539 – Andrei Feb 18 '11 at 18:12
  • ... or this one for Rails 3 http://stackoverflow.com/questions/5045068/extending-controllers-of-a-rails-3-engine-in-the-main-app – Andrei Feb 19 '11 at 14:17
  • by explicitly require, do you mean something like this? ````require Qe::Engine.root.join('app', 'controllers', 'qe', 'answer_sheets_controller')````, where ````qe```` is the Engine's isolated namespace? – westonplatter Jul 11 '12 at 17:26

1 Answers1

6

So I actually went back and wrote the documentation. The answer is to Open Class the Controller and Model classes using either,

  • Class#eval_class
  • ActiveSupport::Concern

More details here, http://guides.rubyonrails.org/engines.html#overriding-models-and-controllers

(edited. Changed from "edgeguides" subdomain to "guides" subdomain)

westonplatter
  • 1,475
  • 2
  • 19
  • 30