Class: KonoUtils::BaseSearch
- Inherits:
-
ActiveType::Object
- Object
- ActiveType::Object
- KonoUtils::BaseSearch
- Defined in:
- lib/kono_utils/base_search.rb
Overview
Classe base per i form di ricerca nel sistema
Defined Under Namespace
Classes: UndefinedSearchModel, UndefinedSearchModelScope
Instance Attribute Summary collapse
-
#scope ⇒ Object
Returns the value of attribute scope.
-
#search_form_builder ⇒ KonoUtils::SearchFormBuilder
readonly
Istanziato.
Class Method Summary collapse
-
.search_model ⇒ Object
alias method di classe.
-
.set_search_attributes(*attributes) ⇒ Object
Definisce gli attributi da utilizzare per la ricerca passandogli un hash finale si possono passare parametri di default per ognuno dei campi settati a formtastic per renderizzare il campo ES: set_search_attributes :nome,:cognome,:as=>:string renderizzerà nome e cognome come stringhe.
-
.set_search_model(model) ⇒ Object
Definisce per la classe quale modello utilizzare per la ricerca.
Instance Method Summary collapse
-
#data_loaded? ⇒ Boolean
deve indicarmi se i dati della ricerca sono stati inseriti.
-
#get_query_params ⇒ Object
Restituisce un hash con tutti i parametri da implementare sulla ricerca.
-
#initialize(attributes = nil) ⇒ BaseSearch
constructor
A new instance of BaseSearch.
-
#make_query ⇒ Object
Genera la query di ricerca, passando i parametri da ricercare nello scoper di ricerca del modelo.
- #method_missing(m, *args, &block) ⇒ Object
-
#search_attributes ⇒ Object
Attributi di ricerca.
-
#search_model ⇒ Object
Restituisce il modello di ricerca.
-
#set_scope(scope) ⇒ Object
Setta lo scope iniziale del modello.
-
#update_attributes(datas) ⇒ Object
Si occupa di aggiornare i valori interni di ricerca.
Constructor Details
#initialize(attributes = nil) ⇒ BaseSearch
Returns a new instance of BaseSearch.
122 123 124 125 126 127 128 129 |
# File 'lib/kono_utils/base_search.rb', line 122 def initialize(attributes = nil) raise UndefinedSearchModel if search_model.nil? raise UndefinedSearchModelScope.new(search_model) unless search_model.respond_to? :search super self.scope = self.class._search_model search_builder_class = search_form_builder_class || KonoUtils.configuration.search_form_builder @search_form_builder = search_builder_class.new(self) end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(m, *args, &block) ⇒ Object
177 178 179 180 181 182 183 |
# File 'lib/kono_utils/base_search.rb', line 177 def method_missing(m, *args, &block) if self.search_attributes.collect(&:field).include?(m.to_s.gsub(/=$/, '')) self.send(m, *args) else super end end |
Instance Attribute Details
#scope ⇒ Object
Returns the value of attribute scope.
21 22 23 |
# File 'lib/kono_utils/base_search.rb', line 21 def scope @scope end |
#search_form_builder ⇒ KonoUtils::SearchFormBuilder (readonly)
Returns istanziato.
26 27 28 |
# File 'lib/kono_utils/base_search.rb', line 26 def search_form_builder @search_form_builder end |
Class Method Details
.search_model ⇒ Object
alias method di classe
45 46 47 |
# File 'lib/kono_utils/base_search.rb', line 45 def self.search_model self._search_model end |
.set_search_attributes(*attributes) ⇒ Object
Definisce gli attributi da utilizzare per la ricerca passandogli un hash finale si possono passare parametri di default per ognuno dei campi settati a formtastic per renderizzare il campo ES:
set_search_attributes :nome,:cognome,:as=>:string
renderizzerà nome e cognome come stringhe
set_search_attributes :datadinascita, :as=>:datetimepicker
renderizzerà un campo predisposto per attivare il datetimepicker
Possiamo anche passare una Proc come ultimo elemento per la generazione degli attributi per la form,
come argomento è presente il current_user
set_search_attributes :datadinascita, Proc.new { |current_user,form| funzione da lanciare per eseguire la generazione
degli attibuti da passare alla form per generare il campo }
come hash di opzioni possiamo anche passargli una chiave :field_options
con dentro configurazioni vedi TikalCore::SearchAttribute
CALLBACKS Quando vengono creati gli attributi, vengono anche creati gli eventi per ogni attributi, sia per la chiamata del getter (nome_metodo) sia per setter (nome_metodo=), ogni callback ha un prefisso per avere quindi un namespace specifico per questa funzionalità: ES:
set_search_attributes :datadinascita....
genererà:
- before_search_attr_datadinascita
- before_search_attr_datadinascita_set => relativo al setter
- around_search_attr_datadinascita
- around_search_attr_datadinascita_set => relativo al setter
- after_search_attr_datadinascita
- after_search_attr_datadinascita_set => relativo al setter
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/kono_utils/base_search.rb', line 83 def self.set_search_attributes(*attributes) = attributes. = {:as => :string}.merge() if attributes.last.is_a?(Proc) = attributes.pop end self._search_attributes = self._search_attributes || [] attributes.each do |a| attr_accessor(a.to_sym) define_method(a.to_sym) do run_callbacks "search_attr_#{a}" do instance_variable_get "@#{a}".to_sym end end define_method("#{a}=".to_sym) do |*args| run_callbacks "search_attr_#{a}_set" do instance_variable_set "@#{a}".to_sym, *args end end #Definisco delle callbacks per ogni attributo define_model_callbacks "search_attr_#{a}".to_sym, "search_attr_#{a}_set".to_sym self._search_attributes += [SearchAttribute.new(a, )] end self._search_attributes.uniq! end |
.set_search_model(model) ⇒ Object
Definisce per la classe quale modello utilizzare per la ricerca
33 34 35 |
# File 'lib/kono_utils/base_search.rb', line 33 def self.set_search_model(model) self._search_model = model end |
Instance Method Details
#data_loaded? ⇒ Boolean
deve indicarmi se i dati della ricerca sono stati inseriti
134 135 136 |
# File 'lib/kono_utils/base_search.rb', line 134 def data_loaded? get_query_params.length > 0 end |
#get_query_params ⇒ Object
Restituisce un hash con tutti i parametri da implementare sulla ricerca
160 161 162 163 164 165 166 167 |
# File 'lib/kono_utils/base_search.rb', line 160 def get_query_params out = {} search_attributes.each do |val| out[val.field] = self.send(val.field) unless self.send(val.field).blank? end out end |
#make_query ⇒ Object
Genera la query di ricerca, passando i parametri da ricercare nello scoper di ricerca del modelo
150 151 152 153 154 |
# File 'lib/kono_utils/base_search.rb', line 150 def make_query run_callbacks :make_query do self.scope.search(get_query_params) end end |
#search_attributes ⇒ Object
Attributi di ricerca
118 119 120 |
# File 'lib/kono_utils/base_search.rb', line 118 def search_attributes self.class._search_attributes end |
#search_model ⇒ Object
Restituisce il modello di ricerca
39 40 41 |
# File 'lib/kono_utils/base_search.rb', line 39 def search_model self.class._search_model end |
#set_scope(scope) ⇒ Object
Setta lo scope iniziale del modello
140 141 142 143 144 |
# File 'lib/kono_utils/base_search.rb', line 140 def set_scope(scope) run_callbacks :set_scope do self.scope = scope end end |
#update_attributes(datas) ⇒ Object
Si occupa di aggiornare i valori interni di ricerca
171 172 173 174 175 |
# File 'lib/kono_utils/base_search.rb', line 171 def update_attributes(datas) search_attributes.each do |val| self.send("#{val.field}=", val.cast_value(datas[val.field])) end end |