fixing conflicts [#9]

This commit is contained in:
rgarcia
2015-07-18 18:19:00 +02:00
13 changed files with 210 additions and 7 deletions

View File

@@ -25,7 +25,7 @@ gem 'sdoc', '~> 0.4.0', group: :doc
gem 'devise'
# Use ActiveModel has_secure_password
# gem 'bcrypt', '~> 3.1.7'
gem 'acts_as_commentable_with_threading'
# Use Unicorn as the app server
# gem 'unicorn'

View File

@@ -38,8 +38,14 @@ GEM
tzinfo (~> 1.1)
acts-as-taggable-on (3.5.0)
activerecord (>= 3.2, < 5)
acts_as_commentable_with_threading (2.0.0)
activerecord (>= 4.0)
activesupport (>= 4.0)
awesome_nested_set (>= 3.0)
acts_as_votable (0.10.0)
arel (6.0.2)
awesome_nested_set (3.0.2)
activerecord (>= 4.0.0, < 5)
bcrypt (3.1.10)
binding_of_caller (0.7.2)
debug_inspector (>= 0.0.1)
@@ -192,6 +198,7 @@ PLATFORMS
DEPENDENCIES
acts-as-taggable-on
acts_as_commentable_with_threading
acts_as_votable
byebug
capybara

View File

@@ -0,0 +1,28 @@
class CommentsController < ApplicationController
before_action :authenticate_user!
before_action :set_debate, :set_parent
def create
comment = Comment.build(@debate, current_user, params[:comment][:body])
comment.save!
comment.move_to_child_of(@parent) if reply?
redirect_to @debate, notice: "Comentario guardado"
end
private
def comment_params
params.require(:comments).permit(:commentable_type, :commentable_id, :body)
end
def set_debate
@debate = Debate.find(params[:debate_id])
end
def set_parent
@parent = Comment.find_parent(params[:comment])
end
def reply?
@parent.class == Comment
end
end

19
app/models/comment.rb Normal file
View File

@@ -0,0 +1,19 @@
class Comment < ActiveRecord::Base
acts_as_nested_set :scope => [:commentable_id, :commentable_type]
validates :body, :presence => true
validates :user, :presence => true
belongs_to :commentable, :polymorphic => true
belongs_to :user
def self.build(commentable, user, body)
new commentable: commentable,
user_id: user.id,
body: body
end
def self.find_parent(params)
params[:commentable_type].constantize.find(params[:commentable_id])
end
end

View File

@@ -1,6 +1,7 @@
require 'numeric'
class Debate < ActiveRecord::Base
acts_as_votable
acts_as_commentable
acts_as_taggable
belongs_to :author, class_name: 'User', foreign_key: 'author_id'

View File

@@ -0,0 +1,13 @@
<div id="comment-<%= comment.id %>" class='comment' style="padding-bottom:30px">
<p><%= comment.user.name %></p>
<p>hace <%= time_ago_in_words(comment.created_at) %></p>
<p><%= comment.body %></p>
<div style="margin-left:50px">
<%= render comment.children %>
</div>
<div style="margin-left:50px">
<%= render 'comments/form', parent: comment %>
</div>
</div>

View File

@@ -0,0 +1,7 @@
<%= form_for [@debate, Comment.new] do |f| %>
<%= f.text_area :body %>
<%= f.hidden_field :commentable_type, value: parent.class %>
<%= f.hidden_field :commentable_id, value: parent.id %>
<%= f.submit 'Publicar comentario' %>
<% end %>

View File

@@ -25,7 +25,14 @@
<%= render 'shared/tags', debate: @debate %>
<br/><br/>
<div>
Deja tu comentario
<%= render 'comments/form', parent: @debate %>
<div id="comments" style="padding-top:20px">
<h2>Comentarios</h2>
<%= render @debate.root_comments %>
</div>
<br/><br/>
<%= link_to 'Edit', edit_debate_path(@debate) %> |
<%= link_to 'Back', debates_path %>
</div>

View File

@@ -8,6 +8,7 @@ Rails.application.routes.draw do
root 'welcome#index'
resources :debates do
resources :votes, only: :create
resources :comments, only: :create
end
# Example of regular route:

View File

@@ -0,0 +1,21 @@
class ActsAsCommentableWithThreadingMigration < ActiveRecord::Migration
def self.up
create_table :comments, :force => true do |t|
t.integer :commentable_id
t.string :commentable_type
t.string :title
t.text :body
t.string :subject
t.integer :user_id, :null => false
t.integer :parent_id, :lft, :rgt
t.timestamps
end
add_index :comments, :user_id
add_index :comments, [:commentable_id, :commentable_type]
end
def self.down
drop_table :comments
end
end

View File

@@ -11,7 +11,24 @@
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 20150718075337) do
ActiveRecord::Schema.define(version: 20150718091702) do
create_table "comments", force: :cascade do |t|
t.integer "commentable_id"
t.string "commentable_type"
t.string "title"
t.text "body"
t.string "subject"
t.integer "user_id", null: false
t.integer "parent_id"
t.integer "lft"
t.integer "rgt"
t.datetime "created_at"
t.datetime "updated_at"
end
add_index "comments", ["commentable_id", "commentable_type"], name: "index_comments_on_commentable_id_and_commentable_type"
add_index "comments", ["user_id"], name: "index_comments_on_user_id"
create_table "debates", force: :cascade do |t|
t.string "title"

View File

@@ -18,6 +18,15 @@ FactoryGirl.define do
association :votable, factory: :debate
association :voter, factory: :user
vote_flag true
factory :comment do
commentable
user
body 'Comment body'
end
factory :commentable do
debate
end
end

View File

@@ -0,0 +1,73 @@
require 'rails_helper'
include ActionView::Helpers::DateHelper
feature 'Comments' do
scenario 'Index' do
debate = create(:debate)
3.times { create(:comment, commentable: debate) }
visit debate_path(debate)
expect(page).to have_css('.comment', count: 3)
comment = Comment.first
within first('.comment') do
expect(page).to have_content comment.user.name
expect(page).to have_content time_ago_in_words(comment.created_at)
expect(page).to have_content comment.body
end
end
scenario 'Create' do
user = create(:user)
debate = create(:debate)
login_as(user)
visit debate_path(debate)
fill_in 'comment_body', with: '¿Has pensado en esto...?'
click_button 'Publicar comentario'
expect(page).to have_content 'Comentario guardado'
within "#comments" do
expect(page).to have_content '¿Has pensado en esto...?'
end
end
scenario 'Reply' do
citizen = create(:user, first_name: 'Ana')
manuela = create(:user, first_name: 'Manuela')
debate = create(:debate)
comment = create(:comment, commentable: debate, user: citizen)
visit debate_path(debate)
login_as(manuela)
within "#comment-#{comment.id}" do
fill_in 'comment_body', with: 'La semana que viene está hecho.'
click_button 'Publicar comentario'
end
expect(page).to have_content 'Comentario guardado'
within "#comment-#{comment.id}" do
expect(page).to have_content 'La semana que viene está hecho.'
end
end
scenario "N replies" do
debate = create(:debate)
parent = create(:comment, commentable: debate)
7.times do
create(:comment, commentable: debate).
move_to_child_of(parent)
parent = parent.children.first
end
visit debate_path(debate)
expect(page).to have_css(".comment.comment.comment.comment.comment.comment.comment.comment")
end
end