2010-01-14 10:30:53 +00:00
|
|
|
class Item
|
|
|
|
|
2011-04-13 10:19:51 +00:00
|
|
|
include Mongoid::Document
|
2011-03-08 09:25:46 +00:00
|
|
|
include Mongoid::Timestamps
|
2012-05-11 08:16:09 +00:00
|
|
|
include Mongoid::Tree
|
|
|
|
include Mongoid::Tree::Ordering
|
2013-07-22 05:45:13 +00:00
|
|
|
LIST = YAML.load(File.read("#{Rails.root}/config/list.yml"))
|
2012-11-29 06:21:46 +00:00
|
|
|
|
2012-05-11 08:16:09 +00:00
|
|
|
field :name
|
|
|
|
field :path
|
|
|
|
field :is_published, :type => Boolean, :default => false
|
|
|
|
field :enabled_for, :type => Array, :default => nil
|
2013-10-23 07:51:49 +00:00
|
|
|
field :menu_enabled_for, :type => Hash, :default => nil
|
2012-07-25 21:07:32 +00:00
|
|
|
field :title, localize: true
|
2013-02-06 04:12:23 +00:00
|
|
|
field :sitemap_enabled, :type => Hash, :default => {}
|
2012-05-22 09:55:45 +00:00
|
|
|
|
2010-01-18 07:52:52 +00:00
|
|
|
|
2010-02-22 07:27:11 +00:00
|
|
|
validates_format_of :name, :with => /^[0-9a-zA-Z\-_]+$/
|
2012-11-29 06:21:46 +00:00
|
|
|
# validates :name, :exclusion => { :in => LIST[:forbidden_item_names] }
|
2013-04-23 02:01:02 +00:00
|
|
|
validates_uniqueness_of :name #, :scope => :parent_id
|
2012-05-11 08:16:09 +00:00
|
|
|
validates_presence_of :name
|
|
|
|
validates_associated :parent, :children
|
2012-05-22 09:55:45 +00:00
|
|
|
|
|
|
|
before_destroy :destroy_children
|
2012-08-07 03:26:17 +00:00
|
|
|
after_rearrange :rebuild_path, :if => "parent_id_changed? || name_changed?"
|
2013-02-18 10:21:33 +00:00
|
|
|
after_save :rebuild_children_path, :if => "path_changed?"
|
2010-02-05 09:04:07 +00:00
|
|
|
|
2012-08-07 11:21:54 +00:00
|
|
|
def enabled_for_lang(lang)
|
2013-05-23 04:07:43 +00:00
|
|
|
enabled_for.include?(lang) rescue false
|
2012-08-07 11:21:54 +00:00
|
|
|
end
|
|
|
|
|
2010-01-14 10:30:53 +00:00
|
|
|
def self.find_by_name(item_name)
|
2010-03-01 08:18:28 +00:00
|
|
|
Item.first(:conditions => { :name => item_name, :is_published => true })
|
2010-01-14 10:30:53 +00:00
|
|
|
end
|
2012-05-05 16:35:13 +00:00
|
|
|
|
2012-05-11 08:16:09 +00:00
|
|
|
def visible_children
|
|
|
|
objects = self.children
|
2012-05-05 16:35:13 +00:00
|
|
|
a = []
|
|
|
|
if objects
|
|
|
|
objects.each do |object|
|
2012-05-07 07:47:14 +00:00
|
|
|
a << object if object.menu_enabled_for.nil? ? true : object.menu_enabled_for.include?(I18n.locale.to_s)
|
2012-05-05 16:35:13 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
a
|
|
|
|
end
|
2012-05-11 09:56:26 +00:00
|
|
|
|
|
|
|
def shift_to(new_parent, position)
|
2013-10-04 07:07:24 +00:00
|
|
|
position = position.to_i
|
2013-10-08 04:33:24 +00:00
|
|
|
new_parent = Item.find(new_parent)
|
|
|
|
current_position_sibling = find_by_parent_and_position(new_parent, position)
|
|
|
|
if current_position_sibling
|
|
|
|
if self.position > current_position_sibling.position
|
|
|
|
self.move_above(current_position_sibling)
|
|
|
|
else
|
|
|
|
self.move_below(current_position_sibling)
|
2012-05-11 09:56:26 +00:00
|
|
|
end
|
2013-10-08 04:33:24 +00:00
|
|
|
elsif self.parent != new_parent
|
|
|
|
self.parent = new_parent
|
|
|
|
save
|
2012-05-11 09:56:26 +00:00
|
|
|
end
|
2012-12-19 09:15:31 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def show_in_sitemap_for(locale)
|
2013-02-06 04:12:23 +00:00
|
|
|
if !sitemap_enabled.blank? && !sitemap_enabled[locale].blank?
|
2012-12-19 09:15:31 +00:00
|
|
|
sitemap_enabled[locale].eql?('true') ? true : false
|
|
|
|
else
|
|
|
|
true
|
|
|
|
end
|
|
|
|
end
|
2013-04-22 08:23:39 +00:00
|
|
|
|
|
|
|
def self.structure_ordered_items
|
|
|
|
self.get_children(Item.root, [])
|
|
|
|
end
|
2010-02-05 09:18:57 +00:00
|
|
|
|
2010-01-14 10:30:53 +00:00
|
|
|
protected
|
2010-02-05 08:53:52 +00:00
|
|
|
|
2012-05-11 08:16:09 +00:00
|
|
|
def rebuild_path
|
|
|
|
self.path = (self.ancestors_and_self - [Item.root]).collect{|x| x.name unless x.root?}.join('/')
|
2012-12-19 09:15:31 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def rebuild_children_path
|
|
|
|
self.children.each do |child|
|
|
|
|
child.path = (child.ancestors_and_self - [Item.root]).collect{|x| x.name}.join('/')
|
|
|
|
child.save
|
|
|
|
child.rebuild_children_path
|
|
|
|
end
|
2010-01-14 10:30:53 +00:00
|
|
|
end
|
2012-05-11 08:16:09 +00:00
|
|
|
|
2012-05-11 09:56:26 +00:00
|
|
|
def find_by_parent_and_position(parent, position)
|
|
|
|
parent.children.detect{|child| child.position == position}
|
|
|
|
end
|
|
|
|
|
2011-04-13 10:19:51 +00:00
|
|
|
# Enable the validation for parent_id
|
|
|
|
def validates_presence_of_parent_id?
|
|
|
|
true
|
|
|
|
end
|
2013-04-22 08:23:39 +00:00
|
|
|
|
|
|
|
def self.get_children(item, tree)
|
|
|
|
tree << item
|
|
|
|
item.children.each do |child|
|
|
|
|
self.get_children(child, tree)
|
|
|
|
end
|
|
|
|
tree
|
|
|
|
end
|
2010-01-14 10:30:53 +00:00
|
|
|
|
2011-04-13 10:19:51 +00:00
|
|
|
end
|