Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
R
rendezvous
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
VeNtura
rendezvous
Commits
f12f53d9
Commit
f12f53d9
authored
Jul 21, 2014
by
tady
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fix watch spec
parent
944106ba
Hide whitespace changes
Inline
Side-by-side
Showing
8 changed files
with
86 additions
and
13 deletions
+86
-13
app/controllers/posts_controller.rb
+2
-2
app/models/user.rb
+35
-5
app/models/watch.rb
+10
-0
app/views/posts/_large_item.html.slim
+1
-1
app/views/templates/_post.html.slim
+1
-1
db/migrate/20140719145016_create_watches.rb
+2
-2
spec/models/user_spec.rb
+23
-1
spec/models/watch_spec.rb
+12
-1
No files found.
app/controllers/posts_controller.rb
View file @
f12f53d9
...
...
@@ -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
|
...
...
app/models/user.rb
View file @
f12f53d9
...
...
@@ -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
######################################################################
#
v
alidations
#
V
alidations
######################################################################
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
...
...
app/models/watch.rb
View file @
f12f53d9
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
app/views/posts/_large_item.html.slim
View file @
f12f53d9
...
...
@@ -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
...
...
app/views/templates/_post.html.slim
View file @
f12f53d9
...
...
@@ -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
...
...
db/migrate/20140719145016_create_watches.rb
View file @
f12f53d9
class
CreateWatches
<
ActiveRecord
::
Migration
def
change
create_table
:watches
do
|
t
|
t
.
integer
:
us
er_id
,
null:
false
t
.
integer
:
watch
er_id
,
null:
false
t
.
string
:watchable_type
,
null:
false
t
.
integer
:watchable_id
,
null:
false
t
.
timestamps
end
add_index
:watches
,
[
:
us
er_id
,
:watchable_type
,
:watchable_id
],
unique:
true
add_index
:watches
,
[
:
watch
er_id
,
:watchable_type
,
:watchable_id
],
unique:
true
add_index
:watches
,
[
:watchable_type
,
:watchable_id
]
end
end
spec/models/user_spec.rb
View file @
f12f53d9
...
...
@@ -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
...
...
spec/models/watch_spec.rb
View file @
f12f53d9
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
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment