mirror of
https://github.com/discourse/discourse.git
synced 2024-11-23 06:49:14 +08:00
add proper post_uploads reverse index
This commit is contained in:
parent
bd1b4d3130
commit
037f62928b
|
@ -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
|
||||
|
||||
|
|
4
app/models/post_upload.rb
Normal file
4
app/models/post_upload.rb
Normal file
|
@ -0,0 +1,4 @@
|
|||
class PostUpload < ActiveRecord::Base
|
||||
belongs_to :post
|
||||
belongs_to :upload
|
||||
end
|
|
@ -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
|
||||
|
|
16
db/migrate/20130613211700_drop_posts_uploads.rb
Normal file
16
db/migrate/20130613211700_drop_posts_uploads.rb
Normal file
|
@ -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
|
15
db/migrate/20130613212230_create_post_uploads.rb
Normal file
15
db/migrate/20130613212230_create_post_uploads.rb
Normal file
|
@ -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
|
|
@ -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."
|
||||
|
|
|
@ -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 }
|
||||
|
||||
|
|
8
spec/models/post_upload_spec.rb
Normal file
8
spec/models/post_upload_spec.rb
Normal file
|
@ -0,0 +1,8 @@
|
|||
require 'spec_helper'
|
||||
|
||||
describe PostUpload do
|
||||
|
||||
it { should belong_to :post }
|
||||
it { should belong_to :upload }
|
||||
|
||||
end
|
|
@ -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 }
|
||||
|
|
Loading…
Reference in New Issue
Block a user