Skip to content
This repository has been archived by the owner on Jan 27, 2021. It is now read-only.

Commit

Permalink
move slug logic into item model. refs #221
Browse files Browse the repository at this point in the history
  • Loading branch information
gpbmike committed Feb 13, 2015
1 parent ad8604f commit f80a48b
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 32 deletions.
17 changes: 2 additions & 15 deletions app/controllers/wh/content/type/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,19 +76,6 @@ export default Ember.ObjectController.extend({

}.property('nameControl.value', 'type.id', 'slugControl.value', 'publishDate'),

// Remove old slug, add new slug
updateSlug: function () {
var slugControl = this.get('controls').findBy('name', 'slug');

window.ENV.firebase.child('slugs').child(slugControl.get('initialValue')).remove();

var slug = slugControl.get('value') || this.get('defaultSlug');

window.ENV.firebase.child('slugs').child(slug).set(true);

slugControl.set('initialValue', slug);
},

isLive: function () {
if (this.get('showSchedule')) {
return false;
Expand Down Expand Up @@ -400,10 +387,10 @@ export default Ember.ObjectController.extend({

return itemModel.set('itemData', itemData).save().then(function (item) {

controller.updateSlug();

this.set('initialValues', controls.getEach('value'));

controller.set('slugControl.initialValue', itemModel.getSlug());

controller.send('buildSignal', itemData.publish_date);

var sendNotify = function (message) {
Expand Down
49 changes: 48 additions & 1 deletion app/models/item.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,56 @@
import SearchIndex from 'appkit/utils/search-index';
import slugger from 'appkit/utils/slugger';

export default DS.Model.extend({
itemData: DS.attr('json'),

updateSearchIndex: function () {
SearchIndex.indexItem(this);
}.on('didUpdate', 'didCreate')
}.on('didUpdate', 'didCreate'),

updateSlugIndex: function () {

var slug = this.getSlug();

if (slug !== this.get('initialSlug')) {

window.ENV.firebase.child('slugs').child(slug).set(true);

window.ENV.firebase.child('slugs').child(this.get('initialSlug')).remove();

this.setInitialSlug();

}

}.on('didUpdate', 'didCreate'),

setInitialSlug: function () {
this.set('initialSlug', this.getSlug());
}.on('didLoad'),

removeSlugIndex: function () {
window.ENV.firebase.child('slugs').child(this.getSlug()).remove();
}.on('didDelete'),

getSlug: function () {
var data = this.get('itemData');
return data.slug || this.getDefaultSlug();
},

getDefaultSlug: function () {

var typeId = this.constructor.typeKey;

var type = this.store.getById('content-type', typeId);

var data = this.get('itemData');

var sluggedDate = (Ember.isEmpty(data.publish_date) ? moment() : moment(data.publish_date)).format();

return slugger({
name: data.name,
publish_date: sluggedDate
}, type.get('id'), type.get('customUrls'));

}
});
17 changes: 1 addition & 16 deletions app/routes/wh/content/type/edit.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import slugger from 'appkit/utils/slugger';
import SearchIndex from 'appkit/utils/search-index';

export default Ember.Route.extend({
Expand Down Expand Up @@ -194,21 +193,7 @@ export default Ember.Route.extend({
var slugControl = type.get('controls').findBy('name', 'slug');
controller.set('slugControl', slugControl);
controller.set('isEditingSlug', false);

if (Ember.isEmpty(slugControl.get('value'))) {
var publishDate = type.get('controls').findBy('name', 'publish_date').get('value');
var sluggedDate = (Ember.isEmpty(publishDate) ? moment() : moment(publishDate)).format();

var defaultSlug = slugger({
name: type.get('controls').findBy('name', 'name').get('value'),
publish_date: sluggedDate
}, type.get('id'), type.get('customUrls'));

slugControl.set('initialValue', defaultSlug);

} else {
slugControl.set('initialValue', Ember.copy(slugControl.get('value')));
}
slugControl.set('initialValue', this.get('itemModel.initialSlug'));

type.get('controls').filterBy('controlType.widget', 'relation').filterBy('value').forEach(function (control) {
controller.get('initialRelations').set(control.get('name'), Ember.copy(control.get('value')));
Expand Down

0 comments on commit f80a48b

Please sign in to comment.