Skip to content

Instantly share code, notes, and snippets.

@juniorz
Created January 19, 2012 11:50
Show Gist options
  • Save juniorz/1639562 to your computer and use it in GitHub Desktop.
Save juniorz/1639562 to your computer and use it in GitHub Desktop.
Rails 3 Auto-concerner
class User < ActiveRecord::Base
end
# Suas engines podem contribuir para a app principal usando Extensions
# AwesomeEngine::Extension.concern! aplica todos os <Model>Concern que ele possui
# ao respectivo Model de maneira "unloadable".
# Isso quer dizer que ao modificar sua Extension, tudo que você precisa é
# fazer reload! no console.
# <3 <3 <3 <3
#app/models/post.rb
class Post < ActiveRecord::Base
belongs_to :user
end
#lib/blog_post/extensions/post_extension.rb
class PostExtension < AwesomeEngine::Extension
module UserConcern
included do
has_many :posts
end
end
end
#lib/blog_post/engine.rb
module BlogPost
class Engine < Rails::Engine
include AwesomeEngine::EngineConcern
config.to_prepare do
PostExtension.concern!
end
end
end
#app/models/comment.rb
class Comment < ActiveRecord::Base
belongs_to :post
end
#lib/blog_comment/extensions/comment_extension.rb
class CommentExtension < AwesomeEngine::Extension
module PostConcern
included do
has_many :comments
end
end
module UserConcern
included do
has_many :comments, :through => :posts
end
end
end
#lib/blog_comment/engine.rb
module BlogComment
class Engine < Rails::Engine
include AwesomeEngine::EngineConcern
config.to_prepare do
CommentExtension.concern!
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment