Validate on report that post or comment does exist
This commit is contained in:
parent
6d6ebd297e
commit
8170ef8363
2 changed files with 25 additions and 2 deletions
|
|
@ -6,6 +6,7 @@ class Report < ActiveRecord::Base
|
||||||
validates :text, presence: true
|
validates :text, presence: true
|
||||||
|
|
||||||
validate :entry_does_not_exist, :on => :create
|
validate :entry_does_not_exist, :on => :create
|
||||||
|
validate :post_or_comment_does_exist, :on => :create
|
||||||
|
|
||||||
belongs_to :user
|
belongs_to :user
|
||||||
belongs_to :post
|
belongs_to :post
|
||||||
|
|
@ -19,6 +20,12 @@ class Report < ActiveRecord::Base
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def post_or_comment_does_exist
|
||||||
|
if Post.find_by_id(item_id).nil? && Comment.find_by_id(item_id).nil?
|
||||||
|
errors[:base] << 'Post or comment was already deleted or doesn\'t exists.'
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
def destroy_reported_item
|
def destroy_reported_item
|
||||||
if item_type == 'post'
|
if item_type == 'post'
|
||||||
delete_post
|
delete_post
|
||||||
|
|
|
||||||
|
|
@ -25,11 +25,27 @@ describe Report do
|
||||||
|
|
||||||
describe '#validation' do
|
describe '#validation' do
|
||||||
it 'validates that post ID is required' do
|
it 'validates that post ID is required' do
|
||||||
@user.reports.build(:item_type => 'post', :text => 'blub').should_not be_valid
|
report = @valid_post_report
|
||||||
|
report.delete(:item_id)
|
||||||
|
@user.reports.build(report).should_not be_valid
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'validates that post type is required' do
|
it 'validates that post type is required' do
|
||||||
@user.reports.build(:item_id => 666, :text => 'blub').should_not be_valid
|
report = @valid_post_report
|
||||||
|
report.delete(:item_type)
|
||||||
|
@user.reports.build(report).should_not be_valid
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'validates that post does exist' do
|
||||||
|
report = @valid_post_report
|
||||||
|
report[:item_id] = 666;
|
||||||
|
@user.reports.build(report).should_not be_valid
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'validates that comment does exist' do
|
||||||
|
report = @valid_comment_report
|
||||||
|
report[:item_id] = 666;
|
||||||
|
@user.reports.build(report).should_not be_valid
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'validates that entry does not exist' do
|
it 'validates that entry does not exist' do
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue