Class: Cms::Section

Inherits:
ActiveRecord::Base
  • Object
show all
Includes:
Addressable, Addressable::NodeAccessors, DefaultAccessible
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.



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

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:



156
157
158
159
160
161
162
163
164
165
166
167
# File 'app/models/cms/section.rb', line 156

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



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

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

Instance Method Details

#actual_pathObject



189
190
191
192
193
194
195
196
# File 'app/models/cms/section.rb', line 189

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



207
208
209
210
211
# File 'app/models/cms/section.rb', line 207

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

#ancestryObject



47
48
49
# File 'app/models/cms/section.rb', line 47

def ancestry
  self.node.ancestry
end

#build_sectionObject

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



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

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

#child_nodesObject

Used by the sitemap to find children to iterate over.



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

def child_nodes
  self.node.children
end

#deletable?Boolean

Callback to determine if this section can be deleted.

Returns:

  • (Boolean)


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

def deletable?
  !root? && empty?
end

#destroy_nodeObject

Callback to clean up related nodes



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

def destroy_node
  node.destroy
end

#editable_by_group?(group) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#empty?Boolean

Returns:

  • (Boolean)


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

def empty?
  child_nodes.empty?
end

#ensure_section_node_existsObject



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

def ensure_section_node_exists
  unless node
    self.node = build_section_node
  end
end

The first page that is a decendent of this section



170
171
172
173
174
175
176
177
178
# File 'app/models/cms/section.rb', line 170

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.



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

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



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

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

#pagesObject



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

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



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

def parent_id
  parent ? parent.id : nil
end

#parent_id=(sec_id) ⇒ Object



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

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

#path_not_reservedObject



198
199
200
201
202
# File 'app/models/cms/section.rb', line 198

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

#prependable_pathObject

Returns the path for this section with a trailing slash



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

def prependable_path
  if path.ends_with?("/")
    path
  else
    "#{path}/"
  end
end

#public?Boolean

Returns:

  • (Boolean)


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

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:



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

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

#statusObject



149
150
151
# File 'app/models/cms/section.rb', line 149

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

#visible_child_nodes(options = {}) ⇒ Object



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

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



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

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