Class: NoCms::Menus::MenuItem

Inherits:
ActiveRecord::Base
  • Object
show all
Extended by:
Enumerize
Includes:
Concerns::TranslationScopes
Defined in:
app/models/no_cms/menus/menu_item.rb

Instance Method Summary collapse

Instance Method Details

#active_for?(options = {}) ⇒ Boolean

Returns:

  • (Boolean)


49
50
51
52
53
54
55
56
57
# File 'app/models/no_cms/menus/menu_item.rb', line 49

def active_for?(options = {})

  return active_for_object?(options[:object]) unless options[:object].nil?
  return active_for_action?(options[:action]) unless options[:action].nil?
  return active_for_external_url?(options[:url]) unless options[:url].nil?

  false

end

#active_for_action?(action) ⇒ Boolean

Returns:

  • (Boolean)


63
64
65
# File 'app/models/no_cms/menus/menu_item.rb', line 63

def active_for_action? action
  !action.nil?  && (menu_action == action)
end

#active_for_external_url?(external_url) ⇒ Boolean

Returns:

  • (Boolean)


67
68
69
# File 'app/models/no_cms/menus/menu_item.rb', line 67

def active_for_external_url? external_url
  !external_url.nil?  && (self.external_url == external_url)
end

#active_for_object?(object) ⇒ Boolean

Returns:

  • (Boolean)


59
60
61
# File 'app/models/no_cms/menus/menu_item.rb', line 59

def active_for_object? object
  !object.nil?  && (menuable == object)
end

#copy_parent_menuObject



97
98
99
# File 'app/models/no_cms/menus/menu_item.rb', line 97

def copy_parent_menu
  self.menu = parent.menu unless parent.nil?
end

#draftObject



111
112
113
# File 'app/models/no_cms/menus/menu_item.rb', line 111

def draft
  menu_kind[:hidden] ? true : translation.draft
end

#draft?Boolean

Returns:

  • (Boolean)


115
116
117
# File 'app/models/no_cms/menus/menu_item.rb', line 115

def draft?
  draft
end


93
94
95
# File 'app/models/no_cms/menus/menu_item.rb', line 93

def menu_kind
  NoCms::Menus.menu_kinds[self.kind]
end

#positionObject



89
90
91
# File 'app/models/no_cms/menus/menu_item.rb', line 89

def position
  self[:position] || 0
end

#route_setObject



85
86
87
# File 'app/models/no_cms/menus/menu_item.rb', line 85

def route_set
  menu_kind[:route_set]
end

#set_leaf_with_draftObject



101
102
103
104
105
106
107
108
109
# File 'app/models/no_cms/menus/menu_item.rb', line 101

def set_leaf_with_draft
  translations.each do |translation|
    I18n.with_locale(translation.locale) do
      previous_leaf_flag = translation.leaf_with_draft
      translation.update_column :leaf_with_draft, !draft && (leaf? || !descendants.no_drafts.exists?)
      self.parent.set_leaf_with_draft unless root? || (previous_leaf_flag == self.leaf_with_draft)
    end
  end
end

#url_forObject



71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'app/models/no_cms/menus/menu_item.rb', line 71

def url_for
  case
    when !menu_kind[:object_class].nil?
      menuable.respond_to?(:path) ? menuable.path : menuable
    when !menu_kind[:action].nil?
      controller, action = menu_action.split('#')
      # When we are inside an engine we need to prepend '/' to the controller, or else it would search the controller in the current engine
      locale = NoCms::Menus.localize_urls ? {locale: I18n.locale.to_s} : {}
      { controller: "/#{controller}", action: action }.merge locale
    else
      external_url
  end
end