Sha256: f187064fabdc08b89fb65e8aea765bfd64db10c565b9da46b893ace639ab8b40

Contents?: true

Size: 562 Bytes

Versions: 1

Compression:

Stored size: 562 Bytes

Contents

module HasComments
  def self.included(base)
    base.extend ClassMethods
  end
  
  module ClassMethods
    def has_comments
      has_many :comments, :as => :comment
      send :include, InstanceMethods
    end
  end
  
  module InstanceMethods
    def method_missing(method, *args )
      case method
      when /first_\d+_comments/i
        self.comments.limit($1)
      when /last_\d+_comments/i
        self.comments.limit($1).order("created_at desc")
      else
        super
      end
    end
  end
end

class ActiveRecord::Base
  include HasComments
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
has_comments-0.3.0 lib/has_comments.rb