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 Squires
http://somethinglearned.com
On 26-Jun-06, at 7:46 AM, cafe babe wrote:
> 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:in
> `const_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:in `view''
>
> Anyone know what''s the problem, thanks in advance ^_^
>
>
> --
> Posted via http://www.ruby-forum.com/.
> _______________________________________________
> Rails mailing list
> Rails@lists.rubyonrails.org
> http://lists.rubyonrails.org/mailman/listinfo/rails