On 26 February 2010 17:35, Yanni Mac <li...@ruby-forum.com> wrote:

> I have several apps that share common base models which I am coding into
> a plugin, but each app may need to add some additional code or override
> some code in the plugin model.
>

OK, so let's assume your base model is like this:

class MyPlugin:: Widget::Base < ActiveRecord::Base
   ... some code ...
end


> One of the main differences is that I use multiple database servers in
> alot of these apps, which is managed by the "use_db" plugin.  I am
> trying to figure out a way to use the "use_db" plugin without hard
> coding it into the plugin class.  I dont want to have to change the
> plugin every time I install it in a new app.
>

OK, so then in your app you declare a model as:

class Widget < MyPlugin:: Widget::Base
  use_db :prefix => "slave2_"
end

Alternatively, if you want to declare a User class in your plugin and have
your app use that if it's found, why not just create an initializer in your
application.  As you can read here -
http://railsguts.com/initialization.html - plugins are loaded before
application initializers, so you could put a file in your
config/initializers folder that just does:

class Widget
  use_db :prefix => "slave2_"
end

The Widget class could already have been defined from your plugin
initializer, so you're then just re-opening the class and adding your
use_db.

Can anyone think of a way to create a class inside the rails directories
> that would be able to extend the plugin model and also be able to
> specify my "use_db" database... overriding the default rails database.
> I would also need to be able to add new methods and override methods
> contained in the plugin model.  The model in the plugin contains lots of
> associations, named_scopes, etc.. so that stuff would still need to be
> active in the extended model... so I dont think a module would work.
>

I'm not sure I follow you.  However, you could do the above with the
config/initializers to open your defined plugin class and add your
functionality.  If you want you could just do a simple require within the
config/initializer/widget.rb file that loads the file from within app/models
(which may be a better place for your modifications).

require Rails.root.join("app", "models", "widget")

The reason for this is that you can't just re-open the class within
app/models/widget.rb as that file will only be opened if the class isn't
defined (it's an autoloading path, not an "open every file in this path at
boot time" path), so you need to specifically load it from an
initializer/environment file.


> I have tried using "extends" inside the class, creating a subclass,
> etc.. but nothing seems to give me the behavior I desire.  Any ideas
> guys?


If you can flesh out your requirement/desires a bit more fully I'll happily
work up a solution with you, but I'm going on guesswork a bit at the moment
:-)

Cheers,


Andy

-- 
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To post to this group, send email to rubyonrails-t...@googlegroups.com.
To unsubscribe from this group, send email to 
rubyonrails-talk+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en.

Reply via email to