Class: Cms::Section

Inherits:
ActiveRecord::Base
  • Object
show all
Extended by:
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]
HIDDEN_NODE_TYPES =
"Cms::Attachment"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from DefaultAccessible

non_permitted_params, permitted_params

Instance Attribute Details

#full_pathObject

Returns the value of attribute full_path.



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

def full_path
  @full_path
end

Class Method Details

.by_group_ids(group_ids) ⇒ Object



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

def self.by_group_ids(group_ids)
  distinct.where("#{Cms::Group.table_name}.id" => group_ids).includes(:groups).references(:groups)
end

.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:



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

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

.named(name) ⇒ Object



31
32
33
# File 'app/models/cms/section.rb', line 31

def self.named(name)
  where name: name
end

.permitted_paramsObject



19
20
21
# File 'app/models/cms/section.rb', line 19

def self.permitted_params
  super + [:allow_groups, group_ids: []]
end

.sitemapObject



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

def self.sitemap
  SectionNode.not_of_type(HIDDEN_NODE_TYPES).fetch_nodes.arrange(:order => :position)
end

.with_path(path) ⇒ Object



35
36
37
# File 'app/models/cms/section.rb', line 35

def self.with_path(path)
  where path: path
end

Instance Method Details

#accessible_to_guests?(public_sections, parent) ⇒ Boolean

Sections are accessible to guests if they marked as such. Variables are passed in for performance reasons since this gets called ‘MANY’ times on the sitemap.

Parameters:

Returns:

  • (Boolean)


224
225
226
# File 'app/models/cms/section.rb', line 224

def accessible_to_guests?(public_sections, parent)
  public_sections.include?(self)
end

#actual_pathObject



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

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



213
214
215
216
217
# File 'app/models/cms/section.rb', line 213

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

#ancestryObject



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

def ancestry
  self.node.ancestry
end

#build_sectionObject

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



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

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

#child_nodesObject

Used by the sitemap to find children to iterate over.



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

def child_nodes
  self.node.children
end

#deletable?Boolean

Callback to determine if this section can be deleted.

Returns:

  • (Boolean)


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

def deletable?
  !root? && empty?
end

#editable_by_group?(group) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#empty?Boolean

Returns:

  • (Boolean)


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

def empty?
  child_nodes.empty?
end

#ensure_section_node_existsObject



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

def ensure_section_node_exists
  unless node
    self.node = build_section_node
  end
end

The first page that is a descendent of this section



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

def first_page_or_link
  types = Cms::ContentType.addressable.collect(&:name).push(LINK).push(PAGE)
  section_node = child_nodes.of_type(types).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.



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

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



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

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

#pagesObject



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

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



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

def parent_id
  parent ? parent.id : nil
end

#parent_id=(sec_id) ⇒ Object



120
121
122
# File 'app/models/cms/section.rb', line 120

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

#path_not_reservedObject



204
205
206
207
208
# File 'app/models/cms/section.rb', line 204

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



187
188
189
190
191
192
193
# File 'app/models/cms/section.rb', line 187

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

#public?Boolean

Returns:

  • (Boolean)


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

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:



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

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

#statusObject



154
155
156
# File 'app/models/cms/section.rb', line 154

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

#visible_child_nodes(options = {}) ⇒ Object



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

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

#with_ancestors(options = {}) ⇒ Object



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

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