Created
October 20, 2013 17:51
-
-
Save superlou/7072908 to your computer and use it in GitHub Desktop.
Missing belongsTo relationship
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Library.Book.url = '/books' | |
#Library.Book.adapter = Library.RESTAdapter.create() | |
attr = DS.attr | |
hasMany = DS.hasMany | |
Library.Book = DS.Model.extend | |
code: attr() | |
volume: attr() | |
stock: attr('number') | |
title: attr() | |
subtitle: attr() | |
adult: attr('boolean') | |
notes: attr() | |
author: attr() | |
created_at: attr('date') | |
checkouts: hasMany('checkout') | |
available: (-> | |
copies_out = @get('checkouts').filterProperty('status', 'out').get('length') | |
@get('stock') - copies_out | |
).property('stock', 'checkouts.@each.status') | |
notAvailable: (-> | |
not @get('available') | |
).property('available') | |
full_title: (-> | |
volume = @get('volume') | |
title = @get('title') | |
if volume | |
@get('title') + ", vol. " + volume | |
else | |
@get('title') | |
).property('title', 'volume') | |
match_filter: (filter)-> | |
filter = filter.toLowerCase() | |
full_title = @get('full_title').toLowerCase() | |
if full_title.indexOf(filter) < 0 | |
return false | |
true | |
noStock: (-> | |
if @get('stock') <= 0 then return true else return false | |
).property('stock') | |
Library.Book.reopenClass | |
findByCode: (code) -> | |
@find('code:' + code) | |
cloneVolumeFrom: (item) -> | |
if parseInt(item.get('volume')) | |
next_volume = parseInt(item.get('volume')) + 1 | |
else | |
next_volume = undefined | |
cloned_book_params = { | |
title: item.get('title') | |
notes: item.get('notes') | |
adult: item.get('adult') | |
volume: next_volume | |
} | |
return cloned_book_params |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
attr = DS.attr | |
belongsTo = DS.belongsTo | |
Library.Checkout = DS.Model.extend | |
book: belongsTo('book') | |
patron: belongsTo('patron') | |
status: attr() | |
closed_at: attr('date') | |
created_at: attr('date') | |
isFullyLoaded: (-> | |
if @get('book.isLoaded') and @get('patron.isLoaded') | |
return true | |
return false | |
).property('book.isLoaded', 'patron.isLoaded') | |
isOpen: (-> | |
if @get('status') == "out" then true else false | |
).property('status') | |
duration: (-> | |
if @get('created_at') and @get('closed_at') | |
diff = moment.duration(moment(@get('closed_at')).valueOf() - moment(@get('created_at')).valueOf()) | |
dur = moment.duration(diff) | |
return dur | |
else if @get('created_at') | |
diff = moment.duration(moment().valueOf() - moment(@get('created_at')).valueOf()) | |
dur = moment.duration(diff) | |
return dur | |
else | |
return null | |
).property('created_at', 'closed_at') | |
Library.Checkout.reopenClass | |
findByInfo: (book_info, patron_info) -> | |
@find('book_info:' + book_info) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{"checkouts":[{"id":223,"status":"out","created_at":"2013-10-20T17:05:15Z","closed_at":null,"book_id":25748,"patron_id":1149},{"id":224,"status":"out","created_at":"2013-10-20T17:05:15Z","closed_at":null,"book_id":25798,"patron_id":1179},{"id":225,"status":"out","created_at":"2013-10-20T17:05:15Z","closed_at":null,"book_id":25725,"patron_id":1150},{"id":226,"status":"out","created_at":"2013-10-20T17:05:15Z","closed_at":null,"book_id":25735,"patron_id":1208},{"id":227,"status":"out","created_at":"2013-10-20T17:05:15Z","closed_at":null,"book_id":25789,"patron_id":1135},{"id":228,"status":"out","created_at":"2013-10-20T17:05:15Z","closed_at":null,"book_id":25753,"patron_id":1138},{"id":229,"status":"out","created_at":"2013-10-20T17:05:16Z","closed_at":null,"book_id":25788,"patron_id":1197},{"id":230,"status":"out","created_at":"2013-10-20T17:05:16Z","closed_at":null,"book_id":25814,"patron_id":1154},{"id":231,"status":"out","created_at":"2013-10-20T17:05:16Z","closed_at":null,"book_id":25802,"patron_id":1185},{"id":232,"status":"out","created_at":"2013-10-20T17:05:16Z","closed_at":null,"book_id":25720,"patron_id":1143},{"id":233,"status":"out","created_at":"2013-10-20T17:05:16Z","closed_at":null,"book_id":25737,"patron_id":1213},{"id":234,"status":"out","created_at":"2013-10-20T17:05:16Z","closed_at":null,"book_id":25788,"patron_id":1160},{"id":235,"status":"out","created_at":"2013-10-20T17:05:16Z","closed_at":null,"book_id":25804,"patron_id":1180},{"id":236,"status":"out","created_at":"2013-10-20T17:05:16Z","closed_at":null,"book_id":25797,"patron_id":1222},{"id":237,"status":"out","created_at":"2013-10-20T17:05:16Z","closed_at":null,"book_id":25758,"patron_id":1225},{"id":238,"status":"out","created_at":"2013-10-20T17:05:16Z","closed_at":null,"book_id":25719,"patron_id":1159},{"id":239,"status":"out","created_at":"2013-10-20T17:05:16Z","closed_at":null,"book_id":25809,"patron_id":1156},{"id":240,"status":"out","created_at":"2013-10-20T17:05:16Z","closed_at":null,"book_id":25738,"patron_id":1199},{"id":241,"status":"out","created_at":"2013-10-20T17:05:16Z","closed_at":null,"book_id":25785,"patron_id":1133},{"id":242,"status":"out","created_at":"2013-10-20T17:05:16Z","closed_at":null,"book_id":25766,"patron_id":1201}]} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
attr = DS.attr | |
hasMany = DS.hasMany | |
Library.Patron = DS.Model.extend | |
code: attr() | |
name: attr() | |
gender: attr() | |
email: attr() | |
notes: attr() | |
adult: attr() | |
keepCheckoutHistory: attr() | |
checkouts: DS.hasMany('checkout') | |
match_filter: (filter)-> | |
filter = filter.toLowerCase() | |
name = @get('name').toLowerCase() | |
if name.indexOf(filter) < 0 | |
return false | |
true | |
Library.Patron.camelizeKeys = true | |
Library.Patron.reopenClass | |
findByCode: (code) -> | |
@find('code:' + code) | |
#Library.Patron.url = '/patrons' | |
#Library.Patron.adapter = Library.RESTAdapter.create() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment