var SharedModelMethods = require('mixins/shared_model_methods');

var Activity = module.exports = Backbone.Model.extend({
  name: 'activity',

  i18nScope: 'activerecord.attributes.',

  timestampFormat: 'd mmm yyyy',

  initialize: function(args) {
    var attributes = this.attributes;
    var data = args.activity;

    this.i18nScope += data.subject_type.toLowerCase();
    attributes.date = new Date(data.updated_at).format(this.timestampFormat);
    attributes.action = this.humanActionName(data.action);
    attributes.subject_changes = this.parseChanges(data.subject_changes);
  },

  humanActionName: function(action) {
    return I18n.t(action, {scope: 'activity.actions'});
  },

  parseChanges: function(changes) {
    return _.map(changes, function(value, key) {
      if (key == 'documents_attributes') key = 'documents';
      return {
        attribute: this.humanAttributeName(key),
        oldValue: value[0],
        newValue: value[1]
      }
    }, this);
  }
});

_.defaults(Activity.prototype, SharedModelMethods);

var SharedModelMethods = require('mixins/shared_model_methods');

var Activity = module.exports = Backbone.Model.extend({
  name: 'activity',

  i18nScope: 'activerecord.attributes.',

  timestampFormat: 'd mmm yyyy',

  initialize: function(args) {
    var data = args.activity;

    this.i18nScope += data.subject_type.toLowerCase();
    this.set({
      date: new Date(data.updated_at).format(this.timestampFormat),
      action: this.humanActionName(data.action),
      subject_changes: this.parseChanges(data.subject_changes)
    });
  },

  humanActionName: function(action) {
    return I18n.t(action, {scope: 'activity.actions'});
  },

  parseChanges: function(changes) {
    return _.map(changes, function(value, key) {
      if (key == 'documents_attributes') key = 'documents';
      return {
        attribute: this.humanAttributeName(key),
        oldValue: value[0],
        newValue: value[1]
      }
    }, this);
  }
});

_.defaults(Activity.prototype, SharedModelMethods);

Fix 'Do not access attributes directly.' error on Activity