Skip to content

Instantly share code, notes, and snippets.

@pixeltrix
Created July 7, 2010 13:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pixeltrix/466668 to your computer and use it in GitHub Desktop.
Save pixeltrix/466668 to your computer and use it in GitHub Desktop.
# Manage cross engine/plugin dependencies by deferring resolution
# until all plugins have been loaded and then including them after
# the main class definition has been loaded.
# lib/mercium.rb
module Mercium
mattr_reader :extensions
@@extensions = {}
def self.register_extension(klass, extension)
extensions[klass] = Array(extensions[klass]) << extension
end
def self.load_extensions(klass)
Array(extensions[klass]).each do |extension|
require_dependency extension
klass.to_s.camelize.constantize.send :include, extension.camelize.constantize
end
end
end
# app/models/user.rb
class User < ActiveRecord::Base
end
Mercium.load_extensions :user
# vendor/plugins/mercium_blog/init.rb
Mercium.register_extension :user, 'user/blogs'
# vendor/plugins/mercium_blog/app/models/user/blogs.rb
class User < ActiveRecord::Base
module Blogs
def self.included(base)
base.class_eval do
has_many :blogs
end
end
end
end
# vendor/plugins/mercium_blog/app/models/blog.rb
class Blog < ActiveRecord::Base
belongs_to :user
end
Mercium.load_extensions :blog
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment