Module: Mokio::Concerns::Models::Menu

Extended by:
ActiveSupport::Concern
Included in:
Menu
Defined in:
lib/mokio/concerns/models/menu.rb

Overview

Concern for Menu model

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Instance Method Details

#always_editable_fieldsObject

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_contentsObject

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_posObject

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
  menu_id = (self.id.nil? ? -1 : self.id)
  if lang_id.nil? || lang_id == 0
    Mokio::AvailableModule.not_selected_for_menu(menu_id).for_lang(Mokio::Lang.first.id).group_by(&:module_position_id)
  else
    Mokio::AvailableModule.not_selected_for_menu(menu_id).for_lang(lang_id).group_by(&:module_position_id)
  end
end

#build_slugObject



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

#contentObject

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_typeObject



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_slugObject

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_contentObject

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_typeObject

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

Returns:

  • (Boolean)


250
251
252
# File 'lib/mokio/concerns/models/menu.rb', line 250

def many_contents?
  self.contents.count > 1
end

#one_content?Boolean

Returns:

  • (Boolean)


246
247
248
# File 'lib/mokio/concerns/models/menu.rb', line 246

def one_content?
  self.contents.count == 1
end

#parent_rootObject



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_treeObject

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_posObject

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_updateObject

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_numberObject



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_candidatesObject

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_modulesObject

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

#titleObject

Just for easier logic



226
227
228
# File 'lib/mokio/concerns/models/menu.rb', line 226

def title
  self.name
end

#touch_contentObject

Update etag for Content records after updating menu



240
241
242
243
244
# File 'lib/mokio/concerns/models/menu.rb', line 240

def touch_content
  Mokio::Content.all.each do |c|
    c.touch(:etag)
  end
end