From e7f5f574240875bcbff29d046c736de242e2e2a3 Mon Sep 17 00:00:00 2001 From: Micha Wrobel Date: Sun, 4 Mar 2012 11:34:40 +0100 Subject: [PATCH] Some refactoring, and make possible to remove own comments --- .../backbone/models/comment.js.coffee | 3 +++ .../backbone/templates/comments/show.jst.hbs | 4 ++++ .../javascripts/backbone/views/base.js.coffee | 4 ++++ .../views/comments/comment_entry.js.coffee | 24 ++++++++++++++++--- .../views/comments/comments_index.js.coffee | 6 ++--- app/models/comment.rb | 2 +- app/views/asciicasts/show.html.erb | 2 +- app/views/layouts/_current_user_js.html.erb | 2 +- spec/models/comment_spec.rb | 10 +++----- 9 files changed, 40 insertions(+), 17 deletions(-) create mode 100644 app/assets/javascripts/backbone/views/base.js.coffee diff --git a/app/assets/javascripts/backbone/models/comment.js.coffee b/app/assets/javascripts/backbone/models/comment.js.coffee index f15a1cd..2f9e590 100644 --- a/app/assets/javascripts/backbone/models/comment.js.coffee +++ b/app/assets/javascripts/backbone/models/comment.js.coffee @@ -1,5 +1,8 @@ class AsciiIo.Models.Comment extends Backbone.Model + user: -> + this.get('user') + class AsciiIo.Collections.Comments extends Backbone.Collection model: AsciiIo.Models.Comment diff --git a/app/assets/javascripts/backbone/templates/comments/show.jst.hbs b/app/assets/javascripts/backbone/templates/comments/show.jst.hbs index b8df7fa..43f92e4 100644 --- a/app/assets/javascripts/backbone/templates/comments/show.jst.hbs +++ b/app/assets/javascripts/backbone/templates/comments/show.jst.hbs @@ -10,4 +10,8 @@
{{body}}
+{{#if show_remove_link}} +remove +{{/if}} +
diff --git a/app/assets/javascripts/backbone/views/base.js.coffee b/app/assets/javascripts/backbone/views/base.js.coffee new file mode 100644 index 0000000..540f0d8 --- /dev/null +++ b/app/assets/javascripts/backbone/views/base.js.coffee @@ -0,0 +1,4 @@ +class AsciiIo.Views.Base extends Backbone.View + + current_user: -> + AsciiIo.current_user diff --git a/app/assets/javascripts/backbone/views/comments/comment_entry.js.coffee b/app/assets/javascripts/backbone/views/comments/comment_entry.js.coffee index fddaa03..5c60fbf 100644 --- a/app/assets/javascripts/backbone/views/comments/comment_entry.js.coffee +++ b/app/assets/javascripts/backbone/views/comments/comment_entry.js.coffee @@ -1,10 +1,28 @@ -class AsciiIo.Views.CommentEntry extends Backbone.View +class AsciiIo.Views.CommentEntry extends AsciiIo.Views.Base template: JST['backbone/templates/comments/show'] tagName: 'li' className: 'comment' - render: -> - $(@el).html(@template(@model.toJSON())) + events: + 'click .remove': 'removeComment' + + initialize:(options) -> + @collection = options.collection + render: -> + context = _.extend(@model.toJSON(), { show_remove_link: @showRemoveLink() }) + $(@el).html @template(context) this + + showRemoveLink: -> + @current_user() && ( @current_user().id == @model.user().id ) + + removeComment: (event) -> + event.preventDefault() + @model.destroy + wait: true + success: => + $(this.el).slideUp("slow", => + this.remove() + ) diff --git a/app/assets/javascripts/backbone/views/comments/comments_index.js.coffee b/app/assets/javascripts/backbone/views/comments/comments_index.js.coffee index 46763f4..ef3cc63 100644 --- a/app/assets/javascripts/backbone/views/comments/comments_index.js.coffee +++ b/app/assets/javascripts/backbone/views/comments/comments_index.js.coffee @@ -1,4 +1,4 @@ -class AsciiIo.Views.CommentsIndex extends Backbone.View +class AsciiIo.Views.CommentsIndex extends AsciiIo.Views.Base el: '#comments' template: JST['backbone/templates/comments/index'] @@ -10,14 +10,12 @@ class AsciiIo.Views.CommentsIndex extends Backbone.View @collection.on('reset', @render, this) @collection.on('add', @render, this) - @current_user = AsciiIo.current_user - render: -> $(@el).html @template( show_form: @current_user ) $comments = this.$('.comments') - @collection.each (comment) -> + @collection.each (comment) => view = new AsciiIo.Views.CommentEntry({ model: comment, collection: @collection}) $comments.append view.render().el diff --git a/app/models/comment.rb b/app/models/comment.rb index 92b81fb..9bd4c5f 100644 --- a/app/models/comment.rb +++ b/app/models/comment.rb @@ -17,7 +17,7 @@ class Comment < ActiveRecord::Base super({ :include => { :user => { - :only => [ :nickname, :avatar_url ] + :only => [ :id, :nickname, :avatar_url ] } }, :methods => [:created] diff --git a/app/views/asciicasts/show.html.erb b/app/views/asciicasts/show.html.erb index 5a1d201..21f051c 100644 --- a/app/views/asciicasts/show.html.erb +++ b/app/views/asciicasts/show.html.erb @@ -26,7 +26,7 @@ var comments = new AsciiIo.Collections.Comments(); var asciicast_id = <%= @asciicast.id %> - comments.url = ['/api/asciicasts/', asciicast_id, '/comments.json'].join(''); + comments.url = ['/api/asciicasts/', asciicast_id, '/comments'].join(''); comments.fetch(); view = new AsciiIo.Views.CommentsIndex({ collection: comments }); diff --git a/app/views/layouts/_current_user_js.html.erb b/app/views/layouts/_current_user_js.html.erb index af2c41c..5b9d201 100644 --- a/app/views/layouts/_current_user_js.html.erb +++ b/app/views/layouts/_current_user_js.html.erb @@ -1,5 +1,5 @@ diff --git a/spec/models/comment_spec.rb b/spec/models/comment_spec.rb index 57525a5..b58baf7 100644 --- a/spec/models/comment_spec.rb +++ b/spec/models/comment_spec.rb @@ -14,16 +14,12 @@ describe Comment do comment.user = user end - it "should include user.gravatar_url" do - hash = comment.as_json - hash.should include(:user) - hash[:user].should include("avatar_url") - end - - it "should include user.nickname" do + it "should include user comment creator properties user" do hash = comment.as_json hash.should include(:user) hash[:user].should include("nickname") + hash[:user].should include("avatar_url") + hash[:user].should include("id") end it "should include comment.created" do