category.rb revision 8d0fb4fb93788e195e8fc2a48b243b3e53e6bd45
class Category < ActiveRecord::Base
has_and_belongs_to_many :ontologies
attr_accessible :name, :parent, :parent_id
validates :name, :uniqueness => { :message => 'Already taken' }
has_ancestry
def self.arrange_as_array(options={}, hash=nil)
hash ||= arrange(options)
arr = []
hash.each do |node, children|
arr << node
arr += arrange_as_array(options, children) unless children.nil?
end
arr
end
def name_for_selects
"#{'-' * depth} #{name}"
end
def possible_parents
parents = Category.arrange_as_array(:order => 'name')
return new_record? ? parents : parents - subtree
end
def to_s
name
end
end