Sha256: f980d5cccc2ba7fc2918f4c845462434006e6246288969d8473a8b40d4ff926c

Contents?: true

Size: 1.35 KB

Versions: 4

Compression:

Stored size: 1.35 KB

Contents

module Rostra
  class Question < ActiveRecord::Base
    extend FriendlyId

    belongs_to :user
    has_many :answers, :dependent => :destroy
    has_many :question_followings, :dependent => :destroy
    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 counter_cache: { unique: true }
    friendly_id :title, use: :slugged

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

    before_create :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).order('impressions_count desc')
    end

    def answer_count
      @answer_count = answers.count
    end

    private

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

  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
rostra-0.2.1 app/models/rostra/question.rb
rostra-0.2.0 app/models/rostra/question.rb
rostra-0.1.26 app/models/rostra/question.rb
rostra-0.1.25 app/models/rostra/question.rb