Orbit/app/models/tag.rb

49 lines
1.3 KiB
Ruby

class Tag
include Mongoid::Document
include Mongoid::Timestamps
include Impressionist::Impressionable
is_impressionable :counter_cache => { :column_name => :view_count }
field :key
field :view_count, :type => Integer, :default => 0
#field :cloud_amper,:type: Integer,:default=> 0
belongs_to :module_app
def cloud_amper
for_now = {:start_at=>20.days.ago,:end_at=>Time.now}
for_yesterday = {:start_at=>19.days.ago,:end_at=>1.days.ago}
range = self.class.all.collect{ |tag|
get_impressionist_diff(tag)
}
range.sort!.reverse!.uniq!
offset_size = range.size+4
section_3 = range.last..range[-(offset_size/4)]
section_2 = range[-(offset_size/4)-1]..range[-2*(offset_size/4)] rescue nil
section_1 = range[-2*(offset_size/4)-1]..range[1] rescue nil
case get_impressionist_diff
when range.first
return :heading1
when section_1
return :heading2
when section_2
return :heading3
when section_3
return :heading4
end
end
protected
def get_impressionist_diff(item_tag = self)
for_now = item_tag.impressions.where(:created_at.gte=> 20.days.ago,:created_at.lte => Time.now).count
for_yesterday = item_tag.impressions.where(:created_at.gte=> 21.days.ago,:created_at.lte => 1.days.ago).count
for_now - for_yesterday
end
end