Skip to content

Re-activate Gender field + chart #489

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 10, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions app/controllers/statistics_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ def statistics_inactive_users
def statistics_demographics
@members = current_organization.members
@age_counts = age_counts
@gender_counts = gender_counts
end

def statistics_last_login
Expand Down Expand Up @@ -168,4 +169,16 @@ def age_counts
counts[age_label] += 1
end
end

def gender_counts
@members.each_with_object(Hash.new(0)) do |member, counts|
gender = member.user_gender
gender_label = if gender.present?
t("simple_form.options.user.gender.#{gender}")
else
t("statistics.statistics_demographics.unknown")
end
counts[gender_label] += 1
end
end
end
4 changes: 4 additions & 0 deletions app/helpers/users_helper.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
module UsersHelper
def genders_collection
User::GENDERS.map(&:to_sym)
end

def phone_to(phone)
link_to phone, "tel://#{phone}"
end
Expand Down
9 changes: 7 additions & 2 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ class User < ActiveRecord::Base

devise *[
:database_authenticatable,
# :registerable,
:recoverable,
:rememberable,
:confirmable,
Expand All @@ -12,7 +11,12 @@ class User < ActiveRecord::Base
:timeoutable
]

GENDERS = %w[male female]
GENDERS = %w(
female
male
other
prefer_not_to_answer
)

attr_accessor :empty_email

Expand All @@ -38,6 +42,7 @@ class User < ActiveRecord::Base
# emails like 'without email'
validates_format_of :email,
with: /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\Z/i
validates :gender, inclusion: { in: GENDERS, allow_blank: true }

def as_member_of(organization)
organization && members.find_by(organization: organization)
Expand Down
40 changes: 30 additions & 10 deletions app/views/statistics/statistics_demographics.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,15 @@
</h1>
<div class="panel panel-default">
<div class="panel-body">
<div id="pie"></div>
<div id="pie-ages"></div>
<div id="pie-gender"></div>
</div>
</div>

<script type="text/javascript">

$(function () {
// Demograficos
// Por edades
var ages = <%= raw @age_counts.to_a.to_json %>;
$('#pie').highcharts({
chart: {
plotBackgroundColor: null,
plotBorderWidth: null,
plotShadow: false
},
$('#pie-ages').highcharts({
title: {
text: "<%= t '.by_ages' %>"
},
Expand All @@ -43,5 +36,32 @@
data: ages
}]
});

var genders = <%= raw @gender_counts.to_a.to_json %>;
$('#pie-gender').highcharts({
title: {
text: "<%= t '.by_gender' %>"
},
tooltip: {
pointFormat: '{series.name}: <b>{point.y}</b>'
},
plotOptions: {
pie: {
allowPointSelect: true,
cursor: 'pointer',
dataLabels: {
enabled: true,
color: '#000000',
connectorColor: '#000000',
format: '<b>{point.name}</b>: {point.percentage:.1f} %'
}
}
},
series: [{
type: 'pie',
name: '<%= t '.num_people' %>',
data: genders
}]
});
});
</script>
1 change: 1 addition & 0 deletions app/views/users/_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

<%= f.input :phone %>
<%= f.input :alt_phone %>
<%= f.input :gender, collection: genders_collection %>
<%= f.input :date_of_birth,
start_year: Date.today.year - 90,
end_year: Date.today.year - 12,
Expand Down
2 changes: 2 additions & 0 deletions config/locales/ca.yml
Original file line number Diff line number Diff line change
Expand Up @@ -435,6 +435,8 @@ ca:
gender:
female: Dona
male: Home
other: Altre
prefer_not_to_answer: Prefereixo no contestar
required:
mark: "*"
text: required
Expand Down
2 changes: 2 additions & 0 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -435,6 +435,8 @@ en:
gender:
female: Female
male: Male
other: Other
prefer_not_to_answer: I prefer not to answer
required:
mark: "*"
text: required
Expand Down
2 changes: 2 additions & 0 deletions config/locales/es.yml
Original file line number Diff line number Diff line change
Expand Up @@ -435,6 +435,8 @@ es:
gender:
female: Mujer
male: Hombre
other: Otro
prefer_not_to_answer: Prefiero no contestar
required:
mark: "*"
text: necesario
Expand Down
2 changes: 2 additions & 0 deletions config/locales/eu.yml
Original file line number Diff line number Diff line change
Expand Up @@ -441,6 +441,8 @@ eu:
gender:
female: Emakumea
male: Gizona
other:
prefer_not_to_answer:
required:
mark: "*"
text: Beharrezkoa
Expand Down
2 changes: 2 additions & 0 deletions config/locales/gl.yml
Original file line number Diff line number Diff line change
Expand Up @@ -435,6 +435,8 @@ gl:
gender:
female: Muller
male: Home
other:
prefer_not_to_answer:
required:
mark: "*"
text: esixido
Expand Down
2 changes: 2 additions & 0 deletions config/locales/pt-BR.yml
Original file line number Diff line number Diff line change
Expand Up @@ -435,6 +435,8 @@ pt-BR:
gender:
female: Mulher
male: Homem
other:
prefer_not_to_answer:
required:
mark: "*"
text: Necessário
Expand Down
2 changes: 2 additions & 0 deletions spec/models/user_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

it { is_expected.to validate_presence_of :username }

it { is_expected.to validate_inclusion_of(:gender).in_array(User::GENDERS) }

describe "#setup_and_save_user" do
it "sets a fake email before attempting to save user" do
user = Fabricate.build(:user, email: "")
Expand Down