How can I dynamiclly generate models?

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… :frowning: )

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 :slight_smile:

Image.find(1).comments() and Image::Comment.new work in the console.

It seems people like generating model methods/attrs much more than
generating the whole model classes :frowning:

I add

def self.reloadable?
false
end

to the Comment class definition, it works now :slight_smile:
and I find

class ::ImageComment
end

defines a top level class…

Hey,

I synthesize classes in my acts_as_enumerated plugin. You can see
how I do it here:

http://svn.protocool.com/rails/plugins/enumerations_mixin/trunk/lib/
active_record/virtual_enumerations.rb

In the “synthesize_if_defined” method I create the toplevel class
like this:

eval(class_def, TOPLEVEL_BINDING)

Hope this helps,
Trevor

Trevor S.
http://somethinglearned.com

eval(class_def, TOPLEVEL_BINDING)

This is very much appreciated. Thank you!

thanks
this following code also works

class OuterClass

class_eval "class ::TopLevelCalss
end
"

end

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs