Orbit/vendor/built_in_modules/faq/app/models/qa.rb

90 lines
1.9 KiB
Ruby
Raw Normal View History

2013-03-28 04:06:10 +00:00
# encoding: utf-8
class Qa
include Mongoid::Document
include Mongoid::Timestamps
include Mongoid::MultiParameterAttributes
scope :available_for_lang, ->(locale){ where("available_for_#{locale}".to_sym => true) }
field :title, localize: true
2013-03-28 04:06:10 +00:00
field :answer, localize: true
has_and_belongs_to_many :tags, :class_name => "FaqTag"
field :create_user_id
field :update_user_id
field :is_top, :type => Boolean, :default => false
field :is_hot, :type => Boolean, :default => false
field :is_hidden, :type => Boolean, :default => false
scope :can_display,where(is_hidden: false)
has_many :qa_links, :autosave => true, :dependent => :destroy
has_many :qa_files, :autosave => true, :dependent => :destroy
accepts_nested_attributes_for :qa_files, :allow_destroy => true
accepts_nested_attributes_for :qa_links, :allow_destroy => true
belongs_to :qa_category
before_save :update_avliable_language, :clean_values
validates :title, :at_least_one => true
2013-03-28 04:06:10 +00:00
def self.search( category_id = nil )
if category_id.to_s.size > 0
find(:all, :conditions => {qa_category_id: category_id}).desc( :is_top, :title )
else
find(:all).desc( :is_top, :title)
end
end
def self.widget_datas
where( :is_hidden => false ).desc(:is_top, :created_at)
end
def is_top?
self.is_top
end
def sorted_tags
tags.order_by(I18n.locale, :asc)
end
def update_avliable_language
VALID_LOCALES.each do |locale|
if (title_translations[locale].blank? rescue true)
2013-03-28 04:06:10 +00:00
self["available_for_#{locale}".to_sym] = false
else
self["available_for_#{locale}".to_sym] = true
end
end
end
protected
def qa_category_with_title
self.qa_category.title
end
def clean_values
self.qa_links.each do |link|
link.delete if link.url.blank? && link.title.blank?
end
self.tag_ids.delete('')
end
end