From 1aa9fc982d2acbe12298d20859d65ceb585690ec Mon Sep 17 00:00:00 2001 From: James Edward Gray II Date: Tue, 15 Oct 2013 09:21:30 -0500 Subject: [PATCH] Adding post details in preparation for the API importer code. --- app/models/post.rb | 13 +++++++++++++ app/models/post_detail.rb | 6 ++++++ .../20131015131652_create_post_details.rb | 14 ++++++++++++++ spec/fabricators/post_detail_fabricator.rb | 5 +++++ spec/models/post_detail_spec.rb | 9 +++++++++ spec/models/post_spec.rb | 18 ++++++++++++++++++ 6 files changed, 65 insertions(+) create mode 100644 app/models/post_detail.rb create mode 100644 db/migrate/20131015131652_create_post_details.rb create mode 100644 spec/fabricators/post_detail_fabricator.rb create mode 100644 spec/models/post_detail_spec.rb diff --git a/app/models/post.rb b/app/models/post.rb index 7828a01b899..4e0c1d13582 100644 --- a/app/models/post.rb +++ b/app/models/post.rb @@ -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) diff --git a/app/models/post_detail.rb b/app/models/post_detail.rb new file mode 100644 index 00000000000..e3e82625129 --- /dev/null +++ b/app/models/post_detail.rb @@ -0,0 +1,6 @@ +class PostDetail < ActiveRecord::Base + belongs_to :post + + validates_presence_of :key, :value + validates_uniqueness_of :key, scope: :post_id +end diff --git a/db/migrate/20131015131652_create_post_details.rb b/db/migrate/20131015131652_create_post_details.rb new file mode 100644 index 00000000000..5a124432cf6 --- /dev/null +++ b/db/migrate/20131015131652_create_post_details.rb @@ -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 diff --git a/spec/fabricators/post_detail_fabricator.rb b/spec/fabricators/post_detail_fabricator.rb new file mode 100644 index 00000000000..8fdcec29106 --- /dev/null +++ b/spec/fabricators/post_detail_fabricator.rb @@ -0,0 +1,5 @@ +Fabricator(:post_detail) do + post + key { sequence(:key) { |i| "key#{i}" } } + value "test value" +end diff --git a/spec/models/post_detail_spec.rb b/spec/models/post_detail_spec.rb new file mode 100644 index 00000000000..9319bfcf6d1 --- /dev/null +++ b/spec/models/post_detail_spec.rb @@ -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 diff --git a/spec/models/post_spec.rb b/spec/models/post_spec.rb index 2cbff032885..847ca356ab4 100644 --- a/spec/models/post_spec.rb +++ b/spec/models/post_spec.rb @@ -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