Permalink
Browse files

Switch Model.className to Model.resourceName.

  • Loading branch information...
airhorns committed Apr 30, 2012
1 parent 74c164b commit a12c66bb109b67233b1e5053679b2a2cf594036f
View

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.
View
@@ -1824,7 +1824,7 @@ class Batman.Dispatcher extends Batman.Object
@paramsFromArgument: (argument) ->
resourceNameFromModel = (model) ->
- helpers.camelize(helpers.pluralize(model.get('className')), true)
+ helpers.camelize(helpers.pluralize(model.get('resourceName')), true)
return argument unless @canInferRoute(argument)
@@ -2700,18 +2700,19 @@ class Batman.Model extends Batman.Object
@_batman.check(@)
@_batman.lifecycle ||= new @LifecycleStateMachine('empty', @)
- @classAccessor 'className', ->
- if @className?
- @className
- else
- developer.error("Please define className on the class or storageKey on the prototype of #{$functionName(@)}in order for your model to be minification safe.") if Batman.config.minificationErrors
- $functionName(@)
+ @classAccessor 'resourceName',
+ get: ->
+ if @resourceName?
+ @resourceName
+ else
+ developer.error("Please define className on the class or storageKey on the prototype of #{$functionName(@)} in order for your model to be minification safe.") if Batman.config.minificationErrors
+ Batman.helpers.underscore($functionName(@))
@urlNestsUnder: (keys...) ->
parents = {}
for key in keys
parents[key + '_id'] = Batman.helpers.pluralize(key)
- childSegment = helpers.pluralize(@get('className').toLowerCase())
+ childSegment = helpers.pluralize(@get('resourceName').toLowerCase())
@url = (options) ->
for key, plural of parents
@@ -2905,7 +2906,7 @@ class Batman.Model extends Batman.Object
@
toString: ->
- "#{@constructor.get('className')}: #{@get('id')}"
+ "#{@constructor.get('resourceName')}: #{@get('id')}"
toParam: -> @get('id')
@@ -3436,7 +3437,7 @@ class Batman.PolymorphicBelongsToAssociation extends Batman.BelongsToAssociation
model = instanceOrProxy.association.model
else
model = instanceOrProxy.constructor
- foreignTypeValue = model.get('className')
+ foreignTypeValue = model.get('resourceName')
base.set @foreignTypeKey, foreignTypeValue
getAccessor: (self, model, label) ->
@@ -3464,10 +3465,12 @@ class Batman.PolymorphicBelongsToAssociation extends Batman.BelongsToAssociation
getRelatedModelForType: (type) ->
scope = @options.namespace or Batman.currentApp
- relatedModel = scope?[type]
+ if type
+ relatedModel = scope?[type]
+ relatedModel ||= scope?[Batman.helpers.camelize(type)]
developer.do ->
if Batman.currentApp? and not relatedModel
- developer.warn "Related model #{className} for polymorhic association not found."
+ developer.warn "Related model #{type} for polymorhic association not found."
relatedModel
setIndexForType: (type) ->
@@ -3518,7 +3521,7 @@ class Batman.HasOneAssociation extends Batman.SingularAssociation
constructor: ->
super
@primaryKey = @options.primaryKey or "id"
- @foreignKey = @options.foreignKey or "#{helpers.underscore(@model.get('className'))}_id"
+ @foreignKey = @options.foreignKey or "#{helpers.underscore(@model.get('resourceName'))}_id"
apply: (baseSaveError, base) ->
if relation = @getFromAttributes(base)
@@ -3551,7 +3554,7 @@ class Batman.HasManyAssociation extends Batman.PluralAssociation
return new Batman.PolymorphicHasManyAssociation(arguments...)
super
@primaryKey = @options.primaryKey or "id"
- @foreignKey = @options.foreignKey or "#{helpers.underscore(@model.get('className'))}_id"
+ @foreignKey = @options.foreignKey or "#{helpers.underscore(@model.get('resourceName'))}_id"
apply: (baseSaveError, base) ->
unless baseSaveError
@@ -3626,7 +3629,7 @@ class Batman.PolymorphicHasManyAssociation extends Batman.HasManyAssociation
getRelatedModelForType: -> @getRelatedModel()
- modelType: -> @model.get('className')
+ modelType: -> @model.get('resourceName')
setIndex: ->
if !@typeIndex
@@ -3769,7 +3772,7 @@ class Batman.StorageAdapter extends Batman.Object
storageKey: (record) ->
model = record?.constructor || @model
- model.get('storageKey') || helpers.pluralize(helpers.underscore(model.get('className')))
+ model.get('storageKey') || helpers.pluralize(helpers.underscore(model.get('resourceName')))
getRecordFromData: (attributes, constructor = @model) ->
record = new constructor()
@@ -421,7 +421,7 @@ asyncTest "hasMany associations render", 4, ->
addedProduct.save (err, savedProduct) ->
delay ->
equal node.children().get(3)?.innerHTML, 'Product Four'
- , ASYNC_TEST_DELAY * 2
+ , ASYNC_TEST_DELAY * 5
asyncTest "hasMany adds new related model instances to its set", ->
@Store.find 1, (err, store) =>
@@ -15,7 +15,7 @@ ex.baseSetup = ->
'metafields1':
id: 1
subject_id: 1
- subject_type: 'Store'
+ subject_type: 'store'
key: 'Store metafield'
'metafields2':
id: 2
@@ -25,7 +25,7 @@ ex.baseSetup = ->
'metafields3':
id: 3
subject_id: 1
- subject_type: 'Store'
+ subject_type: 'store'
key: 'Store metafield 2'
'metafields4':
id: 4
@@ -35,7 +35,7 @@ asyncTest "belongsTo associations are saved", ->
metafield.save (err, record) =>
throw err if err
equal record.get('subject_id'), 11
- equal record.get('subject_type'), 'Store'
+ equal record.get('subject_type'), 'store'
storedJSON = @metafieldAdapter.storage["metafields10"]
deepEqual storedJSON, metafield.toJSON()
QUnit.start()
@@ -58,7 +58,7 @@ asyncTest "belongsTo supports inline saving", 1, ->
deepEqual storageAdapter.storage["inline_metafields#{record.get('id')}"],
key: "SEO Title"
subject: {name: "Inline Store"}
- subject_type: 'Store'
+ subject_type: 'store'
QUnit.start()
asyncTest "belongsTo parent models are added to the identity map", 1, ->
@@ -48,9 +48,9 @@ asyncTest "hasMany associations are saved via the parent model", 7, ->
throw err if err
equal storeSaveSpy.callCount, 1
equal metafield1.get('subject_id'), record.get('id')
- equal metafield1.get('subject_type'), 'Store'
+ equal metafield1.get('subject_type'), 'store'
equal metafield2.get('subject_id'), record.get('id')
- equal metafield2.get('subject_type'), 'Store'
+ equal metafield2.get('subject_type'), 'store'
@Store.find record.get('id'), (err, store2) =>
throw err if err
@@ -59,8 +59,8 @@ asyncTest "hasMany associations are saved via the parent model", 7, ->
# hasMany saves inline by default
sorter = generateSorterOnProperty('key')
deepEqual sorter(storedJSON.metafields), sorter([
- {key: "Gizmo", subject_id: record.get('id'), subject_type: 'Store'}
- {key: "Gadget", subject_id: record.get('id'), subject_type: 'Store'}
+ {key: "Gizmo", subject_id: record.get('id'), subject_type: 'store'}
+ {key: "Gadget", subject_id: record.get('id'), subject_type: 'store'}
])
QUnit.start()
@@ -72,7 +72,7 @@ asyncTest "hasMany associations are saved via the child model", 3, ->
metafield.save (err, savedMetafield) ->
throw err if err
equal savedMetafield.get('subject_id'), store.get('id')
- equal savedMetafield.get('subject_type'), 'Store'
+ equal savedMetafield.get('subject_type'), 'store'
metafields = store.get('metafields')
ok metafields.has(savedMetafield)
QUnit.start()
@@ -83,7 +83,7 @@ asyncTest "hasMany associations should index the loaded set", 3, ->
metafields = product.get('metafields')
ok metafields instanceof Batman.AssociationSet
equal metafields.get('length'), 1
- metafield = new @Metafield(subject_id: 4, subject_type: 'Product', key: "Test Metafield")
+ metafield = new @Metafield(subject_id: 4, subject_type: 'product', key: "Test Metafield")
metafield.save (err) ->
throw err if err
equal metafields.get('length'), 2
@@ -133,7 +133,7 @@ asyncTest "unsaved hasMany models should save their associated children", 4, ->
id: 11
name: "Hello!"
metafields:[
- {key: "test", subject_id: product.get('id'), subject_type: 'Product'}
+ {key: "test", subject_id: product.get('id'), subject_type: 'product'}
]
ok !product.isNew()
@@ -87,17 +87,11 @@ test 'the array of instantiated storage adapters should be returned when persist
for instance in [a,b,c]
ok instance.isTestStorageAdapter
-test "get('className') should use the class level model name property", ->
+test "get('resourceName') should use the class level resourceName property", ->
class Product extends Batman.Model
- @className: 'Product'
+ @resourceName: 'product'
- equal Product.get('className'), 'Product'
-
-test "get('className') should use the prototype level storageKey property", ->
- class Product extends Batman.Model
- storageKey: 'products'
-
- equal Product.get('className'), 'Product'
+ equal Product.get('resourceName'), 'product'
QUnit.module "Batman.Model class clearing"
setup: ->

0 comments on commit a12c66b

Please sign in to comment.