2014-05-08 07:33:39 +00:00
|
|
|
# encoding: utf-8
|
|
|
|
|
2014-05-05 07:57:25 +00:00
|
|
|
class Qa
|
|
|
|
include Mongoid::Document
|
2014-05-08 07:33:39 +00:00
|
|
|
include Mongoid::Timestamps
|
|
|
|
|
|
|
|
# include OrbitModel::LanguageRestrict
|
|
|
|
include OrbitModel::Status
|
|
|
|
include OrbitTag::Taggable
|
|
|
|
include OrbitCategory::Categorizable
|
|
|
|
include Slug
|
|
|
|
|
|
|
|
field :title,as: :slug_title, localize: true
|
2020-03-20 07:20:16 +00:00
|
|
|
field :rich_title, localize: true
|
2014-05-08 07:33:39 +00:00
|
|
|
field :answer, localize: true
|
|
|
|
|
|
|
|
field :create_user_id
|
|
|
|
field :update_user_id
|
|
|
|
field :uid, type: String
|
2016-05-09 17:12:43 +00:00
|
|
|
field :order_position, type: Integer, default: -1
|
2020-03-20 07:20:16 +00:00
|
|
|
field :rss2_id, type: String
|
2014-05-08 07:33:39 +00:00
|
|
|
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
|
2014-06-18 06:41:43 +00:00
|
|
|
|
2016-05-12 08:14:06 +00:00
|
|
|
scope :can_display, ->{where(:is_hidden=>false,:title.ne => "").order_by([:is_top, :desc])}
|
2014-06-18 06:41:43 +00:00
|
|
|
|
2014-05-08 07:33:39 +00:00
|
|
|
|
|
|
|
# belongs_to :qa_category
|
|
|
|
|
|
|
|
before_save :clean_values
|
|
|
|
|
|
|
|
# validates :title, :at_least_one => true
|
2020-03-20 07:20:16 +00:00
|
|
|
after_initialize do |record|
|
|
|
|
if record.rich_title.blank? && !record.title.blank?
|
|
|
|
record.update(:rich_title_translations => record.title_translations)
|
|
|
|
end
|
|
|
|
end
|
2014-05-08 07:33:39 +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.find_by_param(input)
|
|
|
|
self.find_by(uid: input)
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.widget_datas
|
|
|
|
where( :is_hidden => false ).desc(:is_top, :created_at)
|
|
|
|
end
|
|
|
|
|
|
|
|
protected
|
|
|
|
|
|
|
|
def qa_category_with_title
|
|
|
|
self.category.title
|
|
|
|
end
|
|
|
|
|
|
|
|
def clean_values
|
|
|
|
self.qa_links.each do |link|
|
|
|
|
link.delete if link.url.blank? && link.title.blank?
|
|
|
|
end
|
|
|
|
self.tags.delete('')
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|