diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index b6936d1..a5335a9 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -3,8 +3,18 @@ class ApplicationController < ActionController::Base include Pundit + + before_action :configure_permitted_parameters, if: :devise_controller? + + rescue_from Pundit::NotAuthorizedError, with: :user_not_authorized + protected + + def configure_permitted_parameters + devise_parameter_sanitizer.permit(:account_update, keys: [:people_in_household]) + end + private def authorize_user_pundit_rights(record) @@ -19,4 +29,5 @@ def user_not_authorized(exception) default: :default) redirect_to root_path end + end diff --git a/app/controllers/graphs_controller.rb b/app/controllers/graphs_controller.rb index 74adf8e..015629a 100644 --- a/app/controllers/graphs_controller.rb +++ b/app/controllers/graphs_controller.rb @@ -4,6 +4,7 @@ class GraphsController < ApplicationController before_action :authenticate_user! def index + @date = Date.today @graph_presenter = GraphPresenter.new(current_user.id) end end diff --git a/app/models/user.rb b/app/models/user.rb index 66651f6..a68feb9 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -5,4 +5,6 @@ class User < ApplicationRecord # :confirmable, :lockable, :timeoutable, :trackable and :omniauthable devise :database_authenticatable, :registerable, :recoverable, :rememberable, :validatable + + validates :people_in_household, numericality: { only_integer: true, greater_than: 0, less_than: 15 } end diff --git a/app/views/devise/registrations/edit.haml b/app/views/devise/registrations/edit.haml index 02e11a6..7dc6fff 100644 --- a/app/views/devise/registrations/edit.haml +++ b/app/views/devise/registrations/edit.haml @@ -29,6 +29,10 @@ %i (we need your current password to confirm your changes) = f.password_field :current_password, autocomplete: "current-password", class: 'form-control' + .form-group + = f.label :people_in_household + %i (Tell us the number of people contributing to your waste in order to properly compute your per capita stats) + = f.text_field :people_in_household, class: 'form-control' .card-footer = f.submit "Update", class: 'btn btn-success' diff --git a/app/views/graphs/index.haml b/app/views/graphs/index.haml index 133b6bf..d45dfaa 100644 --- a/app/views/graphs/index.haml +++ b/app/views/graphs/index.haml @@ -11,17 +11,22 @@ .card .card-header - %h3 The current per head values for garabe + %h3= "Monthly Statistics: #{@date.strftime("%B %Y")}" .card-body %table.table %thead %tr - %th Kind - %th per head/per month + %th.text-left Kind + %th.text-left Your household (total) + %th.text-left Your household (per person) + %th.text-right Average household (per person) %th Source %tbody - GraphPresenter::GARBAGE.each do |key, value| + - household_weight = Pile.where(user_id: current_user.id).where(kind: key).group("DATE_TRUNC('month', produced_at)").sum(:weight).values.first %tr - %td= key + %td.text-left= key + %td.text-left="#{household_weight}g" + %td.text-left="#{household_weight/current_user.people_in_household}g" %td.text-right= "#{value[:limit]} g" - %td= link_to value[:source] + %td= link_to 'Source', value[:source] diff --git a/db/migrate/20210428202726_add_people_in_household_to_users.rb b/db/migrate/20210428202726_add_people_in_household_to_users.rb new file mode 100644 index 0000000..68ffd23 --- /dev/null +++ b/db/migrate/20210428202726_add_people_in_household_to_users.rb @@ -0,0 +1,8 @@ +class AddPeopleInHouseholdToUsers < ActiveRecord::Migration[6.1] + def change + add_column :users, :people_in_household, :integer, default: 1 + User.all.each do |user| + user.update_attribute(:people_in_household, 1) + end + end +end diff --git a/db/schema.rb b/db/schema.rb index 8cc2ef4..fb3956e 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 2021_03_12_124145) do +ActiveRecord::Schema.define(version: 2021_04_28_202726) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -45,6 +45,7 @@ t.datetime "remember_created_at" t.datetime "created_at", precision: 6, null: false t.datetime "updated_at", precision: 6, null: false + t.integer "people_in_household", default: 1 t.index ["email"], name: "index_users_on_email", unique: true t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true end