lib/impl/refactorRules.js
function updateFormRules(cb) {

    //Filters out any conditional statements that are no longer valid.
    function filterConditionalStatements(rule) {
      return _.filter(rule.ruleConditionalStatements, function(ruleCondStatement) {
        var sourceField = ruleCondStatement.sourceField.toString();

        if (invalidFields[sourceField]) {
          /**
           * If the user flagged the field in this rule for deletion, flag the rule for deletion.
           */
          if (ruleDeletionFlags[sourceField]) {
            rulesFlaggedForDeletion[rule._id.toString()] = true;
          }
          return false;
        } else {
          return true;
        }
      });
    }

    var updatedFieldRules = _.map(form.fieldRules, function(fieldRule) {
      var filteredConditionalStatements = filterConditionalStatements(fieldRule);
      var filteredTargets = _.filter(fieldRule.targetField, function(targetField) {
        if (invalidFields[targetField]) {
          return false;
        } else {
          return true;
        }
      });

      fieldRule.ruleConditionalStatements = filteredConditionalStatements;
      fieldRule.targetField = filteredTargets;
      return fieldRule;
    });

    var updatedPageRules = _.map(form.pageRules, function(pageRule) {
      pageRule.ruleConditionalStatements = filterConditionalStatements(pageRule);
      return pageRule;
    });

    fieldRulesToDelete = _.filter(updatedFieldRules, function(fieldRule) {
      var targetFields = fieldRule.targetField;
      var conditionalStatements = fieldRule.ruleConditionalStatements;
      var fieldRuleId = fieldRule._id.toString();

      return targetFields.length === 0 || conditionalStatements.length === 0 || rulesFlaggedForDeletion[fieldRuleId];
    });

    pageRulesToDelete = _.filter(updatedPageRules, function(pageRule) {
      var conditionalStatements = pageRule.ruleConditionalStatements;
      var pageRuleId = pageRule._id.toString();

      return conditionalStatements.length === 0 || rulesFlaggedForDeletion[pageRuleId];
    });

    fieldRulesToDelete = _.map(fieldRulesToDelete, function(fieldRule) {
      return fieldRule._id.toString();
    });

    pageRulesToDelete = _.map(pageRulesToDelete, function(pageRule) {
      return pageRule._id.toString();
    });

    //Now have all the rules that need to be deleted, these rules need to be removed from the field and page rules
    updatedFieldRules = _.filter(updatedFieldRules, function(updatedFieldRule) {
      return fieldRulesToDelete.indexOf(updatedFieldRule._id.toString()) === -1;
    });


    updatedPageRules = _.filter(updatedPageRules, function(updatedPageRule) {
      return pageRulesToDelete.indexOf(updatedPageRule._id.toString()) === -1;
    });

    form.fieldRules = updatedFieldRules;
    form.pageRules = updatedPageRules;

    form.save(function(err) {
      return cb(err);
    });
  }
function updateFormRules(cb) {

    //Filters out any conditional statements that are no longer valid.
    function filterConditionalStatements(rule) {
      return _.filter(rule.ruleConditionalStatements, function(ruleCondStatement) {
        var sourceField = ruleCondStatement.sourceField.toString();

        if (invalidFields[sourceField]) {
          /**
           * If the user flagged the field in this rule for deletion, flag the rule for deletion.
           */
          if (ruleDeletionFlags[sourceField]) {
            rulesFlaggedForDeletion[rule._id.toString()] = true;
          }
          return false;
        } else {
          return true;
        }
      });
    }

    var updatedFieldRules = _.map(form.fieldRules, function(fieldRule) {
      var filteredConditionalStatements = filterConditionalStatements(fieldRule);
      var filteredTargets = _.filter(fieldRule.targetField, function(targetField) {
        if (invalidFields[targetField]) {
          return false;
        } else {
          return true;
        }
      });

      fieldRule.ruleConditionalStatements = filteredConditionalStatements;
      fieldRule.targetField = filteredTargets;
      return fieldRule;
    });

    var updatedPageRules = _.map(form.pageRules, function(pageRule) {
      pageRule.ruleConditionalStatements = filterConditionalStatements(pageRule);
      return pageRule;
    });

    fieldRulesToDelete = _.filter(updatedFieldRules, function(fieldRule) {
      var targetFields = fieldRule.targetField;
      var conditionalStatements = fieldRule.ruleConditionalStatements;
      var fieldRuleId = fieldRule._id.toString();

      return targetFields.length === 0 || conditionalStatements.length === 0 || rulesFlaggedForDeletion[fieldRuleId];
    });

    pageRulesToDelete = _.filter(updatedPageRules, function(pageRule) {
      var conditionalStatements = pageRule.ruleConditionalStatements;
      var pageRuleId = pageRule._id.toString();

      return conditionalStatements.length === 0 || rulesFlaggedForDeletion[pageRuleId];
    });

    fieldRulesToDelete = _.map(fieldRulesToDelete, function(fieldRule) {
      return fieldRule._id.toString();
    });

    pageRulesToDelete = _.map(pageRulesToDelete, function(pageRule) {
      return pageRule._id.toString();
    });

    //Now have all the rules that need to be deleted, these rules need to be removed from the field and page rules
    updatedFieldRules = _.filter(updatedFieldRules, function(updatedFieldRule) {
      return fieldRulesToDelete.indexOf(updatedFieldRule._id.toString()) === -1;
    });


    updatedPageRules = _.filter(updatedPageRules, function(updatedPageRule) {
      return pageRulesToDelete.indexOf(updatedPageRule._id.toString()) === -1;
    });

    form.fieldRules = updatedFieldRules;
    form.pageRules = updatedPageRules;

    form.save(function(err) {
      return cb(err);
    });
  }
function updateFormRules ( cb ) { //Filters out any conditional statements that are no longer valid. function filterConditionalStatements ( rule ) { return _ . filter ( rule . ruleConditionalStatements , function ( ruleCondStatement ) { var sourceField = ruleCondStatement . sourceField . toString ( ) ; if ( invalidFields [ sourceField ] ) { /**
           * If the user flagged the field in this rule for deletion, flag the rule for deletion.
           */ if ( ruleDeletionFlags [ sourceField ] ) { rulesFlaggedForDeletion [ rule . _id . toString ( ) ] = true ; } return false ; } else { return true ; } } ) ; } var updatedFieldRules = _ . map ( form . fieldRules , function ( fieldRule ) { var filteredConditionalStatements = filterConditionalStatements ( fieldRule ) ; var filteredTargets = _ . filter ( fieldRule . targetField , function ( targetField ) { if ( invalidFields [ targetField ] ) { return false ; } else { return true ; } } ) ; fieldRule . ruleConditionalStatements = filteredConditionalStatements ; fieldRule . targetField = filteredTargets ; return fieldRule ; } ) ; var updatedPageRules = _ . map ( form . pageRules , function ( pageRule ) { pageRule . ruleConditionalStatements = filterConditionalStatements ( pageRule ) ; return pageRule ; } ) ; fieldRulesToDelete = _ . filter ( updatedFieldRules , function ( fieldRule ) { var targetFields = fieldRule . targetField ; var conditionalStatements = fieldRule . ruleConditionalStatements ; var fieldRuleId = fieldRule . _id . toString ( ) ; return targetFields . length === 0 || conditionalStatements . length === 0 || rulesFlaggedForDeletion [ fieldRuleId ] ; } ) ; pageRulesToDelete = _ . filter ( updatedPageRules , function ( pageRule ) { var conditionalStatements = pageRule . ruleConditionalStatements ; var pageRuleId = pageRule . _id . toString ( ) ; return conditionalStatements . length === 0 || rulesFlaggedForDeletion [ pageRuleId ] ; } ) ; fieldRulesToDelete = _ . map ( fieldRulesToDelete , function ( fieldRule ) { return fieldRule . _id . toString ( ) ; } ) ; pageRulesToDelete = _ . map ( pageRulesToDelete , function ( pageRule ) { return pageRule . _id . toString ( ) ; } ) ; //Now have all the rules that need to be deleted, these rules need to be removed from the field and page rules updatedFieldRules = _ . filter ( updatedFieldRules , function ( updatedFieldRule ) { return fieldRulesToDelete . indexOf ( updatedFieldRule . _id . toString ( ) ) === - 1 ; } ) ; updatedPageRules = _ . filter ( updatedPageRules , function ( updatedPageRule ) { return pageRulesToDelete . indexOf ( updatedPageRule . _id . toString ( ) ) === - 1 ; } ) ; form . fieldRules = updatedFieldRules ; form . pageRules = updatedPageRules ; form . save ( function ( err ) { return cb ( err ) ; } ) ; }
Need to delete any rules that are to be deleted
Need to delete any rules that are to be deleted