From 552f811feaa6ba04f3f4abeba7b61453e43f0208 Mon Sep 17 00:00:00 2001 From: Benjamin Cousaert Date: Thu, 4 Dec 2014 20:32:31 +0100 Subject: [PATCH] Add paperclip to gems and avatar to products --- .gitignore | 3 +++ Gemfile | 3 +++ Gemfile.lock | 10 ++++++++++ app/controllers/products_controller.rb | 4 ++-- app/models/product.rb | 19 ++++++++++++------- app/views/orders/new.html.erb | 1 + app/views/products/new.html.erb | 8 +++++--- app/views/products/show.html.erb | 2 +- config/environments/development.rb | 2 ++ config/environments/production.rb | 2 ++ config/environments/test.rb | 2 ++ ...e_product_image_by_paperclip_attachment.rb | 6 ++++++ db/schema.rb | 7 +++++-- test/fixtures/products.yml | 17 ++++++++++------- test/models/product_test.rb | 17 ++++++++++------- 15 files changed, 74 insertions(+), 29 deletions(-) create mode 100644 db/migrate/20141204191328_replace_product_image_by_paperclip_attachment.rb diff --git a/.gitignore b/.gitignore index 6a502e9..031b1dc 100644 --- a/.gitignore +++ b/.gitignore @@ -14,3 +14,6 @@ # Ignore all logfiles and tempfiles. /log/*.log /tmp + +# Avatars of producst +public/system/products diff --git a/Gemfile b/Gemfile index 6499338..7c5b71d 100644 --- a/Gemfile +++ b/Gemfile @@ -62,3 +62,6 @@ gem 'bcrypt', '3.1.7' #paginate stuff gem 'will_paginate', '3.0.7' gem 'bootstrap-will_paginate', '0.0.10' + +# paperclip for easy file attachment +gem 'paperclip' diff --git a/Gemfile.lock b/Gemfile.lock index 5d62c06..7de5de3 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -54,6 +54,10 @@ GEM capistrano-rvm (0.1.2) capistrano (~> 3.0) sshkit (~> 1.2) + climate_control (0.0.3) + activesupport (>= 3.0) + cocaine (0.5.4) + climate_control (>= 0.0.3, < 1.0) coffee-rails (4.0.1) coffee-script (>= 2.2.0) railties (>= 4.0.0, < 5.0) @@ -84,6 +88,11 @@ GEM net-scp (1.2.1) net-ssh (>= 2.6.5) net-ssh (2.9.1) + paperclip (4.2.0) + activemodel (>= 3.0.0) + activesupport (>= 3.0.0) + cocaine (~> 0.5.3) + mime-types rack (1.5.2) rack-test (0.6.2) rack (>= 1.0) @@ -158,6 +167,7 @@ DEPENDENCIES jbuilder (~> 2.0) jquery-rails mysql2 + paperclip rails (= 4.1.7) sass-rails (~> 4.0.3) sdoc (~> 0.4.0) diff --git a/app/controllers/products_controller.rb b/app/controllers/products_controller.rb index d376414..058317c 100644 --- a/app/controllers/products_controller.rb +++ b/app/controllers/products_controller.rb @@ -8,7 +8,7 @@ class ProductsController < ApplicationController if @product.save redirect_to @product else - render "new" + render 'new' end end @@ -42,7 +42,7 @@ class ProductsController < ApplicationController def product_params params.require(:product).permit(:name, :purchase_price, :sale_price, - :img_path) + :avatar) end end diff --git a/app/models/product.rb b/app/models/product.rb index b23028b..222d80f 100644 --- a/app/models/product.rb +++ b/app/models/product.rb @@ -2,19 +2,24 @@ # # Table name: products # -# id :integer not null, primary key -# name :string(255) -# purchase_price :integer -# sale_price :integer -# img_path :string(255) -# created_at :datetime -# updated_at :datetime +# id :integer not null, primary key +# name :string(255) +# purchase_price :integer +# sale_price :integer +# created_at :datetime +# updated_at :datetime +# avatar_file_name :string(255) +# avatar_content_type :string(255) +# avatar_file_size :integer +# avatar_updated_at :datetime # class Product < ActiveRecord::Base has_one :order_product + has_attached_file :avatar, styles: { medium: "100x100>" }, default_style: :medium validates :name, presence: true validates :purchase_price, numericality: { only_integer: true, greater_than_or_equal_to: 0 } validates :sale_price, numericality: { only_integer: true, greater_than_or_equal_to: 0 } + validates_attachment :avatar, presence: true, content_type: { content_type: ["image/jpeg", "image/gif", "image/png"] } end diff --git a/app/views/orders/new.html.erb b/app/views/orders/new.html.erb index 493bc9d..6dee5a4 100644 --- a/app/views/orders/new.html.erb +++ b/app/views/orders/new.html.erb @@ -11,6 +11,7 @@ <%= product_field.text_field :count, class: 'form-control', value: 0 %> + <%= image_tag op.product.avatar %>