I am trying to write a plugin, ‘acts_as_commentable’, for my models -
Image, Book, Music and so on, I do not want to use polymorphic
association, so very model should have its own comment class.
Here is my code
acts_as_commentable.rb
module Commentable
def self.included(base)
base.extend(ClassMethods)
end
module ClassMethods
def acts_as_commentable(options = {})
class_def = "class Comment < ActiveRecord::Base
set_table_name '#{self.name.downcase}_comments'
belongs_to :user, :class_name=>'::User'
belongs_to :#{self.name.downcase},
:class_name=>’::#{self}’
end"
self.class_eval class_def
has_many :comments, :class_name=>"#{self.name}::Comment",
:order=>‘created_at desc’, :dependent => :delete_all
extend Commentable::SingletonMethods
include Commentable::InstanceMethods
end
end
module SingletonMethods
end
module InstanceMethods
end
end
init.rb
require ‘acts_as_commentable’
ActiveRecord::Base.send(:include, Commentable)
image.rb
acts_as_commentable
This results in Image::Comment, Book::Comment…(I have no idea about
how defind top level classes ImageComment, BookComment…
)
and my controller:
def ImageController
def view
…
@comment=Event::Comment.new #line 45
…
end
end
everything is ok when I start webrick and direct to this page, but when
I refresh my brower, an error ocurr:
NameError in ImageController#view
uninitialized constant Comment
c:/ruby/lib/ruby/gems/1.8/gems/activesupport-1.3.1/lib/active_support/dependencies.rb:100:in
const_missing' c:/ruby/lib/ruby/gems/1.8/gems/activesupport-1.3.1/lib/active_support/dependencies.rb:131:inconst_missing’
c:/ruby/lib/ruby/gems/1.8/gems/activesupport-1.3.1/lib/active_support/dependencies.rb:133:in
const_missing' #{RAILS_ROOT}/app/controllers/image_controller.rb:45:inview’
Anyone know what’s the problem, thanks in advance 

