Sha256: 30ad6c3002e3a8443e1a0a483d9836e072eaf567453ecb8f9ef4a7f88b3a2a59

Contents?: true

Size: 1.88 KB

Versions: 2

Compression:

Stored size: 1.88 KB

Contents

# frozen_string_literal: true

module Kadmin
  class Finder
    class Presenter < Kadmin::Presenter
      # @return [Boolean] true if no results, false otherwise
      def empty?
        return self.results.empty?
      end

      # @return [String] how many resources are being displayed (along with indices)
      def currently_showing
        resource = resource_name.downcase
        displayed = empty? ? 0 : displayed_items

        currently_showing_phrase = "#{displayed} #{resource}"
        currently_showing_phrase = "#{currently_showing_phrase} (#{offset_start} - #{offset_end})" if self.results.size > 1

        return currently_showing_phrase
      end

      # @return [String] human readable, singular/plural form of the finder's model
      def resource_name
        return self.scope.model_name.human(count: displayed_items)
      end

      # @return [String] a description of the current applied filters
      def applied_filters
        applied_filters = ''
        filters = self.filters.reduce([]) do |acc, (name, filter)|
          next(acc) if filter.value.blank?
          acc << %(<strong>#{filter.value}</strong> on <em>#{name}</em>)
        end
        applied_filters = "(filtering: #{filters.join('; ')})" unless filters.empty?

        return applied_filters.html_safe
      end

      # @!group Pager properties

      # @return [Integer] the current number of items displayed for this page
      def displayed_items
        return offset_end - self.pager.offset
      end

      # @return [Integer] the index number of the last item for this page
      def offset_end
        next_page_offset = self.pager.offset_at(self.pager.current_page + 1)
        return [next_page_offset, self.pager.total].min
      end

      # @return [Integer] the index number of the start item for this page
      def offset_start
        return self.pager.offset + 1
      end

      # @!endgroup
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
kadmin-0.7.0 app/components/kadmin/finder/presenter.rb
kadmin-0.6.21 app/components/kadmin/finder/presenter.rb