Sha256: b394a20a8253ec758f87ec1e74d30d6b77d7fe7c521f461f579d5c36636e8926

Contents?: true

Size: 1.56 KB

Versions: 10

Compression:

Stored size: 1.56 KB

Contents

require 'showcase'
require 'active_support/core_ext/object/deep_dup'

module Admino
  module Query
    class SortingPresenter < Showcase::Presenter
      def scope_link(scope, *args)
        options = args.extract_options!

        label = args.first || scope_name(scope)

        desc_class = options.delete(:desc_class) { 'is-desc' }
        asc_class = options.delete(:asc_class) { 'is-asc' }

        options = Showcase::Helpers::HtmlOptions.new(options)

        if is_scope_active?(scope)
          options.add_class!(ascending? ? asc_class : desc_class)
        end

        h.link_to label, scope_path(scope), options.to_h
      end

      def scope_path(scope)
        h.request.path + "?" + scope_params(scope).to_query
      end

      def scopes
        @scopes ||= super.map do |scope|
          ScopePresenter.new(scope, self, view_context)
        end
      end

      def scope_params(scope)
        params = ActiveSupport::HashWithIndifferentAccess.new(
          h.request.query_parameters.deep_dup
        )

        if is_scope_active?(scope)
          params.merge!(sorting: scope.to_s, sort_order: ascending? ? 'desc' : 'asc')
        elsif default_scope == scope
          params.merge!(sorting: scope.to_s, sort_order: default_direction.to_s)
        else
          params.merge!(sorting: scope.to_s, sort_order: 'asc')
        end

        params
      end

      def scope_name(scope)
        I18n.t(
          :"#{query_i18n_key}.#{scope}",
          scope: 'query.sorting_scopes',
          default: scope.to_s.titleize.capitalize
        )
      end
    end
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
admino-0.0.22 lib/admino/query/sorting_presenter.rb
admino-0.0.21 lib/admino/query/sorting_presenter.rb
admino-0.0.20 lib/admino/query/sorting_presenter.rb
admino-0.0.19 lib/admino/query/sorting_presenter.rb
admino-0.0.18 lib/admino/query/sorting_presenter.rb
admino-0.0.17 lib/admino/query/sorting_presenter.rb
admino-0.0.16 lib/admino/query/sorting_presenter.rb
admino-0.0.15 lib/admino/query/sorting_presenter.rb
admino-0.0.14 lib/admino/query/sorting_presenter.rb
admino-0.0.13 lib/admino/query/sorting_presenter.rb