Module: RulesEngineView::ModelLoader

Defined in:
lib/rules_engine_view/model_loader.rb

Instance Method Summary collapse

Instance Method Details

#re_load_model(model_name, options = {}) ⇒ Object

options

> :parents array of required parent objects

> :validate method on the model to validate against the parent

> :param_id (default = id)

> :find_by (default = find)

> :redirect_path



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/rules_engine_view/model_loader.rb', line 9

def re_load_model(model_name, options = {})
  model = re_find_model(model_name, options)
  
  parent = options[:parents] == nil ? nil : options[:parents].last
  if (model != nil && parent != nil)
    if options[:validate]
      model = nil if (instance_variable_get("@#{parent}") == nil || !model.send(options[:validate], instance_variable_get("@#{parent}")))
    else  
      model = nil if (instance_variable_get("@#{parent}") == nil || (instance_variable_get("@#{parent}").id != model.send("#{parent}_id")))
    end  
  end

  instance_variable_set("@#{model_name}", model) 
          
  if model == nil
    flash[:error] = "Unable to load #{model_name.to_s.camelcase}."
    redirect_path = model_index_path(model_name, options)
    respond_to do |format|  
      format.html { redirect_to(redirect_path) }
      format.js   { render :text => "window.location.href = '#{redirect_path}';" }  
    end  
  end
end