Orbit/app/models/tag.rb

42 lines
1.0 KiB
Ruby
Raw Normal View History

2012-02-14 16:32:20 +00:00
class Tag
include Mongoid::Document
include Mongoid::Timestamps
2012-07-30 03:59:20 +00:00
include Impressionist::Impressionable
2012-02-14 16:32:20 +00:00
2012-07-30 03:59:20 +00:00
is_impressionable :counter_cache => { :column_name => :view_count }
field :key
field :view_count, :type => Integer, :default => 0
2012-07-30 03:59:20 +00:00
#field :cloud_amper,:type: Integer,:default=> 0
2012-02-14 16:32:20 +00:00
belongs_to :module_app
2012-07-31 03:57:32 +00:00
2012-08-02 17:35:41 +00:00
def self.sorted_for_cloud
tags = {}
self.all.each{ |tag|
tags.merge!({tag => self.get_impressionist(tag)})
2012-07-30 03:59:20 +00:00
}
2012-08-02 17:35:41 +00:00
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
[]
2012-07-31 03:57:32 +00:00
end
2012-07-30 03:59:20 +00:00
end
2012-08-02 17:35:41 +00:00
protected
def self.get_impressionist(item_tag = self)
2012-08-01 10:15:10 +00:00
item_tag.impressions.where(:created_at.gte=> 14.days.ago,:created_at.lte => Time.now).count
2012-07-30 03:59:20 +00:00
end
2012-02-14 16:32:20 +00:00
end