Class: Section

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/section.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#full_pathObject

Returns the value of attribute full_path.



37
38
39
# File 'app/models/section.rb', line 37

def full_path
  @full_path
end

Class Method Details

.find_by_name_path(name_path) ⇒ Object



107
108
109
110
111
112
113
114
# File 'app/models/section.rb', line 107

def self.find_by_name_path(name_path)
  section = Section.root.first
  children = name_path.split("/")[1..-1] || []
  children.each do |name|
    section = section.sections.first(:conditions => {:name => name})
  end
  section
end

Instance Method Details

#actual_pathObject



127
128
129
130
131
132
133
134
# File 'app/models/section.rb', line 127

def actual_path
  if root?
    "/"
  else
    p = first_page_or_link
    p ? p.path : "#"
  end
end

#all_children_with_nameObject



45
46
47
48
49
50
51
52
# File 'app/models/section.rb', line 45

def all_children_with_name
  child_sections.map do |s|
    if s.node
      s.node.full_path = root? ? s.node.name : "#{name} / #{s.node.name}"
      [s.node] << s.node.all_children_with_name
    end
  end.flatten.compact
end

#ancestors(options = {}) ⇒ Object



74
75
76
77
# File 'app/models/section.rb', line 74

def ancestors(options={})
  ancs = node ? node.ancestors : []
  options[:include_self] ? ancs + [self] : ancs
end

#deletable?Boolean

Returns:

  • (Boolean)


95
96
97
# File 'app/models/section.rb', line 95

def deletable?
  !root? && empty?
end

#editable_by_group?(group) ⇒ Boolean

Returns:

  • (Boolean)


99
100
101
# File 'app/models/section.rb', line 99

def editable_by_group?(group)
  group.editable_by_section(self)
end

#empty?Boolean

Returns:

  • (Boolean)


91
92
93
# File 'app/models/section.rb', line 91

def empty?
  child_nodes.reject{|n| n.orphaned?}.empty?
end

The first page that is a decendent of this section



117
118
119
120
121
122
123
124
125
# File 'app/models/section.rb', line 117

def first_page_or_link
  section_node = child_nodes.of_type(['Link','Page']).first(:order => "section_nodes.position")
  return section_node.node if section_node
  sections.each do |s| 
    node = s.first_page_or_link
    return node if node
  end
  nil
end

#move_to(section) ⇒ Object



79
80
81
82
83
84
85
# File 'app/models/section.rb', line 79

def move_to(section)
  if root?
    false
  else
    node.move_to_end(section)
  end
end

#parentObject



58
59
60
# File 'app/models/section.rb', line 58

def parent
  node ? node.section : nil
end

#parent=(sec) ⇒ Object



66
67
68
69
70
71
72
# File 'app/models/section.rb', line 66

def parent=(sec)
  if node
    node.move_to_end(sec)
  else
    build_node(:node => self, :section => sec)
  end      
end

#parent_idObject



54
55
56
# File 'app/models/section.rb', line 54

def parent_id
  parent ? parent.id : nil
end

#parent_id=(sec_id) ⇒ Object



62
63
64
# File 'app/models/section.rb', line 62

def parent_id=(sec_id)
  self.parent = Section.find(sec_id)
end

#path_not_reservedObject



136
137
138
139
140
# File 'app/models/section.rb', line 136

def path_not_reserved
  if Cms.reserved_paths.include?(path)
    errors.add(:path, "is invalid, '#{path}' a reserved path")
  end
end

#public?Boolean

Returns:

  • (Boolean)


87
88
89
# File 'app/models/section.rb', line 87

def public?
  !!(groups.find_by_code('guest'))
end

#statusObject



103
104
105
# File 'app/models/section.rb', line 103

def status
  public? ? :unlocked : :locked
end

#visible_child_nodes(options = {}) ⇒ Object



39
40
41
42
43
# File 'app/models/section.rb', line 39

def visible_child_nodes(options={})
  children = child_nodes.of_type(["Section", "Page", "Link"]).all(:order => 'section_nodes.position')
  visible_children = children.select{|sn| sn.visible?}
  options[:limit] ? visible_children[0...options[:limit]] : visible_children
end