Skip to content
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

Merged
merged 15 commits into from
Sep 24, 2019
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,4 @@
# Ignore master key for decrypting credentials and more.
/config/master.key
/config/database.yml
/.yardoc
6 changes: 6 additions & 0 deletions .hound.yml
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
15 changes: 15 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
language: ruby
rvm:
- 2.6.3

addons:
postgresql: '10'

before_script:
- sudo /etc/init.d/postgresql restart
- cp config/database.yml.sample config/database.yml
- psql -c 'create database rock_test;' -U postgres

script:
- bundle exec rails db:migrate RAILS_ENV=test
- bundle exec rspec
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ group :development, :test do
end

group :development do
gem 'annotate'

Choose a reason for hiding this comment

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

возможно в test еще добавить?

gem 'listen', '>= 3.0.5', '< 3.2'
gem 'spring'
gem 'spring-watcher-listen', '~> 2.0.0'
Expand Down
4 changes: 4 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ GEM
tzinfo (~> 1.1)
addressable (2.7.0)
public_suffix (>= 2.0.2, < 5.0)
annotate (2.7.5)
activerecord (>= 3.2, < 7.0)
rake (>= 10.4, < 13.0)
arel (9.0.0)
bcrypt (3.1.13)
bootsnap (1.4.5)
Expand Down Expand Up @@ -229,6 +232,7 @@ PLATFORMS
ruby

DEPENDENCIES
annotate
bootsnap (>= 1.1.0)
carrierwave (~> 2.0)
database_cleaner
Expand Down
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
[![Build Status](https://travis-ci.org/mikeoleynik/edctn.svg?branch=master)](https://travis-ci.org/mikeoleynik/edctn)

## Getting Started

This section provides quick start guide.

### Prerequisites

- [Ruby](https://www.ruby-lang.org/en/): 2.6.3
- [Ruby on Rails](https://rubyonrails.org/): 2.6.3
- [Ruby on Rails](https://rubyonrails.org/): 5.2.3
- [PostgreSQL](https://www.postgresql.org/) 9.4 or higher.

### Standard Installation
Expand Down
15 changes: 2 additions & 13 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,7 @@ def render_success(serializer)
render json: serializer.serialized_json, status: :ok
end

def render_errors(task)
render json: wrong_attribute(task.errors.full_messages), status: :unprocessable_entity
end

def wrong_attribute(errors)
{
"errors": [
{
"status": '422',
"detail": errors
}
]
}
def respond_with_errors(object)
render json: { errors: ErrorSerializer.serialize(object) }, status: :unprocessable_entity
end
end
17 changes: 7 additions & 10 deletions app/controllers/tasks_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,19 @@
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
def update
task = Task.find(params[:id])

if task.update_attributes(task_params)
if task.update(task_params)
render_success(TaskSerializer.new(task))
else
render_errors(task)
respond_with_errors(task)
end
end

Expand All @@ -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],
Copy link

Choose a reason for hiding this comment

The 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
6 changes: 3 additions & 3 deletions app/controllers/themes_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ def create
if theme.save
render_success(ThemeSerializer.new(theme))
else
render_errors(theme)
respond_with_errors(theme)
end
end

Expand All @@ -17,7 +17,7 @@ def update
if theme.update(theme_params)
render_success(ThemeSerializer.new(theme))
else
render_errors(theme)
respond_with_errors(theme)
end
end

Expand All @@ -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
2 changes: 1 addition & 1 deletion app/controllers/user_tasks_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ def create
if user_task.save
render_success(UserTaskSerializer.new(user_task))
else
render_errors(user_task)
respond_with_errors(user_task)
end
end

Expand Down
56 changes: 56 additions & 0 deletions app/forms/create_task.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
# 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
Copy link

Choose a reason for hiding this comment

The 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
Copy link

Choose a reason for hiding this comment

The 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]

Choose a reason for hiding this comment

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

hound прав, многовато логики для одного метода. выглядит грязно

if valid?
task = Task.create!(title: title, body: body, difficulty: difficulty)
create_association_for(task)
TaskSerializer.new(task)
else
{ errors: ErrorSerializer.serialize(self) }
end
end

# Return status 200 when task valid or 422 when not valid
def status
@status = valid? ? :ok : :unprocessable_entity
end

private

# Creates associate for Task
def create_association_for(task)
task.create_picture(image: picture_attributes[:image]) if picture_attributes.present?
create_nodes(task.id) if nodes_attributes.present?
create_user_tasks(task.id) if user_tasks_attributes.present?
end

# Creates associate with User
def create_user_tasks(task_id)
user_tasks_attributes.each do |attribute|
user_id = attribute['user_id']
UserTask.create!(user_id: user_id, task_id: task_id)
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
2 changes: 1 addition & 1 deletion app/mailers/task_mailer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ def create_task
email = User.find(params[:user]).email
@task = Task.find(params[:task]).title

mail(to: email, subject: t(:subject, scope: [:mailers, :task, :create_task]))
mail(to: email, subject: t(:subject, scope: %i[mailers task create_task]))
end
end
18 changes: 18 additions & 0 deletions app/models/node.rb
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
9 changes: 9 additions & 0 deletions app/models/picture.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
# frozen_string_literal: true

# == Schema Information
#
# Table name: pictures
#
# id :bigint not null, primary key
# image :string not null
# task_id :integer
#

class Picture < ApplicationRecord
belongs_to :task

Expand Down
17 changes: 15 additions & 2 deletions app/models/task.rb
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
15 changes: 14 additions & 1 deletion app/models/theme.rb
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
22 changes: 22 additions & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,27 @@
# frozen_string_literal: true

# == Schema Information
#
# Table name: users
#
# id :bigint not null, primary key
# fullname :string not null
# mentor :boolean default(FALSE)
# email :string default(""), not null
# encrypted_password :string default(""), not null
# reset_password_token :string
# reset_password_sent_at :datetime
# remember_created_at :datetime
# invitation_token :string
# invitation_created_at :datetime
# invitation_sent_at :datetime
# invitation_accepted_at :datetime
# invitation_limit :integer
# invited_by_type :string
# invited_by_id :bigint
# invitations_count :integer default(0)
#

class User < ApplicationRecord
devise :database_authenticatable, :invitable,
:recoverable, :rememberable, :validatable
Expand Down
20 changes: 13 additions & 7 deletions app/models/user_task.rb
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
15 changes: 15 additions & 0 deletions app/serializers/error_serializer.rb
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
Loading