Sha256: d9efc25a617a0a33417a874bacaae781715bac5f0c2405487610c6bbc75f568b

Contents?: true

Size: 1.59 KB

Versions: 10

Compression:

Stored size: 1.59 KB

Contents

# -*- encoding: utf-8 -*-
class Answer < ActiveRecord::Base
  default_scope :order => 'id ASC'
  #scope :public_answers, where(:shared => true)
  #scope :private_answers, where(:shared => false)
  belongs_to :user, :validate => true
  belongs_to :question, :validate => true
  has_many :answer_has_items, :dependent => :destroy
  has_many :items, :through => :answer_has_items

  after_save :save_questions
  before_save :add_items

  validates_associated :user, :question
  validates_presence_of :user, :question, :body
  validate :check_url_list

  def self.per_page
    10
  end

  def save_questions
    self.question.save
  end

  def add_items
    item_list = item_identifier_list.to_s.strip.split.map{|i| Item.where(:item_identifier => i).first}.compact.uniq
    url_list = add_urls
    self.items = item_list + url_list
  end

  def add_urls
    list = url_list.to_s.strip.split.map{|u| Manifestation.where(:access_address => Addressable::URI.parse(u).normalize.to_s).first}.compact.map{|m| m.web_item}.compact.uniq
  end

  def check_url_list
    url_list.to_s.strip.split.each do |url|
      errors.add(:url_list) unless Addressable::URI.parse(url).host
    end
  end
end


# == Schema Information
#
# Table name: answers
#
#  id                   :integer         not null, primary key
#  user_id              :integer         not null
#  question_id          :integer         not null
#  body                 :text
#  created_at           :datetime
#  updated_at           :datetime
#  deleted_at           :datetime
#  state                :string(255)
#  item_identifier_list :text
#  url_list             :text
#

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
enju_question-0.0.15 app/models/answer.rb
enju_question-0.0.14 app/models/answer.rb
enju_question-0.0.13 app/models/answer.rb
enju_question-0.0.12 app/models/answer.rb
enju_question-0.0.11 app/models/answer.rb
enju_question-0.0.10 app/models/answer.rb
enju_question-0.0.9 app/models/answer.rb
enju_question-0.0.8 app/models/answer.rb
enju_question-0.0.7 app/models/answer.rb
enju_question-0.0.6 app/models/answer.rb