Sha256: aa4ac637d7eac203e1a96cbfd9ad18e20a5f5c3e0bbea4d4d074fa62adf95509

Contents?: true

Size: 1.77 KB

Versions: 13

Compression:

Stored size: 1.77 KB

Contents

# -*- encoding: utf-8 -*-
require 'timeout'
class Question < ActiveRecord::Base
  default_scope :order => 'id DESC'
  scope :public_questions, where(:shared => true)
  scope :private_questions, where(:shared => false)
  scope :solved, where(:solved => true)
  scope :unsolved, where(:solved => false)
  belongs_to :user, :validate => true
  has_many :answers, :dependent => :destroy

  validates_associated :user
  validates_presence_of :user, :body

  searchable do
    text :body, :answer_body
    string :username
    time :created_at
    time :updated_at do
      last_updated_at
    end
    boolean :shared
    boolean :solved
    integer :answers_count
    integer :manifestation_id, :multiple => true do
      answers.collect(&:items).flatten.collect{|i| i.manifestation.id}
    end
  end

  acts_as_taggable_on :tags
  enju_ndl_crd

  def self.per_page
    10
  end

  def self.crd_per_page
    5
  end

  def answer_body
    text = ""
    self.reload
    self.answers.each do |answer|
      text += answer.body
    end
    return text
  end

  def username
    self.user.try(:username)
  end

  def last_updated_at
    if answers.last
      time = answers.last.updated_at
    end
    if time
      if time > updated_at
        time
      else
        updated_at
      end
    else
      updated_at
    end
  end

end

# == Schema Information
#
# Table name: questions
#
#  id            :integer         not null, primary key
#  user_id       :integer         not null
#  body          :text
#  shared        :boolean         default(TRUE), not null
#  answers_count :integer         default(0), not null
#  created_at    :datetime
#  updated_at    :datetime
#  deleted_at    :datetime
#  state         :string(255)
#  solved        :boolean         default(FALSE), not null
#  note          :text
#

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
enju_question-0.0.15 app/models/question.rb
enju_question-0.0.14 app/models/question.rb
enju_question-0.0.13 app/models/question.rb
enju_question-0.0.12 app/models/question.rb
enju_question-0.0.11 app/models/question.rb
enju_question-0.0.10 app/models/question.rb
enju_question-0.0.9 app/models/question.rb
enju_question-0.0.8 app/models/question.rb
enju_question-0.0.7 app/models/question.rb
enju_question-0.0.6 app/models/question.rb
enju_question-0.0.5 app/models/question.rb
enju_question-0.0.4 app/models/question.rb
enju_question-0.0.3 app/models/question.rb