Class: FiltrosPersonalizados::Conversor
- Inherits:
-
Object
- Object
- FiltrosPersonalizados::Conversor
- Defined in:
- lib/filtros_personalizados/conversor.rb
Instance Attribute Summary collapse
-
#consulta_convertida ⇒ Object
Returns the value of attribute consulta_convertida.
-
#consulta_personalizada ⇒ Object
Returns the value of attribute consulta_personalizada.
-
#errors ⇒ Object
Returns the value of attribute errors.
-
#outros_filtros_convertido ⇒ Object
Returns the value of attribute outros_filtros_convertido.
Instance Method Summary collapse
- #converte_array_em_hash(campos) ⇒ Object
- #converte_consulta_gerada ⇒ Object
- #converte_nome(nome) ⇒ Object
- #converte_outros_filtros ⇒ Object
- #converte_tipo_selecionado(tipo) ⇒ Object
- #converter ⇒ Object
-
#initialize(consulta_personalizada) ⇒ Conversor
constructor
A new instance of Conversor.
- #modelo_antigo? ⇒ Boolean
- #salva_erro(erro) ⇒ Object
Constructor Details
#initialize(consulta_personalizada) ⇒ Conversor
Returns a new instance of Conversor.
5 6 7 8 9 10 11 12 13 |
# File 'lib/filtros_personalizados/conversor.rb', line 5 def initialize(consulta_personalizada) self.consulta_personalizada = consulta_personalizada self.errors = ActiveModel::Errors.new(self) if modelo_antigo? self.consulta_convertida = converte_consulta_gerada() self.outros_filtros_convertido = converte_outros_filtros() end end |
Instance Attribute Details
#consulta_convertida ⇒ Object
Returns the value of attribute consulta_convertida.
3 4 5 |
# File 'lib/filtros_personalizados/conversor.rb', line 3 def consulta_convertida @consulta_convertida end |
#consulta_personalizada ⇒ Object
Returns the value of attribute consulta_personalizada.
3 4 5 |
# File 'lib/filtros_personalizados/conversor.rb', line 3 def consulta_personalizada @consulta_personalizada end |
#errors ⇒ Object
Returns the value of attribute errors.
3 4 5 |
# File 'lib/filtros_personalizados/conversor.rb', line 3 def errors @errors end |
#outros_filtros_convertido ⇒ Object
Returns the value of attribute outros_filtros_convertido.
3 4 5 |
# File 'lib/filtros_personalizados/conversor.rb', line 3 def outros_filtros_convertido @outros_filtros_convertido end |
Instance Method Details
#converte_array_em_hash(campos) ⇒ Object
87 88 89 90 91 92 93 94 95 96 |
# File 'lib/filtros_personalizados/conversor.rb', line 87 def converte_array_em_hash(campos) hash_convertido = {} campos.each do |campo| key = campo.scan(/^[a-zA-Z_0-9]*\=/).first value = campo.gsub(key, "") key = key[0..-2] hash_convertido[key] = value end return hash_convertido end |
#converte_consulta_gerada ⇒ Object
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/filtros_personalizados/conversor.rb', line 30 def converte_consulta_gerada campos = self.consulta_personalizada.consulta_gerada.to_s campos = campos.split("&") filtros = campos[0].gsub("filtros=", "").split("***") campos.delete_at(0) campos_hash = converte_array_em_hash(campos) auxs = campos_hash["auxs"] auxs = auxs.split("&") auxs_hash = converte_array_em_hash(auxs) select2_hash = {} select2 = converte_array_em_hash(self.consulta_personalizada.select2.split("***")) rescue [] select2.each do |s2| key = s2[0].gsub("filtro_", "") value = JSON.parse(s2[1]).map{|m| m["id"]} select2_hash[key] = value end filtros_novos = [{}] filtros.each do |filtro| key = filtro.scan(/^[a-zA-Z_0-9]*\=/).first value = filtro.gsub(key, "") value = value.split(",") key = key[0..-2] value << auxs_hash[key] unless select2_hash[key].blank? value = [select2_hash[key].join(",")] end tipo_selecionado = URI.decode(campos_hash["filtro_" + key]) # filtros_novos << {key => {"tipo_selecionado" => converte_tipo_selecionado(tipo_selecionado), "values" => value.compact}} filtros_novos.first[converte_nome(key)] = {"tipo_selecionado" => converte_tipo_selecionado(tipo_selecionado), "values" => value.compact} end return filtros_novos rescue Exception => e salva_erro(e..to_s) return self.consulta_personalizada.consulta_gerada end |
#converte_nome(nome) ⇒ Object
129 130 131 132 133 134 135 |
# File 'lib/filtros_personalizados/conversor.rb', line 129 def converte_nome(nome) case nome when "cid_id" then "ciddefinitivo_id" when "tipoacomodacao_id" then "tipoacomod_id" else nome end end |
#converte_outros_filtros ⇒ Object
75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/filtros_personalizados/conversor.rb', line 75 def converte_outros_filtros outros_filtros = self.consulta_personalizada.outros_filtros.to_s outros_filtros = outros_filtros.split("***") outros_filtros_hash = converte_array_em_hash(outros_filtros) return outros_filtros_hash rescue Exception => e salva_erro(e..to_s) return self.consulta_personalizada.outros_filtros end |
#converte_tipo_selecionado(tipo) ⇒ Object
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 |
# File 'lib/filtros_personalizados/conversor.rb', line 109 def converte_tipo_selecionado(tipo) case when (tipo == "==d" or tipo == "==" or tipo == "===") then "Igual a" when tipo == ">" then "Maior que" when tipo == "<" then "Menor que" when (tipo == "><" or tipo == "><h" or tipo == "><n") then "Entre" when tipo == "t-" then "Dias Atrás" when tipo == "t+" then "Dias à Frente" when tipo == "t" then "Hoje" when tipo == "w" then "Essa Semana" when tipo == "m" then "Esse Mês" when tipo == "n" then "Nenhum" when (tipo == "<>" or tipo == "<>=") then "Diferente de" # when tipo == "" then "Todos" when tipo == "!~" then "Não Contém" when tipo == "~" then "Contém" else tipo end end |
#converter ⇒ Object
19 20 21 22 23 24 25 26 27 28 |
# File 'lib/filtros_personalizados/conversor.rb', line 19 def converter if modelo_antigo? self.consulta_personalizada.consulta_gerada = self.consulta_convertida.to_s self.consulta_personalizada.outros_filtros = self.outros_filtros_convertido.to_s self.consulta_personalizada.save end rescue Exception => e salva_erro(e..to_s) return false end |
#modelo_antigo? ⇒ Boolean
15 16 17 |
# File 'lib/filtros_personalizados/conversor.rb', line 15 def modelo_antigo? self.consulta_personalizada.consulta_gerada.to_s[0..6] == 'filtros' end |
#salva_erro(erro) ⇒ Object
98 99 100 101 102 103 104 105 106 |
# File 'lib/filtros_personalizados/conversor.rb', line 98 def salva_erro(erro) self.errors.add(:base, erro) #refaz a busca para pegar o objeto não convertido conversa_erros = self.consulta_personalizada.consulta_personalizada_conversao_erros.build({ erro: erro, objeto: (ConsultaPersonalizada.find(self.consulta_personalizada.id).serializable_hash) }) conversa_erros.save end |