Skip to content

Instantly share code, notes, and snippets.

@westonplatter
Created July 24, 2012 09: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 westonplatter/3169009 to your computer and use it in GitHub Desktop.
Save westonplatter/3169009 to your computer and use it in GitHub Desktop.
Rails Docs - Engines - better formatting
# MyApp/app/models/blorgh/post.rb
# overrides Blorgh's original Post model

class Blorgh::Post < ActiveRecord::Base
  include Blorgh::Concerns::Models::Post

  def time_since_created
    Time.current - created_at
  end
end
# Blorgh/app/models/post.rb
# this class is overriden by the MyApp Post model

class Post < ActiveRecord::Base
  include Blorgh::Concerns::Models::Post
end
# Blorgh/app/concerns/models/post

module Blorg::Concerns::Models::Post
  extend ActiveSupport::Concern
  
  # 'included do' causes the code within to be evaluated in the conext
  #   where it is included, rather be executed in the module's context.
  included do
    attr_accessor :author_name
    belongs_to :author, :class_name => "User"
     
    before_save :set_author
     
    private
    
    def set_author
      self.author = User.find_or_create_by_name(author_name)
    end
  end

  # methods defined here will be instance methods by default
  def some_method
    'some method string'
  end

  module ClassMethods
    def some_class_method
      'some class method string'
    end
  end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment