Commit 0bf67f7f by tady

small_itemに日付表示, counter_cache追加

parent 64824701
......@@ -71,7 +71,7 @@ group :development do
gem 'pry-rails'
# profiler
# gem 'rack-mini-profiler'
gem 'rack-mini-profiler'
# rubocop
gem 'rubocop'
......
......@@ -250,6 +250,8 @@ GEM
pry-rails (0.3.2)
pry (>= 0.9.10)
rack (1.5.2)
rack-mini-profiler (0.9.0)
rack (>= 1.1.3)
rack-test (0.6.2)
rack (>= 1.0)
rails (4.1.0)
......@@ -415,6 +417,7 @@ DEPENDENCIES
poltergeist
premailer
pry-rails
rack-mini-profiler
rails (~> 4)
rspec-rails
rubocop
......
......@@ -10,6 +10,8 @@ class TagDecorator < Draper::Decorator
# end
# end
decorates_association :posts
# tagページのURL
# urlエンコードを施す
def show_path
......@@ -30,7 +32,7 @@ class TagDecorator < Draper::Decorator
_html += %Q{
<a href="#{ self.show_path }" data-name="#{model.name}">
#{model.name} <span class="badge">#{model.posts.count}</span>
#{model.name} <span class="badge">#{model.posts.size}</span>
</a>
}
......
......@@ -61,6 +61,11 @@ class Post < ActiveRecord::Base
_where_list
end)
# 最新のPostを取得
scope :recent, -> (limit = 10) {
order(updated_at: :desc).limit(limit)
}
# generate forked post (not saved)
def generate_fork(user)
......
......@@ -11,5 +11,5 @@
class PostTag < ActiveRecord::Base
belongs_to :post
belongs_to :tag
belongs_to :tag, counter_cache: 'posts_count'
end
......@@ -29,6 +29,10 @@ class Tag < ActiveRecord::Base
having('posts_count > 0')
}
def recent_posts(limit = 30)
self.posts.recent(limit)
end
# 自分のタグに紐づくPostをすべて`other_tag`へ移動する
def move_all_posts_to!(other_tag)
self.posts.each do |_post|
......
/ locals:
/ post {Post}
a.list-group-item.post-list data-post-id=post.id href="#" = post.title
a.list-group-item.post-list data-post-id=post.id href=post_path(post)
span.label.label-default.pull-right = post.display_specified_date if post.specified_date
= post.title
......@@ -40,8 +40,10 @@
h2
small
|#{@tag.name}」のタグが付いた記事
- @tag.posts.each_with_index do |post, i|
a.list-group-item.post-list href=post_path(post) = post.title
- @tag.recent_posts.each do |post|
= render partial: 'posts/small_item', locals: { post: post.decorate }
a.list-group-item.post-list href=search_path(q: "##{@tag.name}") もっと見る
#modal-merge.modal.fade aria-hidden="true" aria-labelledby="myModalLabel" role="dialog" tabindex="-1"
.modal-dialog
......
......@@ -27,7 +27,7 @@ Rendezvous::Application.routes.draw do
# devise_for :users , only: [:sign_in, :sign_out, :session]
devise_for :users,
path_names: {current_user: 'me'},
path_names: { current_user: 'me' },
controllers: { omniauth_callbacks: 'users/omniauth_callbacks' },
skip: [:passwords]
......
class AddPostsCountToTags < ActiveRecord::Migration
def change
add_column :tags, :posts_count, :integer, default: 0, null: false
end
end
......@@ -11,7 +11,7 @@
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 20140328045902) do
ActiveRecord::Schema.define(version: 20140420120819) do
create_table "comments", force: true do |t|
t.integer "author_id"
......@@ -52,6 +52,7 @@ ActiveRecord::Schema.define(version: 20140328045902) do
t.datetime "updated_at"
t.string "ancestry"
t.text "body"
t.integer "posts_count", default: 0, null: false
end
add_index "tags", ["ancestry"], name: "index_tags_on_ancestry", using: :btree
......
namespace :batch do
desc 'Tagに関連するPostのcounter_cacheの再生成'
task reset_counters: :environment do
Tag.all.each{|t| Tag.reset_counters(t.id, :posts) }
end
end
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