Module: Forcast::Controller::RuleEngine::RuleEngine

Extended by:
ActiveSupport::Concern
Included in:
Api::RulesController
Defined in:
lib/forcast/controllers/controller/rule_engine/rule_engine.rb

Instance Method Summary collapse

Instance Method Details

#combine_rules(rules, rule_type) ⇒ Object



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/controller/rule_engine/rule_engine.rb', line 60

def combine_rules(rules,rule_type)
  puts "combine_rules"
  raise Forcast::Application::Error::General.new(t("invalid_combine_type_rule")) unless Rule.valid_combine_type.include?(@combine_type)
  first_rule = rules.first
  @attr_comparable = "rules"
  @rule_model = first_rule.rule_model
  @rule_model_id = first_rule.rule_model_id
  @rule = rules.pluck(:id)
  @rule_class = "array"
  @rule_type = rule_type
  hash_new = Hash.new
  hash_new["active?"] = false
  hash_new["rule_model"] = @rule_model
  hash_new["rule_model_id"] = @rule_model_id
  hash_new["rule_type"] = @rule_type
  hash_new["rule_class"] = @rule_class
  hash_new["attr_comparable"] = @attr_comparable
  hash_new["combine_type"] = @combine_type
  hash_new["rule"] = @rule.join(',')
  raise Forcast::Application::Error::General.new(t("error_unique_rule")) unless Rule.validate_rule_duplicate(hash_new)
  @rule = Rule.create(hash_new)
  return @rule
end

#compatibles_rulesObject



51
52
53
54
55
56
57
58
# File 'lib/forcast/controllers/controller/rule_engine/rule_engine.rb', line 51

def compatibles_rules
  rules = Rule.where(:id => @rules)
  raise Forcast::Application::Error::General.new(t("some_rule_is_missing")) unless @rules.size == rules.size
  raise Forcast::Application::Error::General.new(t("rules_are_not_compatibles")) if rules.pluck(:rule_model,:rule_model_id).uniq.size > 1
  raise Forcast::Application::Error::General.new(t("rule_can_not_be_comparable")) if rules.pluck(:rule_type).include?("logic_comparable")
  rule_type = "logic_comparable"
  return combine_rules(rules, rule_type)
end

#createObject



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/forcast/controllers/controller/rule_engine/rule_engine.rb', line 10

def create
  unless @rules
    raise Forcast::Application::Error::General.new(t("error_rule_model")) unless validate_model.include?(@rule_model)
    unless params[:class_rule?]
      raise Forcast::Application::Error::General.new(t("error_rule_model_id_doesn't_exist")) unless Rule.validate_model_id(@rule_model,@rule_model_id) 
    end
    raise Forcast::Application::Error::General.new(t("error_rule_model_attr_doesn't_exist")) unless @rule_class = Rule.validate_attr_model(@rule_model,@attr_comparable)
    raise Forcast::Application::Error::General.new(t("error_unique_rule")) unless Rule.validate_rule_duplicate(nil)
    raise Forcast::Application::Error::General.new(t("error_rule_model_rule_type_dont_permit")) unless Rule.rules_type_allowed.include?(@rule_type)
    if @rule_type == 'logic'
      raise Forcast::Application::Error::General.new(t("error_rule_model_rule_dont_permit")) unless Rule.rules_allowed.include?(@rule)
      raise Forcast::Application::Error::General.new(t("error_rule_model_rule_class_dont_permit")) unless Rule.rules_class_allowed.include?(@rule_class)
      raise Forcast::Application::Error::General.new(t("error_rule_model_rule_and_class_dont_permit")) unless Rule.validate_rule_and_class(@rule,@rule_class,params)
      params[:rule_class] = @rule_class
    end
    params[:active?] = true
    super
  else
    json_response(compatibles_rules,"ok")
  end
end

#updateObject



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/forcast/controllers/controller/rule_engine/rule_engine.rb', line 32

def update
  case @variable
  when "value"
    attr_comparable = @model_id.attr_comparable
    rule_model = @model_id.rule_model
    model = rule_model.classify.constantize
    rule_class = model.columns_hash["#{attr_comparable}"].type.to_s
    @variable = Rule.validate_class_update(rule_class)
    params[:variable] = @variable
  when "attr_comparable"
    @attr_comparable = @model_id.attr_comparable
    rule_model = @model_id.rule_model
    raise Forcast::Application::Error::General.new(t("error_rule_model_attr_doesn't_exist")) unless Rule.validate_attr_model(rule_model,@dato)
    @model_id.update_columns(:rule_class => @rule_class)
  else
  end
  super
end