# encoding: utf-8

class WebLink
  include Mongoid::Document
  include Mongoid::Timestamps
  include Mongoid::MultiParameterAttributes
  
  
  field :title, localize: true
  field :context, localize: true
  
  has_and_belongs_to_many :tags, :class_name => "WebResourceTag"
  
  field :url
  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 
  
  belongs_to :web_link_category
  
  validates_presence_of :title

  def self.search( category_id = nil )
  
	if category_id.to_s.size > 0
	
	  find(:all, :conditions => {web_link_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, :title)

  end

  def is_top?
    self.is_top
  end 

  def sorted_tags
    tags.order_by(I18n.locale, :asc)
  end
  
end