简体   繁体   中英

Reopen autoloaded class from within a Rails 3 plugin?

I have a Rails 3 app that defines some classes like normal. I am trying to figure out how to reopen one of those classes within a plugin (generated by "rails generate plugin ..."), and have both of the files (the file in the app itself and the file in the plugin) automatically reload on every request in development mode.

A simple example would be:

# root/lib/person.rb
class Person
  Core = 1
end

# root/vendor/plugins/plugin1/lib/person.rb
class Person
  Plugin = 2
end

# root/app/views/home/index.html.erb
<%= Person::Core %> ... <%= Person::Plugin %>

When that view is rendered, I get an error that Bike::Plugin is uninitialized. I have added both root/lib and root/vendor/plugins/plugin1/lib to my autoload_paths (ideally the plugin would add that in its init.rb or somewhere similar, but one thing at a time).

How do I go about this? autoload_at kind of looks like it might help, if I can tell it to autoload the Person class from both locations explicitly, but I have not had any luck (I am completely new to it though, so I may be passing the wrong arguments, etc). In the end I want to do this with classes defined in the standard places (models in particular) and not just lib as well.

one good way plugins can override earlier definitions for any class previously defined (either your libs, or rails internals etc):

#vendor/plugins/myawesomeplugin/lib/person.rb
Person.class_eval do
  Plugin = 2
  # override methods here too if you want
  def name
    "hacked"
  end
end

I've used this approach to write plugins before and overload active record

However, I'm not sure if it's possible to get plugins to auto reload in development mode, they usually reload only when you restart your server in dev mode.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM