Skip to content

Commit

Permalink
feature: Adding picture to task
Browse files Browse the repository at this point in the history
  • Loading branch information
mikeoleynik committed Sep 11, 2019
1 parent 582da6a commit 57e6646
Show file tree
Hide file tree
Showing 17 changed files with 84 additions and 2 deletions.
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ 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
14 changes: 14 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 Down Expand Up @@ -100,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 @@ -117,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 @@ -185,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 @@ -217,6 +230,7 @@ PLATFORMS

DEPENDENCIES
bootsnap (>= 1.1.0)
carrierwave (~> 2.0)
database_cleaner
devise_invitable (~> 2.0.0)
factory_bot_rails
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/tasks_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def destroy
private

def task_params
params.permit(:title, :body, :difficulty, :theme_id)
params.permit(:title, :body, :difficulty, :theme_id, picture_attributes: [:image, :id, :_destroy])
end

def render_success(task)
Expand Down
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
3 changes: 3 additions & 0 deletions app/models/task.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@
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
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]

# 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
8 changes: 8 additions & 0 deletions db/migrate/20190911072214_create_pictures.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
class CreatePictures < ActiveRecord::Migration[5.2]
def change
create_table :pictures do |t|
t.string :image
t.integer :task_id, index: true
end
end
end
8 changes: 7 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,17 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 2019_09_10_134404) do
ActiveRecord::Schema.define(version: 2019_09_11_072214) do

# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"

create_table "pictures", force: :cascade do |t|
t.string "image"
t.integer "task_id"
t.index ["task_id"], name: "index_pictures_on_task_id"
end

create_table "tasks", force: :cascade do |t|
t.string "title", null: false
t.text "body", null: false
Expand Down
Binary file added public/uploads/picture/image/1/image.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added public/uploads/picture/image/2/image.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
20 changes: 20 additions & 0 deletions spec/request/tasks/create_task_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,26 @@
end
end

context 'valid attributes with pictures' do
before {
sign_in user
post '/tasks', params: { title: Faker::Educator.subject,
body: Faker::Lorem.paragraph,
difficulty: rand(1..10),
theme_id: theme.id,
picture_attributes: {image: Rack::Test::UploadedFile.new(Rails.root.join('spec/support/images/image.png'), 'image/png')}}
}

it 'return status 200' do
expect(response.status).to eq 200
end

it 'the picture is added to the task' do
task = Task.last
expect(task.picture).to eql(Picture.last)
end
end

context 'valid attributes' do
before {
sign_in user
Expand Down
Binary file added spec/support/images/image.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 57e6646

Please sign in to comment.