This repository has been archived on 2024-03-16. You can view files and clone it, but cannot push or open issues or pull requests.
orbit-4-1/app/models/snippet.rb

38 lines
970 B
Ruby

class Snippet
include Mongoid::Document
field :name, :index => true
field :full_name, :index => true
field :parent_id, :index => true
field :parent_name
field :content
before_validation :setup_default_value
validates_presence_of :name, :full_name, :parent_id
validates_uniqueness_of :name, :scope => :parent_id
referenced_in :parent, :class_name => "Item", :foreign_key => :parent_id
# Get an array of ancestors
def ancestors
node, nodes = self, []
nodes << node = node.parent while !node.parent.blank? rescue nil
nodes.reverse
end
def setup_default_value
# Set the parent value
self.parent_name = Item.find( self.parent_id ).name rescue nil
# Build the full_name from the ancestors array
full_node = self.ancestors.map{ |a| a.name }.push( self.name )
# Remove root node if not root
full_node.shift if full_node.size >= 2
self.full_name = full_node.join("/")
end
end