-
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 task #8
create task #8
Changes from 14 commits
31eda58
bec1f14
4085091
e7926d0
deb57a0
fa932b5
9cda7f9
ede215f
582da6a
57e6646
dddf917
e1ba4d0
4749937
f4179be
2b733c4
9a091d5
d049c62
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,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)) | ||
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 |
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) | ||
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 |
||
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 |
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 |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 |
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') | ||
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. юзай I18n сразу |
||
end | ||
end |
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 |
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 |
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 |
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 | ||
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. лучше использовать сервисы, чтобы избежать подобных коллбеков. коллбеки сложнее тестировать и они менее предсказуемы чем линейный код |
||
|
||
private | ||
|
||
def create_task | ||
TaskMailer.with(user: user_id, task: task_id).create_task.deliver_later | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
# frozen_string_literal: true | ||
|
||
class TaskSerializer | ||
include FastJsonapi::ObjectSerializer | ||
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. сделай базовый класс и вынеси include в него |
||
attributes :title, :body, :difficulty, :theme_id | ||
end |
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 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
class UserSerializer | ||
include FastJsonapi::ObjectSerializer | ||
attributes :email, :fullname | ||
end |
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 |
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] | ||
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. удаляй этот закомментированный код |
||
|
||
# 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 |
This file was deleted.
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 |
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
= yield |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
%p Task: #{@task} created |
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 |
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 |
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 |
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.
эта конструкция повторяется во всех контроллерах, думаю можно вынести в метод и передавать в него блок