Skip to content
This repository has been archived by the owner on Jan 25, 2022. It is now read-only.

Commit

Permalink
トップ:タグクラウド: 文字サイズが平均してしまう問題を解消
Browse files Browse the repository at this point in the history
  • Loading branch information
YOSHIDA Hiroki committed Jan 13, 2014
1 parent aa0eec5 commit 0de27d1
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 6 deletions.
2 changes: 1 addition & 1 deletion app/controllers/home_controller.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
class HomeController < ApplicationController
def index
@clips = Clip.paginate(page: params[:page]).includes([ { image: :likes, comments: :user }, :user, :likes, :tags ])
@hot_tags = Tag.hot(50).shuffle
@hot_tags = Tag.new(50).shuffle
@new_matomes = Matome.limit(5).all
render action: 'next_page' unless first_page?
end
Expand Down
7 changes: 3 additions & 4 deletions app/helpers/home_helper.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
module HomeHelper
def font_size_for_tag_cloud(tag, options={})
options[:base_font_size] = 9
options[:offset_ratio] = 0.5
options[:max_offset_size] = 50
offset_font_size = [tag.name_count * options[:offset_ratio], options[:max_offset_size]].min.floor
options[:base_font_size] ||= 10
options[:max_offset_size] ||= 30
offset_font_size = (options[:max_offset_size] * (tag.name_count.to_f / options[:max_count].to_f)).floor
"font-size: #{options[:base_font_size] + offset_font_size}pt"
end
end
1 change: 1 addition & 0 deletions app/models/tag.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ class Tag < ActiveRecord::Base
scope :uniques, group(:name)
scope :by, lambda {|user| where(user_id: user.is_a?(User) ? user.id : user) if user }
scope :for, lambda {|tagged| where(tagged_id: tagged.id, tagged_type: tagged.class.name) }
scope :new, lambda {|limit| uniques.select("name, COUNT(name) AS name_count, MAX(updated_at) AS newest_updated_at").reorder('newest_updated_at desc').limit(limit) }
scope :hot, lambda {|limit| uniques.select("name, COUNT(name) AS name_count").reorder('name_count desc').limit(limit) }

validates :tagged_id, :presence => true
Expand Down
3 changes: 2 additions & 1 deletion app/views/home/index.html.slim
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,9 @@
.tags
h2 人気タグ
ul
- max_name_count = @hot_tags.map(&:name_count).max
- @hot_tags.each do |tag|
li style="#{font_size_for_tag_cloud(tag)}"
li style="#{font_size_for_tag_cloud(tag, max_count: max_name_count)}"
= link_to "#{tag.name}&nbsp;(#{tag.name_count})".html_safe, tag_path(tag.name)

h2 新着クリップ
Expand Down

0 comments on commit 0de27d1

Please sign in to comment.