Skip to content
Open
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
3 changes: 3 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ git_source(:github) { |repo| "https://github.com/#{repo}.git" }

ruby '2.7.1'

gem 'haml'
gem "haml-rails", "~> 2.0"
# Bundle edge Rails instead: gem 'rails', github: 'rails/rails', branch: 'main'
gem 'rails', '~> 6.1.3'
# Use postgresql as the database for Active Record
Expand Down Expand Up @@ -33,6 +35,7 @@ group :development, :test do
gem 'byebug', platforms: [:mri, :mingw, :x64_mingw]
gem 'rspec-rails', '~> 4.0.2'
gem 'factory_bot_rails'
gem 'rails-controller-testing'
end

group :development do
Expand Down
26 changes: 26 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ GEM
crass (1.0.6)
diff-lcs (1.4.4)
erubi (1.10.0)
erubis (2.7.0)
factory_bot (6.1.0)
activesupport (>= 5.0.0)
factory_bot_rails (6.1.0)
Expand All @@ -88,6 +89,20 @@ GEM
ffi (1.14.2)
globalid (0.4.2)
activesupport (>= 4.2.0)
haml (5.2.1)
temple (>= 0.8.0)
tilt
haml-rails (2.0.1)
actionpack (>= 5.1)
activesupport (>= 5.1)
haml (>= 4.0.6, < 6.0)
html2haml (>= 1.0.1)
railties (>= 5.1)
html2haml (2.2.0)
erubis (~> 2.7.0)
haml (>= 4.0, < 6)
nokogiri (>= 1.6.0)
ruby_parser (~> 3.5)
i18n (1.8.9)
concurrent-ruby (~> 1.0)
jbuilder (2.11.2)
Expand Down Expand Up @@ -139,6 +154,10 @@ GEM
bundler (>= 1.15.0)
railties (= 6.1.3)
sprockets-rails (>= 2.0.0)
rails-controller-testing (1.0.5)
actionpack (>= 5.0.1.rc1)
actionview (>= 5.0.1.rc1)
activesupport (>= 5.0.1.rc1)
rails-dom-testing (2.0.3)
activesupport (>= 4.2.0)
nokogiri (>= 1.6)
Expand Down Expand Up @@ -172,6 +191,8 @@ GEM
rspec-mocks (~> 3.10)
rspec-support (~> 3.10)
rspec-support (3.10.2)
ruby_parser (3.15.1)
sexp_processor (~> 4.9)
rubyzip (2.3.0)
sass-rails (6.0.0)
sassc-rails (~> 2.1, >= 2.1.1)
Expand All @@ -187,6 +208,7 @@ GEM
childprocess (>= 0.5, < 4.0)
rubyzip (>= 1.2.2)
semantic_range (2.3.1)
sexp_processor (4.15.2)
shoulda-matchers (4.5.1)
activesupport (>= 4.2.0)
spring (2.1.1)
Expand All @@ -197,6 +219,7 @@ GEM
actionpack (>= 4.0)
activesupport (>= 4.0)
sprockets (>= 3.0.0)
temple (0.8.2)
thor (1.1.0)
tilt (2.0.10)
turbolinks (5.2.1)
Expand Down Expand Up @@ -233,12 +256,15 @@ DEPENDENCIES
byebug
capybara (>= 3.26)
factory_bot_rails
haml
haml-rails (~> 2.0)
jbuilder (~> 2.7)
listen (~> 3.3)
pg (~> 1.1)
puma (~> 5.0)
rack-mini-profiler (~> 2.0)
rails (~> 6.1.3)
rails-controller-testing
rspec-rails (~> 4.0.2)
sass-rails (>= 6)
selenium-webdriver
Expand Down
3 changes: 3 additions & 0 deletions app/assets/stylesheets/answers.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// Place all the styles related to the answers controller here.
// They will automatically be included in application.css.
// You can use Sass (SCSS) here: https://sass-lang.com/
3 changes: 3 additions & 0 deletions app/assets/stylesheets/questions.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// Place all the styles related to the questions controller here.
// They will automatically be included in application.css.
// You can use Sass (SCSS) here: https://sass-lang.com/
45 changes: 45 additions & 0 deletions app/controllers/answers_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
class AnswersController < ApplicationController
def new
end

def create
@answer = question.answers.new(answer_params)
if @answer.save
redirect_to @answer.question
else
render :new
end
end

def edit
end

def update
if answer.update(answer_params)
redirect_to answer.question
else
render :edit
end
end

def destroy
answer.destroy
redirect_to answer.question
end

private

def answer
@answer ||= params[:id] ? Answer.find(params[:id]) : question.answers.new
end

helper_method :answer

def question
@question ||= Question.find(params[:question_id])
end

def answer_params
params.require(:answer).permit(:body)
end
end
49 changes: 49 additions & 0 deletions app/controllers/questions_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
class QuestionsController < ApplicationController
def index
@questions = Question.all
end

def show
end

def new
end

def edit
end

def create
@question = Question.new(question_params)

if @question.save
redirect_to @question
else
render :new
end
end

def update
if question.update(question_params)
redirect_to @question
else
render :edit
end
end

def destroy
question.destroy
redirect_to questions_path
end

private

def question
@question ||= params[:id] ? Question.find(params[:id]) : Question.new
end

helper_method :question

def question_params
params.require(:question).permit(:title, :body)
end
end
2 changes: 2 additions & 0 deletions app/helpers/answers_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
module AnswersHelper
end
2 changes: 2 additions & 0 deletions app/helpers/questions_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
module QuestionsHelper
end
Empty file.
Empty file added app/views/answers/new.html.haml
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
9 changes: 9 additions & 0 deletions config/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,14 @@ class Application < Rails::Application
#
# config.time_zone = "Central Time (US & Canada)"
# config.eager_load_paths << Rails.root.join("extras")
config.generators do |g|
g.test_framework :rspec,
controller_specs: true,
view_specs: false,
helper_specs: false,
routing_specs: false,
request_specs: false

end
end
end
4 changes: 3 additions & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
Rails.application.routes.draw do
# For details on the DSL available within this file, see https://guides.rubyonrails.org/routing.html
resources :questions do
resources :answers, shallow: true, except: %i[index show]
end
end
2 changes: 1 addition & 1 deletion db/migrate/20210301193229_create_answers.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
class CreateAnswers < ActiveRecord::Migration[6.1]
def change
create_table :answers do |t|
t.string :body, null: false
t.text :body, null: false
t.references :question, null: false, foreign_key: true

t.timestamps
Expand Down
2 changes: 1 addition & 1 deletion db/schema.rb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

88 changes: 88 additions & 0 deletions spec/controllers/answers_controller_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
require 'rails_helper'

RSpec.describe AnswersController, type: :controller do
let!(:answer) { create(:answer) }

describe 'GET #new' do
before { get :new, params: { question_id: answer.question } }

it 'renders new view' do
expect(response).to render_template :new
end
end

describe 'POST #create' do
context 'with valid attributes' do
it 'saves a new answer in the database' do
expect { post :create, params: { answer: attributes_for(:answer),
question_id: answer.question }
}.to change(Answer, :count).by(1)

Choose a reason for hiding this comment

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

тут стоит в change указать question.answers, чтобы сразу проверить, что ответ связывается с вопросом

end

it 'redirects to associated question' do
post :create, params: { answer: attributes_for(:answer), question_id: answer.question }
expect(response).to redirect_to answer.question
end
end

context 'with invalid attributes' do
it 'does not save the answer' do
expect { post :create, params: { answer: attributes_for(:answer, :invalid_answer),
question_id: answer.question }
}.to_not change(Answer, :count)
end

it 're-renders new view' do
post :create, params: { answer: attributes_for(:answer, :invalid_answer), question_id: answer.question }
expect(response).to render_template :new
end
end
end

describe 'GET #edit' do
before { get :edit, params: { id: answer } }

it 'renders edit view' do
expect(response).to render_template :edit
end
end

describe 'PATCH #update' do
context 'with valid attributes' do
it 'changes answer attributes' do
patch :update, params: { id: answer, answer: { body: 'new body' } }
answer.reload
expect(answer.body).to eq 'new body'
end

it 'redirects to associated question' do
patch :update, params: { id: answer, answer: attributes_for(:answer) }
expect(response).to redirect_to answer.question
end
end

context 'with invalid attributes' do
before { patch :update, params: { id: answer, answer: attributes_for(:answer, :invalid_answer) } }

it 'does not change answer' do
answer.reload
expect(answer.body).to eq "MyText"
end

it 're-renders edit view' do
expect(response).to render_template :edit
end
end
end

describe 'DELETE #destroy' do
it 'deletes the answer' do
expect { delete :destroy, params: { id: answer } }.to change(Answer, :count).by(-1)
end

it 'redirects to associated question' do
delete :destroy, params: { id: answer }
expect(response).to redirect_to answer.question
end
end
end
Loading