Class: BaseEditingBootstrap::Searches::Base

Inherits:
Object
  • Object
show all
Includes:
ActiveModel::Conversion, ActiveModel::Naming
Defined in:
lib/base_editing_bootstrap/searches/base.rb

Overview

PORO per la gestione dei metodi associati alla ricerca.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(scope, user, params: {page: nil}, sorts: ["id"], distinct: true) ⇒ Base

Returns a new instance of Base.

Parameters:

  • user (User)
  • scope (ActiveRecord::Associations::CollectionProxy)
  • sort (Array<String (frozen)>)


15
16
17
18
19
20
21
22
# File 'lib/base_editing_bootstrap/searches/base.rb', line 15

def initialize(scope, user, params: {page: nil}, sorts: ["id"], distinct: true)
  @model_klass = scope.klass
  @user = user
  @scope = scope
  @params = params
  @sorts = sorts
  @distinct = distinct
end

Instance Attribute Details

#distinctObject (readonly)

Returns the value of attribute distinct.



10
11
12
# File 'lib/base_editing_bootstrap/searches/base.rb', line 10

def distinct
  @distinct
end

#model_klassObject (readonly)

Returns the value of attribute model_klass.



10
11
12
# File 'lib/base_editing_bootstrap/searches/base.rb', line 10

def model_klass
  @model_klass
end

#paramsObject (readonly)

Returns the value of attribute params.



10
11
12
# File 'lib/base_editing_bootstrap/searches/base.rb', line 10

def params
  @params
end

#scopeObject (readonly)

Returns the value of attribute scope.



10
11
12
# File 'lib/base_editing_bootstrap/searches/base.rb', line 10

def scope
  @scope
end

#sortsObject (readonly)

Returns the value of attribute sorts.



10
11
12
# File 'lib/base_editing_bootstrap/searches/base.rb', line 10

def sorts
  @sorts
end

#userObject (readonly)

Returns the value of attribute user.



10
11
12
# File 'lib/base_editing_bootstrap/searches/base.rb', line 10

def user
  @user
end

Instance Method Details

#persisted?Boolean

Returns:

  • (Boolean)


54
55
56
# File 'lib/base_editing_bootstrap/searches/base.rb', line 54

def persisted?
  false
end

#policyApplicationPolicy

Returns:

  • (ApplicationPolicy)


60
61
62
# File 'lib/base_editing_bootstrap/searches/base.rb', line 60

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

#ransack_queryObject



35
36
37
38
# File 'lib/base_editing_bootstrap/searches/base.rb', line 35

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

#resultsObject

Risultato della ricerca, fa da pipeline verso ransack Impostando il sort nel caso in cui non sia già stato impostato da ransack



27
28
29
30
31
32
33
# File 'lib/base_editing_bootstrap/searches/base.rb', line 27

def results
  ransack_query
    .tap { |r| r.sorts = @sorts if r.sorts.empty? }
    .result(distinct: @distinct)
    .tap { |q| Rails.logger.debug { "[Ransack] params:#{params} - sql: #{q.to_sql}" } }
    .page(params[:page])
end

#search_fieldsObject



40
41
42
# File 'lib/base_editing_bootstrap/searches/base.rb', line 40

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

#search_result_fieldsObject



44
45
46
# File 'lib/base_editing_bootstrap/searches/base.rb', line 44

def search_result_fields
  policy.search_result_fields
end

#sortable?(field) ⇒ Boolean

Ritorna se il campo deve essere ordinabile o meno

Returns:

  • (Boolean)


50
51
52
# File 'lib/base_editing_bootstrap/searches/base.rb', line 50

def sortable?(field)
  policy.sortable_search_result_fields.include?(field)
end