Skip to content

Commit

Permalink
Adopted Patrick's userSession approach.
Browse files Browse the repository at this point in the history
  • Loading branch information
kueda committed Apr 6, 2017
1 parent f3671c6 commit db7f8c1
Show file tree
Hide file tree
Showing 5 changed files with 9 additions and 7 deletions.
1 change: 0 additions & 1 deletion lib/controllers/v1/observations_controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ ObservationsController.create = function( req, callback ) {
InaturalistAPI.iNatJSWrap( observations.create, req ).then( function( r ) {
var arr = [ { observation_id: r['0'].id } ];
var localeOpts = util.localeOpts( req );
localeOpts.modelOptions = { session: req.userSession };
Observation.preloadInto( arr, localeOpts, function( ) {
return callback( null, arr[0].observation );
});
Expand Down
2 changes: 1 addition & 1 deletion lib/models/es_model.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ var ESModel = class ESModel {
_.each( results.hits.hits, function( h ) {
resultsHash[ h._source.id ] = h._source;
});
Model.injectBelongsTo( objects, model, resultsHash, options.modifier, options.modelOptions );
Model.injectBelongsTo( objects, model, resultsHash, options.modifier, options );
callback( );
});
}
Expand Down
8 changes: 4 additions & 4 deletions lib/models/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,9 @@ var Model = class Model {
});
}

static injectBelongsTo( objects, model, resultsHash, modifier, modelOptions ) {
static injectBelongsTo( objects, model, resultsHash, modifier, options ) {
var modelID = model.modelName + "_id";
var modelOpts = modelOptions || {}
var modelOptions = options ? { session: options.userSession } : { };
// Example: for each obs (where results are users)
_.each( objects, function( o ) {
if( !o ) { return; }
Expand All @@ -78,13 +78,13 @@ var Model = class Model {
// extend obs.user with the properties in the result and turn it into User
association = new model(
_.extend( { }, assocObj, resultsHash[ assocObj.id ] ),
modelOpts
modelOptions
);
}
// obs.user_id exists and we have that user in the results
else if( assocID && !_.isEmpty( resultsHash[ assocID ] ) ) {
// create a new User instance
association = new model( resultsHash[ assocID ], modelOpts );
association = new model( resultsHash[ assocID ], modelOptions );
// remove obs.user_id
delete o[ modelID ];
}
Expand Down
2 changes: 1 addition & 1 deletion lib/models/observation.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ var Observation = class Observation extends Model {
}

static preloadInto( arr, options, callback ) {
ESModel.fetchBelongsTo( arr, Observation, { modelOptions: options.modelOptions }, function( ) {
ESModel.fetchBelongsTo( arr, Observation, options, function( ) {
var observations = arr.map( function( i ) { return i.observation; } );
var preloadCallback = options.minimal ?
Observation.preloadMinimal : Observation.preloadAllAssociations;
Expand Down
3 changes: 3 additions & 0 deletions lib/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ var util = class util {
if( req.inat && req.inat.place ) {
localeOpts.place = req.inat.place;
}
if( req.userSession ) {
localeOpts.userSession = req.userSession;
}
return localeOpts;
}

Expand Down

0 comments on commit db7f8c1

Please sign in to comment.