Skip to content

Commit

Permalink
[BUGFIX] Replace private and deprecated _lookupFactory with public …
Browse files Browse the repository at this point in the history
…& performant `factoryFor` (#442)

[BUGFIX] Replace private and deprecated `_lookupFactory` with public & performant `factoryFor`
  • Loading branch information
cibernox authored and offirgolan committed Jan 24, 2017
1 parent 429647e commit d52dc02
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 2 deletions.
14 changes: 14 additions & 0 deletions addon/-private/factory-for.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/**
Utility to use `ApplicationInstance#factoryFor` if available (2.12+) and
fallback to private `_lookupFactory` otherwise
*/
export default function factoryFor(owner, type) {
let factory;
if (owner.factoryFor) {
let maybeFactory = owner.factoryFor(type);
factory = maybeFactory && maybeFactory.class;
} else {
factory = owner._lookupFactory(type);
}
return factory;
}
3 changes: 2 additions & 1 deletion addon/validations/factory.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import Ember from 'ember';
import flatten from '../utils/flatten';
import assign from '../utils/assign';
import ValidationResult from '../-private/result';
import factoryFor from '../-private/factory-for';
import ResultCollection from './result-collection';
import BaseValidator from '../validators/base';
import cycleBreaker from '../utils/cycle-breaker';
Expand Down Expand Up @@ -738,7 +739,7 @@ function createValidatorsFor(attribute, model) {
* @return {Class} Validator class or undefined if not found
*/
function lookupValidator(owner, type) {
let validatorClass = owner._lookupFactory(`validator:${type}`);
let validatorClass = factoryFor(owner, `validator:${type}`);

if (isNone(validatorClass)) {
throw new Error(`[ember-cp-validations] Validator not found of type: ${type}.`);
Expand Down
3 changes: 2 additions & 1 deletion addon/validators/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import Ember from 'ember';
import Messages from 'ember-cp-validations/validators/messages';
import Options from 'ember-cp-validations/-private/options';
import factoryFor from '../-private/factory-for';
import { unwrapString, getValidatableValue, mergeOptions } from 'ember-cp-validations/utils/utils';

const {
Expand Down Expand Up @@ -88,7 +89,7 @@ const Base = Ember.Object.extend({

if (!isNone(owner)) {
// Since default error messages are stored in app/validators/messages, we have to look it up via the owner
errorMessages = owner._lookupFactory('validator:messages');
errorMessages = factoryFor(owner, 'validator:messages');
}

// If for some reason, we can't find the messages object (i.e. unit tests), use default
Expand Down

0 comments on commit d52dc02

Please sign in to comment.