Sha256: c90a423add682c403db2e8bb60b4dfa1a3aff8fdcf2d6171e73426bacbfe5290

Contents?: true

Size: 1.23 KB

Versions: 10

Compression:

Stored size: 1.23 KB

Contents

# frozen_string_literal: true

module BaseEditingBootstrap::Searches
  ##
  # PORO per la gestione dei metodi associati alla ricerca.
  class Base
    include ActiveModel::Naming
    include ActiveModel::Conversion

    attr_reader :model_klass, :user, :params, :scope, :sorts

    # @param [User] user
    # @param [ActiveRecord::Associations::CollectionProxy] scope
    # @param [Array<String (frozen)>] sort
    def initialize(scope, user, params: {page: nil}, sorts: ["id"])
      @model_klass = scope.klass
      @user = user
      @scope = scope
      @params = params
      @sorts = sorts
    end

    ##
    # Risultato della ricerca, fa da pipeline verso ransack
    # Impostando il sort nel caso in cui non sia giĆ  stato impostato da ransack
    def results
      ransack_query.tap { |r|
        r.sorts = @sorts if r.sorts.empty?
      }.result(distinct: true).page(params[:page])
    end

    def ransack_query
      scope
        .ransack(params[:q], auth_object: user)
    end

    def search_fields
      Pundit.policy(@user, @model_klass).search_fields.collect { |f| Field.new(self, f) }
    end

    def search_result_fields
      Pundit.policy(@user, @model_klass).search_result_fields
    end

    def persisted?
      false
    end
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
base_editing_bootstrap-0.16.1 lib/base_editing_bootstrap/searches/base.rb
base_editing_bootstrap-0.16.0 lib/base_editing_bootstrap/searches/base.rb
base_editing_bootstrap-0.15.0 lib/base_editing_bootstrap/searches/base.rb
base_editing_bootstrap-0.14.0 lib/base_editing_bootstrap/searches/base.rb
base_editing_bootstrap-0.13.0 lib/base_editing_bootstrap/searches/base.rb
base_editing_bootstrap-0.12.0 lib/base_editing_bootstrap/searches/base.rb
base_editing_bootstrap-0.11.0 lib/base_editing_bootstrap/searches/base.rb
base_editing_bootstrap-0.10.2 lib/base_editing_bootstrap/searches/base.rb
base_editing_bootstrap-0.10.1 lib/base_editing_bootstrap/searches/base.rb
base_editing_bootstrap-0.10.0 lib/base_editing_bootstrap/searches/base.rb