Class: RePlan

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/re_plan.rb

Constant Summary collapse

PLAN_STATUS_DRAFT =
0
PLAN_STATUS_CHANGED =
1
PLAN_STATUS_PUBLISHED =
2
PLAN_STATUS_REVERTED =
3

Instance Method Summary collapse

Instance Method Details

#add_workflow(re_workflow) ⇒ Object



77
78
79
80
81
82
# File 'app/models/re_plan.rb', line 77

def add_workflow re_workflow
  return false if self.re_workflows.include?(re_workflow)
  self.re_workflows << re_workflow 
  changed!
  true
end

#before_create_planObject



20
21
22
# File 'app/models/re_plan.rb', line 20

def before_create_plan
  self.plan_status = PLAN_STATUS_DRAFT
end

#before_save_planObject



24
25
26
27
28
29
30
# File 'app/models/re_plan.rb', line 24

def before_save_plan
  if (plan_status == PLAN_STATUS_REVERTED)
    self.plan_status = PLAN_STATUS_PUBLISHED
  elsif changes.detect { |change| !ignore_attributes.include?(change[0])}  
    self.changed!(false) 
  end  
end

#changed!(update = true) ⇒ Object



120
121
122
123
124
125
# File 'app/models/re_plan.rb', line 120

def changed!(update = true)
  if self.plan_status == PLAN_STATUS_PUBLISHED
    self.plan_status = PLAN_STATUS_CHANGED
    self.save! if update
  end  
end

#code=(new_code) ⇒ Object



32
33
34
# File 'app/models/re_plan.rb', line 32

def code=(new_code)
  self[:code] = new_code.strip.downcase.gsub(/[^a-zA-Z0-9]+/i, '_') if new_code && new_record?
end

#default_workflowObject



105
106
107
# File 'app/models/re_plan.rb', line 105

def default_workflow
  self.re_workflows.empty? ? nil : self.re_workflows[0]
end

#default_workflow=(re_workflow) ⇒ Object



92
93
94
95
96
97
98
99
100
101
102
103
# File 'app/models/re_plan.rb', line 92

def default_workflow= re_workflow
  return if self.re_workflows.empty? || self.re_workflows[0] == re_workflow
  re_plan_workflow = re_plan_workflows.detect { | re_plan_workflow | re_plan_workflow.re_workflow_id == re_workflow.id}
  return unless re_plan_workflow

  re_plan_workflow.move_to_top
  
  self.re_plan_workflows(true)
  self.re_workflows(true)
  
  changed!
end

#plan_errorObject



109
110
111
112
113
# File 'app/models/re_plan.rb', line 109

def plan_error
  return 'workflows req\'d' if re_workflows.empty?
  return 'workflow error' if re_workflows.any? { | re_workflow | re_workflow.workflow_error }    
  nil
end

#publishObject



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'app/models/re_plan.rb', line 36

def publish
  data = { 
    "code" => code, 
    "title" => title, 
    "description" => description,
    "workflow" => re_workflows.empty? ? '' : re_workflows[0].code,
  }
  re_workflows.each_with_index do | re_workflow, index | 
    data["workflow_#{re_workflow.code}"] = re_workflow.publish 

    if re_workflows[-1] == re_workflow
      data["workflow_#{re_workflow.code}"]["next_workflow"] = ''
    else  
      data["workflow_#{re_workflow.code}"]["next_workflow"] = re_workflows[index+1].code
    end  
  end
  data
end

#published!Object



115
116
117
118
# File 'app/models/re_plan.rb', line 115

def published!
  self.plan_status = PLAN_STATUS_PUBLISHED
  self.save!
end

#remove_workflow(re_workflow) ⇒ Object



84
85
86
87
88
89
# File 'app/models/re_plan.rb', line 84

def remove_workflow re_workflow
  return false unless self.re_workflows.include?(re_workflow)
  self.re_workflows.delete(re_workflow)
  changed!
  true
end

#revert!(rule_data) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'app/models/re_plan.rb', line 55

def revert!(rule_data)
  self.plan_status = RePlan::PLAN_STATUS_REVERTED
  self.code = rule_data["code"]
  self.title = rule_data["title"]
  self.description = rule_data["description"]
  
  orig_re_workflows = []
  workflow_data = rule_data["workflow_#{rule_data["workflow"]}"]
  while (workflow_data && orig_re_workflows.length < 500)
    workflow_code = workflow_data["code"]
    re_workflow = ReWorkflow.find_by_code(workflow_code) || ReWorkflow.new
    re_workflow.revert!(workflow_data)
    orig_re_workflows << re_workflow
    
    next_workflow = workflow_data["next_workflow"]
    workflow_data = next_workflow.blank? ? nil : rule_data["workflow_#{next_workflow}"]
  end  
  
  self.re_workflows = orig_re_workflows
  self
end