Skip to content

Commit

Permalink
switch to attache
Browse files Browse the repository at this point in the history
  • Loading branch information
choonkeat committed Nov 4, 2015
1 parent 05ec43f commit a1f5d5b
Show file tree
Hide file tree
Showing 11 changed files with 107 additions and 61 deletions.
2 changes: 2 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,5 @@ end

gem "paperclip", "~> 4.3"
gem 'aws-sdk-v1' # If using paperclip <= v4.3.1

gem "attache_rails", "~> 0.3.0"

This comment has been minimized.

Copy link
@choonkeat

choonkeat Nov 6, 2015

Author Owner

Add the attache_rails gem while leaving paperclip and aws-sdk-v1 intact

8 changes: 8 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,12 @@ GEM
thread_safe (~> 0.3, >= 0.3.4)
tzinfo (~> 1.1)
arel (6.0.3)
attache_api (0.1.0)
httpclient
attache_rails (0.3.0)
attache_api
httpclient
rails (>= 4.0.0)
aws-sdk-v1 (1.66.0)
json (~> 1.4)
nokogiri (>= 1.4.4)
Expand All @@ -60,6 +66,7 @@ GEM
execjs (2.6.0)
globalid (0.3.6)
activesupport (>= 4.1.0)
httpclient (2.6.0.1)
i18n (0.7.0)
jbuilder (2.3.2)
activesupport (>= 3.0.0, < 5)
Expand Down Expand Up @@ -153,6 +160,7 @@ PLATFORMS
ruby

DEPENDENCIES
attache_rails (~> 0.3.0)
aws-sdk-v1
byebug
coffee-rails (~> 4.1.0)
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/albums_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,6 @@ def set_album

# Never trust parameters from the scary internet, only allow the white list through.
def album_params
params.require(:album).permit(:title, :cover_art, images_attributes: [:id, :file, :destroy])
params.require(:album).permit(:title, :cover_art, images: [], attaches_discarded: [])
end
end
16 changes: 2 additions & 14 deletions app/models/album.rb
Original file line number Diff line number Diff line change
@@ -1,16 +1,4 @@
class Album < ActiveRecord::Base
has_attached_file :cover_art, styles: { medium: "300x300>", thumb: "100x100>" }, default_url: "/images/:style/missing.png"
validates_attachment_content_type :cover_art, content_type: /\Aimage\/.*\Z/

has_many :images
accepts_nested_attributes_for :images, allow_destroy: true

before_save :extract_dimensions
def extract_dimensions
tempfile = cover_art.queued_for_write[:original]
unless tempfile.nil?
geometry = Paperclip::Geometry.from_file(tempfile)
self.cover_art_dimensions = [geometry.width.to_i, geometry.height.to_i].join('x')
end
end
has_one_attache :cover_art
has_many_attaches :images
end
13 changes: 0 additions & 13 deletions app/models/image.rb

This file was deleted.

16 changes: 5 additions & 11 deletions app/views/albums/_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,14 @@

<div class="field">
<%= f.label :cover_art %><br>
<%= f.file_field :cover_art %>
<%= f.file_field :cover_art, f.object.cover_art_options('100x100>') %>
</div>

<div class="field">
<%= f.label :images %><br>
<%= f.file_field :images, f.object.images_options('100x100>') %>
</div>

<fieldset>
<legend>Images</legend>
<% 3.times { f.object.images.build } %>
<%= f.fields_for :images do |f| %>
<div class="field">
<%= f.label :file %><br>
<%= f.file_field :file %>
</div>
<% end %>
</fieldset>
<div class="actions">
<%= f.submit %>
</div>
Expand Down
6 changes: 3 additions & 3 deletions app/views/albums/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
<%= @album.title %>
</p>

<p><%= image_tag @album.cover_art.url(:medium) %></p>
<% @album.images.each do |image| %>
<p><%= image_tag image.file.url(:thumb) %></p>
<p><%= image_tag @album.cover_art_url("300x300>") %></p>
<% @album.images_urls("100x100>").each do |url| %>
<p><%= image_tag url %></p>
<% end %>
<%= link_to 'Edit', edit_album_path(@album) %> |
Expand Down
6 changes: 6 additions & 0 deletions db/migrate/20151104071426_add_attache_json_columns.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class AddAttacheJsonColumns < ActiveRecord::Migration
def change
add_column :albums, :cover_art, :json
add_column :albums, :images, :json
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
class PopulatePaperclipDataIntoAttacheJson < ActiveRecord::Migration

# always define a specific class for the migration
class Album < ActiveRecord::Base
def self.to_s; "Album"; end # important

has_attached_file :cover_art, styles: { medium: "300x300>", thumb: "100x100>" }, default_url: "/images/:style/missing.png"
validates_attachment_content_type :cover_art, content_type: /\Aimage\/.*\Z/
end

class Image < ActiveRecord::Base
def self.to_s; "Image"; end # important

has_attached_file :file, styles: { medium: "300x300>", thumb: "100x100>" }, default_url: "/images/:style/missing.png"
validates_attachment_content_type :file, content_type: /\Aimage\/.*\Z/
end
# end

def aws_uri_to_path(uri)
case uri.host
when 's3.amazonaws.com'
URI.decode uri.path.split('/')[2..-1].join('/') # skip over the bucket name in path
else
URI.decode uri.path[1..-1]
end
end

def up
Album.find_each do |album|
album.update_columns(
cover_art: {
path: aws_uri_to_path(URI.parse(album.cover_art.url('original'))),
content_type: album.cover_art.content_type,
geometry: album.cover_art_dimensions,
},
images: Image.where(album_id: album.id).collect {|image|
{
path: aws_uri_to_path(URI.parse(image.file.url('original'))),
content_type: image.file.content_type,
geometry: image.file_dimensions,
}
},
)
end
end

def down
Album.update_all cover_art: nil, images: nil
end

end
24 changes: 24 additions & 0 deletions db/migrate/20151104134038_remove_paperclip.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
class RemovePaperclip < ActiveRecord::Migration
def up
remove_attachment :albums, :cover_art
remove_column :albums, :cover_art_dimensions

drop_table :images
end

def down
add_attachment :albums, :cover_art
add_column :albums, :cover_art_dimensions, :string

create_table "images" do |t|
t.integer "album_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "file_file_name"
t.string "file_content_type"
t.integer "file_file_size"
t.datetime "file_updated_at"
t.string "file_dimensions"
end
end
end
24 changes: 5 additions & 19 deletions db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,31 +11,17 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 20151104070856) do
ActiveRecord::Schema.define(version: 20151104134038) do

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

create_table "albums", force: :cascade do |t|
t.string "title"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "cover_art_file_name"
t.string "cover_art_content_type"
t.integer "cover_art_file_size"
t.datetime "cover_art_updated_at"
t.string "cover_art_dimensions"
end

create_table "images", force: :cascade do |t|
t.integer "album_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "file_file_name"
t.string "file_content_type"
t.integer "file_file_size"
t.datetime "file_updated_at"
t.string "file_dimensions"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.json "cover_art"
t.json "images"
end

end

1 comment on commit a1f5d5b

@choonkeat
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

test the upgrade in your dev environment with some production data and using the production s3 config (this step does not modify or destroy any s3 file)
once you are okay, deploy to production and run db:migrate

Please sign in to comment.