Code coverage report for src/helpers/policies/utils.coffee

Statements: 94.03% (63 / 67)      Branches: 80% (20 / 25)      Functions: 100% (14 / 14)      Lines: 94.03% (63 / 67)      Ignored: none     

All files » src/helpers/policies/ » utils.coffee
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117               1029× 1029× 1029× 292×   1029×     2510×     4530× 4530× 4530×     3539× 3539× 3539× 3539×     2510× 2510× 2510×         2510× 2510× 2510×   2510× 2510× 2510×       2510× 1029× 1029× 1029×     2510×     2728× 2728× 4116×   2728×     2510× 2510× 2510× 6303× 6303× 1773×   4530× 4530×   2510×     1565× 1565× 3854× 3854× 2728×   3854×       1437× 1437×     1437×     518× 518×     518×     427× 427×     427×          
var CurrentUserStore, DEFAULT, TaskStore, _, policies, utils;
 
_ = require('underscore');
 
policies = require('./policies');
 
TaskStore = require('../../flux/task').TaskStore;
 
CurrentUserStore = require('../../flux/current-user').CurrentUserStore;
 
DEFAULT = 'default';
 
utils = {
  _dueState: function(task) {
    var state;
    state = 'before';
    if ((task.due_at != null) && TaskStore.isTaskPastDue(task.id)) {
      state = 'after';
    }
    return state;
  },
  _role: function() {
    return CurrentUserStore.getViewingCourseRole();
  },
  _checkQuestionFormat: function(task, step, panel) {
    var question;
    question = step.content.questions[0];
    return question.formats.indexOf(panel.name) > -1;
  },
  _getCheckedPolicy: function(task, step, possiblePolicies) {
    var checkFn, state;
    checkFn = "_" + possiblePolicies.check;
    state = utils[checkFn](task, step);
    return possiblePolicies.states[state];
  },
  _getPolicy: function(task, step, policyFor) {
    var checkedPolicy, nestedCheckedPolicy, policy, possiblePolicies, taskType, warning;
    taskType = task.type;
    Iif (policies[taskType] == null) {
      warning = taskType + " policy is missing. Please check src/helpers/policies/policies file. Default " + policyFor + " policy for " + step.type + " being used.";
      console.warn(warning);
      taskType = DEFAULT;
    }
    possiblePolicies = policies[taskType][step.type][policyFor];
    Eif (possiblePolicies["default"] != null) {
      policy = possiblePolicies["default"];
    }
    Eif (possiblePolicies.check) {
      checkedPolicy = utils._getCheckedPolicy(task, step, possiblePolicies);
      Iif (checkedPolicy != null) {
        policy = checkedPolicy;
      }
    }
    if (policy.check) {
      nestedCheckedPolicy = utils._getCheckedPolicy(task, step, policy);
      Eif (nestedCheckedPolicy != null) {
        policy = nestedCheckedPolicy;
      }
    }
    return policy;
  },
  _isPanelPassed: function(step, checks) {
    var panelPassed;
    panelPassed = _.reduce(checks, function(memo, next) {
      return memo && (step[next] != null) && step[next];
    }, true);
    return panelPassed;
  },
  _getPanels: function(task, step) {
    var allPanels, panels;
    allPanels = utils._getPolicy(task, step, 'panels');
    panels = _.filter(allPanels, function(panel) {
      var optionalFn;
      if (!panel.optional) {
        return true;
      }
      optionalFn = "_" + panel.optional;
      return utils[optionalFn](task, step, panel);
    });
    return panels;
  },
  _arePanelsPassed: function(task, step, panels) {
    var panelsWithPass;
    return panelsWithPass = _.map(panels, function(panel) {
      panel.passed = false;
      if (panel.passCheck != null) {
        panel.passed = utils._isPanelPassed(step, panel.passCheck);
      }
      return panel;
    });
  },
  _getPanel: function(panelsWithStatus) {
    var panel;
    panel = _.findWhere(panelsWithStatus, {
      passed: false
    });
    return panel != null ? panel : panel = _.last(panelsWithStatus);
  },
  _canReview: function(panels) {
    var reviewPanel;
    reviewPanel = _.findWhere(panels, {
      canReview: true
    });
    return reviewPanel != null;
  },
  _canWrite: function(panels) {
    var cannotWrite;
    cannotWrite = _.findWhere(panels, {
      canWrite: false
    });
    return !(cannotWrite != null);
  }
};
 
module.exports = utils;