From 3d76fb9c2c400fb91d5f77247e9b776ea5c8912a Mon Sep 17 00:00:00 2001 From: Guo Xiang Tan Date: Thu, 13 Apr 2017 17:10:55 +0800 Subject: [PATCH] FIX: Don't show category options for reports that can't be scoped to a category. --- .../admin/controllers/admin-reports.js.es6 | 20 ++++++--- .../admin/routes/admin-reports.js.es6 | 4 +- app/controllers/admin/reports_controller.rb | 2 +- app/models/report.rb | 1 - app/models/user.rb | 2 +- .../admin/reports_controller_spec.rb | 41 ++++++++++++++++++- 6 files changed, 58 insertions(+), 12 deletions(-) diff --git a/app/assets/javascripts/admin/controllers/admin-reports.js.es6 b/app/assets/javascripts/admin/controllers/admin-reports.js.es6 index 1d4dac7e644..83fde41c7e9 100644 --- a/app/assets/javascripts/admin/controllers/admin-reports.js.es6 +++ b/app/assets/javascripts/admin/controllers/admin-reports.js.es6 @@ -4,7 +4,7 @@ import Report from 'admin/models/report'; import computed from 'ember-addons/ember-computed-decorators'; export default Ember.Controller.extend({ - queryParams: ["mode", "start-date", "end-date", "category-id", "group-id"], + queryParams: ["mode", "start_date", "end_date", "category_id", "group_id"], viewMode: 'graph', viewingTable: Em.computed.equal('viewMode', 'table'), viewingGraph: Em.computed.equal('viewMode', 'graph'), @@ -28,7 +28,15 @@ export default Ember.Controller.extend({ @computed('model.type') showCategoryOptions(modelType) { - return !modelType.match(/_private_messages$/) && !modelType.match(/^page_view_/); + return [ + 'topics', + 'posts', + 'time_to_first_response_total', + 'topics_with_no_response', + 'flags', + 'likes', + 'bookmarks' + ].includes(modelType); }, @computed('model.type') @@ -42,13 +50,13 @@ export default Ember.Controller.extend({ this.set("refreshing", true); this.setProperties({ - 'start-date': this.get('startDate'), - 'end-date': this.get('endDate'), - 'category-id': this.get('categoryId'), + 'start_date': this.get('startDate'), + 'end_date': this.get('endDate'), + 'category_id': this.get('categoryId'), }); if (this.get('groupId')){ - this.set('group-id', this.get('groupId')); + this.set('group_id', this.get('groupId')); } q = Report.find(this.get("model.type"), this.get("startDate"), this.get("endDate"), this.get("categoryId"), this.get("groupId")); diff --git a/app/assets/javascripts/admin/routes/admin-reports.js.es6 b/app/assets/javascripts/admin/routes/admin-reports.js.es6 index eb89a3306ed..47ece705808 100644 --- a/app/assets/javascripts/admin/routes/admin-reports.js.es6 +++ b/app/assets/javascripts/admin/routes/admin-reports.js.es6 @@ -7,11 +7,11 @@ @module Discourse **/ export default Discourse.Route.extend({ - queryParams: { mode: {}, "start-date": {}, "end-date": {}, "category-id": {}, "group-id": {}}, + queryParams: { mode: {}, "start_date": {}, "end_date": {}, "category_id": {}, "group_id": {} }, model: function(params) { const Report = require('admin/models/report').default; - return Report.find(params.type, params['start-date'], params['end-date'], params['category-id'], params['group-id']); + return Report.find(params.type, params['start_date'], params['end_date'], params['category_id'], params['group_id']); }, setupController: function(controller, model) { diff --git a/app/controllers/admin/reports_controller.rb b/app/controllers/admin/reports_controller.rb index 8a47b8a9d12..6490c96d73e 100644 --- a/app/controllers/admin/reports_controller.rb +++ b/app/controllers/admin/reports_controller.rb @@ -9,7 +9,7 @@ class Admin::ReportsController < Admin::AdminController start_date = params[:start_date].present? ? Time.parse(params[:start_date]) : 30.days.ago end_date = params[:end_date].present? ? Time.parse(params[:end_date]) : start_date + 30.days - + if params.has_key?(:category_id) && params[:category_id].to_i > 0 category_id = params[:category_id].to_i else diff --git a/app/models/report.rb b/app/models/report.rb index b568aec1eab..76c162ffb17 100644 --- a/app/models/report.rb +++ b/app/models/report.rb @@ -82,7 +82,6 @@ class Report .sum(:count) end - def self.report_visits(report) basic_report_about report, UserVisit, :by_day, report.start_date, report.end_date, report.group_id diff --git a/app/models/user.rb b/app/models/user.rb index 6ce645d43d3..3c41e4c1e28 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -740,7 +740,7 @@ class User < ActiveRecord::Base (tl_badge + other_badges).take(limit) end - def self.count_by_signup_date(start_date, end_date, group_id=nil) + def self.count_by_signup_date(start_date, end_date, group_id = nil) result = where('users.created_at >= ? AND users.created_at <= ?', start_date, end_date) if group_id diff --git a/spec/controllers/admin/reports_controller_spec.rb b/spec/controllers/admin/reports_controller_spec.rb index 92da083f324..9d34ebbe1c4 100644 --- a/spec/controllers/admin/reports_controller_spec.rb +++ b/spec/controllers/admin/reports_controller_spec.rb @@ -1,7 +1,6 @@ require 'rails_helper' describe Admin::ReportsController do - it "is a subclass of AdminController" do expect(Admin::ReportsController < Admin::AdminController).to eq(true) end @@ -58,6 +57,46 @@ describe Admin::ReportsController do end + describe 'when report is scoped to a category' do + let(:category) { Fabricate(:category) } + let(:topic) { Fabricate(:topic, category: category) } + let(:other_topic) { Fabricate(:topic) } + + it 'should render the report as JSON' do + topic + other_topic + + xhr :get, :show, type: 'topics', category_id: category.id + + expect(response).to be_success + + report = JSON.parse(response.body)["report"] + + expect(report["type"]).to eq('topics') + expect(report["data"].count).to eq(1) + end + end + + describe 'when report is scoped to a group' do + let(:user) { Fabricate(:user) } + let(:other_user) { Fabricate(:user) } + let(:group) { Fabricate(:group) } + + it 'should render the report as JSON' do + other_user + group.add(user) + + xhr :get, :show, type: 'signups', group_id: group.id + + expect(response).to be_success + + report = JSON.parse(response.body)["report"] + + expect(report["type"]).to eq('signups') + expect(report["data"].count).to eq(1) + end + end + end end