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 c791025 commit 082b713
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 5 deletions.
6 changes: 5 additions & 1 deletion app/controllers/matomes_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,14 @@ def show
# 関連まとめ
@related_matomes = {}
# 1. 同じキュレータ
# TODO: インスタンスメソッド化
@related_matomes[:user] = Matome.related_by_user(@matome).limit(5)
# 2. 同じクリップを含む
# TODO: インスタンスメソッド化
@related_matomes[:clip] = Matome.related_by_clips(@matome).limit(5)
# TODO: 3. 同じタグを含む
# 3. 同じタグを含む
# TODO: SQLの最適化
@related_matomes[:tag] = @matome.related_by_tags.take(5)

return if first_page?

Expand Down
32 changes: 32 additions & 0 deletions app/models/matome.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,36 @@ def increment_view_count!(request = nil)
)
end
end

def related_by_tags
(related_by_clip_tags + related_by_matome_tags).uniq
end

def related_by_clip_tags
self.class.
joins(:clips).
merge(related_clips_by_tags).
where.not('matomes.id' => self.id).
group('matomes.id')
end

def related_by_matome_tags
self.class.
joins(:tags).
merge(related_tags).
where.not('matomes.id' => self.id).
group('matomes.id')
end

private

def related_tags
related_tag_ids_to_matome = self.tags.uniques.pluck(:name)
related_tag_ids_to_clips = Tag.uniques.for(self.clips).pluck(:name)
Tag.uniques.where(name: (related_tag_ids_to_matome + related_tag_ids_to_clips).uniq)
end

def related_clips_by_tags
Clip.joins(:tags).merge(related_tags)
end
end
4 changes: 2 additions & 2 deletions app/models/tag.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ class Tag < ActiveRecord::Base
belongs_to :user

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 :by, lambda {|user| where(user_id: user) }
scope :for, lambda {|tagged| where(tagged_id: tagged, tagged_type: tagged.is_a?(Array) ? tagged.first.class.name : 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
Expand Down
4 changes: 2 additions & 2 deletions app/views/matomes/show.html.slim
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,10 @@
- else
= link_to "続きを見る (#{left_clips_count}件)", new_user_registration_path, remote: true

- if @related_matomes[:clip].present?
- if @related_matomes[:clip].present? || @related_matomes[:tag].present?
.related
h2 関連まとめ
== render partial: 'base/matomes', locals: { matomes: @related_matomes[:clip] }
== render partial: 'base/matomes', locals: { matomes: (@related_matomes[:clip] + @related_matomes[:tag]).uniq }

- if @related_matomes[:user].present?
.related
Expand Down

0 comments on commit 082b713

Please sign in to comment.