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



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

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



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

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

#all_children_with_nameObject



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

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

#ancestorsObject



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

def ancestors
  node ? node.ancestors : []
end

#deletable?Boolean

Returns:

  • (Boolean)


93
94
95
# File 'app/models/section.rb', line 93

def deletable?
  !root? && empty?
end

#editable_by_group?(group) ⇒ Boolean

Returns:

  • (Boolean)


97
98
99
# File 'app/models/section.rb', line 97

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

#empty?Boolean

Returns:

  • (Boolean)


89
90
91
# File 'app/models/section.rb', line 89

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

The first page that is a decendent of this section



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

def first_page_or_link
  section_node = child_nodes.of_type(['Link','Page']).first 
  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



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

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

#parentObject



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

def parent
  node ? node.section : nil
end

#parent=(sec) ⇒ Object



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

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

#parent_idObject



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

def parent_id
  parent ? parent.id : nil
end

#parent_id=(sec_id) ⇒ Object



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

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

#path_not_reservedObject



134
135
136
137
138
# File 'app/models/section.rb', line 134

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)


85
86
87
# File 'app/models/section.rb', line 85

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

#statusObject



101
102
103
# File 'app/models/section.rb', line 101

def status
  public? ? :unlocked : :locked
end

#visible_child_nodesObject



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

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