Skip to content

Commit

Permalink
Fixing formatting issues
Browse files Browse the repository at this point in the history
  • Loading branch information
tclaus committed Feb 8, 2021
1 parent 9bce191 commit a3558a6
Show file tree
Hide file tree
Showing 8 changed files with 37 additions and 38 deletions.
2 changes: 1 addition & 1 deletion app/assets/javascripts/app/collections/likes.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ app.collections.Likes = Backbone.Collection.extend({
// A comment- like has a post reference and a comment reference
this.url = (options.comment != null) ?
// not delegating to post.url() because when it is in a stream collection it delegates to that url
"/comments/" + options.comment.id + "/likes":
"/comments/" + options.comment.id + "/likes" :
"/posts/" + options.post.id + "/likes";
}
});
Expand Down
6 changes: 4 additions & 2 deletions app/assets/javascripts/app/models/comment.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@ app.models.Comment = Backbone.Model.extend({

initialize: function() {
this.post = this.collection.post;
this.interactions = new app.models.Post.LikeInteractions(_.extend({comment: this, post: this.post}, this.get("interactions")));
this.interactions = new app.models.Post.LikeInteractions(
_.extend({comment: this, post: this.post}, this.get("interactions"))
);
this.likes = this.interactions.likes;
this.likes_count = this.attributes.likes_count;
this.likesCount = this.attributes.likes_count;
this.userLike = this.interactions.userLike();
}
});
Expand Down
3 changes: 1 addition & 2 deletions app/assets/javascripts/app/models/post/like_interactions.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,11 @@ app.models.Post.LikeInteractions = Backbone.Model.extend({
},

likesCount: function() {
// return this.attributes.likes_count;
return this.get("likes_count");
},

userLike: function() {
return this.likes.select(function(like){
return this.likes.select(function(like) {
return like.get("author") && like.get("author").guid === app.currentUser.get("guid");
})[0];
},
Expand Down
2 changes: 1 addition & 1 deletion app/assets/javascripts/app/views/comment_view.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ app.views.Comment = app.views.Content.extend({
return _.extend(this.defaultPresenter(), {
canRemove: this.canRemove(),
text: app.helpers.textFormatter(this.model.get("text"), this.model.get("mentioned_people")),
likesCount: this.model.attributes.likes_count,
likesCount: this.model.attributes.likesCount,
userLike: this.model.interactions.userLike()
});
},
Expand Down
10 changes: 5 additions & 5 deletions app/presenters/comment_presenter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,21 +25,21 @@ def as_api_response
}
end

def build_interactions_json
def build_interactions_json
{
likes: as_api(likes),
likes_count: likes_count
likes: as_api(likes),
likes_count: likes_count
}
end

def build_mentioned_people_json
mentioned_people.map {|m| PersonPresenter.new(m).as_api_json }
end

def as_api(collection)
def as_api(collection)
collection.includes(author: :profile).map {|element|
element.as_api_response(:backbone)
}
end

end
end
6 changes: 2 additions & 4 deletions app/services/comment_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,11 @@ def find_for_post(post_id)
end

def find!(comment_guid)
comment = Comment.find_by!(guid: comment_guid)
return comment
Comment.find_by!(guid: comment_guid)
end

def find_by_id!(comment_id)
comment = Comment.find_by!(id: comment_id)
return comment
Comment.find_by!(id: comment_id)
end

def destroy(comment_id)
Expand Down
5 changes: 2 additions & 3 deletions app/services/like_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def create_for_post(post_id)

def create_for_comment(comment_id)
comment = comment_service.find_by_id!(comment_id)
post = post_service.find!(comment.post.id) #checks implicit for public posts
post = post_service.find!(comment.post.id) # checks implicit for public posts
user.like_comment!(comment)
end

Expand All @@ -32,9 +32,8 @@ def find_for_post(post_id)
end

def find_for_comment(comment_id)

comment = comment_service.find_by_id!(comment_id)
post = post_service.find!(comment.post.id) #checks implicit for public posts
post = post_service.find!(comment.post.id) # checks implicit for public posts
likes = comment.likes
user ? likes.order(Arel.sql("author_id = #{user.person.id} DESC")) : likes
end
Expand Down
41 changes: 21 additions & 20 deletions spec/services/like_service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@

it "fails if comment does not exist" do
expect {
LikeService.new(alice).create_for_comment("unknown_id")
LikeService.new(alice).create_for_comment("unknown_id")
}.to raise_error ActiveRecord::RecordNotFound
end

Expand All @@ -73,12 +73,12 @@
}.to raise_error ActiveRecord::RecordNotFound
end

it "fails if user already liked the comment" do
it "fails if user already liked the comment" do
LikeService.new(alice).create_for_comment(bobs_comment.id)
expect {
LikeService.new(alice).create_for_comment(bobs_comment.id)
}.to raise_error ActiveRecord::RecordInvalid
end
end
end

describe "#destroy_post_likes" do
Expand Down Expand Up @@ -107,21 +107,21 @@
end

describe "#destroy_comment_likes" do
let(:like) {LikeService.new(bob).create_for_comment(alice_comment)}
let(:like) { LikeService.new(bob).create_for_comment(alice_comment) }

it "let the user destroy its own comment like" do
it "let the user destroy its own comment like" do
result = LikeService.new(bob).destroy(like.id)
expect(result).to be_truthy
end

it "doesn't let the parent author destroy other comment likes" do
it "doesn't let the parent author destroy other comment likes" do
result = LikeService.new(alice).destroy(like.id)
expect(result).to be_falsey
end

it "fails if the like doesn't exist" do
expect {
result = LikeService.new(alice).destroy("unknown id")
expect {
LikeService.new(alice).destroy("unknown id")
}.to raise_error ActiveRecord::RecordNotFound
end
end
Expand Down Expand Up @@ -182,7 +182,7 @@
end

describe "#find_for_comment" do
context "with user" do
context "with user" do
it "returns likes for a public post comment" do
post = alice.post(:status_message, text: "hello", public: true)
comment = CommentService.new(bob).create(post.id, "Hello comment")
Expand All @@ -206,24 +206,26 @@
post = alice.post(:status_message, text: "hello", public: true)
comment = CommentService.new(alice).create(post.id, "I like my own post")

[alice, bob, eve].map {|user| LikeService.new(user).create_for_comment(comment.id)}
[alice, bob, eve].each do |user|
expect(
LikeService.new(user).find_for_comment(comment.id).first.author.id
).to be user.person.id
[alice, bob, eve].map {|user| LikeService.new(user).create_for_comment(comment.id)}
[alice, bob, eve].each do |user|
expect(
LikeService.new(user).find_for_comment(comment.id).first.author.id
).to be user.person.id
end
end
end

context "without user" do
it "returns likes for a comment on a public post" do
it "returns likes for a comment on a public post" do
post = alice.post(:status_message, text: "hello", public: true)
comment = CommentService.new(bob).create(post.id, "I like my own post")
like = LikeService.new(alice).create_for_comment(comment.id)
expect(LikeService.new.find_for_comment(comment.id)).to include(like)
expect(
LikeService.new.find_for_comment(comment.id)
).to include(like)
end

it "doesn't return likes for a private post comment" do
it "doesn't return likes for a private post comment" do
LikeService.new(alice).create_for_comment(alice_comment.id)
expect {
LikeService.new.find_for_comment(alice_comment)
Expand All @@ -242,9 +244,8 @@
expect(post.likes.length).to eq(0)
end

it "removes the like for a comment" do
it "removes the like for a comment" do
comment = CommentService.new(alice).create(post.id, "I like my own post")

LikeService.new(alice).create_for_comment(comment.id)
expect(comment.likes.length).to eq(1)

Expand All @@ -254,4 +255,4 @@
end

end
end
end

0 comments on commit a3558a6

Please sign in to comment.