Move code to lib

parent b5962d43
# frozen_string_literal: true
module Sidebars
module ContainerWithHtmlOptions
# The attributes returned from this method
# will be applied to helper methods like
# `link_to` or the div containing the container.
def container_html_options
{
aria: { label: title }
}.merge(extra_container_html_options)
end
# Classes will override mostly this method
# and not `container_html_options`.
def extra_container_html_options
{}
end
# Attributes to pass to the html_options attribute
# in the helper method that sets the active class
# on each element.
def nav_link_html_options
{}
end
def title
raise NotImplementedError
end
# The attributes returned from this method
# will be applied right next to the title,
# for example in the span that renders the title.
def title_html_options
{}
end
def link
raise NotImplementedError
end
end
end
# frozen_string_literal: true
module Sidebars
module HasActiveRoutes
# This method will indicate for which paths or
# controllers, the menu or menu item should
# be set as active.
#
# The returned values are passed to the `nav_link` helper method,
# so the params can be either `path`, `page`, `controller`.
# Param 'action' is not supported.
def active_routes
{}
end
end
end
# frozen_string_literal: true
# This module handles elements in a list. All elements
# must have a different class
module Sidebars
module PositionableList
def add_element(list, element)
list << element
end
def insert_element_before(list, before_element, new_element)
index = index_of(list, before_element)
if index
list.insert(index, new_element)
else
list.unshift(new_element)
end
end
def insert_element_after(list, after_element, new_element)
index = index_of(list, after_element)
if index
list.insert(index + 1, new_element)
else
add_element(list, new_element)
end
end
private
def index_of(list, element)
list.index { |e| e.is_a?(element) }
end
end
end
# frozen_string_literal: true
module Sidebars
module Renderable
# This method will control whether the menu or menu_item
# should be rendered. It will be overriden by specific
# classes.
def render?
true
end
end
end
# frozen_string_literal: true
module Sidebars
module Concerns
module ContainerWithHtmlOptions
# The attributes returned from this method
# will be applied to helper methods like
# `link_to` or the div containing the container.
def container_html_options
{
aria: { label: title }
}.merge(extra_container_html_options)
end
# Classes will override mostly this method
# and not `container_html_options`.
def extra_container_html_options
{}
end
# Attributes to pass to the html_options attribute
# in the helper method that sets the active class
# on each element.
def nav_link_html_options
{}
end
def title
raise NotImplementedError
end
# The attributes returned from this method
# will be applied right next to the title,
# for example in the span that renders the title.
def title_html_options
{}
end
def link
raise NotImplementedError
end
end
end
end
# frozen_string_literal: true
module Sidebars
module Concerns
module HasActiveRoutes
# This method will indicate for which paths or
# controllers, the menu or menu item should
# be set as active.
#
# The returned values are passed to the `nav_link` helper method,
# so the params can be either `path`, `page`, `controller`.
# Param 'action' is not supported.
def active_routes
{}
end
end
end
end
......@@ -4,6 +4,7 @@
# hints for menus. Hints are elements displayed
# when the user hover the menu item.
module Sidebars
module Concerns
module HasHint
def show_hint?
false
......@@ -13,4 +14,5 @@ module Sidebars
{}
end
end
end
end
......@@ -3,6 +3,7 @@
# This module has the necessary methods to show
# sprites or images next to the menu item.
module Sidebars
module Concerns
module HasIcon
def sprite_icon
nil
......@@ -24,4 +25,5 @@ module Sidebars
sprite_icon || image_path
end
end
end
end
......@@ -3,6 +3,7 @@
# This module introduces the logic to show the "pill" element
# next to the menu item, indicating the a count.
module Sidebars
module Concerns
module HasPill
def has_pill?
false
......@@ -18,4 +19,5 @@ module Sidebars
{}
end
end
end
end
# frozen_string_literal: true
# This module handles elements in a list. All elements
# must have a different class
module Sidebars
module Concerns
module PositionableList
def add_element(list, element)
list << element
end
def insert_element_before(list, before_element, new_element)
index = index_of(list, before_element)
if index
list.insert(index, new_element)
else
list.unshift(new_element)
end
end
def insert_element_after(list, after_element, new_element)
index = index_of(list, after_element)
if index
list.insert(index + 1, new_element)
else
add_element(list, new_element)
end
end
private
def index_of(list, element)
list.index { |e| e.is_a?(element) }
end
end
end
end
# frozen_string_literal: true
module Sidebars
module Concerns
module Renderable
# This method will control whether the menu or menu_item
# should be rendered. It will be overriden by specific
# classes.
def render?
true
end
end
end
end
......@@ -6,12 +6,12 @@ module Sidebars
include ::Gitlab::Routing
include GitlabRoutingHelper
include Gitlab::Allowable
include ::Sidebars::HasPill
include ::Sidebars::HasIcon
include ::Sidebars::PositionableList
include ::Sidebars::Renderable
include ::Sidebars::ContainerWithHtmlOptions
include ::Sidebars::HasActiveRoutes
include ::Sidebars::Concerns::HasPill
include ::Sidebars::Concerns::HasIcon
include ::Sidebars::Concerns::PositionableList
include ::Sidebars::Concerns::Renderable
include ::Sidebars::Concerns::ContainerWithHtmlOptions
include ::Sidebars::Concerns::HasActiveRoutes
attr_reader :context
delegate :current_user, :container, to: :@context
......
......@@ -6,11 +6,11 @@ module Sidebars
include ::Gitlab::Routing
include GitlabRoutingHelper
include Gitlab::Allowable
include ::Sidebars::HasIcon
include ::Sidebars::HasHint
include ::Sidebars::Renderable
include ::Sidebars::ContainerWithHtmlOptions
include ::Sidebars::HasActiveRoutes
include ::Sidebars::Concerns::HasIcon
include ::Sidebars::Concerns::HasHint
include ::Sidebars::Concerns::Renderable
include ::Sidebars::Concerns::ContainerWithHtmlOptions
include ::Sidebars::Concerns::HasActiveRoutes
attr_reader :context
......
......@@ -3,7 +3,7 @@
module Sidebars
class Panel
extend ::Gitlab::Utils::Override
include ::Sidebars::PositionableList
include ::Sidebars::Concerns::PositionableList
attr_reader :context, :scope_menu, :hidden_menu
......
......@@ -2,10 +2,10 @@
require 'spec_helper'
RSpec.describe Sidebars::ContainerWithHtmlOptions do
RSpec.describe Sidebars::Concerns::ContainerWithHtmlOptions do
subject do
Class.new do
include Sidebars::ContainerWithHtmlOptions
include Sidebars::Concerns::ContainerWithHtmlOptions
def title
'Foo'
......
......@@ -2,10 +2,10 @@
require 'spec_helper'
RSpec.describe Sidebars::PositionableList do
RSpec.describe Sidebars::Concerns::PositionableList do
subject do
Class.new do
include Sidebars::PositionableList
include Sidebars::Concerns::PositionableList
end.new
end
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment