Class: BaseEditingBootstrap::Searches::Base
- Inherits:
-
Object
- Object
- BaseEditingBootstrap::Searches::Base
- 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
-
#distinct ⇒ Object
readonly
Returns the value of attribute distinct.
-
#model_klass ⇒ Object
readonly
Returns the value of attribute model_klass.
-
#params ⇒ Object
readonly
Returns the value of attribute params.
-
#scope ⇒ Object
readonly
Returns the value of attribute scope.
-
#sorts ⇒ Object
readonly
Returns the value of attribute sorts.
-
#user ⇒ Object
readonly
Returns the value of attribute user.
Instance Method Summary collapse
-
#initialize(scope, user, params: {page: nil}, sorts: ["id"], distinct: true) ⇒ Base
constructor
A new instance of Base.
- #persisted? ⇒ Boolean
- #policy ⇒ ApplicationPolicy
- #ransack_query ⇒ Object
-
#results ⇒ Object
Risultato della ricerca, fa da pipeline verso ransack Impostando il sort nel caso in cui non sia già stato impostato da ransack.
- #search_fields ⇒ Object
- #search_result_fields ⇒ Object
-
#sortable?(field) ⇒ Boolean
Ritorna se il campo deve essere ordinabile o meno.
Constructor Details
#initialize(scope, user, params: {page: nil}, sorts: ["id"], distinct: true) ⇒ Base
Returns a new instance of Base.
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
#distinct ⇒ Object (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_klass ⇒ Object (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 |
#params ⇒ Object (readonly)
Returns the value of attribute params.
10 11 12 |
# File 'lib/base_editing_bootstrap/searches/base.rb', line 10 def params @params end |
#scope ⇒ Object (readonly)
Returns the value of attribute scope.
10 11 12 |
# File 'lib/base_editing_bootstrap/searches/base.rb', line 10 def scope @scope end |
#sorts ⇒ Object (readonly)
Returns the value of attribute sorts.
10 11 12 |
# File 'lib/base_editing_bootstrap/searches/base.rb', line 10 def sorts @sorts end |
#user ⇒ Object (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
54 55 56 |
# File 'lib/base_editing_bootstrap/searches/base.rb', line 54 def persisted? false end |
#policy ⇒ ApplicationPolicy
60 61 62 |
# File 'lib/base_editing_bootstrap/searches/base.rb', line 60 def policy Pundit.policy(@user, @model_klass) end |
#ransack_query ⇒ Object
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 |
#results ⇒ Object
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_fields ⇒ Object
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_fields ⇒ Object
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
50 51 52 |
# File 'lib/base_editing_bootstrap/searches/base.rb', line 50 def sortable?(field) policy.sortable_search_result_fields.include?(field) end |