module RulesEngine module Rule class <%=rule_class%> < RulesEngine::Rule::Definition attr_reader :plan_code ################################################################## # class options self.options = { :group => 'Workflow', :display_name => 'Plan Start', :help_partial => '/re_rules/<%=rule_name%>/help', :new_partial => '/re_rules/<%=rule_name%>/new', :edit_partial => '/re_rules/<%=rule_name%>/edit' } ################################################################## # set the rule data def data= data if data.nil? @title = nil @plan_code = nil else @title, @plan_code = ActiveSupport::JSON.decode(data) end end ################################################################## # get the rule attributes def title @title end def summary "Run the plan : #{plan_code}" end def data [title, plan_code].to_json end def expected_outcomes [ {:outcome => RulesEngine::Rule::Outcome::NEXT}, {:outcome => RulesEngine::Rule::Outcome::STOP_FAILURE, :title => "Stop if plan fails"} ] end ################################################################## # set the rule attributes def attributes=(params) param_hash = params.symbolize_keys @title = param_hash[:<%=rule_name%>_title] @plan_code = param_hash[:<%=rule_name%>_plan_code] end ################################################################## # validation and errors def valid? @errors = {} @errors[:<%=rule_name%>_title] = "Title required" if title.blank? @errors[:<%=rule_name%>_plan_code] = "Plan code required" if plan_code.blank? return @errors.empty? end ################################################################## # callbacks when the rule is added and removed from a workflow def before_create() end def before_update() end def before_destroy() end ################################################################## # execute the rule def process(process_id, plan, data) sub_process_id = RulesEngine::Process.runner.create sub_plan = RulesEngine::Publish.publisher.get(plan_code) RulesEngine::Process.auditor.audit(process_id, "Starting Sub Plan #{sub_process_id}", RulesEngine::Process::AUDIT_INFO) success = RulesEngine::Process.runner.run_plan(sub_process_id, sub_plan, data) RulesEngine::Process.auditor.audit(process_id, "Finished Sub Plan #{sub_process_id}", success ? RulesEngine::Process::AUDIT_SUCCESS : RulesEngine::Process::AUDIT_FAILURE) return success ? RulesEngine::Rule::Outcome.new(RulesEngine::Rule::Outcome::NEXT) : RulesEngine::Rule::Outcome.new(RulesEngine::Rule::Outcome::STOP_FAILURE) end end end end