Skip to content

Commit

Permalink
chore: Adding theme with tasks
Browse files Browse the repository at this point in the history
  • Loading branch information
mikeoleynik committed Sep 20, 2019
1 parent b767cdd commit 5e2472b
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 6 deletions.
2 changes: 1 addition & 1 deletion app/controllers/themes_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,6 @@ def destroy
private

def theme_params
params.permit(:title)
params.permit(:title, nodes_attributes: %i[id task_id theme_id _destroy])
end
end
14 changes: 10 additions & 4 deletions db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,19 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 2019_09_11_072214) do
ActiveRecord::Schema.define(version: 2019_09_20_124750) do

# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"

create_table "nodes", force: :cascade do |t|
t.bigint "theme_id", null: false
t.bigint "task_id", null: false
t.boolean "access", default: false
t.index ["task_id"], name: "index_nodes_on_task_id"
t.index ["theme_id"], name: "index_nodes_on_theme_id"
end

create_table "pictures", force: :cascade do |t|
t.string "image", null: false
t.integer "task_id"
Expand All @@ -25,10 +33,8 @@
t.string "title", null: false
t.text "body", null: false
t.integer "difficulty", null: false
t.integer "theme_id", null: false
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["theme_id"], name: "index_tasks_on_theme_id"
end

create_table "themes", force: :cascade do |t|
Expand All @@ -46,7 +52,7 @@

create_table "users", force: :cascade do |t|
t.string "fullname", null: false
t.boolean "mentor", default: true
t.boolean "mentor", default: false
t.string "email", default: "", null: false
t.string "encrypted_password", default: "", null: false
t.string "reset_password_token"
Expand Down
1 change: 0 additions & 1 deletion spec/factories/task.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,5 @@
title { Faker::Quote.yoda }
body { Faker::Lorem.paragraph }
difficulty { rand(1..10) }
theme_id { nil }
end
end
41 changes: 41 additions & 0 deletions spec/request/nodes/create_node_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
require 'rails_helper'

describe 'Create Node', type: :request do
describe 'POST /themes' do
let!(:user) { create(:user) }

context 'authorized' do
context 'valid attributes' do
before {
sign_in user
post '/themes', params: { title: Faker::Educator.subject }
}

it_behaves_like 'status 200'
it_behaves_like 'success data'
end

context 'invalid attributes' do
before {
sign_in user
post '/themes', params: { title: nil }
}

it 'return status 422' do
expect(response.status).to eq 422
end

it 'return errors in the response' do
expect(json_response).to have_jsonapi_errors
end
end
end

context 'unauthorized' do
it 'return 401 status if user is not authorized' do
post '/themes'
expect(response.status).to eq 302
end
end
end
end
12 changes: 12 additions & 0 deletions spec/request/themes/create_theme_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
describe 'Create theme', type: :request do
describe 'POST /themes' do
let!(:user) { create(:user) }
let!(:task) { create(:task) }

context 'authorized' do
context 'valid attributes' do
Expand All @@ -15,6 +16,17 @@
it_behaves_like 'success data'
end

context 'valid attributes with tasks' do
before {
sign_in user
post '/themes', params: { title: Faker::Educator.subject,
nodes_attributes: [{task_id: task.id}] }
}

it_behaves_like 'status 200'
it_behaves_like 'success data'
end

context 'invalid attributes' do
before {
sign_in user
Expand Down

0 comments on commit 5e2472b

Please sign in to comment.