Plugin not reloading in development mode
I am having a weird problem with a plugin I wrote. What is weird is that I have one other plugin that opens various ActiveRecord classes and it has no problems auto-reloading.
In the plugins init.rb file I have
User.class_eval do
has_one :reputation
include Karma
alias :rep :reputation
end
If I run it in production mode or in rails console there are no issues. When I run it in development it does load it once, but never again unless I restart the server.
I couldn't find anything special in the other plugin I am using(acts_as_readable) and it opens User in the same manner.
Edit:
I did:
ActiveRecord::Base.class_eval do
class << self
def has_karma_values
has_one :reputation
alias :rep :reputation
end
end
end
in the plugiin's init.rb file and did
class User < ActiveRecord::Base
...
has_karma_values
...
end
And get the same error about has_karma_values not existing. The plugin hasn't been loaded at this point.
What is confusing is that acts-as-readable has no issues at all wit开发者_开发问答h
User.class_eval do
has_many :readings
end
ActiveRecord::Base.send :include, ActiveRecord::Acts::Readable
but yet mine does.
Here is my complete init.rb file
require 'reputation'
require 'karma_name'
require 'karma_tag'
require 'karma_title'
require 'acts_as_karmable'
require 'karma'
require File.expand_path('../lib/generators/karma_generator', __FILE__)
KarmaTitle.setup 'Default'
ActiveRecord::Base.class_eval do
class << self
def has_karma_values
has_one :reputation
alias :rep :reputation
end
end
end
ActiveRecord::Base.send :include, ActiveRecord::Acts::Karmable
Here is the error /home/david/apps/ruby/lib/ruby/gems/1.9.1/gems/activerecord-3.0.9/lib/active_record/base.rb:1014:in
method_missing': undefined local variable or method has_karma_values' for #<Class:0x9ad1b24> (NameError)
This should work:
# plugin init.rb
Rails.configuration.to_prepare do
User.class_eval do
has_one :reputation
include Karma
alias :rep :reputation
end
end
The init file is loaded once, you should not access your models from your plugin, it should be the other way around, your init file should probably be like this:
ActiveRecord::Base.class_eval do
class << self
def acts_as_readable
has_one :reputation
include Karma
alias :rep :reputation
end
end
end
And at your user.rb file you should do it like this:
class User < ActiveRecord::Base
acts_as_readable
end
And this should give you the functionality you expect.
精彩评论