parent
cd09c75c50
commit
163ffdb19b
2 changed files with 35 additions and 9 deletions
|
|
@ -6,7 +6,7 @@ class Reshare < Post
|
||||||
belongs_to :root, class_name: "Post", foreign_key: :root_guid, primary_key: :guid, optional: true
|
belongs_to :root, class_name: "Post", foreign_key: :root_guid, primary_key: :guid, optional: true
|
||||||
validate :root_must_be_public
|
validate :root_must_be_public
|
||||||
validates_presence_of :root, :on => :create
|
validates_presence_of :root, :on => :create
|
||||||
validates_uniqueness_of :root_guid, :scope => :author_id
|
validates :root_guid, uniqueness: {scope: :author_id}, allow_nil: true
|
||||||
delegate :author, to: :root, prefix: true
|
delegate :author, to: :root, prefix: true
|
||||||
|
|
||||||
before_validation do
|
before_validation do
|
||||||
|
|
|
||||||
|
|
@ -3,15 +3,41 @@ describe Reshare, type: :model do
|
||||||
expect(FactoryGirl.build(:reshare)).to be_valid
|
expect(FactoryGirl.build(:reshare)).to be_valid
|
||||||
end
|
end
|
||||||
|
|
||||||
it "requires root" do
|
context "validation" do
|
||||||
reshare = FactoryGirl.build(:reshare, root: nil)
|
it "requires root" do
|
||||||
expect(reshare).not_to be_valid
|
reshare = FactoryGirl.build(:reshare, root: nil)
|
||||||
end
|
expect(reshare).not_to be_valid
|
||||||
|
end
|
||||||
|
|
||||||
it "require public root" do
|
it "require public root" do
|
||||||
reshare = FactoryGirl.build(:reshare, root: FactoryGirl.create(:status_message, public: false))
|
reshare = FactoryGirl.build(:reshare, root: FactoryGirl.create(:status_message, public: false))
|
||||||
expect(reshare).not_to be_valid
|
expect(reshare).not_to be_valid
|
||||||
expect(reshare.errors[:base]).to include("Only posts which are public may be reshared.")
|
expect(reshare.errors[:base]).to include("Only posts which are public may be reshared.")
|
||||||
|
end
|
||||||
|
|
||||||
|
it "allows two reshares without a root" do
|
||||||
|
reshare1 = FactoryGirl.create(:reshare, author: alice.person)
|
||||||
|
reshare2 = FactoryGirl.create(:reshare, author: alice.person)
|
||||||
|
|
||||||
|
reshare1.update_attributes(root_guid: nil)
|
||||||
|
|
||||||
|
reshare2.root_guid = nil
|
||||||
|
expect(reshare2).to be_valid
|
||||||
|
end
|
||||||
|
|
||||||
|
it "doesn't allow to reshare the same post twice" do
|
||||||
|
post = FactoryGirl.create(:status_message, public: true)
|
||||||
|
FactoryGirl.create(:reshare, author: alice.person, root: post)
|
||||||
|
|
||||||
|
expect(FactoryGirl.build(:reshare, author: alice.person, root: post)).not_to be_valid
|
||||||
|
end
|
||||||
|
|
||||||
|
it "allows to reshare the same post with different people" do
|
||||||
|
post = FactoryGirl.create(:status_message, public: true)
|
||||||
|
FactoryGirl.create(:reshare, author: alice.person, root: post)
|
||||||
|
|
||||||
|
expect(FactoryGirl.build(:reshare, author: bob.person, root: post)).to be_valid
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
it "forces public" do
|
it "forces public" do
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue