diff options
| author | mo <mokha@cisco.com> | 2017-09-02 14:48:43 -0600 |
|---|---|---|
| committer | mo <mokha@cisco.com> | 2017-09-02 14:48:43 -0600 |
| commit | b8dfe131c6c5fe9d0e5b4ee2d271feeb5d411502 (patch) | |
| tree | 05b475a8b6442e2a43d9529c42362a3460df7d13 /app/assets/javascripts/models | |
| parent | bba21d02643d632fee5fa4bdbc876d44832ccf4b (diff) | |
promote backbone subdirectory up to root.
Diffstat (limited to 'app/assets/javascripts/models')
| -rw-r--r-- | app/assets/javascripts/models/.gitkeep | 0 | ||||
| -rw-r--r-- | app/assets/javascripts/models/cake.js.coffee | 27 | ||||
| -rw-r--r-- | app/assets/javascripts/models/category.js.coffee | 10 | ||||
| -rw-r--r-- | app/assets/javascripts/models/photo.js.coffee | 30 | ||||
| -rw-r--r-- | app/assets/javascripts/models/profile.js.coffee | 38 | ||||
| -rw-r--r-- | app/assets/javascripts/models/tutorials.js.coffee | 24 |
6 files changed, 129 insertions, 0 deletions
diff --git a/app/assets/javascripts/models/.gitkeep b/app/assets/javascripts/models/.gitkeep new file mode 100644 index 00000000..e69de29b --- /dev/null +++ b/app/assets/javascripts/models/.gitkeep diff --git a/app/assets/javascripts/models/cake.js.coffee b/app/assets/javascripts/models/cake.js.coffee new file mode 100644 index 00000000..7f925394 --- /dev/null +++ b/app/assets/javascripts/models/cake.js.coffee @@ -0,0 +1,27 @@ +class CakeSide.Models.Cake extends Backbone.Model + paramRoot: 'cake' + + defaults: + id: null + name: null + story: null + created_at: null + updated_at: null + category_id: null + + validate: (attributes, options) -> + return "Name can't be blank" unless attributes.name && attributes.name.trim() + return "Category can't be blank" unless attributes.category_id + + photos: -> + CakeSide.Application.request('PhotosRepository', @id) + + public_url: -> + "#{window.location.origin}/creations/#{@get('slug')}" + + category_id: -> + @get('category_id') || @get('category').id + +class CakeSide.Collections.CakesCollection extends Backbone.Collection + model: CakeSide.Models.Cake + url: '/api/v1/cakes' diff --git a/app/assets/javascripts/models/category.js.coffee b/app/assets/javascripts/models/category.js.coffee new file mode 100644 index 00000000..98437966 --- /dev/null +++ b/app/assets/javascripts/models/category.js.coffee @@ -0,0 +1,10 @@ +class CakeSide.Models.Category extends Backbone.Model + paramRoot: 'category' + + defaults: + name: null + slug: null + +class CakeSide.Collections.CategoriesCollection extends Backbone.Collection + model: CakeSide.Models.Category + url: '/api/v1/categories' diff --git a/app/assets/javascripts/models/photo.js.coffee b/app/assets/javascripts/models/photo.js.coffee new file mode 100644 index 00000000..eb1cff76 --- /dev/null +++ b/app/assets/javascripts/models/photo.js.coffee @@ -0,0 +1,30 @@ +class CakeSide.Models.Photo extends Backbone.Model + paramRoot: 'photo' + fileAttribute: 'image' + + defaults: + id: null + cake_id: null + content_type: null + original_filename: null + thumb_url: null + large_url: null + original_url: null + created_at: null + updated_at: null + + initialize: (options) -> + @set('cake_id', options.cake_id) + if options.id + @url="/api/v1/cakes/#{options.cake_id}/photos/#{options.id}" + else + @url="/api/v1/cakes/#{options.cake_id}/photos" + + super(options) + +class CakeSide.Collections.PhotosCollection extends Backbone.Collection + model: CakeSide.Models.Photo + + initialize: (options) -> + @set('cake_id', options.cake_id) + @url="/api/v1/cakes/#{options.cake_id}/photos" diff --git a/app/assets/javascripts/models/profile.js.coffee b/app/assets/javascripts/models/profile.js.coffee new file mode 100644 index 00000000..189d83e4 --- /dev/null +++ b/app/assets/javascripts/models/profile.js.coffee @@ -0,0 +1,38 @@ +class CakeSide.Models.Profile extends Backbone.Model + paramRoot: 'profile' + urlRoot: '/api/v1/profiles' + modelEvents: + "change": "render" + + defaults: + id: null + name: null + email: null + city: null + website: null + facebook: null + twitter: null + + validate: (attributes, options) -> + return "Name can't be blank" unless attributes.name && attributes.name.trim() + return "Email can't be blank" unless attributes.email && attributes.email.trim() + return "Email is invalid" unless @validateEmail(attributes.email) + return "URL is invalid" if attributes.website && !@validateUrl(attributes.website) + return "Twitter handle is invalid" if !@validateTwitter(attributes.twitter) + + validateEmail: (email) -> + regex = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/ + regex.test(email) + + validateUrl: (url) -> + regex = new RegExp( "^" + "(?:(?:https?|ftp)://)" + "(?:\\S+(?::\\S*)?@)?" + "(?:" + "(?!(?:10|127)(?:\\.\\d{1,3}){3})" + "(?!(?:169\\.254|192\\.168)(?:\\.\\d{1,3}){2})" + "(?!172\\.(?:1[6-9]|2\\d|3[0-1])(?:\\.\\d{1,3}){2})" + "(?:[1-9]\\d?|1\\d\\d|2[01]\\d|22[0-3])" + "(?:\\.(?:1?\\d{1,2}|2[0-4]\\d|25[0-5])){2}" + "(?:\\.(?:[1-9]\\d?|1\\d\\d|2[0-4]\\d|25[0-4]))" + "|" + "(?:(?:[a-z\\u00a1-\\uffff0-9]-*)*[a-z\\u00a1-\\uffff0-9]+)" + "(?:\\.(?:[a-z\\u00a1-\\uffff0-9]-*)*[a-z\\u00a1-\\uffff0-9]+)*" + "(?:\\.(?:[a-z\\u00a1-\\uffff]{2,}))" + ")" + "(?::\\d{2,5})?" + "(?:/\\S*)?" + "$", "i") + regex.test(url) + + validateTwitter: (twitter) -> + return true unless twitter + regex = /^@?(\w){1,15}$/ + regex.test(twitter) + +class CakeSide.Collections.ProfilesCollection extends Backbone.Collection + model: CakeSide.Models.Profile + url: '/api/v1/profiles' diff --git a/app/assets/javascripts/models/tutorials.js.coffee b/app/assets/javascripts/models/tutorials.js.coffee new file mode 100644 index 00000000..9e2e8a1f --- /dev/null +++ b/app/assets/javascripts/models/tutorials.js.coffee @@ -0,0 +1,24 @@ +class CakeSide.Models.Tutorial extends Backbone.Model + paramRoot: 'tutorial' + + defaults: + id: null + url: null + image_url: null + heading: null + description: null + + validate: (attributes, options) -> + return "Invalid Url." unless @isValidUrl(attributes.url || '') + return "This tutorial has no photos." unless @hasImage(attributes.image_url) + + isValidUrl: (url) -> + expression = /[-a-zA-Z0-9@:%_\+.~#?&//=]{2,256}\.[a-z]{2,4}\b(\/[-a-zA-Z0-9@:%_\+.~#?&//=]*)?/gi; + url.match(new RegExp(expression)) + + hasImage: (image_url) -> + image_url + +class CakeSide.Collections.TutorialsCollection extends Backbone.Collection + model: CakeSide.Models.Tutorial + url: '/api/v1/tutorials' |
