From 037f62928ba0203dad3c3346a785e6c5cbc8cbce Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9gis=20Hanol?= Date: Thu, 13 Jun 2013 23:44:24 +0200 Subject: [PATCH] add proper post_uploads reverse index --- app/models/post.rb | 3 ++- app/models/post_upload.rb | 4 ++++ app/models/upload.rb | 3 ++- db/migrate/20130613211700_drop_posts_uploads.rb | 16 ++++++++++++++++ db/migrate/20130613212230_create_post_uploads.rb | 15 +++++++++++++++ lib/tasks/images.rake | 4 ++-- spec/models/post_spec.rb | 3 ++- spec/models/post_upload_spec.rb | 8 ++++++++ spec/models/upload_spec.rb | 3 ++- 9 files changed, 53 insertions(+), 6 deletions(-) create mode 100644 app/models/post_upload.rb create mode 100644 db/migrate/20130613211700_drop_posts_uploads.rb create mode 100644 db/migrate/20130613212230_create_post_uploads.rb create mode 100644 spec/models/post_upload_spec.rb diff --git a/app/models/post.rb b/app/models/post.rb index 90174c2f612..75bb2d6f642 100644 --- a/app/models/post.rb +++ b/app/models/post.rb @@ -27,7 +27,8 @@ class Post < ActiveRecord::Base has_many :post_actions has_many :topic_links - has_and_belongs_to_many :upload + has_many :post_uploads + has_many :uploads, through: :post_uploads has_one :post_search_data diff --git a/app/models/post_upload.rb b/app/models/post_upload.rb new file mode 100644 index 00000000000..bacec268768 --- /dev/null +++ b/app/models/post_upload.rb @@ -0,0 +1,4 @@ +class PostUpload < ActiveRecord::Base + belongs_to :post + belongs_to :upload +end diff --git a/app/models/upload.rb b/app/models/upload.rb index ca3674552ce..06858502b98 100644 --- a/app/models/upload.rb +++ b/app/models/upload.rb @@ -7,7 +7,8 @@ class Upload < ActiveRecord::Base belongs_to :user belongs_to :topic - has_and_belongs_to_many :post + has_many :post_uploads + has_many :posts, through: :post_uploads validates_presence_of :filesize validates_presence_of :original_filename diff --git a/db/migrate/20130613211700_drop_posts_uploads.rb b/db/migrate/20130613211700_drop_posts_uploads.rb new file mode 100644 index 00000000000..d383d227104 --- /dev/null +++ b/db/migrate/20130613211700_drop_posts_uploads.rb @@ -0,0 +1,16 @@ +class DropPostsUploads < ActiveRecord::Migration + def up + drop_table :posts_uploads + end + + def down + create_table :posts_uploads, id: false do |t| + t.integer :post_id + t.integer :upload_id + end + + add_index :posts_uploads, :post_id + add_index :posts_uploads, :upload_id + add_index :posts_uploads, [:post_id, :upload_id], unique: true + end +end diff --git a/db/migrate/20130613212230_create_post_uploads.rb b/db/migrate/20130613212230_create_post_uploads.rb new file mode 100644 index 00000000000..baf08b71bc0 --- /dev/null +++ b/db/migrate/20130613212230_create_post_uploads.rb @@ -0,0 +1,15 @@ +class CreatePostUploads < ActiveRecord::Migration + def up + create_table :post_uploads do |t| + t.integer :post_id, null: false + t.integer :upload_id, null: false + end + + # no support for this till rails 4 + execute 'create unique index idx_unique_post_uploads on post_uploads(post_id, upload_id)' + end + + def down + drop_table :post_uploads + end +end diff --git a/lib/tasks/images.rake b/lib/tasks/images.rake index f41185e4548..662ff9e612d 100644 --- a/lib/tasks/images.rake +++ b/lib/tasks/images.rake @@ -20,12 +20,12 @@ task "images:reindex" => :environment do src = img['src'] if src.present? && has_been_uploaded?(src) && m = uploaded_regex.match(src) begin - Post.exec_sql("INSERT INTO posts_uploads (post_id, upload_id) VALUES (?, ?)", p.id, m[:upload_id]) + PostUpload.create({ post_id: p.id, upload_id: m[:upload_id] }) rescue ActiveRecord::RecordNotUnique end end end - putc "." + putc "." end end puts "\ndone." diff --git a/spec/models/post_spec.rb b/spec/models/post_spec.rb index b80768174c0..4aa8f41231a 100644 --- a/spec/models/post_spec.rb +++ b/spec/models/post_spec.rb @@ -26,7 +26,8 @@ describe Post do it { should have_many :post_replies } it { should have_many :replies } - it { should have_and_belong_to_many :upload } + it { should have_many :post_uploads } + it { should have_many :uploads } it { should rate_limit } diff --git a/spec/models/post_upload_spec.rb b/spec/models/post_upload_spec.rb new file mode 100644 index 00000000000..a4e8e5782b0 --- /dev/null +++ b/spec/models/post_upload_spec.rb @@ -0,0 +1,8 @@ +require 'spec_helper' + +describe PostUpload do + + it { should belong_to :post } + it { should belong_to :upload } + +end diff --git a/spec/models/upload_spec.rb b/spec/models/upload_spec.rb index 50a88681bbc..8378da26c63 100644 --- a/spec/models/upload_spec.rb +++ b/spec/models/upload_spec.rb @@ -5,7 +5,8 @@ describe Upload do it { should belong_to :user } it { should belong_to :topic } - it { should have_and_belong_to_many :post } + it { should have_many :post_uploads } + it { should have_many :posts } it { should validate_presence_of :original_filename } it { should validate_presence_of :filesize }