collection.coffee 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. Spine = require('spine/core')
  2. utils = require('lib/utils')
  3. moment = require('lib/moment')
  4. BaseModel = require('models/base')
  5. class Collection extends BaseModel
  6. @configure "Collection", "site", "slug", "name", "intro", "photo", "pinned", "hidden", "updated_at", "sponsor_id", "sponsor_start", "sponsor_end", "sponsor_propagate", "sponsors_history", "_attachments"
  7. @extend @CouchAjax
  8. @extend @CouchChanges
  9. handler: @CouchChanges.PrivateChanges
  10. @dateSort: (a, b) ->
  11. if (a.updated_at or a.name) < (b.updated_at or b.name) then 1 else -1
  12. @queryOn: ['name','slug']
  13. validate: ->
  14. @slug = utils.cleanSlug @slug
  15. return 'Site is required' unless @site
  16. return 'Slug is required' unless @slug
  17. return 'Name is required' unless @name
  18. # Validate the `slug` to be unique within site
  19. found = Collection.select (collection) =>
  20. matched = collection.site is @site and collection.slug is @slug
  21. if @isNew()
  22. matched
  23. else
  24. collection.id isnt @id and matched
  25. return 'Slug has been already used for this site.' if found.length
  26. # Take care of some dates
  27. updated_at = moment(@updated_at) or moment()
  28. return "Updated #{utils.msg.DATE_NOT_VALID}" unless updated_at.isValid()
  29. @updated_at = updated_at.utc().format()
  30. # Convert some boolean properties
  31. @pinned = Boolean(@pinned)
  32. @hidden = Boolean(@hidden)
  33. @sponsor_propagate = Boolean(@sponsor_propagate)
  34. # Sponsor dates if setting a sponsor
  35. if @sponsor_id
  36. return 'Sponsor Start Date is required' unless @sponsor_start
  37. return 'Sponsor End Date is required' unless @sponsor_end
  38. sponsor_start = moment(@sponsor_start)
  39. sponsor_end = moment(@sponsor_end)
  40. return "Sponsor Start #{utils.msg.DATE_NOT_VALID}" unless sponsor_start.isValid()
  41. return "Sponsor End #{utils.msg.DATE_NOT_VALID}" unless sponsor_end.isValid()
  42. return 'Sponsor Start Date cannot be later than End Date' if sponsor_start >= sponsor_end
  43. # Save in UTC format string
  44. @sponsor_start = sponsor_start.utc().format()
  45. @sponsor_end = sponsor_end.utc().format()
  46. # Some content transformation
  47. @intro = utils.cleanContent @intro
  48. return false
  49. module.exports = Collection