From 5de03814fb5620454e4489fc9bd48ce2ccff50fa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9gis=20Hanol?= Date: Sun, 16 Jun 2013 10:39:48 +0200 Subject: [PATCH] created `optimized_image` model --- app/models/optimized_image.rb | 7 +++++++ app/models/upload.rb | 2 ++ .../20130616082327_create_optimized_images.rb | 17 +++++++++++++++++ spec/models/optimized_image_spec.rb | 7 +++++++ spec/models/upload_spec.rb | 2 ++ 5 files changed, 35 insertions(+) create mode 100644 app/models/optimized_image.rb create mode 100644 db/migrate/20130616082327_create_optimized_images.rb create mode 100644 spec/models/optimized_image_spec.rb diff --git a/app/models/optimized_image.rb b/app/models/optimized_image.rb new file mode 100644 index 00000000000..7566c09ffa4 --- /dev/null +++ b/app/models/optimized_image.rb @@ -0,0 +1,7 @@ +class OptimizedImage < ActiveRecord::Base + belongs_to :upload + + def filename + "#{sha[0..2]}/#{sha[3..5]}/#{sha[6..16]}_#{width}x#{height}#{ext}" + end +end diff --git a/app/models/upload.rb b/app/models/upload.rb index 362318202e4..953749767a4 100644 --- a/app/models/upload.rb +++ b/app/models/upload.rb @@ -9,6 +9,8 @@ class Upload < ActiveRecord::Base has_many :post_uploads has_many :posts, through: :post_uploads + has_many :optimized_images + validates_presence_of :filesize validates_presence_of :original_filename diff --git a/db/migrate/20130616082327_create_optimized_images.rb b/db/migrate/20130616082327_create_optimized_images.rb new file mode 100644 index 00000000000..24d73e26076 --- /dev/null +++ b/db/migrate/20130616082327_create_optimized_images.rb @@ -0,0 +1,17 @@ +class CreateOptimizedImages < ActiveRecord::Migration + def up + create_table :optimized_images do |t| + t.string :sha, null: false + t.string :ext, null: false + t.integer :width, null: false + t.integer :height, null: false + t.integer :upload_id, null: false + end + + add_index :optimized_images, :upload_id + end + + def down + drop_table :optimized_images + end +end diff --git a/spec/models/optimized_image_spec.rb b/spec/models/optimized_image_spec.rb new file mode 100644 index 00000000000..aecb23f1dd4 --- /dev/null +++ b/spec/models/optimized_image_spec.rb @@ -0,0 +1,7 @@ +require 'spec_helper' + +describe OptimizedImage do + + it { should belong_to :upload } + +end diff --git a/spec/models/upload_spec.rb b/spec/models/upload_spec.rb index 52b523d6661..d20464a539d 100644 --- a/spec/models/upload_spec.rb +++ b/spec/models/upload_spec.rb @@ -8,6 +8,8 @@ describe Upload do it { should have_many :post_uploads } it { should have_many :posts } + it { should have_many :optimized_images } + it { should validate_presence_of :original_filename } it { should validate_presence_of :filesize }