Skip to content
Open
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
11 changes: 11 additions & 0 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -19,4 +29,5 @@ def user_not_authorized(exception)
default: :default)
redirect_to root_path
end

end
1 change: 1 addition & 0 deletions app/controllers/graphs_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 2 additions & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
4 changes: 4 additions & 0 deletions app/views/devise/registrations/edit.haml
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand Down
15 changes: 10 additions & 5 deletions app/views/graphs/index.haml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should go to a presenter, but anywise my plan was also to refactor the whole logic, so maybe currently i see this as a optimization.

%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]
8 changes: 8 additions & 0 deletions db/migrate/20210428202726_add_people_in_household_to_users.rb
Original file line number Diff line number Diff line change
@@ -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|
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Normally this is not a good practise and i would think this will also fail, when you run rollback.

user.update_attribute(:people_in_household, 1)
end
end
end
3 changes: 2 additions & 1 deletion db/schema.rb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.