Skip to content

Commit 1aa9fc9

Browse files
committed
Adding post details in preparation for the API importer code.
1 parent 2003986 commit 1aa9fc9

6 files changed

Lines changed: 65 additions & 0 deletions

File tree

app/models/post.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ class Post < ActiveRecord::Base
3434

3535
has_one :post_search_data
3636

37+
has_many :post_details
38+
3739
validates_with ::Validators::PostValidator
3840

3941
# We can pass several creating options to a post via attributes
@@ -56,6 +58,17 @@ def self.types
5658
@types ||= Enum.new(:regular, :moderator_action)
5759
end
5860

61+
def self.find_by_detail(key, value)
62+
includes(:post_details).where( "post_details.key = ? AND " +
63+
"post_details.value = ?",
64+
key,
65+
value ).first
66+
end
67+
68+
def add_detail(key, value, extra = nil)
69+
post_details.build(key: key, value: value, extra: extra)
70+
end
71+
5972
def limit_posts_per_day
6073
if user.created_at > 1.day.ago && post_number > 1
6174
RateLimiter.new(user, "first-day-replies-per-day:#{Date.today.to_s}", SiteSetting.max_replies_in_first_day, 1.day.to_i)

app/models/post_detail.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
class PostDetail < ActiveRecord::Base
2+
belongs_to :post
3+
4+
validates_presence_of :key, :value
5+
validates_uniqueness_of :key, scope: :post_id
6+
end
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class CreatePostDetails < ActiveRecord::Migration
2+
def change
3+
create_table :post_details do |t|
4+
t.belongs_to :post
5+
t.string :key
6+
t.string :value, size: 512
7+
t.text :extra
8+
9+
t.timestamps
10+
end
11+
12+
add_index :post_details, [:post_id, :key], unique: true
13+
end
14+
end
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Fabricator(:post_detail) do
2+
post
3+
key { sequence(:key) { |i| "key#{i}" } }
4+
value "test value"
5+
end

spec/models/post_detail_spec.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
require 'spec_helper'
2+
3+
describe PostDetail do
4+
it { should belong_to :post }
5+
6+
it { should validate_presence_of :key }
7+
it { should validate_presence_of :value }
8+
it { should validate_uniqueness_of(:key).scoped_to(:post_id) }
9+
end

spec/models/post_spec.rb

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ def post_with_body(body, user=nil)
2626
it { should have_many :post_uploads }
2727
it { should have_many :uploads }
2828

29+
it { should have_many :post_details }
30+
2931
it { should rate_limit }
3032

3133
let(:topic) { Fabricate(:topic) }
@@ -760,4 +762,20 @@ def post_with_body(body, user=nil)
760762
end
761763
end
762764

765+
describe "details" do
766+
it "adds details" do
767+
post = Fabricate.build(:post)
768+
post.add_detail("key", "value")
769+
post.post_details.size.should == 1
770+
post.post_details.first.key.should == "key"
771+
post.post_details.first.value.should == "value"
772+
end
773+
774+
it "can find a post by a detail" do
775+
detail = Fabricate(:post_detail)
776+
post = detail.post
777+
Post.find_by_detail(detail.key, detail.value).id.should == post.id
778+
end
779+
end
780+
763781
end

0 commit comments

Comments
 (0)