-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #68 from leifg/add_statistics
Add Statistics
- Loading branch information
Showing
15 changed files
with
16,988 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
var app = angular.module('Marozi', ['angles']); | ||
|
||
app.config(function($httpProvider) { | ||
var authToken = $('meta[name="csrf-token"]').attr('content'); | ||
$httpProvider.defaults.headers.common['X-CSRF-TOKEN'] = authToken; | ||
}); | ||
|
||
app.controller('MaleFemaleChartCtrl', function($scope, $http, $location) { | ||
var id = parseId($location.absUrl()); | ||
$http.get('/api/statistics/clubs/'+id+'/gender_ratio').success(function(data){ | ||
$scope.chart = [ | ||
{ | ||
value: data.female, | ||
color:"#F7464A", | ||
}, | ||
{ | ||
value: data.male, | ||
color:"#949FB1", | ||
}, | ||
]; | ||
}); | ||
}); | ||
|
||
function parseId(location){ | ||
var id = /(\d+)\/statistics/.exec(location)[1]; | ||
return id; | ||
} | ||
|
||
$(document).on('ready page:load', function() { | ||
angular.bootstrap(document, ['Marozi']); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
class StatisticsController < ApplicationController | ||
def gender_ratio | ||
type = params[:type] | ||
unless %w{clubs districts multiple_districts}.include?(type) | ||
render :status => :forbidden, :text => "Forbidden type: #{type.inspect}" \ | ||
and return | ||
end | ||
constant = Kernel.const_get(type.singularize.camelize) | ||
instance = constant.find_by!(oid: params[:id]) | ||
render json: instance.gender_ratio(:active) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
module MemberStatistics | ||
extend ActiveSupport::Concern | ||
|
||
def gender_ratio status | ||
f,m = members | ||
.select{|member| member.status == status} | ||
.partition{|member| member.gender == :female} | ||
{ | ||
female: f.size, | ||
male: m.size, | ||
} | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
.row{'ng-app' => 'Marozi'} | ||
.span4 | ||
%p.lead | ||
.info-box{'ng-controller' => 'MaleFemaleChartCtrl'} | ||
.info-box-caption{'ng-controller' => 'MaleFemaleChartCtrl'} | ||
=t(:'clubs.statistics.female_male') | ||
%canvas{piechart: '', options: 'options', data: 'chart', id: 'male_female_ratio', width: '300', height: '300'} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
require 'spec_helper' | ||
|
||
describe StatisticsController do | ||
context 'protected methods' do | ||
include_context 'protected controller' | ||
|
||
let(:existing_id) {300066} | ||
|
||
describe '#gender_ratio' do | ||
it 'dispatches correctly to club' do | ||
club_mock = double('Club') | ||
Club.stub(:find_by!).and_return(club_mock) | ||
expect(club_mock).to receive(:gender_ratio).and_return('correctly stubbed') | ||
get :gender_ratio, id: existing_id, type: 'clubs' | ||
expect(response.body).to eq 'correctly stubbed' | ||
end | ||
|
||
it 'dispatches correctly to district' do | ||
district_mock = double('District') | ||
District.stub(:find_by!).and_return(district_mock) | ||
expect(district_mock).to receive(:gender_ratio).and_return('correctly stubbed') | ||
get :gender_ratio, id: existing_id, type: 'districts' | ||
expect(response.body).to eq 'correctly stubbed' | ||
end | ||
|
||
it 'dispatches correctly to multiple district' do | ||
multiple_district_mock = double('MultipleDistrict') | ||
MultipleDistrict.stub(:find_by!).and_return(multiple_district_mock) | ||
expect(multiple_district_mock).to receive(:gender_ratio).and_return('correctly stubbed') | ||
get :gender_ratio, id: existing_id, type: 'multiple_districts' | ||
expect(response.body).to eq 'correctly stubbed' | ||
end | ||
|
||
it 'returns 403 for wrong type' do | ||
get :gender_ratio, id: existing_id, type: 'fancy_injection' | ||
expect(response.status).to eq 403 | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
require 'spec_helper' | ||
|
||
describe MemberStatistics do | ||
describe '#gender_ratio' do | ||
let(:active_members_only) do | ||
members = [] | ||
{ | ||
female: 11, | ||
male: 12, | ||
}.each do |gender, num| | ||
num.times do | ||
member = double('Member') | ||
member.stub(:gender){ gender } | ||
member.stub(:status){ :active } | ||
members << member | ||
end | ||
end | ||
|
||
o = double() | ||
o.stub(:members) { members } | ||
o.extend(described_class) | ||
end | ||
|
||
let(:active_inactive_members) do | ||
members = [] | ||
[ | ||
{ | ||
status: :active, | ||
gender: :female, | ||
num: 5, | ||
}, | ||
{ | ||
status: :passive, | ||
gender: :female, | ||
num: 9, | ||
}, | ||
{ | ||
status: :active, | ||
gender: :male, | ||
num: 3, | ||
}, | ||
{ | ||
status: :passive, | ||
gender: :male, | ||
num: 16, | ||
} | ||
].each do |hash| | ||
hash[:num].times do | ||
member = double('Member') | ||
member.stub(:gender){ hash[:gender] } | ||
member.stub(:status){ hash[:status] } | ||
members << member | ||
end | ||
end | ||
|
||
o = double() | ||
o.stub(:members) { members } | ||
o.extend(described_class) | ||
end | ||
|
||
it 'returns correct ratio for active only members' do | ||
expected_result = { | ||
female: 11, | ||
male: 12, | ||
} | ||
expect(active_members_only.gender_ratio(:active)).to eq(expected_result) | ||
end | ||
|
||
it 'returns correct ratio for mixed members active' do | ||
expected_result = { | ||
female: 5, | ||
male: 3, | ||
} | ||
expect(active_inactive_members.gender_ratio(:active)).to eq(expected_result) | ||
end | ||
|
||
it 'returns correct ratio for mixed members active' do | ||
expected_result = { | ||
female: 9, | ||
male: 16, | ||
} | ||
expect(active_inactive_members.gender_ratio(:passive)).to eq(expected_result) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
var angles = angular.module("angles", []); | ||
|
||
|
||
angles.chart = function (type) { | ||
return { | ||
restrict: "A", | ||
scope: { | ||
data: "=", | ||
options: "=", | ||
id: "@" | ||
}, | ||
link: function ($scope, $elem) { | ||
var ctx = $elem[0].getContext("2d"); | ||
var chart = new Chart(ctx); | ||
|
||
$scope.$watch("data", function (newVal, oldVal) { | ||
// if data not defined, exit | ||
if (!newVal) return; | ||
|
||
chart[type]($scope.data, $scope.options); | ||
}, true); | ||
} | ||
} | ||
} | ||
|
||
|
||
/* General Chart Wrapper */ | ||
angles.directive("chart", function () { | ||
return { | ||
restrict: "A", | ||
scope: { | ||
data: "=", | ||
type: "@", | ||
options: "=", | ||
id: "@" | ||
}, | ||
link: function ($scope, $elem) { | ||
var ctx = $elem[0].getContext("2d"); | ||
var chart = new Chart(ctx); | ||
|
||
$scope.$watch("data", function (newVal, oldVal) { | ||
chart[$scope.type]($scope.data, $scope.options); | ||
}, true); | ||
} | ||
} | ||
}); | ||
|
||
|
||
/* Aliases for various chart types */ | ||
angles.directive("linechart", function () { return angles.chart("Line"); }); | ||
angles.directive("barchart", function () { return angles.chart("Bar"); }); | ||
angles.directive("radarchart", function () { return angles.chart("Radar"); }); | ||
angles.directive("polarchart", function () { return angles.chart("PolarArea"); }); | ||
angles.directive("piechart", function () { return angles.chart("Pie"); }); | ||
angles.directive("donutchart", function () { return angles.chart("Doughnut"); }); |
Oops, something went wrong.