FIX: N+1 query, loadscript error

This commit is contained in:
Robin Ward 2016-03-11 15:50:57 -05:00
parent 4d95ffde63
commit 2dbe071f96
2 changed files with 28 additions and 12 deletions

View File

@ -1,5 +1,6 @@
import { withPluginApi } from 'discourse/lib/plugin-api'; import { withPluginApi } from 'discourse/lib/plugin-api';
import showModal from 'discourse/lib/show-modal'; import showModal from 'discourse/lib/show-modal';
import loadScript from 'discourse/lib/load-script';
export default { export default {
name: 'enable-staff-notes', name: 'enable-staff-notes',
@ -11,12 +12,14 @@ export default {
withPluginApi('0.2', api => { withPluginApi('0.2', api => {
function showStaffNotes(userId, callback) { function showStaffNotes(userId, callback) {
return store.find('staff-note', { user_id: userId }).then(model => { return loadScript('defer/html-sanitizer-bundle').then(() => {
const controller = showModal('staff-notes', { model, title: 'staff_notes.title' }); return store.find('staff-note', { user_id: userId }).then(model => {
controller.reset(); const controller = showModal('staff-notes', { model, title: 'staff_notes.title' });
controller.set('userId', userId); controller.reset();
controller.set('callback', callback); controller.set('userId', userId);
return controller; controller.set('callback', callback);
return controller;
});
}); });
} }

View File

@ -71,10 +71,7 @@ after_initialize do
end end
def created_by def created_by
user = User.where(id: object[:created_by]).first BasicUserSerializer.new(object[:created_by], scope: scope, root: false)
return nil if user.blank?
BasicUserSerializer.new(user, scope: scope, root: false)
end end
def created_at def created_at
@ -94,7 +91,7 @@ after_initialize do
notes = ::DiscourseStaffNotes.notes_for(params[:user_id]) notes = ::DiscourseStaffNotes.notes_for(params[:user_id])
render json: { render json: {
extras: { username: user.username }, extras: { username: user.username },
staff_notes: serialize_data(notes, ::StaffNoteSerializer) staff_notes: create_json(notes)
} }
end end
@ -103,7 +100,7 @@ after_initialize do
raise Discourse::NotFound if user.blank? raise Discourse::NotFound if user.blank?
staff_note = ::DiscourseStaffNotes.add_note(user, params[:staff_note][:raw], current_user.id) staff_note = ::DiscourseStaffNotes.add_note(user, params[:staff_note][:raw], current_user.id)
render json: serialize_data(staff_note, ::StaffNoteSerializer) render json: create_json(staff_note)
end end
def destroy def destroy
@ -114,6 +111,22 @@ after_initialize do
render json: success_json render json: success_json
end end
protected
def create_json(obj)
# Avoid n+1
if obj.is_a?(Array)
by_ids = {}
User.where(id: obj.map {|o| o[:created_by] }).each do |u|
by_ids[u.id] = u
end
obj.each {|o| o[:created_by] = by_ids[o[:created_by].to_i] }
else
obj[:created_by] = User.where(id: obj[:created_by]).first
end
serialize_data(obj, ::StaffNoteSerializer)
end
end end
whitelist_staff_user_custom_field(STAFF_NOTE_COUNT_FIELD) whitelist_staff_user_custom_field(STAFF_NOTE_COUNT_FIELD)