Skip to content

Commit

Permalink
adds images to items using aws s3 and paperclip
Browse files Browse the repository at this point in the history
  • Loading branch information
jameshamann committed Jun 11, 2017
1 parent e19350f commit f69ea07
Show file tree
Hide file tree
Showing 14 changed files with 61 additions and 9 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,7 @@
!/log/.keep
!/tmp/.keep

.env

# Ignore Byebug command history file.
.byebug_history
3 changes: 3 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ gem 'dotenv-rails'

gem 'pg'

gem 'paperclip'
gem 'aws-sdk', '~> 2.3'

# Use jquery as the JavaScript library
gem 'jquery-rails'
# Turbolinks makes navigating your web application faster. Read more: https://github.com/turbolinks/turbolinks
Expand Down
21 changes: 21 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,14 @@ GEM
addressable (2.5.1)
public_suffix (~> 2.0, >= 2.0.2)
arel (7.1.4)
aws-sdk (2.9.37)
aws-sdk-resources (= 2.9.37)
aws-sdk-core (2.9.37)
aws-sigv4 (~> 1.0)
jmespath (~> 1.0)
aws-sdk-resources (2.9.37)
aws-sdk-core (= 2.9.37)
aws-sigv4 (1.0.0)
bindex (0.5.0)
builder (3.2.3)
byebug (9.0.6)
Expand All @@ -51,6 +59,9 @@ GEM
rack (>= 1.0.0)
rack-test (>= 0.5.4)
xpath (~> 2.0)
climate_control (0.2.0)
cocaine (0.5.8)
climate_control (>= 0.0.3, < 1.0)
coffee-rails (4.2.2)
coffee-script (>= 2.2.0)
railties (>= 4.0.0)
Expand Down Expand Up @@ -84,6 +95,7 @@ GEM
jbuilder (2.7.0)
activesupport (>= 4.2.0)
multi_json (>= 1.2)
jmespath (1.3.1)
jquery-rails (4.3.1)
rails-dom-testing (>= 1, < 3)
railties (>= 4.2.0)
Expand All @@ -101,6 +113,7 @@ GEM
mime-types (3.1)
mime-types-data (~> 3.2015)
mime-types-data (3.2016.0521)
mimemagic (0.3.2)
mini_portile2 (2.2.0)
minitest (5.10.2)
multi_json (1.12.1)
Expand All @@ -113,6 +126,12 @@ GEM
mini_portile2 (~> 2.2.0)
nokogiri (1.8.0-x86-mingw32)
mini_portile2 (~> 2.2.0)
paperclip (5.1.0)
activemodel (>= 4.2.0)
activesupport (>= 4.2.0)
cocaine (~> 0.5.5)
mime-types
mimemagic (~> 0.3.0)
pg (0.20.0)
pg (0.20.0-x64-mingw32)
pg (0.20.0-x86-mingw32)
Expand Down Expand Up @@ -233,6 +252,7 @@ PLATFORMS
x86-mswin32

DEPENDENCIES
aws-sdk (~> 2.3)
byebug
capybara
coffee-rails (~> 4.2)
Expand All @@ -241,6 +261,7 @@ DEPENDENCIES
jbuilder (~> 2.5)
jquery-rails
listen (~> 3.0.5)
paperclip
pg
puma (~> 3.0)
rails (~> 5.0.3)
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/items_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def create
private

def item_params
params.require(:item).permit(:name, :description, :price)
params.require(:item).permit(:name, :description, :price, :avatar)
end


Expand Down
4 changes: 3 additions & 1 deletion app/models/item.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
class Item < ApplicationRecord

has_attached_file :avatar, styles: { medium: "300x300>"}, default_url: "/images/:style/missing.png"
validates_attachment_content_type :avatar, content_type: /\Aimage\/.*\z/

end
1 change: 1 addition & 0 deletions app/views/items/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
<% @items.each do |i| %>
<p> <%= i.name %></p>
<p> <%= i.description %></p>
<%= image_tag i.avatar.url(:medium) %>
<% end %>
<% else %>
<h2>No items yet!</h2>
Expand Down
2 changes: 2 additions & 0 deletions app/views/items/new.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,7 @@
Name: <%= f.text_field :name %>
Description: <%= f.text_field :description %>
Price: <%= f.number_field :price %>
<%= f.label :avatar %>
<%= f.file_field :avatar %>
<p><%= f.submit %></p>
<% end %>
10 changes: 9 additions & 1 deletion config/environments/production.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
Rails.application.configure do
# Settings specified here will take precedence over those in config/application.rb.

config.paperclip_defaults = {
storage: :s3,
s3_credentials: {
bucket: ENV.fetch('S3_BUCKET_NAME'),
access_key_id: ENV.fetch('AWS_ACCESS_KEY_ID'),
secret_access_key: ENV.fetch('AWS_SECRET_ACCESS_KEY'),
s3_region: ENV.fetch('AWS_REGION'),
}
}
# Code is not reloaded between requests.
config.cache_classes = true

Expand Down
2 changes: 1 addition & 1 deletion coverage/.last_run.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"result": {
"covered_percent": 97.8
"covered_percent": 97.83
}
}
4 changes: 2 additions & 2 deletions coverage/.resultset.json
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,7 @@
],
"/Users/jameshamann/Documents/Dev_Work/webshop/app/models/item.rb": [
1,
null,
1,
null
],
"/Users/jameshamann/Documents/Dev_Work/webshop/app/models/application_record.rb": [
Expand All @@ -356,6 +356,6 @@
null
]
},
"timestamp": 1497205560
"timestamp": 1497207383
}
}
9 changes: 9 additions & 0 deletions db/migrate/20170611184948_add_avatar_to_items.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class AddAvatarToItems < ActiveRecord::Migration[5.0]
def self.up
add_attachment :items, :avatar
end

def self.down
remove_attachment :items, :avatar
end
end
10 changes: 7 additions & 3 deletions db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 20170608200817) do
ActiveRecord::Schema.define(version: 20170611184948) do

# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
Expand All @@ -19,8 +19,12 @@
t.string "name"
t.text "description"
t.integer "price"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "avatar_file_name"
t.string "avatar_content_type"
t.integer "avatar_file_size"
t.datetime "avatar_updated_at"
end

end
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.

0 comments on commit f69ea07

Please sign in to comment.