orbit-basic/app/models/item.rb

51 lines
1.3 KiB
Ruby

class Item
include Mongoid::Document
include Mongoid::Timestamps
include Mongoid::Tree
include Mongoid::Tree::Ordering
field :name
field :path
field :is_published, :type => Boolean, :default => false
field :enabled_for, :type => Array, :default => nil
field :menu_enabled_for, :type => Array, :default => nil
validates_format_of :name, :with => /^[0-9a-zA-Z\-_]+$/
validates :name, :exclusion => { :in => LIST[:forbidden_item_names] }
validates_uniqueness_of :name, :scope => :parent_id
validates_presence_of :name
validates_associated :parent, :children
after_rearrange :rebuild_path
has_one :i18n_variable, :as => :language_value, :autosave => true, :dependent => :destroy
def self.find_by_name(item_name)
Item.first(:conditions => { :name => item_name, :is_published => true })
end
def visible_children
objects = self.children
a = []
if objects
objects.each do |object|
a << object if object.menu_enabled_for.nil? ? true : object.menu_enabled_for.include?(I18n.locale.to_s)
end
end
a
end
protected
def rebuild_path
self.path = (self.ancestors_and_self - [Item.root]).collect{|x| x.name unless x.root?}.join('/')
end
# Enable the validation for parent_id
def validates_presence_of_parent_id?
true
end
end