Skip to content

Commit

Permalink
Merge pull request #8 from mikeoleynik/2-create-task
Browse files Browse the repository at this point in the history
create task
  • Loading branch information
mikeoleynik authored Sep 12, 2019
2 parents 44c7d3c + d049c62 commit ca991d8
Show file tree
Hide file tree
Showing 57 changed files with 690 additions and 62 deletions.
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))
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(theme_params)
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: t(:subject, scope: [:mailers, :task, :create_task]))
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

private

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

class BaseSerializer
include FastJsonapi::ObjectSerializer
end
5 changes: 5 additions & 0 deletions app/serializers/task_serializer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# frozen_string_literal: true

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

class ThemeSerializer < BaseSerializer
attributes :title
end
5 changes: 5 additions & 0 deletions app/serializers/user_serializer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# frozen_string_literal: true

class UserSerializer < BaseSerializer
attributes :email, :fullname
end
5 changes: 5 additions & 0 deletions app/serializers/user_task_serializer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# frozen_string_literal: true

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

storage :file

def store_dir
"uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
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
36 changes: 4 additions & 32 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
@@ -1,33 +1,5 @@
# Files in the config/locales directory are used for internationalization
# and are automatically loaded by Rails. If you want to use locales other
# than English, add the necessary files in this directory.
#
# To use the locales, use `I18n.t`:
#
# I18n.t 'hello'
#
# In views, this is aliased to just `t`:
#
# <%= t('hello') %>
#
# To use a different locale, set it with `I18n.locale`:
#
# I18n.locale = :es
#
# This would use the information in config/locales/es.yml.
#
# The following keys must be escaped otherwise they will not be retrieved by
# the default I18n backend:
#
# true, false, on, off, yes, no
#
# Instead, surround them with single quotes.
#
# en:
# 'true': 'foo'
#
# To learn more, please read the Rails Internationalization guide
# available at http://guides.rubyonrails.org/i18n.html.

en:
hello: "Hello world"
mailers:
task:
create_task:
subject: '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
Loading

0 comments on commit ca991d8

Please sign in to comment.