Skip to content

Commit

Permalink
Refactor date range validation
Browse files Browse the repository at this point in the history
Fix scss-lint.yml

Add rbenv to gitignore
  • Loading branch information
MusikAnimal committed Aug 21, 2016
1 parent adb64ad commit 7939e36
Show file tree
Hide file tree
Showing 20 changed files with 303 additions and 288 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ log/
tmp/
*.js.map
*.scssc
*.ruby-version
node_modules/
config.php
composer.lock
Expand Down
3 changes: 1 addition & 2 deletions .scss-lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ linters:

Shorthand:
enabled: true
allowed_shorthands: [1, 2, 3]
allowed_shorthands: [1, 2, 3, 4]

SingleLinePerProperty:
enabled: true
Expand Down Expand Up @@ -251,4 +251,3 @@ linters:

Compass::*:
enabled: false

28 changes: 4 additions & 24 deletions javascripts/langviews/langviews.js
Original file line number Diff line number Diff line change
Expand Up @@ -505,36 +505,16 @@ class LangViews extends mix(Pv).with(ChartHelpers, ListHelpers) {
* @returns {null} nothing
*/
popParams() {
let startDate, endDate, params = this.parseQueryString('pages');
let params = this.parseQueryString('pages');

$(this.config.projectInput).val(params.project || this.config.defaults.project);
if (this.validateProject()) return;

this.patchUsage('lv');

/**
* Check if we're using a valid range, and if so ignore any start/end dates.
* If an invalid range, throw and error and use default dates.
*/
if (params.range) {
if (!this.setSpecialRange(params.range)) {
this.addSiteNotice('danger', $.i18n('param-error-3'), $.i18n('invalid-params'), true);
this.setSpecialRange(this.config.defaults.dateRange);
}
} else if (params.start) {
startDate = moment(params.start || moment().subtract(this.config.defaults.daysAgo, 'days'));
endDate = moment(params.end || Date.now());
if (startDate < this.config.minDate || endDate < this.config.minDate) {
this.addSiteNotice('danger', $.i18n('param-error-1', `${$.i18n('july')} 2015`), $.i18n('invalid-params'), true);
return;
} else if (startDate > endDate) {
this.addSiteNotice('warning', $.i18n('param-error-2'), $.i18n('invalid-params'), true);
return;
}
this.daterangepicker.setStartDate(startDate);
this.daterangepicker.setEndDate(endDate);
} else {
this.setSpecialRange(this.config.defaults.dateRange);
// if date range is invalid, remove page from params so we don't process the default date range
if (!this.checkDateRange(params)) {
delete params.page;
}

$(this.config.platformSelector).val(params.platform || 'all-access');
Expand Down
28 changes: 4 additions & 24 deletions javascripts/massviews/massviews.js
Original file line number Diff line number Diff line change
Expand Up @@ -481,7 +481,7 @@ class MassViews extends mix(Pv).with(ChartHelpers, ListHelpers) {
* @returns {null} nothing
*/
popParams() {
let startDate, endDate, params = this.parseQueryString();
let params = this.parseQueryString();

this.patchUsage('mv');

Expand All @@ -499,29 +499,9 @@ class MassViews extends mix(Pv).with(ChartHelpers, ListHelpers) {
);
}

/**
* Check if we're using a valid range, and if so ignore any start/end dates.
* If an invalid range, throw and error and use default dates.
*/
if (params.range) {
if (!this.setSpecialRange(params.range)) {
this.addSiteNotice('danger', $.i18n('param-error-3'), $.i18n('invalid-params'), true);
this.setSpecialRange(this.config.defaults.dateRange);
}
} else if (params.start) {
startDate = moment(params.start || moment().subtract(this.config.defaults.daysAgo, 'days'));
endDate = moment(params.end || Date.now());
if (startDate < moment('2015-08-01') || endDate < moment('2015-08-01')) {
this.addSiteNotice('danger', $.i18n('param-error-1'), $.i18n('invalid-params'), true);
return;
} else if (startDate > endDate) {
this.addSiteNotice('warning', $.i18n('param-error-2'), $.i18n('invalid-params'), true);
return;
}
this.daterangepicker.setStartDate(startDate);
this.daterangepicker.setEndDate(endDate);
} else {
this.setSpecialRange(this.config.defaults.dateRange);
// if date range is invalid, remove target so we don't process the default date range
if (!this.checkDateRange(params)) {
delete params.target;
}

$(this.config.platformSelector).val(params.platform || 'all-access');
Expand Down
31 changes: 2 additions & 29 deletions javascripts/pageviews.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,40 +98,13 @@ class PageViews extends mix(Pv).with(ChartHelpers) {
/** show loading indicator and add error handling for timeouts */
this.startSpinny();

let startDate, endDate, params = this.parseQueryString('pages');
let params = this.parseQueryString('pages');

$(this.config.projectInput).val(params.project || this.config.defaults.project);
if (this.validateProject()) return;

this.patchUsage('pv');

/**
* Check if we're using a valid range, and if so ignore any start/end dates.
* If an invalid range, throw and error and use default dates.
*/
if (params.range) {
if (!this.setSpecialRange(params.range)) {
this.addSiteNotice('danger', $.i18n('param-error-3'), $.i18n('invalid-params'), true);
this.setSpecialRange(this.config.defaults.dateRange);
}
} else if (params.start) {
startDate = moment(params.start || moment().subtract(this.config.defaults.daysAgo, 'days'));
endDate = moment(params.end || Date.now());
if (startDate < this.config.minDate || endDate < this.config.minDate) {
this.addSiteNotice('danger', $.i18n('param-error-1', `${$.i18n('july')} 2015`), $.i18n('invalid-params'), true);
this.resetView();
return;
} else if (startDate > endDate) {
this.addSiteNotice('warning', $.i18n('param-error-2'), $.i18n('invalid-params'), true);
this.resetView();
return;
}
/** directly assign startDate before calling setEndDate so events will be fired once */
this.daterangepicker.startDate = startDate;
this.daterangepicker.setEndDate(endDate);
} else {
this.setSpecialRange(this.config.defaults.dateRange);
}
this.checkDateRange(params);

$(this.config.platformSelector).val(params.platform || 'all-access');
$('#agent-select').val(params.agent || 'user');
Expand Down
28 changes: 4 additions & 24 deletions javascripts/redirectviews/redirectviews.js
Original file line number Diff line number Diff line change
Expand Up @@ -473,36 +473,16 @@ class RedirectViews extends mix(Pv).with(ChartHelpers, ListHelpers) {
* @returns {null} nothing
*/
popParams() {
let startDate, endDate, params = this.parseQueryString('pages');
let params = this.parseQueryString('pages');

$(this.config.projectInput).val(params.project || this.config.defaults.project);
if (this.validateProject()) return;

this.patchUsage('rv');

/**
* Check if we're using a valid range, and if so ignore any start/end dates.
* If an invalid range, throw and error and use default dates.
*/
if (params.range) {
if (!this.setSpecialRange(params.range)) {
this.addSiteNotice('danger', $.i18n('param-error-3'), $.i18n('invalid-params'), true);
this.setSpecialRange(this.config.defaults.dateRange);
}
} else if (params.start) {
startDate = moment(params.start || moment().subtract(this.config.defaults.daysAgo, 'days'));
endDate = moment(params.end || Date.now());
if (startDate < this.config.minDate || endDate < this.config.minDate) {
this.addSiteNotice('danger', $.i18n('param-error-1', `${$.i18n('july')} 2015`), $.i18n('invalid-params'), true);
return;
} else if (startDate > endDate) {
this.addSiteNotice('warning', $.i18n('param-error-2'), $.i18n('invalid-params'), true);
return;
}
this.daterangepicker.setStartDate(startDate);
this.daterangepicker.setEndDate(endDate);
} else {
this.setSpecialRange(this.config.defaults.dateRange);
// if date range is invalid, remove page from params so we don't process the default date range
if (!this.checkDateRange(params)) {
delete params.page;
}

$(this.config.platformSelector).val(params.platform || 'all-access');
Expand Down
37 changes: 37 additions & 0 deletions javascripts/shared/pv.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,43 @@ class Pv extends PvConfig {
);
}

/**
* Check the validity of the date range of given params
* and throw errors as necessary and/or set defaults
* @param {Object} params - as returned by this.parseQueryString()
* @returns {Boolean} true if there were no errors, false otherwise
*/
checkDateRange(params) {
if (params.range) {
if (!this.setSpecialRange(params.range)) {
this.addSiteNotice('danger', $.i18n('param-error-3'), $.i18n('invalid-params'), true);
this.setSpecialRange(this.config.defaults.dateRange);
}
} else if (params.start) {
const startDate = moment(params.start || moment().subtract(this.config.defaults.daysAgo, 'days')),
endDate = moment(params.end || Date.now());

if (startDate < this.config.minDate || endDate < this.config.minDate) {
this.addSiteNotice('danger',
$.i18n('param-error-1', moment(this.config.minDate).format(this.dateFormat)),
$.i18n('invalid-params'),
true
);
return false;
} else if (startDate > endDate) {
this.addSiteNotice('warning', $.i18n('param-error-2'), $.i18n('invalid-params'), true);
return false;
}
/** directly assign startDate before calling setEndDate so events will be fired once */
this.daterangepicker.startDate = startDate;
this.daterangepicker.setEndDate(endDate);
} else {
this.setSpecialRange(this.config.defaults.dateRange);
}

return true;
}

clearSiteNotices() {
$('.site-notice').html('');
}
Expand Down
31 changes: 2 additions & 29 deletions javascripts/siteviews/siteviews.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,37 +58,10 @@ class SiteViews extends mix(Pv).with(ChartHelpers) {
popParams() {
this.startSpinny();

let startDate, endDate, params = this.parseQueryString('sites');
let params = this.parseQueryString('sites');

this.patchUsage('sv');

/**
* Check if we're using a valid range, and if so ignore any start/end dates.
* If an invalid range, throw and error and use default dates.
*/
if (params.range) {
if (!this.setSpecialRange(params.range)) {
this.addSiteNotice('danger', $.i18n('param-error-3'), $.i18n('invalid-params'), true);
this.setSpecialRange(this.config.defaults.dateRange);
}
} else if (params.start) {
startDate = moment(params.start || moment().subtract(this.config.defaults.daysAgo, 'days'));
endDate = moment(params.end || Date.now());
if (startDate < this.config.minDate || endDate < this.config.minDate) {
this.addSiteNotice('danger', $.i18n('param-error-1', `${$.i18n('july')} 2015`), $.i18n('invalid-params'), true);
this.resetView();
return;
} else if (startDate > endDate) {
this.addSiteNotice('warning', $.i18n('param-error-2'), $.i18n('invalid-params'), true);
this.resetView();
return;
}
/** directly assign startDate before calling setEndDate so events will be fired once */
this.daterangepicker.startDate = startDate;
this.daterangepicker.setEndDate(endDate);
} else {
this.setSpecialRange(this.config.defaults.dateRange);
}
this.checkDateRange(params);

$(this.config.dataSourceSelector).val(params.source || 'pageviews');

Expand Down
4 changes: 2 additions & 2 deletions public_html/application.js

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions public_html/langviews/application.js

Large diffs are not rendered by default.

66 changes: 40 additions & 26 deletions public_html/langviews/langviews.js
Original file line number Diff line number Diff line change
Expand Up @@ -645,38 +645,16 @@ var LangViews = function (_mix$with) {
value: function popParams() {
var _this7 = this;

var startDate = void 0,
endDate = void 0,
params = this.parseQueryString('pages');
var params = this.parseQueryString('pages');

$(this.config.projectInput).val(params.project || this.config.defaults.project);
if (this.validateProject()) return;

this.patchUsage('lv');

/**
* Check if we're using a valid range, and if so ignore any start/end dates.
* If an invalid range, throw and error and use default dates.
*/
if (params.range) {
if (!this.setSpecialRange(params.range)) {
this.addSiteNotice('danger', $.i18n('param-error-3'), $.i18n('invalid-params'), true);
this.setSpecialRange(this.config.defaults.dateRange);
}
} else if (params.start) {
startDate = moment(params.start || moment().subtract(this.config.defaults.daysAgo, 'days'));
endDate = moment(params.end || Date.now());
if (startDate < this.config.minDate || endDate < this.config.minDate) {
this.addSiteNotice('danger', $.i18n('param-error-1', $.i18n('july') + ' 2015'), $.i18n('invalid-params'), true);
return;
} else if (startDate > endDate) {
this.addSiteNotice('warning', $.i18n('param-error-2'), $.i18n('invalid-params'), true);
return;
}
this.daterangepicker.setStartDate(startDate);
this.daterangepicker.setEndDate(endDate);
} else {
this.setSpecialRange(this.config.defaults.dateRange);
// if date range is invalid, remove page from params so we don't process the default date range
if (!this.checkDateRange(params)) {
delete params.page;
}

$(this.config.platformSelector).val(params.platform || 'all-access');
Expand Down Expand Up @@ -2426,6 +2404,42 @@ var Pv = function (_PvConfig) {

$('.site-notice').append('<div class=\'alert alert-' + level + dismissable + '\'>' + markup + '</div>');
}

/**
* Check the validity of the date range of given params
* and throw errors as necessary and/or set defaults
* @param {Object} params - as returned by this.parseQueryString()
* @returns {Boolean} true if there were no errors, false otherwise
*/

}, {
key: 'checkDateRange',
value: function checkDateRange(params) {
if (params.range) {
if (!this.setSpecialRange(params.range)) {
this.addSiteNotice('danger', $.i18n('param-error-3'), $.i18n('invalid-params'), true);
this.setSpecialRange(this.config.defaults.dateRange);
}
} else if (params.start) {
var startDate = moment(params.start || moment().subtract(this.config.defaults.daysAgo, 'days')),
endDate = moment(params.end || Date.now());

if (startDate < this.config.minDate || endDate < this.config.minDate) {
this.addSiteNotice('danger', $.i18n('param-error-1', moment(this.config.minDate).format(this.dateFormat)), $.i18n('invalid-params'), true);
return false;
} else if (startDate > endDate) {
this.addSiteNotice('warning', $.i18n('param-error-2'), $.i18n('invalid-params'), true);
return false;
}
/** directly assign startDate before calling setEndDate so events will be fired once */
this.daterangepicker.startDate = startDate;
this.daterangepicker.setEndDate(endDate);
} else {
this.setSpecialRange(this.config.defaults.dateRange);
}

return true;
}
}, {
key: 'clearSiteNotices',
value: function clearSiteNotices() {
Expand Down
4 changes: 2 additions & 2 deletions public_html/massviews/application.js

Large diffs are not rendered by default.

Loading

0 comments on commit 7939e36

Please sign in to comment.