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 task #8

Merged
merged 17 commits into from
Sep 12, 2019
Merged
Show file tree
Hide file tree
Changes from 14 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: 2 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ gem 'puma', '~> 3.11'
gem 'bootsnap', '>= 1.1.0', require: false
gem 'haml-rails', '~> 2.0'
gem 'devise_invitable', '~> 2.0.0'
gem 'fast_jsonapi'
gem 'carrierwave', '~> 2.0'

group :development, :test do
gem 'pry-byebug'
Expand Down
17 changes: 17 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,13 @@ GEM
msgpack (~> 1.0)
builder (3.2.3)
byebug (11.0.1)
carrierwave (2.0.1)
activemodel (>= 5.0.0)
activesupport (>= 5.0.0)
addressable (~> 2.6)
image_processing (~> 1.1)
mimemagic (>= 0.3.0)
mini_mime (>= 0.1.3)
coderay (1.1.2)
concurrent-ruby (1.1.5)
crass (1.0.4)
Expand All @@ -79,6 +86,8 @@ GEM
factory_bot_rails (5.0.2)
factory_bot (~> 5.0.2)
railties (>= 4.2.0)
fast_jsonapi (1.5)
activesupport (>= 4.2)
ffi (1.11.1)
globalid (0.4.2)
activesupport (>= 4.2.0)
Expand All @@ -98,6 +107,9 @@ GEM
ruby_parser (~> 3.5)
i18n (1.6.0)
concurrent-ruby (~> 1.0)
image_processing (1.9.3)
mini_magick (>= 4.9.5, < 5)
ruby-vips (>= 2.0.13, < 3)
launchy (2.4.3)
addressable (~> 2.3)
letter_opener (1.7.0)
Expand All @@ -115,6 +127,7 @@ GEM
mimemagic (~> 0.3.2)
method_source (0.9.2)
mimemagic (0.3.3)
mini_magick (4.9.5)
mini_mime (1.0.2)
mini_portile2 (2.4.0)
minitest (5.11.3)
Expand Down Expand Up @@ -183,6 +196,8 @@ GEM
rspec-mocks (~> 3.8.0)
rspec-support (~> 3.8.0)
rspec-support (3.8.2)
ruby-vips (2.0.15)
ffi (~> 1.9)
ruby_dep (1.5.0)
ruby_parser (3.13.1)
sexp_processor (~> 4.9)
Expand Down Expand Up @@ -215,10 +230,12 @@ PLATFORMS

DEPENDENCIES
bootsnap (>= 1.1.0)
carrierwave (~> 2.0)
database_cleaner
devise_invitable (~> 2.0.0)
factory_bot_rails
faker!
fast_jsonapi
haml-rails (~> 2.0)
letter_opener
listen (>= 3.0.5, < 3.2)
Expand Down
19 changes: 19 additions & 0 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,23 @@

class ApplicationController < ActionController::API
before_action :authenticate_user!

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
}
]
}
end
end
39 changes: 39 additions & 0 deletions app/controllers/tasks_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# frozen_string_literal: true

class TasksController < ApplicationController
# POST /tasks
def create
task = Task.new(task_params)

if task.save
render_success(TaskSerializer.new(task))

Choose a reason for hiding this comment

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

эта конструкция повторяется во всех контроллерах, думаю можно вынести в метод и передавать в него блок

else
render_errors(task)
end
end

# PATCH /tasks/:id
def update
task = Task.find(params[:id])

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

# DELETE /tasks/:id
def destroy
task = Task.find(params[:id])
task.destroy
render json: {}, status: :no_content
end

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])
end
end
36 changes: 36 additions & 0 deletions app/controllers/themes_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# frozen_string_literal: true

class ThemesController < ApplicationController
# POST /themes
def create
theme = Theme.new(theme_params)
if theme.save
render_success(ThemeSerializer.new(theme))
else
render_errors(theme)
end
end

# PATCH /themes/:id
def update
theme = Theme.find(params[:id])
if theme.update_attributes(theme_params)

Choose a reason for hiding this comment

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

update

render_success(ThemeSerializer.new(theme))
else
render_errors(theme)
end
end

# DELETE /themes/:id
def destroy
theme = Theme.find(params[:id])
theme.destroy
render json: {}, status: :no_content
end

private

def theme_params
params.permit(:title)
end
end
20 changes: 20 additions & 0 deletions app/controllers/user_tasks_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# frozen_string_literal: true

class UserTasksController < ApplicationController
# POST /user_tasks
def create
user_task = UserTask.new(user_tasks_params)

if user_task.save
render_success(UserTaskSerializer.new(user_task))
else
render_errors(user_task)
end
end

private

def user_tasks_params
params.permit(:user_id, :task_id)
end
end
4 changes: 2 additions & 2 deletions app/controllers/users_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
class UsersController < ApplicationController
# POST /users
def create
User.invite!(email: '[email protected]', fullname: 'John Doe')
render json: { success: 'invite sent' }, status: :ok
user = User.invite!(email: '[email protected]', fullname: 'John Doe')
render_success(UserSerializer.new(user))
end
end
10 changes: 10 additions & 0 deletions app/mailers/task_mailer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# frozen_string_literal: true

class TaskMailer < ApplicationMailer
def create_task
email = User.find(params[:user]).email
@task = Task.find(params[:task]).title

mail(to: email, subject: 'Task created')

Choose a reason for hiding this comment

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

юзай I18n сразу

end
end
7 changes: 7 additions & 0 deletions app/models/picture.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# frozen_string_literal: true

class Picture < ApplicationRecord
belongs_to :task

mount_uploader :image, PictureUploader
end
15 changes: 15 additions & 0 deletions app/models/task.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# frozen_string_literal: true

class Task < ApplicationRecord
belongs_to :theme

has_one :picture, dependent: :destroy
has_many :user_tasks, dependent: :destroy
has_many :users, through: :user_tasks

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
end
7 changes: 7 additions & 0 deletions app/models/theme.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# frozen_string_literal: true

class Theme < ApplicationRecord
has_many :tasks, dependent: :destroy

validates :title, presence: true
end
3 changes: 3 additions & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ class User < ApplicationRecord
devise :database_authenticatable, :invitable,
:recoverable, :rememberable, :validatable

has_many :user_tasks, dependent: :destroy
has_many :tasks, through: :user_tasks

validates :email, :password, presence: true
validates :email, uniqueness: true
validates :password, length: { in: 8..20 }
Expand Down
14 changes: 14 additions & 0 deletions app/models/user_task.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# frozen_string_literal: true

class UserTask < ApplicationRecord
belongs_to :user
belongs_to :task

after_commit :create_task

Choose a reason for hiding this comment

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

лучше использовать сервисы, чтобы избежать подобных коллбеков. коллбеки сложнее тестировать и они менее предсказуемы чем линейный код


private

def create_task
TaskMailer.with(user: user_id, task: task_id).create_task.deliver_later
end
end
6 changes: 6 additions & 0 deletions app/serializers/task_serializer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# frozen_string_literal: true

class TaskSerializer
include FastJsonapi::ObjectSerializer

Choose a reason for hiding this comment

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

сделай базовый класс и вынеси include в него

attributes :title, :body, :difficulty, :theme_id
end
6 changes: 6 additions & 0 deletions app/serializers/theme_serializer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# frozen_string_literal: true

class ThemeSerializer
include FastJsonapi::ObjectSerializer
attributes :title
end
4 changes: 4 additions & 0 deletions app/serializers/user_serializer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
class UserSerializer
include FastJsonapi::ObjectSerializer
attributes :email, :fullname
end
6 changes: 6 additions & 0 deletions app/serializers/user_task_serializer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# frozen_string_literal: true

class UserTaskSerializer
include FastJsonapi::ObjectSerializer
attributes :user_id, :task_id
end
23 changes: 23 additions & 0 deletions app/uploaders/picture_uploader.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
class PictureUploader < CarrierWave::Uploader::Base
include CarrierWave::MiniMagick

storage :file

def store_dir
"uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
end

# process scale: [200, 300]

Choose a reason for hiding this comment

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

удаляй этот закомментированный код


# def scale(width, height)
# # do something
# end

# version :thumb do
# process resize_to_fit: [50, 50]
# end

def extension_whitelist
%w(jpg jpeg gif png)
end
end
13 changes: 0 additions & 13 deletions app/views/layouts/mailer.html.erb

This file was deleted.

8 changes: 8 additions & 0 deletions app/views/layouts/mailer.html.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
!!!
%html
%head
%meta{:content => "text/html; charset=utf-8", "http-equiv" => "Content-Type"}/
:css
/* Email styles need to be inline */
%body
= yield
1 change: 0 additions & 1 deletion app/views/layouts/mailer.text.erb

This file was deleted.

1 change: 1 addition & 0 deletions app/views/layouts/mailer.text.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
= yield
1 change: 1 addition & 0 deletions app/views/task_mailer/create_task.html.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
%p Task: #{@task} created
3 changes: 3 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,7 @@
root 'home#homepage'

resources :users, only: :create
resources :themes
resources :tasks
resources :user_tasks, only: [:create, :destroy]
end
2 changes: 1 addition & 1 deletion db/migrate/20190908064945_create_users.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
class CreateUsers < ActiveRecord::Migration[5.2]
def change
create_table :users do |t|
t.string :fullname, null: false
t.string :fullname, null: false
t.boolean :mentor, default: true
end
end
Expand Down
9 changes: 9 additions & 0 deletions db/migrate/20190910101032_create_themes.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class CreateThemes < ActiveRecord::Migration[5.2]
def change
create_table :themes do |t|
t.string :title, null: false

t.timestamps
end
end
end
12 changes: 12 additions & 0 deletions db/migrate/20190910101038_create_tasks.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
class CreateTasks < ActiveRecord::Migration[5.2]
def change
create_table :tasks do |t|
t.string :title, null: false
t.text :body, null: false
t.integer :difficulty, null: false
t.integer :theme_id, null: false, index: true

t.timestamps
end
end
end
8 changes: 8 additions & 0 deletions db/migrate/20190910134404_create_user_tasks.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
class CreateUserTasks < ActiveRecord::Migration[5.2]
def change
create_table :user_tasks do |t|
t.belongs_to :user
t.belongs_to :task
end
end
end
Loading