Sha256: 55d95b6a3d06993868fd60f9af3f362e740377ced22f4a3e7058ccbbf893cc3d

Contents?: true

Size: 1.25 KB

Versions: 6

Compression:

Stored size: 1.25 KB

Contents

module Rostra
  class Question < ActiveRecord::Base

    belongs_to :user
    has_many :answers
    has_many :question_followings
    has_many :followers, through: :question_followings, source: :user

    acts_as_taggable
    acts_as_voteable
    is_impressionable

    validates :title, :presence => true, :uniqueness => true, :length => { :minimum => 15 }
    validates :user, :presence => true
    validates :tag_list, :presence => true

    before_save :create_question_following
    attr_accessor :follow_by_email

    # Finds questions asked within the last 15 days ordered by non-unique page views.
    #
    def self.trending(limit = 5)
      Question.where(created_at: (15.days.ago)..(Time.now)).limit(limit)

      # This code doesn't work in postgres.
      # Question
      #   .where(created_at: (15.days.ago)..(Time.now))
      #   .limit(limit)
      #   .joins(:impressions)
      #   .group('impressions.impressionable_id')
      #   .order('count(impressions.impressionable_id) desc')
    end

    def answer_count
      @answer_count = answers.count
    end

    def unique_page_views
      impressionist_count(filter: :ip_address)
    end

    private

    def create_question_following
      followers << user unless follow_by_email.to_i.zero?
    end

  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
rostra-0.0.14 app/models/rostra/question.rb
rostra-0.0.13 app/models/rostra/question.rb
rostra-0.0.12 app/models/rostra/question.rb
rostra-0.0.11 app/models/rostra/question.rb
rostra-0.0.10 app/models/rostra/question.rb
rostra-0.0.9 app/models/rostra/question.rb