From 322990ad358c39f40b2b69dbd1bd745d2b257e31 Mon Sep 17 00:00:00 2001 From: leejaew Date: Tue, 15 Oct 2013 11:08:03 -0500 Subject: [PATCH 01/10] add gem: carrierwave (image uploader) --- Gemfile | 2 ++ Gemfile.lock | 5 +++++ 2 files changed, 7 insertions(+) diff --git a/Gemfile b/Gemfile index 6f8c763..b8f760c 100644 --- a/Gemfile +++ b/Gemfile @@ -39,6 +39,8 @@ end gem 'devise' +gem 'carrierwave' + # Use ActiveModel has_secure_password # gem 'bcrypt-ruby', '~> 3.0.0' diff --git a/Gemfile.lock b/Gemfile.lock index af8e247..781e8c7 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -29,6 +29,10 @@ GEM atomic (1.1.14) bcrypt-ruby (3.1.2) builder (3.1.4) + carrierwave (0.9.0) + activemodel (>= 3.2.0) + activesupport (>= 3.2.0) + json (>= 1.7) coderay (1.0.9) coffee-rails (4.0.0) coffee-script (>= 2.2.0) @@ -132,6 +136,7 @@ PLATFORMS ruby DEPENDENCIES + carrierwave coffee-rails (~> 4.0.0) devise haml From c7dea2e66fc9f254396adb0658239844e8913390 Mon Sep 17 00:00:00 2001 From: leejaew Date: Tue, 15 Oct 2013 11:14:16 -0500 Subject: [PATCH 02/10] create image_uploader.rb, add public/uploads to .gitignore --- .gitignore | 3 ++ app/uploaders/image_uploader.rb | 51 +++++++++++++++++++++++++++++++++ 2 files changed, 54 insertions(+) create mode 100644 app/uploaders/image_uploader.rb diff --git a/.gitignore b/.gitignore index 25a742d..1fa01b9 100644 --- a/.gitignore +++ b/.gitignore @@ -14,3 +14,6 @@ # Ignore all logfiles and tempfiles. /log/*.log /tmp + +# Ignore file uploads +public/uploads diff --git a/app/uploaders/image_uploader.rb b/app/uploaders/image_uploader.rb new file mode 100644 index 0000000..e8e12b7 --- /dev/null +++ b/app/uploaders/image_uploader.rb @@ -0,0 +1,51 @@ +# encoding: utf-8 + +class ImageUploader < CarrierWave::Uploader::Base + + # Include RMagick or MiniMagick support: + # include CarrierWave::RMagick + # include CarrierWave::MiniMagick + + # Choose what kind of storage to use for this uploader: + storage :file + # storage :fog + + # Override the directory where uploaded files will be stored. + # This is a sensible default for uploaders that are meant to be mounted: + def store_dir + "uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}" + end + + # Provide a default URL as a default if there hasn't been a file uploaded: + # def default_url + # # For Rails 3.1+ asset pipeline compatibility: + # # ActionController::Base.helpers.asset_path("fallback/" + [version_name, "default.png"].compact.join('_')) + # + # "/images/fallback/" + [version_name, "default.png"].compact.join('_') + # end + + # Process files as they are uploaded: + # process :scale => [200, 300] + # + # def scale(width, height) + # # do something + # end + + # Create different versions of your uploaded files: + # version :thumb do + # process :scale => [50, 50] + # end + + # Add a white list of extensions which are allowed to be uploaded. + # For images you might use something like this: + # def extension_white_list + # %w(jpg jpeg gif png) + # end + + # Override the filename of the uploaded files: + # Avoid using model.id or version_name here, see uploader/store.rb for details. + # def filename + # "something.jpg" if original_filename + # end + +end From 30ea989a35032d9981f1dbfa18ebe7c6f282aa31 Mon Sep 17 00:00:00 2001 From: leejaew Date: Tue, 15 Oct 2013 11:21:45 -0500 Subject: [PATCH 03/10] mount uploader, change attachment from text_field to file_field --- app/models/post.rb | 2 ++ app/views/projects/posts/_new.html.haml | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/app/models/post.rb b/app/models/post.rb index 8ddedb0..af83541 100644 --- a/app/models/post.rb +++ b/app/models/post.rb @@ -5,6 +5,8 @@ class Post < ActiveRecord::Base default_scope order('created_at DESC') before_save :set_defaults + mount_uploader :attachment, ImageUploader + private def set_defaults diff --git a/app/views/projects/posts/_new.html.haml b/app/views/projects/posts/_new.html.haml index aa8d14f..4696a0e 100644 --- a/app/views/projects/posts/_new.html.haml +++ b/app/views/projects/posts/_new.html.haml @@ -8,7 +8,7 @@ .field = f.text_area :message, placeholder: "Type your message..." .field - = f.text_field :attachment, rows: 1, style: 'width: 90%;', placeholder: "Paste your attachment..." + = f.file_field :attachment, rows: 1, style: 'width: 90%;', placeholder: "Paste your attachment..." = f.hidden_field :project_id, value: @project.id = f.hidden_field :phase_id, value: params[:phase] From 335af44663bae918b41739c305465b5dec97de73 Mon Sep 17 00:00:00 2001 From: leejaew Date: Tue, 15 Oct 2013 11:27:13 -0500 Subject: [PATCH 04/10] change views to show the attachment using Carrierwave's conventions --- app/views/projects/posts/_index.html.haml | 6 +++--- app/views/projects/posts/_show.html.haml | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/app/views/projects/posts/_index.html.haml b/app/views/projects/posts/_index.html.haml index ab36f6c..195cfa8 100644 --- a/app/views/projects/posts/_index.html.haml +++ b/app/views/projects/posts/_index.html.haml @@ -4,11 +4,11 @@ - @posts.each do |post| .row - .large-2.columns + .small-2.columns = image_tag post.user.profile_pic %p= post.user.name - .large-10.columns + .small-10.columns %b= link_to post.title, project_url(@project.id, page: 'show', phase: @show_phase.name, post_id: post.id) %small= post.updated_at.strftime("%m/%d/%y at %I:%M%P") = if post.attachment? then image_tag 'https://s3.amazonaws.com/mks_learn_app/paperclip.png', width: '12px' end - %p= post.message.truncate(300) + %p= post.message.truncate(300) \ No newline at end of file diff --git a/app/views/projects/posts/_show.html.haml b/app/views/projects/posts/_show.html.haml index 4c1de44..4a0a2ff 100644 --- a/app/views/projects/posts/_show.html.haml +++ b/app/views/projects/posts/_show.html.haml @@ -3,4 +3,4 @@ - if @post.attachment == "" no attachment available -else - %iframe{height: "800", width:"100%", frameBorder: "0", src: @post.attachment} \ No newline at end of file + %iframe{height: "800", width:"100%", frameBorder: "0", src: @post.attachment_url} \ No newline at end of file From 12e0b1f11e153ff0f2698af19bdcdd60ca18a674 Mon Sep 17 00:00:00 2001 From: leejaew Date: Tue, 15 Oct 2013 11:34:07 -0500 Subject: [PATCH 05/10] change views to show the attachment using Carrierwave's conventions --- app/views/projects/posts/_index.html.haml | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/app/views/projects/posts/_index.html.haml b/app/views/projects/posts/_index.html.haml index 195cfa8..b8ed1e8 100644 --- a/app/views/projects/posts/_index.html.haml +++ b/app/views/projects/posts/_index.html.haml @@ -6,9 +6,10 @@ .row .small-2.columns = image_tag post.user.profile_pic - %p= post.user.name + %p= post.user.name? ? post.user.name : "Anonymous" .small-10.columns %b= link_to post.title, project_url(@project.id, page: 'show', phase: @show_phase.name, post_id: post.id) %small= post.updated_at.strftime("%m/%d/%y at %I:%M%P") = if post.attachment? then image_tag 'https://s3.amazonaws.com/mks_learn_app/paperclip.png', width: '12px' end - %p= post.message.truncate(300) \ No newline at end of file + %p= post.message.truncate(300) + =if post.attachment? then image_tag post.attachment_url end From 6b4ca923e0cbba8019a79bceca6d23df82d7190b Mon Sep 17 00:00:00 2001 From: leejaew Date: Tue, 15 Oct 2013 11:38:21 -0500 Subject: [PATCH 06/10] add gem: RMagick --- Gemfile | 1 + app/uploaders/image_uploader.rb | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/Gemfile b/Gemfile index b8f760c..2058049 100644 --- a/Gemfile +++ b/Gemfile @@ -40,6 +40,7 @@ end gem 'devise' gem 'carrierwave' +gem 'rmagick' # You should know where gems should go by now! # Use ActiveModel has_secure_password # gem 'bcrypt-ruby', '~> 3.0.0' diff --git a/app/uploaders/image_uploader.rb b/app/uploaders/image_uploader.rb index e8e12b7..eead103 100644 --- a/app/uploaders/image_uploader.rb +++ b/app/uploaders/image_uploader.rb @@ -3,7 +3,7 @@ class ImageUploader < CarrierWave::Uploader::Base # Include RMagick or MiniMagick support: - # include CarrierWave::RMagick + include CarrierWave::RMagick # include CarrierWave::MiniMagick # Choose what kind of storage to use for this uploader: From 0040e29488beaf79aab75eda543a6e26a6cb7b40 Mon Sep 17 00:00:00 2001 From: leejaew Date: Tue, 15 Oct 2013 11:42:26 -0500 Subject: [PATCH 07/10] add RMagick methods to resize images --- Gemfile.lock | 2 ++ app/uploaders/image_uploader.rb | 11 ++++++++--- app/views/projects/posts/_index.html.haml | 2 +- app/views/projects/posts/_show.html.haml | 2 +- 4 files changed, 12 insertions(+), 5 deletions(-) diff --git a/Gemfile.lock b/Gemfile.lock index 781e8c7..fdcd91e 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -97,6 +97,7 @@ GEM rake (10.1.0) rdoc (3.12.2) json (~> 1.4) + rmagick (2.13.2) sass (3.2.12) sass-rails (4.0.0) railties (>= 4.0.0.beta, < 5.0) @@ -145,6 +146,7 @@ DEPENDENCIES jquery-rails pry rails (= 4.0.0) + rmagick sass-rails (~> 4.0.0) sdoc sqlite3 diff --git a/app/uploaders/image_uploader.rb b/app/uploaders/image_uploader.rb index eead103..3a5edbb 100644 --- a/app/uploaders/image_uploader.rb +++ b/app/uploaders/image_uploader.rb @@ -32,9 +32,14 @@ def store_dir # end # Create different versions of your uploaded files: - # version :thumb do - # process :scale => [50, 50] - # end + version :thumb do + # process :scale => [50, 50] + process :resize_to_fit [75, 75] + end + + version :medium do + process :resize_to_fit [800, 800] + end # Add a white list of extensions which are allowed to be uploaded. # For images you might use something like this: diff --git a/app/views/projects/posts/_index.html.haml b/app/views/projects/posts/_index.html.haml index b8ed1e8..0889363 100644 --- a/app/views/projects/posts/_index.html.haml +++ b/app/views/projects/posts/_index.html.haml @@ -12,4 +12,4 @@ %small= post.updated_at.strftime("%m/%d/%y at %I:%M%P") = if post.attachment? then image_tag 'https://s3.amazonaws.com/mks_learn_app/paperclip.png', width: '12px' end %p= post.message.truncate(300) - =if post.attachment? then image_tag post.attachment_url end + =if post.attachment? then image_tag post.attachment_url(:thumb) end diff --git a/app/views/projects/posts/_show.html.haml b/app/views/projects/posts/_show.html.haml index 4a0a2ff..76751f5 100644 --- a/app/views/projects/posts/_show.html.haml +++ b/app/views/projects/posts/_show.html.haml @@ -3,4 +3,4 @@ - if @post.attachment == "" no attachment available -else - %iframe{height: "800", width:"100%", frameBorder: "0", src: @post.attachment_url} \ No newline at end of file + %iframe{height: "800", width:"100%", frameBorder: "0", src: @post.attachment_url(:medium)} \ No newline at end of file From 56cc4a2e725dce93bc51c9271c32a2c720abb537 Mon Sep 17 00:00:00 2001 From: leejaew Date: Tue, 15 Oct 2013 11:43:31 -0500 Subject: [PATCH 08/10] add gem: figaro --- Gemfile | 1 + Gemfile.lock | 4 ++++ 2 files changed, 5 insertions(+) diff --git a/Gemfile b/Gemfile index 2058049..6674423 100644 --- a/Gemfile +++ b/Gemfile @@ -41,6 +41,7 @@ gem 'devise' gem 'carrierwave' gem 'rmagick' # You should know where gems should go by now! +gem 'figaro' # Use ActiveModel has_secure_password # gem 'bcrypt-ruby', '~> 3.0.0' diff --git a/Gemfile.lock b/Gemfile.lock index fdcd91e..edca48d 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -49,6 +49,9 @@ GEM warden (~> 1.2.3) erubis (2.7.0) execjs (2.0.2) + figaro (0.7.0) + bundler (~> 1.0) + rails (>= 3, < 5) haml (4.0.3) tilt haml-rails (0.4) @@ -140,6 +143,7 @@ DEPENDENCIES carrierwave coffee-rails (~> 4.0.0) devise + figaro haml haml-rails jbuilder (~> 1.2) From 2a747efebf9544033a595b4284d167a94f753bcd Mon Sep 17 00:00:00 2001 From: leejaew Date: Tue, 15 Oct 2013 11:44:06 -0500 Subject: [PATCH 09/10] generate application.yml via figaro --- .gitignore | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.gitignore b/.gitignore index 1fa01b9..b2d98d8 100644 --- a/.gitignore +++ b/.gitignore @@ -17,3 +17,6 @@ # Ignore file uploads public/uploads + +# Ignore application configuration +/config/application.yml From 373f2e6d710eb98a970a9cda0e09f7fe7549cd08 Mon Sep 17 00:00:00 2001 From: leejaew Date: Wed, 16 Oct 2013 09:58:50 -0500 Subject: [PATCH 10/10] add gem: fog --- Gemfile | 1 + Gemfile.lock | 18 ++++++++++++++++++ app/uploaders/image_uploader.rb | 4 ++-- config/initializers/carrierwave.rb | 8 ++++++++ 4 files changed, 29 insertions(+), 2 deletions(-) create mode 100644 config/initializers/carrierwave.rb diff --git a/Gemfile b/Gemfile index 6674423..dabbea9 100644 --- a/Gemfile +++ b/Gemfile @@ -42,6 +42,7 @@ gem 'devise' gem 'carrierwave' gem 'rmagick' # You should know where gems should go by now! gem 'figaro' +gem "fog", "~> 1.3.1" # This is the version Fog that Carrierwave recommends at the time of this writing # Use ActiveModel has_secure_password # gem 'bcrypt-ruby', '~> 3.0.0' diff --git a/Gemfile.lock b/Gemfile.lock index edca48d..0a16e80 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -48,10 +48,22 @@ GEM thread_safe (~> 0.1) warden (~> 1.2.3) erubis (2.7.0) + excon (0.13.4) execjs (2.0.2) figaro (0.7.0) bundler (~> 1.0) rails (>= 3, < 5) + fog (1.3.1) + builder + excon (~> 0.13.0) + formatador (~> 0.2.0) + mime-types + multi_json (~> 1.0) + net-scp (~> 1.0.4) + net-ssh (>= 2.1.3) + nokogiri (~> 1.5.0) + ruby-hmac + formatador (0.2.4) haml (4.0.3) tilt haml-rails (0.4) @@ -75,6 +87,10 @@ GEM mime-types (1.25) minitest (4.7.5) multi_json (1.8.1) + net-scp (1.0.4) + net-ssh (>= 1.99.1) + net-ssh (2.7.0) + nokogiri (1.5.10) orm_adapter (0.4.0) polyglot (0.3.3) pry (0.9.12.2) @@ -101,6 +117,7 @@ GEM rdoc (3.12.2) json (~> 1.4) rmagick (2.13.2) + ruby-hmac (0.4.0) sass (3.2.12) sass-rails (4.0.0) railties (>= 4.0.0.beta, < 5.0) @@ -144,6 +161,7 @@ DEPENDENCIES coffee-rails (~> 4.0.0) devise figaro + fog (~> 1.3.1) haml haml-rails jbuilder (~> 1.2) diff --git a/app/uploaders/image_uploader.rb b/app/uploaders/image_uploader.rb index 3a5edbb..80c94ed 100644 --- a/app/uploaders/image_uploader.rb +++ b/app/uploaders/image_uploader.rb @@ -7,8 +7,8 @@ class ImageUploader < CarrierWave::Uploader::Base # include CarrierWave::MiniMagick # Choose what kind of storage to use for this uploader: - storage :file - # storage :fog + # storage :file + storage :fog # Override the directory where uploaded files will be stored. # This is a sensible default for uploaders that are meant to be mounted: diff --git a/config/initializers/carrierwave.rb b/config/initializers/carrierwave.rb new file mode 100644 index 0000000..62093b4 --- /dev/null +++ b/config/initializers/carrierwave.rb @@ -0,0 +1,8 @@ +CarrierWave.configure do |config| + config.fog_credentials = { + provider: "AWS", + aws_access_key_id: ENV['AS3_ACCESS_KEY'], + aws_secret_access_key: ENV['AS3_SECRET_ACCESS_KEY'] + } + config.fog_directory = ENV['AS3_BUCKET_NAME'] +end \ No newline at end of file