Sha256: c1ea61152373aa974bc54fcb126a4a43a99e4c7abfa864ab282822c16fd493e6

Contents?: true

Size: 1.99 KB

Versions: 12

Compression:

Stored size: 1.99 KB

Contents

module Pageflow
  module Admin
    class TabsView < ViewComponent
      builder_method :tabs_view

      attr_reader :tabs, :options

      def build(tabs, options = {})
        super(class: 'admin_tabs_view')

        @options = options
        @tabs = filter_tabs(tabs)

        build_tab_list
        build_tab_containers
      end

      private

      def filter_tabs(tabs)
        return tabs unless options[:authorize]

        tabs.select do |tab_options|
          authorized?(:view, tab_options[:component])
        end
      end

      def build_tab_list
        ul(class: 'tabs') do
          tabs.each do |tab_options|
            build_tab_item(tab_options)
          end
        end
      end

      def build_tab_item(tab_options)
        li(class: tab_class(tab_options[:name])) do
          link_to(t(tab_options[:name], scope: options[:i18n]), tab_href(tab_options[:name]))
        end
      end

      def tab_href(name)
        params = request.query_parameters.except(:page, :scope)
        params_string = params.merge(tab: name).to_param
        "?#{params_string}".html_safe
      end

      def build_tab_containers
        tabs.each do |tab_options|
          build_tab_container(tab_options)
        end
      end

      def build_tab_container(tab_options)
        div(class: tab_container_class(tab_options[:name])) do
          insert_tag(tab_options[:component], *options.fetch(:build_args, []))
        end
      end

      def tab_class(tab_name)
        [
          tab_name,
          current_tab?(tab_name) ? 'active' : nil
        ].compact.join(' ')
      end

      def tab_container_class(tab_name)
        [
          'tab_container',
          "#{tab_name}_tab_container",
          current_tab?(tab_name) ? 'active' : nil
        ].compact.join(' ')
      end

      def current_tab?(name)
        name.to_s == current_tab_name.to_s
      end

      def current_tab_name
        options[:current_tab] ||
          request.params[:tab] ||
          tabs.first[:name]
      end
    end
  end
end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
pageflow-0.11.4 app/views/components/pageflow/admin/tabs_view.rb
pageflow-0.11.3 app/views/components/pageflow/admin/tabs_view.rb
pageflow-0.11.2 app/views/components/pageflow/admin/tabs_view.rb
pageflow-0.11.1 app/views/components/pageflow/admin/tabs_view.rb
pageflow-0.11.0 app/views/components/pageflow/admin/tabs_view.rb
pageflow-0.10.0 app/views/components/pageflow/admin/tabs_view.rb
pageflow-0.9.2 app/views/components/pageflow/admin/tabs_view.rb
pageflow-0.9.1 app/views/components/pageflow/admin/tabs_view.rb
pageflow-0.9.0 app/views/components/pageflow/admin/tabs_view.rb
pageflow-0.8.2 app/views/components/pageflow/admin/tabs_view.rb
pageflow-0.8.1 app/views/components/pageflow/admin/tabs_view.rb
pageflow-0.8.0 app/views/components/pageflow/admin/tabs_view.rb