diff --git a/app/assets/stylesheets/home.css.scss b/app/assets/stylesheets/home.css.scss index 154f3e1..38093f2 100644 --- a/app/assets/stylesheets/home.css.scss +++ b/app/assets/stylesheets/home.css.scss @@ -12,6 +12,26 @@ } } +#home > .matomes { + margin: 0 auto 50px; + text-align: center; + + li { + @include inline-block; + } +} + +#home > .tags { + margin: 0 auto 50px; + width: $contents_width; + text-align: center; + + li { + display: inline; + margin: 0 10px; + } +} + // bookmarklet #bookmarklet { margin: 20px auto; diff --git a/app/controllers/home_controller.rb b/app/controllers/home_controller.rb index 48d0857..c48d8f2 100644 --- a/app/controllers/home_controller.rb +++ b/app/controllers/home_controller.rb @@ -3,6 +3,8 @@ 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 + @new_matomes = Matome.limit(5).all render action: 'next_page' unless first_page? end diff --git a/app/helpers/home_helper.rb b/app/helpers/home_helper.rb index 23de56a..679573b 100644 --- a/app/helpers/home_helper.rb +++ b/app/helpers/home_helper.rb @@ -1,2 +1,9 @@ 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 + "font-size: #{options[:base_font_size] + offset_font_size}pt" + end end diff --git a/app/models/matome.rb b/app/models/matome.rb index 317c51f..778ab85 100644 --- a/app/models/matome.rb +++ b/app/models/matome.rb @@ -11,7 +11,6 @@ class Matome < ActiveRecord::Base where(user_id: matome.user_id). where.not(id: matome.id) } - scope :related_by_clips, lambda {|matome| related_clip_ids = matome.clips.map(&:id) + matome.clips.map(&:parent_id) joins(:clips). @@ -19,6 +18,7 @@ class Matome < ActiveRecord::Base where.not('matomes.id' => matome.id). group('matomes.id') } + scope :hot, lambda {|limit| reorder('view_count desc').limit(limit) } default_scope order: 'matomes.created_at DESC' diff --git a/app/models/tag.rb b/app/models/tag.rb index 3781c78..9682078 100644 --- a/app/models/tag.rb +++ b/app/models/tag.rb @@ -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 :hot, lambda {|limit| uniques.select("name, COUNT(name) AS name_count").reorder('name_count desc').limit(limit) } validates :tagged_id, :presence => true validates :user_id, :presence => true diff --git a/app/views/home/index.html.slim b/app/views/home/index.html.slim index 77301c2..91b5849 100644 --- a/app/views/home/index.html.slim +++ b/app/views/home/index.html.slim @@ -1,6 +1,16 @@ #home - h2 新着クリップ + .matomes + h2 新着まとめ + == render partial: 'base/matomes', locals: { matomes: @new_matomes } + + .tags + h2 人気タグ + ul + - @hot_tags.each do |tag| + li style="#{font_size_for_tag_cloud(tag)}" + = link_to "#{tag.name} (#{tag.name_count})", tag_path(tag.name) + h2 新着クリップ - if @clips.present? == render partial: '/base/clip_wall' - else