-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
complete addon with intl messages support
- Loading branch information
Showing
9 changed files
with
226 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
// Inspiration: https://github.com/NullVoxPopuli/ember-contextual-services/blob/master/addon/contextual-service.ts | ||
|
||
// To integrate with Ember's D.I. system, this is all that's needed. | ||
// This is provided as a convience class for creating injectable classes | ||
export default class DIProvider { | ||
static create(injections) { | ||
return new this(injections); | ||
} | ||
|
||
constructor(injections) { | ||
Object.assign(this, injections); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import Messages from '../messages'; | ||
|
||
export function initialize(application) { | ||
application.register('validation-state:messages', Messages); | ||
} | ||
|
||
export default { | ||
initialize | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import EmberObject from '@ember/object'; | ||
import { inject as service } from '@ember/service'; | ||
import { warn } from '@ember/debug'; | ||
import ValidatorsMessages from 'ember-validators/messages'; | ||
import DIProvider from '../-private/di-provider'; | ||
|
||
export default class ValidationMessages extends DIProvider { | ||
@service intl; | ||
|
||
prefix = 'errors'; | ||
|
||
constructor() { | ||
super(...arguments); | ||
|
||
if (!this.intl) { | ||
warn( | ||
`[validation-state] ember-intl not present. Falling back to default ember-validators Message builder`, | ||
{ id: 'validation-state.messages.no-intl' } | ||
); | ||
} | ||
} | ||
|
||
getDescription(context) { | ||
if (this.intl) { | ||
return this.intl.exists(`${this.prefix}${context.descriptionKey}`) && | ||
this.intl.t(`${this.prefix}${context.descriptionKey}`); | ||
} | ||
|
||
return context.description || ValidatorsMessages.defaultDescription; | ||
} | ||
|
||
getMessageFor(type, context) { | ||
const { prefix } = this; | ||
const intlKey = context.intlKey || type; | ||
|
||
context.description = this.getDescription(context); | ||
|
||
// if ember-intl addon is not installed, call the validators method | ||
if (!this.intl) { | ||
return ValidatorsMessages.getMessageFor(...arguments); | ||
} | ||
|
||
// Otherwise, look it up in intl | ||
if (this.intl.exists(key)) { | ||
return this.intl.t(`${prefix}.${intlKey}`, context); | ||
} | ||
|
||
warn( | ||
`[validation-state] Internationalized string not provided for <${type}>. Falling back to ember-validators Message builder`, | ||
{ id: 'validation-state.messages.not-defined-intl' } | ||
); | ||
|
||
return ValidatorsMessages.getMessageFor(...arguments); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { default, initialize } from 'ember-validation-state/initializers/validation-state'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { default } from 'ember-validation-state/validators/messages'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,13 @@ | ||
{ | ||
"name": "ember-validation-state", | ||
"version": "0.0.0", | ||
"version": "0.0.1", | ||
"description": "The default blueprint for ember-cli addons.", | ||
"keywords": [ | ||
"ember-addon" | ||
], | ||
"repository": "", | ||
"repository": "https://github.com/chrismllr/ember-validation-state", | ||
"license": "MIT", | ||
"author": "", | ||
"author": "Chris Miller <[email protected]>", | ||
"directories": { | ||
"doc": "doc", | ||
"test": "tests" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import Application from '@ember/application'; | ||
|
||
import { initialize } from 'dummy/initializers/validation-state'; | ||
import { module, test } from 'qunit'; | ||
import { run } from '@ember/runloop'; | ||
|
||
module('Unit | Initializer | validation-state', function(hooks) { | ||
hooks.beforeEach(function() { | ||
this.TestApplication = Application.extend(); | ||
this.TestApplication.initializer({ | ||
name: 'initializer under test', | ||
initialize | ||
}); | ||
|
||
this.application = this.TestApplication.create({ autoboot: false }); | ||
}); | ||
|
||
hooks.afterEach(function() { | ||
run(this.application, 'destroy'); | ||
}); | ||
|
||
// Replace this with your real tests. | ||
test('it works', async function(assert) { | ||
await this.application.boot(); | ||
|
||
assert.ok(true); | ||
}); | ||
}); |