Commit 043d601a by tady

fix stock view

parent 3684b76d
......@@ -26,6 +26,15 @@ class TagDecorator < Draper::Decorator
h.event_tag_path(name: model.name)
end
# TODO: module
def created_date
model.created_at.strftime('%Y-%m-%d')
end
def structured_name
[model.ancestors.map(&:name), model.name].flatten.join('/')
end
# tagをtree viewで表示する
def tree_view_node
html = ''
......
......@@ -80,12 +80,13 @@ class Post < ActiveRecord::Base
where_list
end)
# 最新のPostを取得
scope :recent, (lambda do |limit = 10|
order(updated_at: :desc).limit(limit)
end)
scope :today, -> { where(arel_table[:updated_at].gt 1.day.ago) }
scope :this_month, -> { where(arel_table[:updated_at].gt 1.month.ago) }
scope :last_month, -> { where(arel_table[:updated_at].gt 2.month.ago).where(arel_table[:updated_at].lt 1.month.ago) }
######################################################################
# Class method
......
......@@ -12,8 +12,6 @@
#
class Tag < ActiveRecord::Base
has_many :post_tags
has_many :posts, through: :post_tags
# for tree structure
has_ancestry
......@@ -21,6 +19,15 @@ class Tag < ActiveRecord::Base
# for versioning
has_paper_trail
######################################################################
# Associations
######################################################################
has_many :post_tags
has_many :posts, through: :post_tags
######################################################################
# Named scope
######################################################################
default_scope { order(updated_at: :desc) }
scope :posts_exist, lambda {
......@@ -30,13 +37,50 @@ class Tag < ActiveRecord::Base
.having('posts_count > 0')
}
scope :recently_created, (lambda do |limit = 10|
order(created_at: :desc).limit(limit)
end)
######################################################################
# Class method
######################################################################
class << self
# 最近投稿されたTagを取得
def recent(limit = 10)
Post.recent(20).map(&:tags).flatten.compact.uniq.take(limit)
end
# TODO: cache
def monthly_popular(limit = 10)
tags_this_month = Hash.new { |h, k| h[k] = 0 } # { <tag.id> => <count> }
tags_last_month = Hash.new { |h, k| h[k] = 0 } # { <tag.id> => <count> }
Post.this_month.each do |post|
post.tags.each { |tag| tags_this_month[tag.id] += 1 }
end
Post.last_month.each do |post|
post.tags.each { |tag| tags_last_month[tag.id] += 1 }
end
tags_this_month_with_score = {}
tags_this_month.each do |tag_id, this_month_count|
next if this_month_count <= 3
tags_this_month_with_score[tag_id] = this_month_count.to_f / (tags_last_month[tag_id] + 1)
end
sorted = tags_this_month_with_score.sort_by { |k, v| -v }.take(limit).to_h
Tag.find(sorted.keys).to_a.zip(sorted.values).to_h
end
end
######################################################################
# Instance method
######################################################################
def recent_posts(limit = 30)
posts.recent(limit)
end
......
......@@ -31,7 +31,7 @@
h2.panel-title
i.fa.fa-rss
| &nbsp;Flow
span.small - 最近投稿された記事
span.small &nbsp;- 最近投稿された記事
ul.list-group.panel-body
- @posts.each do |post|
......@@ -73,4 +73,4 @@
- Tag.recent(10).each_with_index do |tag, i|
a.list-group-item data-tag-id=tag.id href=search_path(q: "##{tag.name}")
= tag.name
span.badge.badge-transparent = tag.posts_count
span.badge = tag.posts_count
......@@ -8,16 +8,15 @@
h4
a href=post_path(post) = post.title
.col-xs-3
span.label.label-danger.label-date = post.display_specified_date if post.specified_date
small.pull-right ##{post.id}
span.label.label-date.pull-right = post.display_specified_date if post.specified_date
.row
.col-xs-8
small.text-success.posted-name
| #{post.author.name} posted&nbsp;
abbr.js-time-ago data-time-ago-at=post.updated_at
|.&nbsp;&nbsp;
- post.tags.each do |tag|
a.label.label-tag href=tag.decorate.show_path = tag.name
- post.tags.map(&:decorate).each do |tag|
a.label.label-tag href=tag.show_path = tag.structured_name
| &nbsp;
.col-xs-4
small.pull-right
......
/! view:stock/show
.row
.col-md-12
.panel.stock-wrapper
.col-xs-12.col-md-8 role="navigation"
.panel.panem-main
.panel-heading
h1
| &nbsp;Stocks
small - 保存・蓄積された記事
.panel-body
#sidebar role="navigation"
#tab-tree.tab-pane
- cache('tag-tree', :expires_in => 1.hour) do
.list-group
= Tag.posts_exist.decorate.tree_view
h2.panel-title
i.fa.fa-stack-overflow
| &nbsp;Stock
span.small &nbsp;- 保存・蓄積された記事
.panel-body.list-group
#tab-tree
= Tag.posts_exist.decorate.tree_view
.col-xs-12.col-md-4
.panel
.panel-heading
h3.panel-title
i.fa.fa-star
| &nbsp;今月のホットタグ
.panel-body.list-group
- Tag.monthly_popular(10).each do |tag, score|
a.list-group-item href=search_path(q: "##{tag.name}")
= tag.name
span.badge #{score.round(2)} pt.
.panel
.panel-heading
h3.panel-title
i.fa.fa-tags
| &nbsp;最近作成されたタグ
.panel-body.list-group
- Tag.recently_created(10).each do |tag|
a.list-group-item href=search_path(q: "##{tag.name}")
= tag.name
span.label.pull-right = tag.decorate.created_date
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment