Skip to content

Commit 922304d

Browse files
committed
Add Ember Suave and fix all errors
1 parent 4f9d8a4 commit 922304d

39 files changed

+720
-745
lines changed

.jscsrc

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"preset": "ember-suave"
3+
}

addon/errors.js

+9-5
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
import Ember from 'ember';
22

3-
var get = Ember.get;
4-
var set = Ember.set;
3+
const {
4+
A: emberArray,
5+
Object: EmberObject,
6+
get,
7+
set
8+
} = Ember;
59

6-
export default Ember.Object.extend({
7-
unknownProperty: function(property) {
8-
set(this, property, Ember.A());
10+
export default EmberObject.extend({
11+
unknownProperty(property) {
12+
set(this, property, emberArray());
913
return get(this, property);
1014
}
1115
});

addon/index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@ import Mixin from 'ember-validations/mixin';
22

33
export default Mixin;
44
export function validator(callback) {
5-
return { callback: callback };
5+
return { callback };
66
}

addon/messages.js

+32-27
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,45 @@
11
import Ember from 'ember';
22

3+
const { I18n } = Ember;
4+
35
export default {
4-
render: function(attribute, context) {
5-
if (Ember.I18n) {
6-
return Ember.I18n.t('errors.' + attribute, context);
6+
render(attribute, context) {
7+
if (I18n) {
8+
return I18n.t(`errors.${attribute}`, context);
79
} else {
8-
var regex = new RegExp("{{(.*?)}}"),
9-
attributeName = "";
10+
let regex = new RegExp('{{(.*?)}}');
11+
let attributeName = '';
12+
1013
if (regex.test(this.defaults[attribute])) {
1114
attributeName = regex.exec(this.defaults[attribute])[1];
1215
}
16+
1317
return this.defaults[attribute].replace(regex, context[attributeName]);
1418
}
1519
},
20+
1621
defaults: {
17-
inclusion: "is not included in the list",
18-
exclusion: "is reserved",
19-
invalid: "is invalid",
20-
confirmation: "doesn't match {{attribute}}",
21-
accepted: "must be accepted",
22-
empty: "can't be empty",
23-
blank: "can't be blank",
24-
present: "must be blank",
25-
tooLong: "is too long (maximum is {{count}} characters)",
26-
tooShort: "is too short (minimum is {{count}} characters)",
27-
wrongLength: "is the wrong length (should be {{count}} characters)",
28-
notANumber: "is not a number",
29-
notAnInteger: "must be an integer",
30-
greaterThan: "must be greater than {{count}}",
31-
greaterThanOrEqualTo: "must be greater than or equal to {{count}}",
32-
equalTo: "must be equal to {{count}}",
33-
lessThan: "must be less than {{count}}",
34-
lessThanOrEqualTo: "must be less than or equal to {{count}}",
35-
otherThan: "must be other than {{count}}",
36-
odd: "must be odd",
37-
even: "must be even",
38-
url: "is not a valid URL"
22+
inclusion: 'is not included in the list',
23+
exclusion: 'is reserved',
24+
invalid: 'is invalid',
25+
confirmation: 'doesn\'t match {{attribute}}',
26+
accepted: 'must be accepted',
27+
empty: 'can\'t be empty',
28+
blank: 'can\'t be blank',
29+
present: 'must be blank',
30+
tooLong: 'is too long (maximum is {{count}} characters)',
31+
tooShort: 'is too short (minimum is {{count}} characters)',
32+
wrongLength: 'is the wrong length (should be {{count}} characters)',
33+
notANumber: 'is not a number',
34+
notAnInteger: 'must be an integer',
35+
greaterThan: 'must be greater than {{count}}',
36+
greaterThanOrEqualTo: 'must be greater than or equal to {{count}}',
37+
equalTo: 'must be equal to {{count}}',
38+
lessThan: 'must be less than {{count}}',
39+
lessThanOrEqualTo: 'must be less than or equal to {{count}}',
40+
otherThan: 'must be other than {{count}}',
41+
odd: 'must be odd',
42+
even: 'must be even',
43+
url: 'is not a valid URL'
3944
}
4045
};

addon/mixin.js

+106-66
Original file line numberDiff line numberDiff line change
@@ -3,37 +3,50 @@ import Errors from 'ember-validations/errors';
33
import Base from 'ember-validations/validators/base';
44
import getOwner from 'ember-getowner-polyfill';
55

6-
var get = Ember.get;
7-
var set = Ember.set;
8-
9-
var setValidityMixin = Ember.Mixin.create({
10-
isValid: Ember.computed('[email protected]', function() {
11-
var compactValidators = get(this, 'validators').compact();
12-
var filteredValidators = compactValidators.filter(function(validator) {
13-
return !get(validator, 'isValid');
14-
});
6+
const {
7+
A: emberArray,
8+
ArrayProxy,
9+
Mixin,
10+
RSVP: { all, reject },
11+
computed,
12+
computed: { alias, not },
13+
get,
14+
inject: { service },
15+
isArray,
16+
isNone,
17+
isPresent,
18+
set,
19+
warn
20+
} = Ember;
21+
22+
const setValidityMixin = Mixin.create({
23+
isValid: computed('[email protected]', function() {
24+
let compactValidators = get(this, 'validators').compact();
25+
let filteredValidators = compactValidators.filter((validator) => !get(validator, 'isValid'));
1526

1627
return get(filteredValidators, 'length') === 0;
1728
}),
18-
isInvalid: Ember.computed.not('isValid')
29+
30+
isInvalid: not('isValid')
1931
});
2032

21-
var pushValidatableObject = function(model, property) {
22-
var content = get(model, property);
33+
const pushValidatableObject = function(model, property) {
34+
let content = get(model, property);
2335

2436
model.removeObserver(property, pushValidatableObject);
25-
if (Ember.isArray(content)) {
26-
model.validators.pushObject(ArrayValidatorProxy.create({model: model, property: property, contentBinding: 'model.' + property}));
37+
38+
if (isArray(content)) {
39+
model.validators.pushObject(ArrayValidatorProxy.create({ model, property, contentBinding: `model.${property}` }));
2740
} else {
2841
model.validators.pushObject(content);
2942
}
3043
};
3144

32-
var lookupValidator = function(validatorName) {
33-
var owner = getOwner(this);
34-
var service = owner.lookup('service:validations');
35-
var validators = [];
36-
var cache;
45+
const lookupValidator = function(validatorName) {
46+
let owner = getOwner(this);
47+
let service = get(this, 'validationService');
48+
let validators = [];
49+
let cache;
3750

3851
if (service) {
3952
cache = get(service, 'cache');
@@ -44,19 +57,19 @@ var lookupValidator = function(validatorName) {
4457
if (cache[validatorName]) {
4558
validators = validators.concat(cache[validatorName]);
4659
} else {
47-
var local = owner.resolveRegistration('validator:local/' + validatorName);
48-
var remote = owner.resolveRegistration('validator:remote/' + validatorName);
60+
let local = owner.resolveRegistration(`validator:local/${validatorName}`);
61+
let remote = owner.resolveRegistration(`validator:remote/${validatorName}`);
4962

5063
if (local || remote) {
5164
validators = validators.concat([local, remote]);
5265
} else {
53-
var base = owner.resolveRegistration('validator:'+validatorName);
66+
let base = owner.resolveRegistration(`validator:${validatorName}`);
5467

5568
if (base) {
5669
validators = validators.concat([base]);
5770
} else {
58-
local = owner.resolveRegistration('ember-validations@validator:local/'+validatorName);
59-
remote = owner.resolveRegistration('ember-validations@validator:remote/'+validatorName);
71+
local = owner.resolveRegistration(`ember-validations@validator:local/${validatorName}`);
72+
remote = owner.resolveRegistration(`ember-validations@validator:remote/${validatorName}`);
6073

6174
if (local || remote) {
6275
validators = validators.concat([local, remote]);
@@ -67,46 +80,64 @@ var lookupValidator = function(validatorName) {
6780
cache[validatorName] = validators;
6881
}
6982

70-
Ember.warn('Could not find the "'+validatorName+'" validator.', !Ember.isEmpty(validators), {id: 'ember-validations.faild-to-find-validator'});
83+
warn(`Could not find the "${validatorName}" validator.`, isPresent(validators), {
84+
id: 'ember-validations.faild-to-find-validator'
85+
});
7186

7287
return validators;
7388
};
7489

75-
var ArrayValidatorProxy = Ember.ArrayProxy.extend(setValidityMixin, {
76-
validate: function() {
90+
const ArrayValidatorProxy = ArrayProxy.extend(setValidityMixin, {
91+
init() {
92+
this._validate();
93+
},
94+
95+
validate() {
7796
return this._validate();
7897
},
79-
_validate: Ember.on('init', function() {
80-
var promises = get(this, 'content').invoke('_validate').without(undefined);
81-
return Ember.RSVP.all(promises);
82-
}),
83-
validators: Ember.computed.alias('content')
98+
99+
_validate() {
100+
let promises = get(this, 'content').invoke('_validate').without(undefined);
101+
return all(promises);
102+
},
103+
104+
validators: alias('content')
84105
});
85106

86-
export default Ember.Mixin.create(setValidityMixin, {
87-
init: function() {
88-
this._super();
107+
export default Mixin.create(setValidityMixin, {
108+
validationService: service('validations'),
109+
110+
init() {
111+
this._super(...arguments);
89112
this.errors = Errors.create();
90113
this.dependentValidationKeys = {};
91-
this.validators = Ember.A();
114+
this.validators = emberArray();
115+
92116
if (get(this, 'validations') === undefined) {
93117
this.validations = {};
94118
}
119+
95120
this.buildValidators();
96-
this.validators.forEach(function(validator) {
121+
122+
this.validators.forEach((validator) => {
97123
validator.addObserver('errors.[]', this, function(sender) {
98-
var errors = Ember.A();
99-
this.validators.forEach(function(validator) {
124+
let errors = emberArray();
125+
126+
this.validators.forEach((validator) => {
100127
if (validator.property === sender.property) {
101128
errors.addObjects(validator.errors);
102129
}
103-
}, this);
104-
set(this, 'errors.' + sender.property, errors);
130+
});
131+
132+
set(this, `errors.${sender.property}`, errors);
105133
});
106-
}, this);
134+
});
135+
136+
this._validate();
107137
},
108-
buildValidators: function() {
109-
var property;
138+
139+
buildValidators() {
140+
let property;
110141

111142
for (property in this.validations) {
112143
if (this.validations[property].constructor === Object) {
@@ -116,57 +147,66 @@ export default Ember.Mixin.create(setValidityMixin, {
116147
}
117148
}
118149
},
119-
buildRuleValidator: function(property) {
120-
var pushValidator = function(validator) {
150+
151+
buildRuleValidator(property) {
152+
let pushValidator = (validator, validatorName) => {
121153
if (validator) {
122-
this.validators.pushObject(validator.create({model: this, property: property, options: this.validations[property][validatorName]}));
154+
this.validators.pushObject(validator.create({ model: this, property, options: this.validations[property][validatorName] }));
123155
}
124156
};
125157

126158
if (this.validations[property].callback) {
127159
this.validations[property] = { inline: this.validations[property] };
128160
}
129161

130-
var createInlineClass = function(callback) {
162+
let createInlineClass = (callback) => {
131163
return Base.extend({
132-
call: function() {
133-
var errorMessage = this.callback.call(this);
164+
call() {
165+
let errorMessage = this.callback.call(this);
134166

135167
if (errorMessage) {
136168
this.errors.pushObject(errorMessage);
137169
}
138170
},
139-
callback: callback
171+
172+
callback
140173
});
141174
};
142175

143-
for (var validatorName in this.validations[property]) {
176+
Object.keys(this.validations[property]).forEach((validatorName) => {
144177
if (validatorName === 'inline') {
145-
pushValidator.call(this, createInlineClass(this.validations[property][validatorName].callback));
178+
let validator = createInlineClass(this.validations[property][validatorName].callback);
179+
pushValidator(validator, validatorName);
146180
} else if (this.validations[property].hasOwnProperty(validatorName)) {
147-
lookupValidator.call(this, validatorName).forEach(pushValidator, this);
181+
lookupValidator.call(this, validatorName).forEach((validator) => {
182+
return pushValidator.call(this, validator, validatorName);
183+
});
148184
}
149-
}
185+
});
150186
},
151-
buildObjectValidator: function(property) {
152-
if (Ember.isNone(get(this, property))) {
187+
188+
buildObjectValidator(property) {
189+
if (isNone(get(this, property))) {
153190
this.addObserver(property, this, pushValidatableObject);
154191
} else {
155192
pushValidatableObject(this, property);
156193
}
157194
},
158-
validate: function() {
159-
var self = this;
160-
return this._validate().then(function(vals) {
161-
var errors = get(self, 'errors');
195+
196+
validate() {
197+
return this._validate().then((vals) => {
198+
let errors = get(this, 'errors');
199+
162200
if (vals.indexOf(false) > -1) {
163-
return Ember.RSVP.reject(errors);
201+
return reject(errors);
164202
}
203+
165204
return errors;
166205
});
167206
},
168-
_validate: Ember.on('init', function() {
169-
var promises = this.validators.invoke('_validate').without(undefined);
170-
return Ember.RSVP.all(promises);
171-
})
207+
208+
_validate() {
209+
let promises = this.validators.invoke('_validate').without(undefined);
210+
return all(promises);
211+
}
172212
});

addon/patterns.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import Ember from 'ember';
22

3-
export default Ember.Namespace.create({
3+
const { Namespace } = Ember;
4+
5+
export default Namespace.create({
46
numericality: /^(-|\+)?(?:\d+|\d{1,3}(?:,\d{3})+)(?:\.\d*)?$/,
57
blank: /^\s*$/
68
});

0 commit comments

Comments
 (0)