Skip to content
This repository has been archived by the owner on Jun 5, 2019. It is now read-only.

Entries get updated when field slugs change #80

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions server/models/bucket.coffee
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
inflection = require 'inflection'
mongoose = require 'mongoose'
uniqueValidator = require 'mongoose-unique-validator'
winston = require 'winston'

Route = require '../models/route'
db = require '../lib/database'
Expand All @@ -21,6 +22,9 @@ fieldSchema = new mongoose.Schema
type: String
required: yes
settings: {}
,
toJSON:
getters: yes

fieldSchema
.path 'slug'
Expand All @@ -40,6 +44,27 @@ fieldSchema
]
, 'Sorry, that’s a reserved field slug.'

fieldSchema.virtual 'slug.old'
.get -> @_slug
.set (slug) -> @_slug = slug

fieldSchema.post 'init', ->
@set 'slug.old', @get 'slug'

fieldSchema.post 'save', ->
# There is a very annoying case where slug.old doesn't get filled in properly
if @get('slug.old') and @get('slug.old') isnt @get('slug')
oldPath = "content.#{@get 'slug.old'}"
newPath = "content.#{@get 'slug'}"
q = {}
q[oldPath] = $exists: yes
u = $rename: {}
u.$rename[oldPath] = newPath
# TODO will probably have to change due to #168
# careful with this, updates circumvent middleware
mongoose.model('Entry').update q, u, {}, (err) ->
winston.error err if err?

bucketSchema = new mongoose.Schema
name:
type: String
Expand Down
56 changes: 56 additions & 0 deletions test/server/models/bucket.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,62 @@ describe 'Model#Bucket', ->
expect(bucket.singular).to.equal 'Article'
done()

describe 'Update', ->
Entry = require '../../../server/models/entry'

before reset.db

user = null
before (done) ->
User.create {name: 'Bucketer', email: '[email protected]', password: 'S3cr3ts'}, (err, u) ->
expect(err).to.not.exist
user = u
done()

bucket = null
beforeEach (done) ->
bucket = new Bucket
name: 'Articles'
slug: 'articles'
#fields: [
# fieldType: 'markdown'
# slug: 'body'
# name: 'body'
#]
bucket.fields.push
fieldType: 'markdown'
slug: 'body'
name: 'body'
bucket.save (err, b) ->
expect(err).to.not.exist
done()

afterEach (done) -> Bucket.remove {}, -> done()

it 'updates entry fields when slug changes', (done) ->
Entry.create
title: 'Some Entry'
bucket: bucket._id
author: user._id
content: body: 'Bodyslam'
, (err, entry) ->
expect(err).to.not.exist
expect(entry.get 'content.body').to.exist

Bucket.findById bucket._id, (err, bucket) ->
field = bucket.get('fields')[0]
field.set 'slug', 'new-body'

bucket.save (err, field) ->
expect(err).to.not.exist
Entry.find {bucket: bucket._id}, (err, entries) ->
expect(err).to.not.exist
expect(entries.length).to.not.equal 0
for entry in entries
expect(entry.get 'content.body').to.not.exist
expect(entry.get 'content.new-body').to.exist
done()

describe '#getMembers', ->
u = null
b = null
Expand Down
2 changes: 1 addition & 1 deletion test/server/models/entry.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ reset = require '../../reset'

{expect} = require 'chai'

describe 'Entry', ->
describe 'Model#Entry', ->

user = null

Expand Down