Sha256: 653734c6a0d84fe2ffd7d87ced234c1e2f1f7c04bf23e8f929bed72f9ca01bf8

Contents?: true

Size: 1.79 KB

Versions: 5

Compression:

Stored size: 1.79 KB

Contents

require 'active_admin/helpers/collection'
require 'active_admin/view_helpers/method_or_proc_helper'

module ActiveAdmin
  module Views

    # Renders a collection of ActiveAdmin::Scope objects as a
    # simple list with a seperator
    class Scopes < ActiveAdmin::Component
      builder_method :scopes_renderer

      include ActiveAdmin::ScopeChain
      include ::ActiveAdmin::Helpers::Collection


      def default_class_name
        "scopes table_tools_segmented_control"
      end

      def tag_name
        'ul'
      end

      def build(scopes, options = {})
        scopes.each do |scope|
          build_scope(scope, options) if call_method_or_proc_on(self, scope.display_if_block)
        end
      end

      protected

      def build_scope(scope, options)
        li :class => classes_for_scope(scope) do
          scope_name = I18n.t "active_admin.scopes.#{scope.id}", default: scope.name
          params     = request.query_parameters.except :page, :scope, :commit, :format

          a href: url_for(scope: scope.id, params: params), class: 'table_tools_button' do
            text_node scope_name
            span class: 'count' do
              "(#{get_scope_count(scope)})"
            end if options[:scope_count] && scope.show_count
          end
        end
      end

      def classes_for_scope(scope)
        classes = ["scope", scope.id]
        classes << "selected" if current_scope?(scope)
        classes.join(" ")
      end

      def current_scope?(scope)
        if params[:scope]
          params[:scope] == scope.id
        else
          active_admin_config.default_scope(self) == scope
        end
      end

      # Return the count for the scope passed in.
      def get_scope_count(scope)
        collection_size(scope_chain(scope, collection_before_scope))
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
yousty-activeadmin-1.0.4.pre lib/active_admin/views/components/scopes.rb
yousty-activeadmin-1.0.3.pre lib/active_admin/views/components/scopes.rb
yousty-activeadmin-1.0.2.pre lib/active_admin/views/components/scopes.rb
yousty-activeadmin-1.0.1.pre lib/active_admin/views/components/scopes.rb
yousty-activeadmin-1.0.0.pre lib/active_admin/views/components/scopes.rb