-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[BUGFIX] AMS polymorphic type for namespaced models #3026
Merged
fivetanley
merged 1 commit into
emberjs:master
from
artych:3019-AMS-polymorphic-type-for-namespaced-models
May 3, 2015
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
118 changes: 118 additions & 0 deletions
118
...ctivemodel-adapter/tests/integration/active-model-serializer-namespaced-modelname-test.js
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,118 @@ | ||
var SuperVillain, EvilMinion, YellowMinion, DoomsdayDevice, MediocreVillain, env; | ||
var run = Ember.run; | ||
|
||
module("integration/active_model - AMS-namespaced-model-names", { | ||
setup: function() { | ||
SuperVillain = DS.Model.extend({ | ||
firstName: DS.attr('string'), | ||
lastName: DS.attr('string'), | ||
evilMinions: DS.hasMany("evilMinion") | ||
}); | ||
|
||
EvilMinion = DS.Model.extend({ | ||
superVillain: DS.belongsTo('superVillain'), | ||
name: DS.attr('string') | ||
}); | ||
YellowMinion = EvilMinion.extend(); | ||
DoomsdayDevice = DS.Model.extend({ | ||
name: DS.attr('string'), | ||
evilMinion: DS.belongsTo('evilMinion', { polymorphic: true }) | ||
}); | ||
MediocreVillain = DS.Model.extend({ | ||
name: DS.attr('string'), | ||
evilMinions: DS.hasMany('evilMinion', { polymorphic: true }) | ||
}); | ||
env = setupStore({ | ||
superVillain: SuperVillain, | ||
evilMinion: EvilMinion, | ||
'evilMinions/yellowMinion': YellowMinion, | ||
doomsdayDevice: DoomsdayDevice, | ||
mediocreVillain: MediocreVillain | ||
}); | ||
env.store.modelFor('superVillain'); | ||
env.store.modelFor('evilMinion'); | ||
env.store.modelFor('evilMinions/yellowMinion'); | ||
env.store.modelFor('doomsdayDevice'); | ||
env.store.modelFor('mediocreVillain'); | ||
env.registry.register('serializer:application', DS.ActiveModelSerializer); | ||
env.registry.register('serializer:-active-model', DS.ActiveModelSerializer); | ||
env.registry.register('adapter:-active-model', DS.ActiveModelAdapter); | ||
env.amsSerializer = env.container.lookup("serializer:-active-model"); | ||
env.amsAdapter = env.container.lookup("adapter:-active-model"); | ||
}, | ||
|
||
teardown: function() { | ||
run(env.store, 'destroy'); | ||
} | ||
}); | ||
|
||
test("serialize polymorphic", function() { | ||
var tom, ray; | ||
run(function() { | ||
tom = env.store.createRecord('evilMinions/yellowMinion', { name: "Alex", id: "124" }); | ||
ray = env.store.createRecord(DoomsdayDevice, { evilMinion: tom, name: "DeathRay" }); | ||
}); | ||
|
||
var json = env.amsSerializer.serialize(ray._createSnapshot()); | ||
|
||
deepEqual(json, { | ||
name: "DeathRay", | ||
evil_minion_type: "EvilMinions::YellowMinion", | ||
evil_minion_id: "124" | ||
}); | ||
}); | ||
|
||
test("serialize polymorphic when type key is not camelized", function() { | ||
YellowMinion.typeKey = 'evil-minions/yellow-minion'; | ||
var tom, ray; | ||
run(function() { | ||
tom = env.store.createRecord(YellowMinion, { name: "Alex", id: "124" }); | ||
ray = env.store.createRecord(DoomsdayDevice, { evilMinion: tom, name: "DeathRay" }); | ||
}); | ||
|
||
var json = env.amsSerializer.serialize(ray._createSnapshot()); | ||
|
||
deepEqual(json["evil_minion_type"], "EvilMinions::YellowMinion"); | ||
}); | ||
|
||
test("extractPolymorphic hasMany", function() { | ||
var json_hash = { | ||
mediocre_villain: { id: 1, name: "Dr Horrible", evil_minions: [{ type: "EvilMinions::YellowMinion", id: 12 }] }, | ||
evil_minions: [{ id: 12, name: "Alex", doomsday_device_ids: [1] }] | ||
}; | ||
var json; | ||
|
||
run(function() { | ||
json = env.amsSerializer.extractSingle(env.store, MediocreVillain, json_hash); | ||
}); | ||
|
||
deepEqual(json, { | ||
"id": 1, | ||
"name": "Dr Horrible", | ||
"evilMinions": [{ | ||
type: "evilMinions/yellowMinion", | ||
id: 12 | ||
}] | ||
}); | ||
}); | ||
|
||
test("extractPolymorphic", function() { | ||
var json_hash = { | ||
doomsday_device: { id: 1, name: "DeathRay", evil_minion: { type: "EvilMinions::YellowMinion", id: 12 } }, | ||
evil_minions: [{ id: 12, name: "Alex", doomsday_device_ids: [1] }] | ||
}; | ||
var json; | ||
|
||
run(function() { | ||
json = env.amsSerializer.extractSingle(env.store, DoomsdayDevice, json_hash); | ||
}); | ||
|
||
deepEqual(json, { | ||
"id": 1, | ||
"name": "DeathRay", | ||
"evilMinion": { | ||
type: "evilMinions/yellowMinion", | ||
id: 12 | ||
} | ||
}); | ||
}); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Perhaps this could be rewritten as
classify(belongsTo.typeKey).split('/').map(classify).join('::')
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Seems fine to me overall though.