Sha256: e97a1f9041686ae564e032f7a659d5e738b563538bb487a1a30aa2a3754bf5b3

Contents?: true

Size: 1.49 KB

Versions: 70

Compression:

Stored size: 1.49 KB

Contents

module WithScopedQueries::Sort
  extend ActiveSupport::Concern

  included do
    class_attribute :sorting_fields, :default_sorting_field, instance_writer: false
  end

  def self.query_by(params, scope, klass)
    sort_param = params[:sort] || klass.default_sorting_field.to_s
    normalized_params = normalize_params(sort_param)
    if sort_param.present? && klass.sorting_params_allowed?(*normalized_params)
      sort_method_for(klass, scope, *normalized_params)
    else
      scope
    end
  end

  def self.normalize_params(param)
    param&.split(/_(?!.*_)/)
  end

  def self.add_queriable_attributes_to(klass, attributes)
    klass.default_sorting_field = attributes.extract_options![:default]
    klass.sorting_fields = attributes
    klass.queriable_attributes.merge!(sort: :sort)
  end

  def self.sort_method_for(klass, scope, field, direction)
    if klass.column_names.include? field
      scope.public_send(:reorder, "#{klass.table_name}.#{field} #{direction}")
    else
      scope.public_send("order_by_#{field}", direction)
    end
  end

  class_methods do
    def opposite(direction)
      dir = direction.to_s.downcase.to_sym
      [:asc, :desc].find { |it| it != dir }
    end

    def sorting_filters
      sorting_fields.product([:asc, :desc]).map do |it|
        "#{it.first}_#{it.second}"
      end
    end

    def sorting_params_allowed?(sort_param, direction_param = nil)
      sorting_fields.include?(sort_param.to_sym) && [:asc, :desc].include?(direction_param&.to_sym)
    end
  end
end

Version data entries

70 entries across 70 versions & 2 rubygems

Version Path
mumuki-domain-9.23.0 app/models/concerns/with_scoped_queries/sort.rb
mumuki-domain-9.22.1 app/models/concerns/with_scoped_queries/sort.rb
mumuki-domain-9.22.0 app/models/concerns/with_scoped_queries/sort.rb
mumuki-domain-9.21.0 app/models/concerns/with_scoped_queries/sort.rb
mumuki-domain-9.20.0 app/models/concerns/with_scoped_queries/sort.rb
mumuki-domain-9.19.0 app/models/concerns/with_scoped_queries/sort.rb
mumuki-domain-9.18.0 app/models/concerns/with_scoped_queries/sort.rb
mumuki-domain-9.17.0 app/models/concerns/with_scoped_queries/sort.rb
mumuki-domain-9.16.0 app/models/concerns/with_scoped_queries/sort.rb
mumuki-domain-9.15.0 app/models/concerns/with_scoped_queries/sort.rb
mumuki-domain-9.14.1 app/models/concerns/with_scoped_queries/sort.rb
mumuki-domain-9.14.0 app/models/concerns/with_scoped_queries/sort.rb
mumuki-domain-9.13.1 app/models/concerns/with_scoped_queries/sort.rb
mumuki-domain-9.13.0 app/models/concerns/with_scoped_queries/sort.rb
mumuki-domain-9.12.0 app/models/concerns/with_scoped_queries/sort.rb
mumuki-domain-9.11.0 app/models/concerns/with_scoped_queries/sort.rb
mumuki-domain-9.10.0 app/models/concerns/with_scoped_queries/sort.rb
mumuki-domain-9.9.0 app/models/concerns/with_scoped_queries/sort.rb
mumuki-domain-9.8.1 app/models/concerns/with_scoped_queries/sort.rb
mumuki-domain-9.8.0 app/models/concerns/with_scoped_queries/sort.rb