diff --git a/app/models/optimized_image.rb b/app/models/optimized_image.rb new file mode 100644 index 000000000..7566c09ff --- /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 362318202..953749767 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 000000000..24d73e260 --- /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 000000000..aecb23f1d --- /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 52b523d66..d20464a53 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 }