From 4127d7f2eba28387ea1f450f8e79beb163e53f5b Mon Sep 17 00:00:00 2001 From: Christophe Vilayphiou Date: Fri, 3 Aug 2012 01:35:41 +0800 Subject: [PATCH] Fix tags --- app/models/tag.rb | 47 +++++++++---------- .../widget/bulletins_controller.rb | 2 +- .../bulletins/bulletins_side_bar.html.erb | 4 +- .../news/widget/news_bulletins_controller.rb | 2 +- .../news_bulletins_side_bar.html.erb | 4 +- 5 files changed, 27 insertions(+), 32 deletions(-) diff --git a/app/models/tag.rb b/app/models/tag.rb index fbf57615..eb7e1f77 100644 --- a/app/models/tag.rb +++ b/app/models/tag.rb @@ -9,37 +9,32 @@ class Tag 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=>14.days.ago,:end_at=>Time.now} - for_yesterday = {:start_at=>15.days.ago,:end_at=>1.days.ago} - - range = self.class.all.collect{ |tag| - get_impressionist(tag) + def self.sorted_for_cloud + tags = {} + self.all.each{ |tag| + tags.merge!({tag => self.get_impressionist(tag)}) } - range.sort!.reverse!.uniq! - offset_size = range.size+4 - section_3 = range.first..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]..last rescue nil - - - case get_impressionist - when range.first - return :hot1 - when section_1 - return :hot2 - when section_2 - return :hot3 - when section_3 - return :hot4 + 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 get_impressionist(item_tag = self) + 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 diff --git a/vendor/built_in_modules/announcement/app/controllers/panel/announcement/widget/bulletins_controller.rb b/vendor/built_in_modules/announcement/app/controllers/panel/announcement/widget/bulletins_controller.rb index 152844fa..b19eb46d 100644 --- a/vendor/built_in_modules/announcement/app/controllers/panel/announcement/widget/bulletins_controller.rb +++ b/vendor/built_in_modules/announcement/app/controllers/panel/announcement/widget/bulletins_controller.rb @@ -43,7 +43,7 @@ class Panel::Announcement::Widget::BulletinsController < OrbitWidgetController end def bulletins_side_bar - @tags = AnnouncementTag.all + @tags_and_class = AnnouncementTag.sorted_for_cloud get_categorys end diff --git a/vendor/built_in_modules/announcement/app/views/panel/announcement/widget/bulletins/bulletins_side_bar.html.erb b/vendor/built_in_modules/announcement/app/views/panel/announcement/widget/bulletins/bulletins_side_bar.html.erb index ce06d2e7..de7d9651 100644 --- a/vendor/built_in_modules/announcement/app/views/panel/announcement/widget/bulletins/bulletins_side_bar.html.erb +++ b/vendor/built_in_modules/announcement/app/views/panel/announcement/widget/bulletins/bulletins_side_bar.html.erb @@ -12,8 +12,8 @@

<%= t('announcement.tag_cloud') %>

- <% @tags.each do |tag| %> - <%= link_to tag[I18n.locale], panel_announcement_front_end_bulletins_path(:tag_id => tag.id, :category_id => params[:category_id]), :class => "#{tag.cloud_amper} hot#{rand(4) + 1} #{(tag.id.to_s.eql?(params[:tag_id]) || tag.key.eql?(params[:tag_id])) ? 'active' : nil} " %> + <% @tags_and_class.each do |tag| %> + <%= link_to tag[0][I18n.locale], panel_announcement_front_end_bulletins_path(:tag_id => tag[0].id, :category_id => params[:category_id]), :class => "#{tag[1]} #{(tag[0].id.to_s.eql?(params[:tag_id]) || tag[0].key.eql?(params[:tag_id])) ? 'active' : nil} " %> <% end %>
diff --git a/vendor/built_in_modules/news/app/controllers/panel/news/widget/news_bulletins_controller.rb b/vendor/built_in_modules/news/app/controllers/panel/news/widget/news_bulletins_controller.rb index e4040ec0..c713fbb7 100644 --- a/vendor/built_in_modules/news/app/controllers/panel/news/widget/news_bulletins_controller.rb +++ b/vendor/built_in_modules/news/app/controllers/panel/news/widget/news_bulletins_controller.rb @@ -43,7 +43,7 @@ class Panel::News::Widget::NewsBulletinsController < OrbitWidgetController end def news_bulletins_side_bar - @tags = NewsTag.all + @tags_and_class = NewsTag.sorted_for_cloud get_categorys end diff --git a/vendor/built_in_modules/news/app/views/panel/news/widget/news_bulletins/news_bulletins_side_bar.html.erb b/vendor/built_in_modules/news/app/views/panel/news/widget/news_bulletins/news_bulletins_side_bar.html.erb index bf127ed2..d60cf04b 100644 --- a/vendor/built_in_modules/news/app/views/panel/news/widget/news_bulletins/news_bulletins_side_bar.html.erb +++ b/vendor/built_in_modules/news/app/views/panel/news/widget/news_bulletins/news_bulletins_side_bar.html.erb @@ -12,8 +12,8 @@

<%= t('announcement.tag_cloud') %>

- <% @tags.each do |tag| %> - <%= link_to tag[I18n.locale], panel_news_front_end_news_bulletins_path(:tag_id => tag.id, :category_id => params[:category_id]),:class => "#{tag.cloud_amper} hot#{rand(4) + 1} #{(tag.id.to_s.eql?(params[:tag_id]) || tag.key.eql?(params[:tag_id])) ? 'active' : nil} " %> + <% @tags_and_class.each do |tag| %> + <%= link_to tag[0][I18n.locale], panel_news_front_end_news_bulletins_path(:tag_id => tag[0].id, :category_id => params[:category_id]),:class => "#{tag[1]} #{(tag[0].id.to_s.eql?(params[:tag_id]) || tag[0].key.eql?(params[:tag_id])) ? 'active' : nil} " %> <% end %>