Skip to content

Commit

Permalink
made fields part of metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
lancejpollard committed Apr 7, 2012
1 parent 53ab7d8 commit 0b8c333
Show file tree
Hide file tree
Showing 9 changed files with 31 additions and 20 deletions.
7 changes: 6 additions & 1 deletion lib/tower/model/attribute.js
Expand Up @@ -129,7 +129,7 @@ Tower.Model.Attribute = (function() {
};

function Attribute(owner, name, options, block) {
var key, normalizedKey, serializer, validations, _ref;
var index, key, normalizedKey, serializer, validations, _ref;
if (options == null) options = {};
this.owner = owner;
this.name = key = name;
Expand Down Expand Up @@ -178,6 +178,11 @@ Tower.Model.Attribute = (function() {
this._default = [];
}
}
if (this.type === 'Geo' && !options.index) {
index = {};
index[name] = "2d";
options.index = index;
}
this.get = options.get || (serializer ? serializer.from : void 0);
this.set = options.set || (serializer ? serializer.to : void 0);
if (this.get === true) {
Expand Down
2 changes: 1 addition & 1 deletion lib/tower/model/attributes.js
Expand Up @@ -7,7 +7,7 @@ Tower.Model.Attributes = {
},
fields: function() {
var fields, name, names, options, _i, _len, _ref;
fields = this._fields || (this._fields = {});
fields = this.metadata().fields;
switch (arguments.length) {
case 0:
fields;
Expand Down
2 changes: 1 addition & 1 deletion lib/tower/model/conversion.js
Expand Up @@ -62,7 +62,7 @@ Tower.Model.Conversion = {
controllerName: controllerName,
indexes: indexes,
validators: validators,
fields: {}
fields: fields
};
}
},
Expand Down
5 changes: 5 additions & 0 deletions src/tower/model/attribute.coffee
Expand Up @@ -107,6 +107,11 @@ class Tower.Model.Attribute
else if @type == 'Array'
@_default = []

if @type == 'Geo' && !options.index
index = {}
index[name] = "2d"
options.index = index

@get = options.get || (serializer.from if serializer)
@set = options.set || (serializer.to if serializer)

Expand Down
2 changes: 1 addition & 1 deletion src/tower/model/attributes.coffee
Expand Up @@ -22,7 +22,7 @@ Tower.Model.Attributes =
#
# @return [Object]
fields: ->
fields = @_fields ||= {}
fields = @metadata().fields

switch arguments.length
when 0
Expand Down
2 changes: 1 addition & 1 deletion src/tower/model/conversion.coffee
Expand Up @@ -79,7 +79,7 @@ Tower.Model.Conversion =
controllerName: controllerName
indexes: indexes
validators: validators
fields: {}
fields: fields

# A label for this model when rendered to a string.
#
Expand Down
18 changes: 9 additions & 9 deletions src/tower/model/criteria.coffee
Expand Up @@ -396,16 +396,16 @@ class Tower.Model.Criteria extends Tower.Class
@

toJSON: ->
where: @_where
order: @_order
offset: @_offset
limit: @_limit
fields: @_fields
except: @_except
includes: @_includes
joins: @_joins
where: @_where
order: @_order
offset: @_offset
limit: @_limit
fields: @_fields
except: @_except
includes: @_includes
joins: @_joins
eagerLoad: @_eagerLoad
near: @_near
near: @_near

# Compiled result from the {#where} arguments.
#
Expand Down
7 changes: 5 additions & 2 deletions test/cases/model/geoQueryTest.coffee
Expand Up @@ -69,8 +69,10 @@ describeWith = (store) ->
async.forEachSeries data, iterator, done

test 'near', (done) ->
App.Address.near(coordinates.paris).all (error, records) =>
#console.log _.map records, (i) -> i.get('coordinates')
paris = coordinates.paris

App.Address.near(lat: paris.latitude, lng: paris.longitude).all (error, records) =>
assert.equal records.length, 7
done()

describe 'within', ->
Expand All @@ -79,3 +81,4 @@ describeWith = (store) ->
test 'within(distance: 5, unit: "miles")'

describeWith(Tower.Store.MongoDB)
# describeWith(Tower.Store.Memory)
6 changes: 2 additions & 4 deletions test/config.coffee
Expand Up @@ -68,10 +68,8 @@ Tower.Controller::redirectTo = (options = {}) ->
app = Tower.Application.instance()

before (done) ->
app.initialize =>
require "#{Tower.root}/app/models/address"

App.Address.store().collection().ensureIndex {coordinates:"2d"}, done
app.initialize done
# App.Address.store().collection().ensureIndex {coordinates:"2d"}, done

beforeEach (done) ->
#Tower.Application.instance().teardown()
Expand Down

0 comments on commit 0b8c333

Please sign in to comment.