Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Switch Model.className to Model.resourceName.

  • Loading branch information...
commit a12c66bb109b67233b1e5053679b2a2cf594036f 1 parent 74c164b
@airhorns airhorns authored
View
39 lib/batman.js
@@ -3572,7 +3572,7 @@
Dispatcher.paramsFromArgument = function(argument) {
var resourceNameFromModel;
resourceNameFromModel = function(model) {
- return helpers.camelize(helpers.pluralize(model.get('className')), true);
+ return helpers.camelize(helpers.pluralize(model.get('resourceName')), true);
};
if (!this.canInferRoute(argument)) {
return argument;
@@ -5133,14 +5133,16 @@
return (_base = this._batman).lifecycle || (_base.lifecycle = new this.LifecycleStateMachine('empty', this));
});
- Model.classAccessor('className', function() {
- if (this.className != null) {
- return this.className;
- } else {
- if (Batman.config.minificationErrors) {
- developer.error("Please define className on the class or storageKey on the prototype of " + ($functionName(this)) + "in order for your model to be minification safe.");
+ Model.classAccessor('resourceName', {
+ get: function() {
+ if (this.resourceName != null) {
+ return this.resourceName;
+ } else {
+ if (Batman.config.minificationErrors) {
+ developer.error("Please define className on the class or storageKey on the prototype of " + ($functionName(this)) + " in order for your model to be minification safe.");
+ }
+ return Batman.helpers.underscore($functionName(this));
}
- return $functionName(this);
}
});
@@ -5152,7 +5154,7 @@
key = keys[_i];
parents[key + '_id'] = Batman.helpers.pluralize(key);
}
- childSegment = helpers.pluralize(this.get('className').toLowerCase());
+ childSegment = helpers.pluralize(this.get('resourceName').toLowerCase());
this.url = function(options) {
var key, parentID, plural;
for (key in parents) {
@@ -5451,7 +5453,7 @@
};
Model.prototype.toString = function() {
- return "" + (this.constructor.get('className')) + ": " + (this.get('id'));
+ return "" + (this.constructor.get('resourceName')) + ": " + (this.get('id'));
};
Model.prototype.toParam = function() {
@@ -6424,7 +6426,7 @@
} else {
model = instanceOrProxy.constructor;
}
- foreignTypeValue = model.get('className');
+ foreignTypeValue = model.get('resourceName');
return base.set(this.foreignTypeKey, foreignTypeValue);
}
};
@@ -6460,10 +6462,13 @@
PolymorphicBelongsToAssociation.prototype.getRelatedModelForType = function(type) {
var relatedModel, scope;
scope = this.options.namespace || Batman.currentApp;
- relatedModel = scope != null ? scope[type] : void 0;
+ if (type) {
+ relatedModel = scope != null ? scope[type] : void 0;
+ relatedModel || (relatedModel = scope != null ? scope[Batman.helpers.camelize(type)] : void 0);
+ }
developer["do"](function() {
if ((Batman.currentApp != null) && !relatedModel) {
- return developer.warn("Related model " + className + " for polymorhic association not found.");
+ return developer.warn("Related model " + type + " for polymorhic association not found.");
}
});
return relatedModel;
@@ -6545,7 +6550,7 @@
function HasOneAssociation() {
HasOneAssociation.__super__.constructor.apply(this, arguments);
this.primaryKey = this.options.primaryKey || "id";
- this.foreignKey = this.options.foreignKey || ("" + (helpers.underscore(this.model.get('className'))) + "_id");
+ this.foreignKey = this.options.foreignKey || ("" + (helpers.underscore(this.model.get('resourceName'))) + "_id");
}
HasOneAssociation.prototype.apply = function(baseSaveError, base) {
@@ -6607,7 +6612,7 @@
}
HasManyAssociation.__super__.constructor.apply(this, arguments);
this.primaryKey = this.options.primaryKey || "id";
- this.foreignKey = this.options.foreignKey || ("" + (helpers.underscore(this.model.get('className'))) + "_id");
+ this.foreignKey = this.options.foreignKey || ("" + (helpers.underscore(this.model.get('resourceName'))) + "_id");
}
HasManyAssociation.prototype.apply = function(baseSaveError, base) {
@@ -6727,7 +6732,7 @@
};
PolymorphicHasManyAssociation.prototype.modelType = function() {
- return this.model.get('className');
+ return this.model.get('resourceName');
};
PolymorphicHasManyAssociation.prototype.setIndex = function() {
@@ -7055,7 +7060,7 @@
StorageAdapter.prototype.storageKey = function(record) {
var model;
model = (record != null ? record.constructor : void 0) || this.model;
- return model.get('storageKey') || helpers.pluralize(helpers.underscore(model.get('className')));
+ return model.get('storageKey') || helpers.pluralize(helpers.underscore(model.get('resourceName')));
};
StorageAdapter.prototype.getRecordFromData = function(attributes, constructor) {
View
35 src/batman.coffee
@@ -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()
View
2  tests/batman/model/associations/has_many_test.coffee
@@ -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) =>
View
4 tests/batman/model/associations/polymorphic_association_helper.coffee
@@ -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
View
4 tests/batman/model/associations/polymorphic_belongs_to_test.coffee
@@ -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, ->
View
14 tests/batman/model/associations/polymorphic_has_many_test.coffee
@@ -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()
View
12 tests/batman/model/model_test.coffee
@@ -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: ->
Please sign in to comment.
Something went wrong with that request. Please try again.