mirror of
https://github.com/codeninjasllc/discourse.git
synced 2024-11-23 15:48:43 -05:00
Adding post details in preparation for the API importer code.
This commit is contained in:
parent
20039860eb
commit
1aa9fc982d
6 changed files with 65 additions and 0 deletions
|
@ -34,6 +34,8 @@ class Post < ActiveRecord::Base
|
|||
|
||||
has_one :post_search_data
|
||||
|
||||
has_many :post_details
|
||||
|
||||
validates_with ::Validators::PostValidator
|
||||
|
||||
# We can pass several creating options to a post via attributes
|
||||
|
@ -56,6 +58,17 @@ class Post < ActiveRecord::Base
|
|||
@types ||= Enum.new(:regular, :moderator_action)
|
||||
end
|
||||
|
||||
def self.find_by_detail(key, value)
|
||||
includes(:post_details).where( "post_details.key = ? AND " +
|
||||
"post_details.value = ?",
|
||||
key,
|
||||
value ).first
|
||||
end
|
||||
|
||||
def add_detail(key, value, extra = nil)
|
||||
post_details.build(key: key, value: value, extra: extra)
|
||||
end
|
||||
|
||||
def limit_posts_per_day
|
||||
if user.created_at > 1.day.ago && post_number > 1
|
||||
RateLimiter.new(user, "first-day-replies-per-day:#{Date.today.to_s}", SiteSetting.max_replies_in_first_day, 1.day.to_i)
|
||||
|
|
6
app/models/post_detail.rb
Normal file
6
app/models/post_detail.rb
Normal file
|
@ -0,0 +1,6 @@
|
|||
class PostDetail < ActiveRecord::Base
|
||||
belongs_to :post
|
||||
|
||||
validates_presence_of :key, :value
|
||||
validates_uniqueness_of :key, scope: :post_id
|
||||
end
|
14
db/migrate/20131015131652_create_post_details.rb
Normal file
14
db/migrate/20131015131652_create_post_details.rb
Normal file
|
@ -0,0 +1,14 @@
|
|||
class CreatePostDetails < ActiveRecord::Migration
|
||||
def change
|
||||
create_table :post_details do |t|
|
||||
t.belongs_to :post
|
||||
t.string :key
|
||||
t.string :value, size: 512
|
||||
t.text :extra
|
||||
|
||||
t.timestamps
|
||||
end
|
||||
|
||||
add_index :post_details, [:post_id, :key], unique: true
|
||||
end
|
||||
end
|
5
spec/fabricators/post_detail_fabricator.rb
Normal file
5
spec/fabricators/post_detail_fabricator.rb
Normal file
|
@ -0,0 +1,5 @@
|
|||
Fabricator(:post_detail) do
|
||||
post
|
||||
key { sequence(:key) { |i| "key#{i}" } }
|
||||
value "test value"
|
||||
end
|
9
spec/models/post_detail_spec.rb
Normal file
9
spec/models/post_detail_spec.rb
Normal file
|
@ -0,0 +1,9 @@
|
|||
require 'spec_helper'
|
||||
|
||||
describe PostDetail do
|
||||
it { should belong_to :post }
|
||||
|
||||
it { should validate_presence_of :key }
|
||||
it { should validate_presence_of :value }
|
||||
it { should validate_uniqueness_of(:key).scoped_to(:post_id) }
|
||||
end
|
|
@ -26,6 +26,8 @@ describe Post do
|
|||
it { should have_many :post_uploads }
|
||||
it { should have_many :uploads }
|
||||
|
||||
it { should have_many :post_details }
|
||||
|
||||
it { should rate_limit }
|
||||
|
||||
let(:topic) { Fabricate(:topic) }
|
||||
|
@ -760,4 +762,20 @@ describe Post do
|
|||
end
|
||||
end
|
||||
|
||||
describe "details" do
|
||||
it "adds details" do
|
||||
post = Fabricate.build(:post)
|
||||
post.add_detail("key", "value")
|
||||
post.post_details.size.should == 1
|
||||
post.post_details.first.key.should == "key"
|
||||
post.post_details.first.value.should == "value"
|
||||
end
|
||||
|
||||
it "can find a post by a detail" do
|
||||
detail = Fabricate(:post_detail)
|
||||
post = detail.post
|
||||
Post.find_by_detail(detail.key, detail.value).id.should == post.id
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue