Module: Forcast::Application::Crud

Extended by:
ActiveSupport::Concern
Defined in:
lib/forcast/controllers/application/crud.rb

Instance Method Summary collapse

Instance Method Details

#createObject

Raises:



40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/forcast/controllers/application/crud.rb', line 40

def create
  @create_model = @model.new
  raise Error::General.new(t("bad_params")) if @model.parametros_estan_create?(@params)
  #raise Error::General.new(t("bad_name")) if @model.nombre_existe?(@nombre)
  #raise Error::General.new(t("#{c}.#{a}.bad_custom", param: 'nombre')) if @model.parametro_existe?('nombre',@nombre)
  ## Metodo General para comprobar parametros de creación
  comprobar = @model.parametros_existen_create?(params)
  raise Error::General.new(t("bad_custom", param: comprobar[1])) if comprobar[0]
  comprobar = @model.parametros_a_validar_create(@datos)
  raise Error::General.new(t("bad_params_not_allow_custom", param: t(comprobar[1].to_s))) if comprobar[0]
  @response ||= @create_model
  @create_model.create(params) ? json_response(@response,t("ok")) : (raise Error::General.new(@create_model.errores))
  return @create_model
end

#destroyObject



85
86
87
# File 'lib/forcast/controllers/application/crud.rb', line 85

def destroy
  @model_id.destroy ? json_response(t("ok"))  : (raise Error::General.new(@model_id.errores))
end

#indexObject

model = Metodo que devuelve el objecto ActiveRecord por Nombre del controlador



19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/forcast/controllers/application/crud.rb', line 19

def index
  # buscar_por_nombre if params_present?('nombre')
  # buscar_por_parametro_exacto if params_present?('buscar_por') && params_present?('dato')
  # buscar_por_parametro_contiene if params_present?('buscar_por') && params_present?('contiene')

  #Arreglar HARDCODEO
  #@response = @model.last(@limit) if controller_name == 'alarmas'
  meta = Meta::MetaRelation.new(@model)
  @model = meta.egear_load(@model)
  @response ||= @model.all.limit(@limit).order('id desc').select(*params_permitidos_enviar).map {|e| e.as_json.merge("meta" => meta.has_relation(e)) }
  json_response(@response,t("ok"),200)
end

#showObject



32
33
34
35
36
37
38
# File 'lib/forcast/controllers/application/crud.rb', line 32

def show
  meta = Meta::MetaRelation.new(@model)
  @model = meta.egear_load(@model)
  @response ||= @model_id.attributes.slice(*params_permitidos_enviar)
  @response[:meta] = meta.has_relation(@model_id)
  json_response(@response,t("ok"))
end

#updateObject



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/forcast/controllers/application/crud.rb', line 55

def update
    unless @variable == "all"
      raise Error::General.new(t("bad_param")) unless params_permitidos_update.include?(@variable)
      raise Error::General.new(t("bad_param_exists_custom", param: @variable)) if @model.parametro_existe?(@variable,@dato) && @model.parametros_a_comprobar_si_existen_update.include?(@variable)
      comprobar = @model.validaciones_update(@variable,@dato)
      raise Error::General.new(t("bad_param_not_allow_custom", param: t(comprobar[0].to_s))) if comprobar[1]
      @dato = true  if @dato == 'true'
      @dato = false if @dato == 'false'
      @model_id.update(Hash[@variable, @dato])
      #@model_id.update_columns(Hash[@variable, @dato]) 
    else
      @datos_mod = @datos.without("variable") 
      @datos_mod.keys.each do |key|
        @variable = key
        @dato = @datos_mod[key]
        raise Error::General.new(t("bad_param")) unless params_permitidos_update.include?(@variable)
        raise Error::General.new(t("bad_param_exists_custom", param: @variable)) if @model.parametro_existe?(@variable,@dato) && @model.parametros_a_comprobar_si_existen_update.include?(@variable)
        comprobar = @model.validaciones_update(@variable,@dato)
        raise Error::General.new(t("bad_param_not_allow_custom", param: t(comprobar[0].to_s))) if comprobar[1]
        @dato = true  if @dato == 'true'
        @dato = false if @dato == 'false'
      end
      @model_id.update(@datos_mod)
      #@model_id.update_columns(@datos_mod)               
    end
    @response ||= @model_id.attributes.slice(*params_permitidos_enviar)
    json_response(@response,t("ok"))
end