Skip to content

Commit

Permalink
Merge pull request #2252 from Ana06/abstract_class
Browse files Browse the repository at this point in the history
[api][webui] Add validation for Comment type
  • Loading branch information
bgeuken committed Oct 25, 2016
2 parents 104bba9 + b1c5840 commit 73187f7
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/api/app/models/comment.rb
Expand Up @@ -9,6 +9,10 @@ class Comment < ApplicationRecord

validates :body, :user, :type, presence: true

# Only instances of Comment's children can be created, not directly from Comment.
# So the type attribute, which is reserved for storing the inheritance class, must be the name of a child class.
validate :check_is_child

after_create :create_notification

has_many :children, dependent: :destroy, :class_name => 'Comment', :foreign_key => 'parent_id'
Expand Down Expand Up @@ -75,4 +79,12 @@ def blank_or_destroy
def destroy
super
end

private

def check_is_child
unless type.safe_constantize.try(:superclass) == Comment
errors[:type] << "is reserved for storing the inheritance class which was not found"
end
end
end

0 comments on commit 73187f7

Please sign in to comment.