Skip to content

Instantly share code, notes, and snippets.

@olivierchatry
Created May 19, 2015 12:17
Show Gist options
  • Select an option

  • Save olivierchatry/fc294fa2251f2020b37a to your computer and use it in GitHub Desktop.

Select an option

Save olivierchatry/fc294fa2251f2020b37a to your computer and use it in GitHub Desktop.
newActivity: (attrs) ->
return false unless @can('manage project', @currentModel)
time = this.get('world').get('time')
endDate = moment(time).add(1, 'days').startOf('day')
startDate = moment(time).startOf('day')
activityAction = @store.createRecord 'activityAction'
activityAction.save().then =>
activity = @store.createRecord 'activity',
project: @modelFor('project')
startDate: startDate.toDate()
endDate: endDate.toDate()
activityType: 'task'
action: activityAction
whenSaved = (activity) =>
# work around having to duplicate routes etc?
activity.set 'hackIsNew', true
@transitionTo 'project.plan.activity', activity
whenSavedWithParent = (activity) =>
# work around having to duplicate routes etc?
activity.set 'hackIsNew', true
sortOrder = activity.get('sortorder')
@activityReorder activity.id, sortOrder
@transitionTo 'project.plan.activity', activity
if attrs?.parent
@store.find('activity', attrs.parent).then (parentActivity) =>
children = @currentModel.get('activities').filterBy('parent', parentActivity)
length = 1
if children
length = children.get("length") + 1
activity.set 'parent', parentActivity
activity.set 'sortorder', length + parentActivity.get('sortorder')
activity.save().then whenSavedWithParent
else
length = @currentModel.get('activities').length
# new activity are always push at the end.
activity.set 'sortorder',length
activity.save().then whenSaved
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment