Adding post details in preparation for the API importer code.

This commit is contained in:
James Edward Gray II 2013-10-15 09:21:30 -05:00
parent 20039860eb
commit 1aa9fc982d
6 changed files with 65 additions and 0 deletions

View file

@ -34,6 +34,8 @@ class Post < ActiveRecord::Base
has_one :post_search_data has_one :post_search_data
has_many :post_details
validates_with ::Validators::PostValidator validates_with ::Validators::PostValidator
# We can pass several creating options to a post via attributes # 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) @types ||= Enum.new(:regular, :moderator_action)
end 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 def limit_posts_per_day
if user.created_at > 1.day.ago && post_number > 1 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) RateLimiter.new(user, "first-day-replies-per-day:#{Date.today.to_s}", SiteSetting.max_replies_in_first_day, 1.day.to_i)

View 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

View 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

View file

@ -0,0 +1,5 @@
Fabricator(:post_detail) do
post
key { sequence(:key) { |i| "key#{i}" } }
value "test value"
end

View 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

View file

@ -26,6 +26,8 @@ describe Post do
it { should have_many :post_uploads } it { should have_many :post_uploads }
it { should have_many :uploads } it { should have_many :uploads }
it { should have_many :post_details }
it { should rate_limit } it { should rate_limit }
let(:topic) { Fabricate(:topic) } let(:topic) { Fabricate(:topic) }
@ -760,4 +762,20 @@ describe Post do
end end
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 end