Reputation: 815
I'm extracting part of my app into a Gem, which involves creating some new ActiveRecord models. At the moment these models look something like this:
Class Wordcount < ActiveRecord::Base
belongs_to :keyword
belongs_to :article
end
Class Keyword < ActiveRecord::Base
has_many :wordcounts
has_many :articles, :through => :wordcounts
end
The gem will hook into any ActiveRecord model with String or Text fields, not just my defined Article
model in the existing Rails app this code comes from.
What do I need to do in order to generate the relevant AR associations based on whichever model the gem's functionality is being applied to? I'm hoping to end up with:
has_my_gem_functionality :on => [:field1, :field2, ...]
And for the rest to be safely encapsulated within the gem.
I'm assuming I'll also have to metaprogram the Migrations as well. I think what I'm trying to do is relatively simple, it's just slightly too far out of my comfort zone.
Upvotes: 1
Views: 1086
Reputation: 30404
Use the source, luke! :)
Just take a look at the source of some other gems that do similar things. For example:
Upvotes: 1