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 with
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