Sha256: c969e4420931f3bf48d62aa962d3dd724bb476388e5c7abd31ce2b8eb6861ca8

Contents?: true

Size: 1.66 KB

Versions: 10

Compression:

Stored size: 1.66 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
    has_many :notified_followers, through: :question_followings, source: :user,
      conditions: { 'rostra_question_followings.send_email_notifications' => true }

    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

    # Set number of questions per page for will paginate
    #
    paginates_per Rostra::Config.number_of_question_per_page

    # 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)

      # Maybe try to use a subquery?
      # User.where(:id => accounts.project(:user_id).where(accounts[:user_id].not_eq(6)))

      # 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

10 entries across 10 versions & 1 rubygems

Version Path
rostra-0.1.8 app/models/rostra/question.rb
rostra-0.1.7 app/models/rostra/question.rb
rostra-0.1.6 app/models/rostra/question.rb
rostra-0.1.5 app/models/rostra/question.rb
rostra-0.1.4 app/models/rostra/question.rb
rostra-0.1.3 app/models/rostra/question.rb
rostra-0.1.2 app/models/rostra/question.rb
rostra-0.1.1 app/models/rostra/question.rb
rostra-0.0.17 app/models/rostra/question.rb
rostra-0.0.16 app/models/rostra/question.rb