-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
107 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
51 changes: 51 additions & 0 deletions
51
db/migrate/20151104071547_populate_paperclip_data_into_attache_json.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 comment
on commit a1f5d5b
There was a problem hiding this comment.
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
Add the
attache_rails
gem while leavingpaperclip
andaws-sdk-v1
intact