Commit e4b41f69 by tady

Merge pull request #86 from tadyjp/140430/flow-view

[WIP] 通知機能
parents 23b983e6 ec272634
class NotificationsController < ApplicationController
before_action :set_notification, only: [:bridge]
def bridge
@notification.set_read!
redirect_to @notification.detail_path
end
private
def set_notification
@notification = Notification.find(params[:id])
end
end
......@@ -17,4 +17,13 @@ class Comment < ActiveRecord::Base
validates :author_id, presence: true
validates :post_id, presence: true
validates :body, presence: true
### Callback ###
after_save :notify_author
private
def notify_author
post.author.push_notification(post.decorate.show_path, "#{author.name}さんがあなたの投稿にコメントしました")
end
end
class Notification < ActiveRecord::Base
belongs_to :user
######################################################################
# Named scope
######################################################################
# 最新のPostを取得
scope :unread, -> {
where(is_read: false)
}
######################################################################
# Instance method
######################################################################
# 既読にする
def set_read!
update!(is_read: true, read_at: Time.now)
end
end
......@@ -37,6 +37,7 @@ class User < ActiveRecord::Base
######################################################################
has_many :posts, foreign_key: 'author_id'
has_many :comments, foreign_key: 'author_id'
has_many :notifications
######################################################################
# scope
......@@ -105,6 +106,10 @@ class User < ActiveRecord::Base
)
end
# push通知を追加
def push_notification(detail_path, body)
notifications.create(detail_path: detail_path, body: body, is_read: false)
end
end
......@@ -24,6 +24,8 @@
h2.panel-title 最近投稿されたタグ
.panel-body.list-group
- 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} (#{tag.posts_count})"
a.list-group-item data-tag-id=tag.id href=search_path(q: "##{tag.name}")
= tag.name
span.badge = tag.posts_count
......@@ -7,7 +7,7 @@ footer
| &nbsp;|&nbsp;
a data-target="#header-search-description" data-toggle="modal" href="#" Search
| &nbsp;|&nbsp;
a href="https://help.github.com/articles/github-flavored-markdown" target="_blank" Markdown
a href="http://bamka.info/wp-content/uploads/1686d2821a5a118a770e70e2fe50a14f.jpg" target="_blank" Markdown
| &nbsp;
a href="https://travis-ci.org/tadyjp/rendezvous"
img src="https://travis-ci.org/tadyjp/rendezvous.png"/
......
......@@ -41,5 +41,21 @@ nav.navbar.navbar-default.navbar-fixed-top role="navigation"
li.divider
li
a href=me_session_path data-method="delete" rel="nofollow" SignOut
li.dropdown
a.dropdown-toggle data-toggle="dropdown"
| 通知
- if current_user.notifications.unread.any?
span.badge = current_user.notifications.unread.count
b.caret
ul.dropdown-menu
- if current_user.notifications.unread.any?
- current_user.notifications.unread.each do |notification|
li
a href=notification_bridge_path(notification.id)
= notification.body
- else
li
a 通知はありません
# config valid only for Capistrano 3.1
lock '3.1.0'
lock '3.2.0'
set :application, 'rendezvous'
set :repo_url, 'git@github.com:tadyjp/rendezvous.git'
......
......@@ -21,7 +21,7 @@ role :db, %w{tady@rendezvous}
# you can see them in [net/ssh documentation](http://net-ssh.github.io/net-ssh/classes/Net/SSH.html#method-c-start)
# set it globally
set :ssh_options, {
keys: %w(/Users/tady/.ssh/id_rsa_07PC12),
keys: %w(/Users/tady/.ssh/id_rsa),
forward_agent: true,
auth_methods: %w(publickey)
}
......
......@@ -18,6 +18,8 @@ Rendezvous::Application.routes.draw do
get 'posts/:id/slideshow' => 'posts#slideshow', as: 'slideshow_post'
resources :posts, except: [:index]
get 'notification_bridge/:id' => 'notifications#bridge', as: 'notification_bridge'
post 'tags/:name/merge_to/:merge_to_name' => 'tags#merge_to', as: 'merge_to_tag'
post 'tags/:name/move_to/:move_to_name' => 'tags#move_to', as: 'move_to_tag'
resources :tags, :param => :name, except: [:index]
......
class CreateNotifications < ActiveRecord::Migration
def change
create_table :notifications do |t|
t.integer :user_id
t.datetime :read_at
t.boolean :is_read, null: false, default: false
t.string :detail_path
t.text :body
t.timestamps
end
add_index :notifications, [:user_id, :is_read, :read_at]
end
end
......@@ -11,7 +11,7 @@
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 20140420120819) do
ActiveRecord::Schema.define(version: 20140501045300) do
create_table "comments", force: true do |t|
t.integer "author_id"
......@@ -24,6 +24,18 @@ ActiveRecord::Schema.define(version: 20140420120819) do
add_index "comments", ["author_id", "updated_at"], name: "index_comments_on_author_id_and_updated_at", using: :btree
add_index "comments", ["post_id", "updated_at"], name: "index_comments_on_post_id_and_updated_at", using: :btree
create_table "notifications", force: true do |t|
t.integer "user_id"
t.datetime "read_at"
t.boolean "is_read", default: false, null: false
t.string "detail_path"
t.text "body"
t.datetime "created_at"
t.datetime "updated_at"
end
add_index "notifications", ["user_id", "is_read", "read_at"], name: "index_notifications_on_user_id_and_is_read_and_read_at", using: :btree
create_table "post_tags", force: true do |t|
t.integer "post_id", null: false
t.integer "tag_id", null: false
......
require 'spec_helper'
describe Notification do
pending "add some examples to (or delete) #{__FILE__}"
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