Class: Section
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- Section
- Defined in:
- app/models/section.rb
Instance Attribute Summary collapse
-
#full_path ⇒ Object
Returns the value of attribute full_path.
Class Method Summary collapse
Instance Method Summary collapse
- #actual_path ⇒ Object
- #all_children_with_name ⇒ Object
-
#allow_groups=(code = :none) ⇒ Object
Set which groups are allowed to access this section.
- #ancestors(options = {}) ⇒ Object
- #deletable? ⇒ Boolean
- #editable_by_group?(group) ⇒ Boolean
- #empty? ⇒ Boolean
-
#first_page_or_link ⇒ Object
The first page that is a decendent of this section.
- #move_to(section) ⇒ Object
- #parent ⇒ Object
- #parent=(sec) ⇒ Object
- #parent_id ⇒ Object
- #parent_id=(sec_id) ⇒ Object
- #path_not_reserved ⇒ Object
- #public? ⇒ Boolean
- #status ⇒ Object
- #visible_child_nodes(options = {}) ⇒ Object
- #with_ancestors(options = {}) ⇒ Object
Instance Attribute Details
#full_path ⇒ Object
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
112 113 114 115 116 117 118 119 |
# File 'app/models/section.rb', line 112 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_path ⇒ Object
132 133 134 135 136 137 138 139 |
# File 'app/models/section.rb', line 132 def actual_path if root? "/" else p = first_page_or_link p ? p.path : "#" end end |
#all_children_with_name ⇒ Object
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 |
#allow_groups=(code = :none) ⇒ Object
Set which groups are allowed to access this section.
150 151 152 153 154 |
# File 'app/models/section.rb', line 150 def allow_groups=(code=:none) if code == :all self.groups = Group.all end end |
#ancestors(options = {}) ⇒ Object
74 75 76 77 |
# File 'app/models/section.rb', line 74 def ancestors(={}) ancs = node ? node.ancestors : [] [:include_self] ? ancs + [self] : ancs end |
#deletable? ⇒ Boolean
100 101 102 |
# File 'app/models/section.rb', line 100 def deletable? !root? && empty? end |
#editable_by_group?(group) ⇒ Boolean
104 105 106 |
# File 'app/models/section.rb', line 104 def editable_by_group?(group) group.editable_by_section(self) end |
#empty? ⇒ Boolean
96 97 98 |
# File 'app/models/section.rb', line 96 def empty? child_nodes.reject{|n| n.orphaned?}.empty? end |
#first_page_or_link ⇒ Object
The first page that is a decendent of this section
122 123 124 125 126 127 128 129 130 |
# File 'app/models/section.rb', line 122 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
84 85 86 87 88 89 90 |
# File 'app/models/section.rb', line 84 def move_to(section) if root? false else node.move_to_end(section) end end |
#parent ⇒ Object
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_id ⇒ Object
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_reserved ⇒ Object
141 142 143 144 145 |
# File 'app/models/section.rb', line 141 def path_not_reserved if Cms.reserved_paths.include?(path) errors.add(:path, "is invalid, '#{path}' a reserved path") end end |
#public? ⇒ Boolean
92 93 94 |
# File 'app/models/section.rb', line 92 def public? !!(groups.find_by_code('guest')) end |
#status ⇒ Object
108 109 110 |
# File 'app/models/section.rb', line 108 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(={}) children = child_nodes.of_type(["Section", "Page", "Link"]).all(:order => 'section_nodes.position') visible_children = children.select{|sn| sn.visible?} [:limit] ? visible_children[0...[:limit]] : visible_children end |
#with_ancestors(options = {}) ⇒ Object
79 80 81 82 |
# File 'app/models/section.rb', line 79 def with_ancestors( = {}) .merge! :include_self => true self.ancestors() end |