Module: Mokio::Concerns::Models::Menu
Overview
Concern for Menu model
Defined Under Namespace
Modules: ClassMethods
Instance Method Summary collapse
-
#always_editable_fields ⇒ Object
Returns always editable fields.
-
#available_contents ⇒ Object
Returns list of contents available for assignment to given menu element (based on lang_id) ordered by title.
-
#available_modules_by_pos ⇒ Object
Returns list of static modules available for assignment to given menu menu element - divided per positions.
- #build_slug ⇒ Object
-
#content ⇒ Object
First content on menu list.
- #content_type ⇒ Object
-
#full_slug ⇒ Object
Hierarchical slug.
-
#invisible_content ⇒ Object
Checks if menu has any non-active or non-visible content assigned.
-
#linked_content_type ⇒ Object
Returns type of content added to menu.
- #many_contents? ⇒ Boolean
- #one_content? ⇒ Boolean
- #parent_root ⇒ Object
-
#parent_tree ⇒ Object
Return parent subtree.
-
#real_slug(hierarchical) ⇒ Object
Real slug - menu slug hierarchical or not, with lang prefix or not.
-
#selected_modules_by_pos ⇒ Object
Returns list of selected modules divided by positions.
-
#seq_and_lang_update ⇒ Object
Updating seq and lang_id fields.
- #sequence_number ⇒ Object
-
#slug_candidates ⇒ Object
Friendly_id slug_candidates (gem ‘friendly_id’).
-
#static_modules ⇒ Object
Return static modules for menu, does not include always displayed modules.
-
#title ⇒ Object
Just for easier logic.
-
#touch_content ⇒ Object
Update etag for Content records after updating menu.
Instance Method Details
#always_editable_fields ⇒ Object
Returns always editable fields
194 195 196 197 198 199 |
# File 'lib/mokio/concerns/models/menu.rb', line 194 def always_editable_fields @always = %w(active seq visible always_displayed ancestry) @always << 'contents' if self.content_editable @always << 'available_modules' if self.modules_editable @always end |
#available_contents ⇒ Object
Returns list of contents available for assignment to given menu element (based on lang_id) ordered by title
102 103 104 105 106 107 108 109 |
# File 'lib/mokio/concerns/models/menu.rb', line 102 def available_contents raise "NoLangDefinedException" if Mokio::Lang.count < 1 if lang_id.nil? || lang_id == 0 Mokio::Content.lang(Mokio::Lang.first.id).order(:title) - contents else Mokio::Content.lang(lang_id).order(:title) - contents end end |
#available_modules_by_pos ⇒ Object
Returns list of static modules available for assignment to given menu menu element - divided per positions
138 139 140 141 142 143 144 145 |
# File 'lib/mokio/concerns/models/menu.rb', line 138 def available_modules_by_pos = (self.id.nil? ? -1 : self.id) if lang_id.nil? || lang_id == 0 Mokio::AvailableModule.().for_lang(Mokio::Lang.first.id).group_by(&:module_position_id) else Mokio::AvailableModule.().for_lang(lang_id).group_by(&:module_position_id) end end |
#build_slug ⇒ Object
78 79 80 81 82 83 84 85 86 |
# File 'lib/mokio/concerns/models/menu.rb', line 78 def build_slug if parent.nil? '' elsif !parent.fake parent.slug else parent.name end end |
#content ⇒ Object
First content on menu list
233 234 235 |
# File 'lib/mokio/concerns/models/menu.rb', line 233 def content self.contents.active.first end |
#content_type ⇒ Object
179 180 181 182 183 184 185 186 187 188 189 |
# File 'lib/mokio/concerns/models/menu.rb', line 179 def content_type if contents.length > 1 I18n.t('menus.list').titleize elsif contents.length == 1 I18n.t(contents[0].type.underscore).titleize elsif !external_link.blank? I18n.t('external_link', Menu).titleize else '' end end |
#full_slug ⇒ Object
Hierarchical slug
204 205 206 207 208 209 210 211 212 213 214 215 |
# File 'lib/mokio/concerns/models/menu.rb', line 204 def full_slug m = self slug = m.slug unless m.parent.nil? || slug.nil? while m.parent && !m.parent.fake slug = m.parent.slug + "/" + slug m = m.parent end slug end slug end |
#invisible_content ⇒ Object
Checks if menu has any non-active or non-visible content assigned
170 171 172 173 174 175 176 177 |
# File 'lib/mokio/concerns/models/menu.rb', line 170 def invisible_content contents.each do |content| if !content.active or !content.displayed? return true end end false end |
#linked_content_type ⇒ Object
Returns type of content added to menu
257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 |
# File 'lib/mokio/concerns/models/menu.rb', line 257 def linked_content_type return nil unless self.contents type = "" self.contents.each_with_index do |content, i| if i == 0 type = content.type else return "mixed_content" if type != content.type type = content.type end end type.tableize end |
#many_contents? ⇒ Boolean
250 251 252 |
# File 'lib/mokio/concerns/models/menu.rb', line 250 def many_contents? self.contents.count > 1 end |
#one_content? ⇒ Boolean
246 247 248 |
# File 'lib/mokio/concerns/models/menu.rb', line 246 def one_content? self.contents.count == 1 end |
#parent_root ⇒ Object
111 112 113 114 115 |
# File 'lib/mokio/concerns/models/menu.rb', line 111 def parent_root root = Mokio::Menu.find_by_lang_id(lang_id) unless lang_id.nil? root = Mokio::Menu.find_by_lang_id(Mokio::Lang.first.id) if root.nil? root end |
#parent_tree ⇒ Object
Return parent subtree
120 121 122 123 124 125 |
# File 'lib/mokio/concerns/models/menu.rb', line 120 def parent_tree tree = parent_root.subtree tree.to_a.delete(self) tree.to_a.delete(parent_root) tree end |
#real_slug(hierarchical) ⇒ Object
Real slug - menu slug hierarchical or not, with lang prefix or not
219 220 221 |
# File 'lib/mokio/concerns/models/menu.rb', line 219 def real_slug (hierarchical) "/#{hierarchical ? full_slug : slug}" end |
#selected_modules_by_pos ⇒ Object
Returns list of selected modules divided by positions
130 131 132 133 |
# File 'lib/mokio/concerns/models/menu.rb', line 130 def selected_modules_by_pos # AvailableModule.selected_for_menu(id).group_by(&:module_position_id) (self.available_modules + Mokio::AvailableModule.always_displayed).uniq.group_by(&:module_position_id) end |
#seq_and_lang_update ⇒ Object
Updating seq and lang_id fields
90 91 92 93 94 95 96 97 |
# File 'lib/mokio/concerns/models/menu.rb', line 90 def seq_and_lang_update self.seq = sequence_number if self.lang_id.nil? self.lang_id = root.lang_id else self.lang_id = self.root.lang_id end end |
#sequence_number ⇒ Object
154 155 156 157 158 159 160 161 162 163 164 165 |
# File 'lib/mokio/concerns/models/menu.rb', line 154 def sequence_number if seq.nil? if parent.nil? Mokio::Menu.where('ancestry is null').count +1 else max = parent.children.maximum(:seq) max.nil? ? 1 : max + 1 end else seq end end |
#slug_candidates ⇒ Object
Friendly_id slug_candidates (gem ‘friendly_id’)
71 72 73 74 75 76 |
# File 'lib/mokio/concerns/models/menu.rb', line 71 def slug_candidates [ :name, [build_slug, :name] ] end |
#static_modules ⇒ Object
Return static modules for menu, does not include always displayed modules
150 151 152 |
# File 'lib/mokio/concerns/models/menu.rb', line 150 def static_modules Mokio::StaticModule.where(:id => self.available_modules.map(&:static_module_id)) end |
#title ⇒ Object
Just for easier logic
226 227 228 |
# File 'lib/mokio/concerns/models/menu.rb', line 226 def title self.name end |