Module: Locomotive::Extensions::Page::Tree

Extended by:
ActiveSupport::Concern
Included in:
Page
Defined in:
app/models/locomotive/extensions/page/tree.rb

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Instance Method Details

#children_with_minimal_attributes(attrs = []) ⇒ Array

Returns the children of this node but with the minimal set of required attributes

Returns:

  • (Array)

    The children pages ordered by their position



85
86
87
# File 'app/models/locomotive/extensions/page/tree.rb', line 85

def children_with_minimal_attributes(attrs = [])
  self.children.minimal_attributes(attrs)
end

#depthObject



110
111
112
# File 'app/models/locomotive/extensions/page/tree.rb', line 110

def depth
  self.parent_ids.count
end

#siblings_and_self_with_scopingMongoid::Criteria

Returns this document’s siblings and itself, all scoped by the site

Returns:

  • (Mongoid::Criteria)

    Mongoid criteria to retrieve the document’s siblings and itself



106
107
108
# File 'app/models/locomotive/extensions/page/tree.rb', line 106

def siblings_and_self_with_scoping
  base_class.where(:parent_id => self.parent_id, :site_id => self.site_id)
end

#sort_children!(ids) ⇒ Object

Assigns the new position of each child of this node.

Parameters:

  • ids (Array)

    The ordered list of page ids (string)



93
94
95
96
97
98
99
100
# File 'app/models/locomotive/extensions/page/tree.rb', line 93

def sort_children!(ids)
  cached_children = self.children.to_a
  ids.each_with_index do |id, position|
    child = cached_children.detect { |p| p._id == BSON::ObjectId(id) }
    child.position = position
    child.save
  end
end