Commit f12f53d9 by tady

fix watch spec

parent 944106ba
......@@ -110,9 +110,9 @@ class PostsController < ApplicationController
def watch
if current_user.watching?(post: @post)
@post.watchings.where(user: current_user).destroy_all
current_user.unwatch!(post: @post)
else
@post.watchings.create!(user: current_user)
current_user.watch!(post: @post)
end
respond_to do |format|
......
......@@ -33,14 +33,16 @@ class User < ActiveRecord::Base
devise :omniauthable, omniauth_providers: [:google_oauth2]
######################################################################
# association
# Associations
######################################################################
has_many :posts, foreign_key: 'author_id'
has_many :comments, foreign_key: 'author_id'
has_many :notifications
has_many :footprints
has_many :watches
has_many :watches, :as => :watchable, :dependent => :destroy
has_many :watchers, :through => :watches
has_many :watchings, class_name: 'Watch', foreign_key: 'watcher_id'
has_many :watching_posts, :through => :watchings, :source => :watchable, :source_type => "Post"
# has_many :watchings, :as => :resource
......@@ -58,7 +60,7 @@ class User < ActiveRecord::Base
######################################################################
# validations
# Validations
######################################################################
validates :name, presence: true
validates :email, presence: true
......@@ -125,13 +127,41 @@ class User < ActiveRecord::Base
footprints.create!(post: post)
end
def watch!(hash)
if hash[:post]
watching_posts << hash[:post] unless watching_posts.include?(hash[:post])
elsif hash[:tag]
raise 'Not Implemented.'
elsif hash[:user]
raise 'Not Implemented.'
else
raise 'No hash argument set.'
end
end
def unwatch!(hash)
if hash[:post]
hash[:post].watches.where(watcher: self).destroy_all
elsif hash[:tag]
raise 'Not Implemented.'
elsif hash[:user]
raise 'Not Implemented.'
else
raise 'No hash argument set.'
end
end
# check if user watching post/tag/user
# TODO: tag/user
def watching?(hash)
if hash[:post]
hash[:post].watchings.where(user_id: self.id).exists?
hash[:post].watches.where(watcher: self).exists?
elsif hash[:tag]
raise 'Not Implemented.'
elsif hash[:user]
raise 'Not Implemented.'
else
false
raise 'No hash argument set.'
end
end
......
class Watch < ActiveRecord::Base
######################################################################
# Associations
######################################################################
belongs_to :watcher, class_name: 'User'
belongs_to :watchable, polymorphic: true
######################################################################
# Validations
######################################################################
validates :watcher_id, uniqueness: { scope: [:watchable_type, :watchable_id] }
end
......@@ -8,7 +8,7 @@ a.list-group-item.post-list.mod-hover-hidden data-post-id=post.id href=post_path
h4.text-link #{post.title}
.col-xs-3
span.label.label-danger = post.display_specified_date if post.specified_date
small.pull-right "##{post.id}"
small.pull-right ##{post.id}
.row
.col-xs-8
small.text-success
......
......@@ -7,7 +7,7 @@
.col-xs-9
h4.text-link #{post.title}
.col-xs-3
small.pull-right "##{post.id}"
small.pull-right ##{post.id}
.row
.col-xs-8
p.small.text-success
......
class CreateWatches < ActiveRecord::Migration
def change
create_table :watches do |t|
t.integer :user_id, null: false
t.integer :watcher_id, null: false
t.string :watchable_type, null: false
t.integer :watchable_id, null: false
t.timestamps
end
add_index :watches, [:user_id, :watchable_type, :watchable_id], unique: true
add_index :watches, [:watcher_id, :watchable_type, :watchable_id], unique: true
add_index :watches, [:watchable_type, :watchable_id]
end
end
......@@ -31,9 +31,9 @@ describe User do
let(:alice) { create(:alice) }
let(:bob) { create(:bob) }
let(:post) { create(:post) }
describe '#google_oauth_token_expired?' do
it 'not expired' do
expect(alice.google_oauth_token_expired?).to be_falsey
end
......@@ -41,7 +41,29 @@ describe User do
it 'expired' do
expect(bob.google_oauth_token_expired?).to be_truthy
end
end
describe '#unwatch / #watch / #watching?' do
it 'not watching' do
expect(alice.watching?(post: post)).to be_falsey
end
it '#watch!' do
alice.watch!(post: post)
expect(alice.watching?(post: post)).to be_truthy
end
it '#watch! (uniqueness)' do
alice.watch!(post: post)
alice.watch!(post: post)
expect(alice.watching_posts.size).to be(1)
end
it '#unwatch!' do
alice.watch!(post: post)
alice.unwatch!(post: post)
expect(alice.watching?(post: post)).to be_falsey
end
end
end
......
require 'rails_helper'
RSpec.describe Watch, :type => :model do
pending "add some examples to (or delete) #{__FILE__}"
# describe "validations" do
# let(:alice) { create(:alice) }
# let(:post) { create(:post) }
# describe "watcher_id" do
# it "uniqueness" do
# alice.watch!(post: post)
# alice.watch!(post: post)
# expect(alice.watching_posts.size).to be(1)
# end
# end
# 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