Class: Releaf::Content::Node::RootValidator

Inherits:
ActiveModel::Validator
  • Object
show all
Defined in:
app/validators/releaf/content/node/root_validator.rb

Overview

Validator to test if node is valid root node

Validator needs :allow option.

:allow option specifies which nodes are valid root nodes.

In example above only Text and Book nodes can be created as root nodes

Examples:


class Node < ActiveRecord::Base
  includes Releaf::Content::Node
  validates_with Releaf::Content::Node::RootValidator, allow: [Text, Store]
end

Instance Method Summary collapse

Instance Method Details

#validate(node) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
# File 'app/validators/releaf/content/node/root_validator.rb', line 19

def validate node
  @node = node

  if allowed_root_node?
    node.errors.add(:content_type, "can't be subnode") if @node.parent.present?
  else
    node.errors.add(:content_type, "can't be root node") if @node.parent.nil?
  end

  remove_instance_variable(:@node)
end