Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ group :development, :test do
gem 'rails-controller-testing'
gem 'capybara'
gem 'launchy'
gem 'selenium-webdriver', '~> 2.53.4'
gem 'capybara-webkit'
gem 'geckodriver-helper'
gem 'database_cleaner'
end
Expand Down
14 changes: 5 additions & 9 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,9 @@ GEM
rack (>= 1.0.0)
rack-test (>= 0.5.4)
xpath (~> 2.0)
childprocess (0.7.1)
ffi (~> 1.0, >= 1.0.11)
capybara-webkit (1.1.0)
capybara (~> 2.0, >= 2.0.2)
json
coffee-rails (4.2.2)
coffee-script (>= 2.2.0)
railties (>= 4.0.0)
Expand Down Expand Up @@ -113,6 +114,7 @@ GEM
rails-dom-testing (>= 1, < 3)
railties (>= 4.2.0)
thor (>= 0.14, < 2.0)
json (2.1.0)
launchy (2.4.3)
addressable (~> 2.3)
listen (3.0.8)
Expand Down Expand Up @@ -206,7 +208,6 @@ GEM
ruby-progressbar (1.8.1)
ruby_parser (3.9.0)
sexp_processor (~> 4.1)
rubyzip (1.2.1)
sass (3.5.0)
sass-listen (~> 3.0.7)
sass-listen (3.0.7)
Expand All @@ -218,10 +219,6 @@ GEM
sprockets (>= 2.8, < 4.0)
sprockets-rails (>= 2.0, < 4.0)
tilt (>= 1.1, < 3)
selenium-webdriver (2.53.4)
childprocess (~> 0.5)
rubyzip (~> 1.0)
websocket (~> 1.0)
sexp_processor (4.9.0)
shoulda-matchers (3.1.2)
activesupport (>= 4.0.0)
Expand Down Expand Up @@ -256,7 +253,6 @@ GEM
activemodel (>= 5.0)
bindex (>= 0.4.0)
railties (>= 5.0)
websocket (1.2.4)
websocket-driver (0.6.5)
websocket-extensions (>= 0.1.0)
websocket-extensions (0.1.2)
Expand All @@ -271,6 +267,7 @@ DEPENDENCIES
bootstrap-sass
byebug
capybara
capybara-webkit
coffee-rails (~> 4.2)
database_cleaner
devise
Expand All @@ -288,7 +285,6 @@ DEPENDENCIES
rspec-rails
rubocop
sass-rails (~> 5.0)
selenium-webdriver (~> 2.53.4)
shoulda-matchers
spring
spring-watcher-listen (~> 2.0.0)
Expand Down
9 changes: 9 additions & 0 deletions app/assets/javascripts/answers.js.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
editanswer = ->
$('.edit-answer-link').click (e) ->
e.preventDefault()
$(this).hide()
answer_id = $(this).data('answerId')
$('form#edit-answer-' + answer_id).show()

$(document).ready(editanswer)
$(document).on('turbolinks:load', editanswer)
1 change: 1 addition & 0 deletions app/assets/stylesheets/application.css.scss
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
@import "bootstrap-sprockets";
@import "bootstrap";

.edit_answer {display: none;}
22 changes: 19 additions & 3 deletions app/controllers/answers_controller.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
class AnswersController < ApplicationController
before_action :authenticate_user!
before_action :set_question, only: [:new, :create, :destroy]
before_action :set_answer, only: [:destroy]
before_action :set_question, only: [:new, :create, :destroy, :update, :best]
before_action :set_answer, only: [:destroy, :update, :best]

def create
@answer = @question.answers.new(answer_params)
Expand All @@ -10,14 +10,30 @@ def create

end

def update
if current_user.author_of?(@answer)
render "error" unless @answer.update(answer_params)
else
flash[:notice] = "You are not auther of the answer"
end
end

def destroy
if current_user.author_of?(@answer)
flash[:notice] = "Your answer successfully deleted."
@answer.destroy
else
flash[:notice] = "You are not author of the answer"
end
redirect_to question_path(@question)
end

def best
if current_user.author_of?(@question)
@answer.set_best
else
render[:notice] = "You are not author of the question"
end

end

private
Expand Down
15 changes: 12 additions & 3 deletions app/controllers/questions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,22 @@ def show
end

def edit
if current_user.author_of?(@question)
render :edit
else
redirect_to questions_path
end
end

def update
if @question.update(questions_params)
redirect_to question_path(@question)
if current_user.author_of?(@question)
if @question.update(questions_params)
redirect_to questions_path
else
render :edit
end
else
render :edit
redirect_to questions_path
end
end

Expand Down
7 changes: 7 additions & 0 deletions app/models/answer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,11 @@ class Answer < ApplicationRecord
belongs_to :user

validates :body, presence: true

def set_best
Answer.transaction do
self.question.answers.update_all(best: false)
self.update!(best: true)
end
end
end
2 changes: 1 addition & 1 deletion app/models/question.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
class Question < ApplicationRecord
has_many :answers, dependent: :destroy
has_many :answers,-> { order(best: :desc) }, dependent: :destroy
belongs_to :user

validates :title, presence: true
Expand Down
17 changes: 13 additions & 4 deletions app/views/answers/_answer.html.haml
Original file line number Diff line number Diff line change
@@ -1,7 +1,16 @@
- if answer.persisted?
.row
.row{id: "answer-body-#{answer.id}", class: ("best-answer" unless answer.best?)}
= answer.body
.row
- if current_user&.author_of?(answer)
= link_to 'Delete answer', question_answer_path(@question, answer), method: :delete
.row
- if current_user && current_user.author_of?(@question) && !answer.best?
= link_to "best", best_question_answer_path(@question, answer), remote: true, method: :put
- if current_user&.author_of?(answer)
%p
= link_to "Edit", '', class: "edit-answer-link", data: {answer_id: answer.id}
= form_for [@question, answer], remote: true, html: {id: "edit-answer-#{answer.id}"} do |f|
= f.label :body
= f.text_area :body
= f.submit "Save"
%p
= link_to 'Delete answer', question_answer_path(@question, answer), method: :delete, remote: true

5 changes: 5 additions & 0 deletions app/views/answers/best.js.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
alert('Hello');
$(".answers").html("<%= j render @question.answers%>");
$(".alert").html("You chose best answer.");


2 changes: 2 additions & 0 deletions app/views/answers/create.js.erb
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
$("textarea#new_answer_body").val("");
$(".answers").append("<%= j render @answer%>");


4 changes: 4 additions & 0 deletions app/views/answers/destroy.js.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
$(".row#answer-body-<%= @answer.id%>").html('');
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Лучше использовать remove()

$(".alert").html("Your answer successfully deleted.")


5 changes: 5 additions & 0 deletions app/views/answers/update.js.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
$('.row#answer-body-' + <%= @answer.id %>).html('<%= j @answer.body%>');
$('form#edit-answer-' + <%= @answer.id %>).hide();
$('.edit-answer-link[data-answer-id="<%= @answer.id %>"]').show();


2 changes: 1 addition & 1 deletion app/views/questions/_form.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@
.col-md-8
= f.text_area :body
.row
= f.submit "Create"
= f.submit "Save"
4 changes: 3 additions & 1 deletion app/views/questions/edit.html.haml
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@

= render "form"
%br
= link_to "Назад", root_path
2 changes: 2 additions & 0 deletions app/views/questions/index.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
- @questions.each do |question|
.row
%h2= link_to question.title, question_path(question)
- if current_user && current_user.author_of?(question)
%p= link_to "Edit",edit_question_path(question)
.row
%br
%br
Expand Down
4 changes: 2 additions & 2 deletions app/views/questions/show.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
= @question.body
.row
- if current_user&.author_of?(@question)
= link_to 'Delete question', question_path(@question), method: :delete
= link_to 'Delete question', question_path(@question), method: :delete, remote: true
.row
%h3Комментарии
.row
Expand All @@ -19,7 +19,7 @@
= render "shared/error_form", object: f.object
.row
= f.label :body
= f.text_field :body
= f.text_area :body, id: "new_answer_body"
= f.submit "Create answer"
.row
= link_to "Назад", questions_path
Empty file modified bin/bundle
100755 → 100644
Empty file.
Empty file modified bin/rails
100755 → 100644
Empty file.
Empty file modified bin/rake
100755 → 100644
Empty file.
Empty file modified bin/setup
100755 → 100644
Empty file.
Empty file modified bin/spring
100755 → 100644
Empty file.
Empty file modified bin/update
100755 → 100644
Empty file.
4 changes: 3 additions & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
root "questions#index"

resources :questions do
resources :answers
resources :answers do
put :best, on: :member
end
end
end
5 changes: 5 additions & 0 deletions db/migrate/20170929121314_add_best_to_answers.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddBestToAnswers < ActiveRecord::Migration[5.0]
def change
add_column :answers, :best, :boolean, default: false
end
end
3 changes: 2 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 20170713165150) do
ActiveRecord::Schema.define(version: 20170929121314) do

# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
Expand All @@ -21,6 +21,7 @@
t.datetime "updated_at", null: false
t.integer "question_id"
t.integer "user_id"
t.boolean "best"
t.index ["question_id"], name: "index_answers_on_question_id", using: :btree
t.index ["user_id"], name: "index_answers_on_user_id", using: :btree
end
Expand Down
47 changes: 41 additions & 6 deletions spec/controllers/answers_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
RSpec.describe AnswersController, :type => :controller do
sign_in_user
let!(:question) { create(:question, user: @user) }
let!(:answer) {create( :answer, question: question)}
let!(:answer) {create( :answer, question: question, user: @user)}

describe 'POST #create' do
context 'valid answer' do
Expand Down Expand Up @@ -36,23 +36,58 @@
end
end

describe 'PATCH #update' do

it 'assigns @answer to requested answer' do
patch :update, params: {id: answer, question_id: question, answer: attributes_for(:answer)}, format: :js
expect(assigns(:answer)).to eq answer
end

it 'changes answer attributes' do
patch :update, params: {id: answer, question_id: question, answer: {body: "NewBody"}}, format: :js
answer.reload
expect(answer.body).to eq "NewBody"

end

it 'render template update' do
patch :update, params: {id: answer, question_id: question, answer: {body: "NewBody"}}, format: :js
expect(response).to render_template :update
end

it 'render error template if errors exists' do
patch :update, params: {id: answer, question_id: question, answer: {body: ""}}, format: :js
expect(response).to render_template :error
end

let!(:another_user) {create(:user)}
let(:another_answer) {create(:answer, question: question, user: another_user)}
it 'not change answer if user not author' do
patch :update, params: {id: another_answer, question_id: question, answer: {body: "NewBody"}}, format: :js
another_answer.reload
expect(another_answer.body).to eq "Answer body"

end

end

describe 'DELETE #destroy' do
sign_in_user
it "user tries to delete own answer" do
answer = create(:answer, question: question, user: @user)

expect{delete :destroy, params:{ question_id: question, id: answer}}.to change(Answer, :count).by(-1)
expect{delete :destroy, params:{ question_id: question, id: answer}, format: :js}.to change(Answer, :count).by(-1)

end

it "user can't delete someone else question" do
another_answer = create(:answer, question: question, user: create(:user))
expect{delete :destroy, params: {question_id: question, id: another_answer}}.to_not change(Answer, :count)
expect{delete :destroy, params: {question_id: question, id: another_answer}, format: :js}.to_not change(Answer, :count)
end

it "redirects to question show page" do
delete :destroy, params: {question_id: question, id: answer}
expect(response).to redirect_to question_path(question)
it "render destroy template" do
delete :destroy, params: {question_id: question, id: answer}, format: :js
expect(response).to render_template :destroy
end
end
end
Expand Down
Loading