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, display_action_column: true) ⇒ Base

Returns a new instance of Base.

Parameters:

  • user (User)
  • scope (ActiveRecord::Associations::CollectionProxy)
  • sort (Array<String (frozen)>)
  • display_action_column (TrueClass, FalseClass) (defaults to: true)

    > visualizzare o meno la colonna delle azioni



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

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

Instance Attribute Details

#display_action_columnObject (readonly)

Returns the value of attribute display_action_column.



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

def display_action_column
  @display_action_column
end

#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)


56
57
58
# File 'lib/base_editing_bootstrap/searches/base.rb', line 56

def persisted?
  false
end

#policyApplicationPolicy

Returns:

  • (ApplicationPolicy)


62
63
64
# File 'lib/base_editing_bootstrap/searches/base.rb', line 62

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

#ransack_queryObject



37
38
39
40
# File 'lib/base_editing_bootstrap/searches/base.rb', line 37

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



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

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



42
43
44
# File 'lib/base_editing_bootstrap/searches/base.rb', line 42

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

#search_result_fieldsObject



46
47
48
# File 'lib/base_editing_bootstrap/searches/base.rb', line 46

def search_result_fields
  policy.search_result_fields
end

#sortable?(field) ⇒ Boolean

Ritorna se il campo deve essere ordinabile o meno

Returns:

  • (Boolean)


52
53
54
# File 'lib/base_editing_bootstrap/searches/base.rb', line 52

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