<commit_before>window.App = Ember.Application.create({});


App.Router.map(function(){
  this.resource('insight', { path: ':insight_id' }, function(){
    this.route('chart');
    this.route('describe');
    this.route('comment');
  });

  this.resource('queries', { path: '/query' }, function(){
    this.route('new');
    this.resource('query', { path: ':query_id' });
  });

  

  this.route('views');
  this.route('schemas');
  this.route('functions');

});

App.ObjectTransform = DS.Transform.extend({
  deserialize: function(serialized) {
    return serialized;
  },
  serialize: function(deserialized) {
    return deserialized;
  }
});


App.FirebaseAdapter =  DS.Adapter.extend({
  baseRef: "https://caravela.firebaseio.com/",


  emptyPromise: function(result){
    // resolve immediatly we'll update the store
    // via push as the records come in
    result = result || {};
    return new Ember.RSVP.Promise(function(resolve, reject) {
      resolve(result);
    });

  },

  refForType: function(type){
    return new Firebase(this.get('baseRef')).child(
      Em.String.pluralize(type.typeKey)
    );
  },

  createRecord: function(store, type, record){

    var serializer = store.serializerFor(type.typeKey);
    var data = serializer.serialize(record, { includeId: true });

    var ref = this.refForType(type);   
    return new Ember.RSVP.Promise(function(resolve) {

      var childRef = ref.push(
        data
      );
      data.id = childRef.name();
      
      console.log("adding", type.typeKey, data)
      resolve(data);


    });

  },

  updateRecord: function(store, type, record){
    var serializer = store.serializerFor(type.typeKey);
    var data = serializer.serialize(record, { includeId: true });
    
    var ref = this.refForType(type).child(data.id);   
    return new Ember.RSVP.Promise(function(resolve,reject) {

      ref.set(
        data,
        function(err){
          if(err){
            reject(err);
          }else{
            resolve(data);
          }
        }
      );

    });

  },

  find: function(store, type, id){
    return this.emptyPromise({'id': id});
  },

  findAll: function(store, type){
    var ref = this.refForType(type);

    var controller = this;
    ref.on('child_added', function(snapshot){
      var record = snapshot.val();
      record.id = snapshot.name();

      // schedule in next loop so that if this was called because
      // of createRecord we preform an update rather than a creating
      // a duplicate.
      Em.run.next(null, function(){
        store.push(type, record);
      });        


    });

    /*
    ref.on('child_removed', function(snapshot){
      controller.removeItem(snapshot.name());
    });
    */

    ref.on('child_changed', function(snapshot){
      var record = snapshot.val();
      record.id = snapshot.name();
      store.push(type, record);

    });

    
    return this.emptyPromise();

  }<commit_msg>child_remove updates in the UI<commit_after>window.App = Ember.Application.create({});


App.Router.map(function(){
  this.resource('insight', { path: ':insight_id' }, function(){
    this.route('chart');
    this.route('describe');
    this.route('comment');
  });

  this.resource('queries', { path: '/query' }, function(){
    this.route('new');
    this.resource('query', { path: ':query_id' });
  });

  

  this.route('views');
  this.route('schemas');
  this.route('functions');

});

App.ObjectTransform = DS.Transform.extend({
  deserialize: function(serialized) {
    return serialized;
  },
  serialize: function(deserialized) {
    return deserialized;
  }
});


App.FirebaseAdapter =  DS.Adapter.extend({
  baseRef: "https://caravela.firebaseio.com/",


  emptyPromise: function(result){
    // resolve immediatly we'll update the store
    // via push as the records come in
    result = result || {};
    return new Ember.RSVP.Promise(function(resolve, reject) {
      resolve(result);
    });

  },

  refForType: function(type){
    return new Firebase(this.get('baseRef')).child(
      Em.String.pluralize(type.typeKey)
    );
  },

  createRecord: function(store, type, record){

    var serializer = store.serializerFor(type.typeKey);
    var data = serializer.serialize(record, { includeId: true });

    var ref = this.refForType(type);   
    return new Ember.RSVP.Promise(function(resolve) {

      var childRef = ref.push(
        data
      );
      data.id = childRef.name();
      data.url = childRef.toString();
      console.log("adding", type.typeKey, data)
      resolve(data);


    });

  },

  updateRecord: function(store, type, record){
    var serializer = store.serializerFor(type.typeKey);
    var data = serializer.serialize(record, { includeId: true });
    
    var ref = this.refForType(type).child(data.id);

    return new Ember.RSVP.Promise(function(resolve,reject) {

      ref.set(
        data,
        function(err){
          if(err){
            reject(err);
          }else{
            resolve(data);
          }
        }
      );

    });

  },

  find: function(store, type, id){
    return this.emptyPromise({'id': id});
  },

  findAll: function(store, type){
    var ref = this.refForType(type);

    var controller = this;
    ref.on('child_added', function(snapshot){
      var record = snapshot.val();
      record.id = snapshot.name();
      record.url = snapshot.ref().toString();

      // schedule in next loop so that if this was called because
      // of createRecord we preform an update rather than a creating
      // a duplicate.
      Em.run.next(null, function(){
        store.push(type, record);
      });        


    });

    
    ref.on('child_removed', function(snapshot){

      var id = snapshot.name();
      var record = store.recordForId(type, id);
      record.unloadRecord();
    });
    

    ref.on('child_changed', function(snapshot){
      var record = snapshot.val();
      record.id = snapshot.name();
      record.url = snapshot.ref().toString();
      store.push(type, record);

    });

    
    return this.emptyPromise();

  }
  
{'fraction_non_alphanumeric': 0.1534400263982841, 'fraction_numerical': 0.0, 'mean_word_length': 2.636472705458908, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1961590', 'n_tokens_mistral': 1906, 'n_tokens_neox': 1839, 'n_words': 377}