Class: Cms::Section

Inherits:
ActiveRecord::Base
  • Object
show all
Includes:
Addressable, Addressable::NodeAccessors
Defined in:
app/models/cms/section.rb

Constant Summary collapse

SECTION =
"Cms::Section"
PAGE =
"Cms::Page"
"Cms::Link"
VISIBLE_NODE_TYPES =
[SECTION, PAGE, LINK]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Addressable::NodeAccessors

#node, #node=

Methods included from Addressable

#ancestors, #cache_parent, #parent, #parent=, #partial_for

Instance Attribute Details

#full_pathObject

Returns the value of attribute full_path.



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

def full_path
  @full_path
end

Class Method Details

.find_by_name_path(name_path) ⇒ Section

Used by the file browser to look up a section by the combined names as a path.

i.e. /A/B/

Returns:



151
152
153
154
155
156
157
158
159
160
161
162
# File 'app/models/cms/section.rb', line 151

def self.find_by_name_path(name_path)
  current_section = Cms::Section.root.first
  path_names = name_path.split("/")[1..-1] || []

  # This implementation is very slow as it has to loop over the entire tree in memory to match each name element.
  path_names.each do |name|
    current_section.sections.each do |s|
      current_section = s if s.name == name
    end
  end
  current_section
end

.sitemapObject



81
82
83
# File 'app/models/cms/section.rb', line 81

def self.sitemap
  SectionNode.of_type(VISIBLE_NODE_TYPES).fetch_nodes.arrange(:order => :position)
end

Instance Method Details

#actual_pathObject



175
176
177
178
179
180
181
182
# File 'app/models/cms/section.rb', line 175

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

#allow_groups=(code = :none) ⇒ Object

Set which groups are allowed to access this section.

Parameters:

  • code (Symbol) (defaults to: :none)

    Set of groups to allow (Options :all, :none) Defaults to :none



193
194
195
196
197
# File 'app/models/cms/section.rb', line 193

def allow_groups=(code=:none)
  if code == :all
    self.groups = Cms::Group.all
  end
end

#ancestryObject



42
43
44
# File 'app/models/cms/section.rb', line 42

def ancestry
  self.node.ancestry
end

#build_sectionObject

Since #sections isn’t an association anymore, callers can use this rather than #sections.build



65
66
67
# File 'app/models/cms/section.rb', line 65

def build_section
  Section.new(:parent => self)
end

#child_nodesObject

Used by the sitemap to find children to iterate over.



70
71
72
# File 'app/models/cms/section.rb', line 70

def child_nodes
  self.node.children
end

#deletable?Boolean

Callback to determine if this section can be deleted.

Returns:

  • (Boolean)


131
132
133
# File 'app/models/cms/section.rb', line 131

def deletable?
  !root? && empty?
end

#destroy_nodeObject

Callback to clean up related nodes



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

def destroy_node
  node.destroy
end

#editable_by_group?(group) ⇒ Boolean

Returns:

  • (Boolean)


140
141
142
# File 'app/models/cms/section.rb', line 140

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

#empty?Boolean

Returns:

  • (Boolean)


126
127
128
# File 'app/models/cms/section.rb', line 126

def empty?
  child_nodes.empty?
end

#ensure_section_node_existsObject



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

def ensure_section_node_exists
  unless node
    self.node = build_section_node
  end
end

The first page that is a decendent of this section



165
166
167
168
169
170
171
172
173
# File 'app/models/cms/section.rb', line 165

def first_page_or_link
  section_node = child_nodes.of_type([LINK, PAGE]).fetch_nodes.in_order.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

#master_section_listObject

Returns a complete list of all sections that are desecendants of this sections, in order, as a single flat list. Used by Section selectors where users have to pick a single section from a complete list of all sections.



94
95
96
97
98
99
# File 'app/models/cms/section.rb', line 94

def master_section_list
  sections.map do |section|
    section.full_path = root? ? section.name : "#{name} / #{section.name}"
    [section] << section.master_section_list
  end.flatten.compact
end

#move_to(section) ⇒ Object



114
115
116
117
118
119
120
# File 'app/models/cms/section.rb', line 114

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

#pagesObject



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

def pages
  child_pages = self.node.children.collect do |section_node|
    section_node.node if section_node.page?
  end
  child_pages.compact
end

#parent_idObject



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

def parent_id
  parent ? parent.id : nil
end

#parent_id=(sec_id) ⇒ Object



105
106
107
# File 'app/models/cms/section.rb', line 105

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

#path_not_reservedObject



184
185
186
187
188
# File 'app/models/cms/section.rb', line 184

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)


122
123
124
# File 'app/models/cms/section.rb', line 122

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

#sectionsArray<Section> Also known as: child_sections

Returns a list of all children which are sections.

Returns:



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

def sections
  child_nodes.of_type(SECTION).fetch_nodes.in_order.collect do |section_node|
    section_node.node
  end
end

#statusObject



144
145
146
# File 'app/models/cms/section.rb', line 144

def status
  @status ||= public? ? :unlocked : :locked
end

#visible_child_nodes(options = {}) ⇒ Object



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

def visible_child_nodes(options={})
  children = child_nodes.of_type(VISIBLE_NODE_TYPES).fetch_nodes.in_order.all
  visible_children = children.select { |sn| sn.visible? }
  options[:limit] ? visible_children[0...options[:limit]] : visible_children
end

#with_ancestors(options = {}) ⇒ Object



109
110
111
112
# File 'app/models/cms/section.rb', line 109

def with_ancestors(options = {})
  options.merge! :include_self => true
  self.ancestors(options)
end