2012-12-28 02:43:48 +00:00
|
|
|
# class Tag < ProtoTag
|
|
|
|
# belongs_to :module_app
|
|
|
|
|
|
|
|
# end
|
|
|
|
|
|
|
|
class Tag
|
|
|
|
include Mongoid::Document
|
|
|
|
include Mongoid::Timestamps
|
|
|
|
include Impressionist::Impressionable
|
|
|
|
|
|
|
|
is_impressionable :counter_cache => { :column_name => :view_count }
|
|
|
|
|
2013-04-22 11:34:37 +00:00
|
|
|
field :name, localize: true
|
2013-04-23 12:17:35 +00:00
|
|
|
field :cloud_view_count, type: Integer, default: 0
|
2013-04-25 04:34:06 +00:00
|
|
|
field :is_default, type: Boolean, default: false
|
2013-04-23 12:17:35 +00:00
|
|
|
field :view_count, type: Integer, default: 0
|
2013-01-08 04:36:50 +00:00
|
|
|
|
2013-04-25 04:34:06 +00:00
|
|
|
belongs_to :tag_lease, polymorphic: true
|
2013-04-22 11:34:37 +00:00
|
|
|
has_many :taggings, dependent: :destroy
|
|
|
|
|
2013-04-25 04:34:06 +00:00
|
|
|
validates :name, :at_least_one => true
|
|
|
|
|
|
|
|
before_destroy :destroy_module_tag, if: Proc.new { |tag| tag.tag_lease_type.eql?("ModuleTag") }
|
|
|
|
|
2012-12-28 02:43:48 +00:00
|
|
|
def self.sorted_for_cloud
|
|
|
|
tags = {}
|
|
|
|
self.all.each{ |tag|
|
|
|
|
tags.merge!({tag => self.get_impressionist(tag)})
|
|
|
|
}
|
|
|
|
if !tags.blank?
|
|
|
|
sorted_tags = tags.sort{|a,b| a[1]<=>b[1]}.reverse
|
|
|
|
sorted_tags[0][1] = :hot1
|
|
|
|
offset = (sorted_tags.size - 1) / 3
|
|
|
|
i = 1
|
|
|
|
class_i = 2
|
|
|
|
sorted_tags[1..-1].collect!{ |x|
|
|
|
|
x[1] = "hot#{class_i}"
|
|
|
|
i == offset ? i = 1 : i += 1 if class_i < 4
|
|
|
|
class_i += 1 if i == offset && class_i < 4
|
|
|
|
}
|
|
|
|
sorted_tags
|
|
|
|
else
|
|
|
|
[]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
protected
|
|
|
|
|
|
|
|
def self.get_impressionist(item_tag = self)
|
|
|
|
item_tag.impressions.where(:created_at.gte=> 14.days.ago,:created_at.lte => Time.now).count
|
|
|
|
end
|
2012-07-31 03:57:32 +00:00
|
|
|
|
2013-04-25 04:34:06 +00:00
|
|
|
private
|
|
|
|
|
|
|
|
def destroy_module_tag
|
|
|
|
ModuleTag.without_callback(:destroy, :before, :destroy_tag) do
|
|
|
|
self.tag_lease.destroy
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-02-14 16:32:20 +00:00
|
|
|
end
|