-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
create theme #9
create theme #9
Changes from 13 commits
9fb4999
cc47c47
34dcbb6
b767cdd
5e2472b
6226803
870d2b6
d06ef59
c5f6b9f
51aab49
60622d1
ea9ece6
09c37e0
9208046
4ec087e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
reek: | ||
enable: true | ||
config_file: .reek.yml | ||
|
||
rubocop: | ||
config_file: .rubocop.yml |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
language: ruby | ||
rvm: | ||
- 2.6.3 | ||
|
||
addons: | ||
postgresql: 10.10 | ||
|
||
before_script: | ||
- cp config/database.yml.sample config/database.yml | ||
- psql -c 'create database rock_test;' -U postgres | ||
|
||
script: | ||
- bundle exec rails db:create RAILS_ENV=test | ||
- bundle exec rails db:migrate RAILS_ENV=test | ||
- bundle exec rspec |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,13 +3,9 @@ | |
class TasksController < ApplicationController | ||
# POST /tasks | ||
def create | ||
task = Task.new(task_params) | ||
task = CreateTask.new(task_params) | ||
|
||
if task.save | ||
render_success(TaskSerializer.new(task)) | ||
else | ||
render_errors(task) | ||
end | ||
render json: task.save, status: task.status | ||
end | ||
|
||
# PATCH /tasks/:id | ||
|
@@ -19,7 +15,7 @@ def update | |
if task.update_attributes(task_params) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. update вместо update_attributes |
||
render_success(TaskSerializer.new(task)) | ||
else | ||
render_errors(task) | ||
respond_with_errors(task) | ||
end | ||
end | ||
|
||
|
@@ -33,7 +29,8 @@ def destroy | |
private | ||
|
||
def task_params | ||
params.permit(:title, :body, :difficulty, :theme_id, picture_attributes: %i[image id _destroy], | ||
user_tasks_attributes: %i[id task_id user_id _destroy]) | ||
params.permit(:title, :body, :difficulty, picture_attributes: %i[image id _destroy], | ||
user_tasks_attributes: %i[id task_id user_id _destroy], | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Metrics/LineLength: Line is too long. [101/100] |
||
nodes_attributes: %i[id task_id theme_id _destroy]) | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
# frozen_string_literal: true | ||
|
||
# FormObject to create task and nested object. | ||
# Associate tasks with users, themes, and picture. | ||
|
||
class CreateTask | ||
include ActiveModel::Model | ||
|
||
attr_accessor :title, :body, :difficulty, :picture_attributes, :user_tasks_attributes, :nodes_attributes | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Metrics/LineLength: Line is too long. [106/100] |
||
|
||
validates :title, :body, :difficulty, presence: true | ||
validates :difficulty, numericality: { only_integer: true, less_than_or_equal_to: 10 } | ||
|
||
# Creates a Task and associate it with a user, theme, and image | ||
# Returns JSON API response for Task (:title, :body, :difficulty) | ||
def save | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Metrics/AbcSize: Assignment Branch Condition size for save is too high. [20.42/15] There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. hound прав, многовато логики для одного метода. выглядит грязно |
||
if valid? | ||
task = Task.create!(title: title, body: body, difficulty: difficulty) | ||
task.create_picture(image: picture_attributes[:image]) if picture_attributes.present? | ||
create_nodes(task.id) if nodes_attributes.present? | ||
create_user_task(task.id) if user_tasks_attributes.present? | ||
TaskSerializer.new(task) | ||
else | ||
{ errors: ErrorSerializer.serialize(self) } | ||
end | ||
end | ||
|
||
def status | ||
@status = valid? ? :ok : :unprocessable_entity | ||
end | ||
|
||
private | ||
|
||
# Creates associate with User | ||
def create_user_task(task_id) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. create_user_tasks There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. user.tasks.create!() |
||
user_tasks_attributes.each do |attribute| | ||
user_id = attribute['user_id'] | ||
UserTask.create(user_id: user_id, task_id: task_id) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. create! |
||
TaskMailer.with(user: user_id, task: task_id).create_task.deliver_later | ||
end | ||
end | ||
|
||
# Creates associate with Theme | ||
def create_nodes(task_id) | ||
nodes_attributes.each do |attribute| | ||
theme_id = attribute['theme_id'] | ||
Node.create(theme_id: theme_id, task_id: task_id) | ||
end | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
# frozen_string_literal: true | ||
|
||
# Join table for Theme and Task | ||
|
||
# == Schema Information | ||
# | ||
# Table name: nodes | ||
# | ||
# id :bigint not null, primary key | ||
# theme_id :bigint not null | ||
# task_id :bigint not null | ||
# access :boolean default(FALSE) | ||
# | ||
|
||
class Node < ApplicationRecord | ||
belongs_to :theme | ||
belongs_to :task | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,28 @@ | ||
# frozen_string_literal: true | ||
|
||
class Task < ApplicationRecord | ||
belongs_to :theme | ||
# == Schema Information | ||
# | ||
# Table name: tasks | ||
# | ||
# id :bigint not null, primary key | ||
# title :string not null | ||
# body :text not null | ||
# difficulty :integer not null | ||
# created_at :datetime not null | ||
# updated_at :datetime not null | ||
# | ||
|
||
class Task < ApplicationRecord | ||
has_one :picture, dependent: :destroy | ||
has_many :user_tasks, dependent: :destroy | ||
has_many :users, through: :user_tasks | ||
has_many :nodes, dependent: :destroy | ||
has_many :themes, through: :nodes | ||
|
||
validates :title, :body, :difficulty, presence: true | ||
validates :difficulty, numericality: { only_integer: true, less_than_or_equal_to: 10 } | ||
|
||
accepts_nested_attributes_for :picture | ||
accepts_nested_attributes_for :user_tasks | ||
accepts_nested_attributes_for :nodes | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,20 @@ | ||
# frozen_string_literal: true | ||
|
||
# == Schema Information | ||
# | ||
# Table name: themes | ||
# | ||
# id :bigint not null, primary key | ||
# title :string not null | ||
# created_at :datetime not null | ||
# updated_at :datetime not null | ||
# | ||
|
||
class Theme < ApplicationRecord | ||
has_many :tasks, dependent: :destroy | ||
has_many :nodes, dependent: :destroy | ||
has_many :tasks, through: :nodes | ||
|
||
validates :title, presence: true | ||
|
||
accepts_nested_attributes_for :nodes | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,20 @@ | ||
# frozen_string_literal: true | ||
|
||
# Join table for User and Task | ||
|
||
# == Schema Information | ||
# | ||
# Table name: user_tasks | ||
# | ||
# id :bigint not null, primary key | ||
# user_id :bigint not null | ||
# task_id :bigint not null | ||
# | ||
|
||
class UserTask < ApplicationRecord | ||
belongs_to :user | ||
belongs_to :task | ||
|
||
after_commit :create_task | ||
|
||
private | ||
|
||
def create_task | ||
TaskMailer.with(user: user_id, task: task_id).create_task.deliver_later | ||
end | ||
validates :user_id, uniqueness: { scope: :task_id, | ||
message: 'The student is already doing this task' } | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
# frozen_string_literal: true | ||
|
||
class ErrorSerializer | ||
def self.serialize(object) | ||
object.errors.messages.map do |field, errors| | ||
errors.map do |error_message| | ||
{ | ||
status: 422, | ||
source: { pointer: "/data/attributes/#{field}" }, | ||
detail: error_message | ||
} | ||
end | ||
end.flatten | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,17 @@ | ||
# frozen_string_literal: true | ||
|
||
# == Schema Information | ||
# | ||
# Table name: tasks | ||
# | ||
# id :bigint not null, primary key | ||
# title :string not null | ||
# body :text not null | ||
# difficulty :integer not null | ||
# created_at :datetime not null | ||
# updated_at :datetime not null | ||
# | ||
|
||
class TaskSerializer < BaseSerializer | ||
attributes :title, :body, :difficulty, :theme_id | ||
attributes :title, :body, :difficulty | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
возможно в test еще добавить?