How/When/Where to Extend Gem Classes (via class_eval and Modules) in Rails 3?
Posted
by viatropos
on Stack Overflow
See other posts from Stack Overflow
or by viatropos
Published on 2010-05-17T08:52:45Z
Indexed on
2010/05/17
9:00 UTC
Read the original article
Hit count: 194
What is the recommended way to extend class behavior, via class_eval
and modules (not by inheritance) if I want to extend a class buried in a Gem from a Rails 3 app?
An example is this:
I want to add the ability to create permalinks for tags and categories (through the ActsAsTaggableOn and ActsAsCategory gems).
They have defined Tag
and Category
models.
I want to basically do this:
Category.class_eval do
has_friendly_id :title
end
Tag.class_eval do
has_friendly_id :title
end
Even if there are other ways of adding this functionality that might be specific to the gem, what is the recommended way to add behavior to classes in a Rails 3 application like this?
I have a few other gems I've created that I want to do this to, such as a Configuration
model and an Asset
model. I would like to be able to add create an app/models/configuration.rb
model class to my app, and it would act as if I just did class_eval
.
Anyways, how is this supposed to work? I can't find anything that covers this from any of the current Rails 3 blogs/docs/gists.
© Stack Overflow or respective owner