Skip to content
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

Accept terms of use #322

Open
wants to merge 9 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 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
10 changes: 10 additions & 0 deletions app/assets/stylesheets/application.css.scss
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,16 @@ form.schedule {
margin: 10px;
}

.term-of-use {
margin-top: $header-height + 10px;
margin-bottom: $footer-height;
padding: 10px;
}

.term-of-use-button {
margin: 10px;
float:right;
gahermar marked this conversation as resolved.
Show resolved Hide resolved
}

.kid-mentor-schedules {
.header {
Expand Down
5 changes: 5 additions & 0 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ class ApplicationController < ActionController::Base

before_action :load_site_configuration
before_action :logout_inactive
before_action :terms_of_use
gahermar marked this conversation as resolved.
Show resolved Hide resolved
before_action :authenticate_user!
before_action :intercept_sensitive_params!
protect_from_forgery prepend: true, with: :exception
Expand Down Expand Up @@ -37,6 +38,10 @@ def load_site_configuration
@site = Site.load
end

def terms_of_use
@content = Site.load.terms_of_use_content_parsed
end

def logout_inactive
return true if 'sessions' == controller_name
return true if controller_name == 'self_registrations'
Expand Down
9 changes: 8 additions & 1 deletion app/controllers/sites_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,14 @@ def show
end

def update
if @site.update(site_params)
@site.attributes = site_params
if(@site.terms_of_use_content_changed?)
gahermar marked this conversation as resolved.
Show resolved Hide resolved
id = current_user.id
users = User.where("id != ?", id)
gahermar marked this conversation as resolved.
Show resolved Hide resolved
users.update_all(terms_of_use_accepted: false)

Choose a reason for hiding this comment

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

This is one possible way of course. What would you think about a solution that uses terms_accepted_at (DateTime) on a user model - this way you would simply compare updated_at on Site with the terms_accepted_at on a model to check whether the user has accepted the updated terms.

Copy link
Author

Choose a reason for hiding this comment

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

Yeah this solution is nice to. I following instructions in task (#317)
And it depend ... if we need to save date of accepted it will be better your solution. but if we wouldn't need it, i think solution with boolean is better .... for example when we need check in DB who accept and who not it will be more readeble when we have this data. Of course we can have both or it is just simple sql query to show it but if we no need date of accept i think it is useles.

Copy link
Owner

@panterch panterch Mar 23, 2022

Choose a reason for hiding this comment

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

Since you introduced terms_of_use_accepted_date the boolean field terms_of_use_accepted is a duplicate. I'd would stay with terms_of_use_accepted_date on the user and simply add a virtual field on user (pseudo code):

def terms_of_use_accepted
  user.terms_of_use_accepted_date < site.terms_of_use_accepted_date 
end

Then you don't have to update all database rows when the terms change.

Copy link
Author

Choose a reason for hiding this comment

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

yes, good tip. I changed it..

end

if @site.save
Copy link

@jiri1337 jiri1337 Mar 23, 2022

Choose a reason for hiding this comment

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

I see one little issue here - What if theoretically speaking saving failed. In this case, we would still set terms_of_use_accepted to false for all users.
This is not a big issue but it could maybe be addressed as well.

Copy link
Owner

Choose a reason for hiding this comment

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

There's a convention to end date fields with _at. Maybe you rename this to site.terms_of_use_changed_at to make the code more readable.

Copy link
Author

Choose a reason for hiding this comment

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

Ok. when i changed logic to dates it is unnecessarily now.

i changed column names to _at

redirect_to edit_site_url, notice: I18n.t('crud.action.update_success')
else
render action: :edit
Expand Down
19 changes: 19 additions & 0 deletions app/controllers/users_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
class UsersController < ApplicationController

def edit_terms
if(current_user != nil)

Choose a reason for hiding this comment

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

This project is using cancancan and devise - I think there are better ways to ensure the user is not nil or that the user has to be authorized to perform this action.
This of course will work but it's not the cleanest solution.

Also, you could use .nil? and you don't need to find the user when you have current_user available in this context - it already has found the user for you.

Copy link
Author

Choose a reason for hiding this comment

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

I must say i need to look more on cancancan ....

i was repair nil and remove finding user.

user = User.find_by(id: current_user.id)
user.update(terms_of_use_accepted: true)
end

if current_user.is_a?(Mentor) && current_user.kids.empty?
# if a menotr has no kids yet assigned, go to available kids
redirect_to available_kids_path
elsif current_user.is_a?(Teacher) && current_user.mentor_matchings.pluck(:state).include?('pending')

Choose a reason for hiding this comment

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

This is a way but the performance could be better-using database query -> using where or exists?

Copy link
Author

Choose a reason for hiding this comment

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

I came out of the solution which is in application_controller.
And i don't understand what you mean with DB query. I thought DB queries are more difficult..

Choose a reason for hiding this comment

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

Ah I meant something in this manner (using Activerecord) current_user.mentor_matchings.exists?(state: 'pending')

Copy link
Author

Choose a reason for hiding this comment

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

Ah this. ok. I changed it. And now question - we have same things in application controller after login. Change it too? I just ask because it is not my code but on the other side it is improve.

# if teacher has some pending requests from mentors
redirect_to mentor_matchings_path
else
redirect_to root_path
end
end
end
65 changes: 37 additions & 28 deletions app/views/layouts/application.html.haml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

!!!
%html{lang: "de"}
%head
Expand Down Expand Up @@ -46,39 +47,47 @@
%li.divider
%li= link_to 'Abmelden', destroy_user_session_path, method: :delete


#sidebar-wrapper
- if (user_signed_in? && current_user.terms_of_use_accepted == false)
gahermar marked this conversation as resolved.
Show resolved Hide resolved
.container
#sidebar.hidden-xs.hidden-sm
%h1.logo
- if @site.logo.present?
%img{:src => rails_representation_url(@site.logo_medium)}
.term-of-use
%h1
= t('activerecord.attributes.site.terms_of_use_accept.terms_of_use_headline')
= @content&.html_safe
= button_to t('activerecord.attributes.site.terms_of_use_accept.button_accept'), { action: "edit_terms", :controller=>"users", :method => :get }, { class: 'btn btn-xs btn-success term-of-use-button' }

- if user_signed_in?
.contextual_links_panel.panel.panel-default.hidden-xs.hidden-sm
= render 'layouts/actions'
= yield :sidebar
-else
gahermar marked this conversation as resolved.
Show resolved Hide resolved
#sidebar-wrapper
.container
#sidebar.hidden-xs.hidden-sm
%h1.logo
- if @site.logo.present?
%img{:src => rails_representation_url(@site.logo_medium)}

#main
.container
.row
#content.col-md-offset-3.col-md-9
- flash.each do |name, msg|
= boot_alert(msg, name) unless msg.blank?
= yield
- if user_signed_in?
.contextual_links_panel.panel.panel-default.hidden-xs.hidden-sm
= render 'layouts/actions'
= yield :sidebar

.container
.row.visible-xs.visible-sm.hidden-md.hidden-lg
- if user_signed_in?
.contextual_links_panel.mobile-actions.panel.panel-default
= render 'layouts/actions'
#main
.container
.row
#content.col-md-offset-3.col-md-9
- flash.each do |name, msg|
= boot_alert(msg, name) unless msg.blank?
= yield

- if @site.footer_address.present? || @site.footer_email.present?
#footer
.container
.col-md12
= @site.footer_address
- if @site.footer_email.present?
= mail_to @site.footer_email, nil, :encode => "hex"
.row.visible-xs.visible-sm.hidden-md.hidden-lg
- if user_signed_in?
.contextual_links_panel.mobile-actions.panel.panel-default
= render 'layouts/actions'

- if @site.footer_address.present? || @site.footer_email.present?
#footer
.container
.col-md12
= @site.footer_address
- if @site.footer_email.present?
= mail_to @site.footer_email, nil, :encode => "hex"

-
3 changes: 3 additions & 0 deletions config/locales/future_kids.de.yml
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,9 @@ de:
public_signups_active: öffentliche Registrierungen aktiv
title: Titel des Browsertabs
css: Seitenspezifisches CSS
terms_of_use_accept:
terms_of_use_headline: "Bitte akzeptieren sie die geändertern Nutzungsbedingungen"
gahermar marked this conversation as resolved.
Show resolved Hide resolved
button_accept: "Nutzungsbedingungen Akzeptieren"
substitution:
kid: "Kind"
mentor: "Mentor/in"
Expand Down
5 changes: 5 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@
resources :teachers
resources :principals
resource :site
resource :user do
member do
post 'edit_terms'
end
end
resources :substitutions do
member do
put 'inactivate'
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddTermsOfUseAcceptedToUsers < ActiveRecord::Migration[6.1]
def change
add_column :users, :terms_of_use_accepted, :boolean, default: false
end
end
3 changes: 3 additions & 0 deletions spec/factories.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

factory :admin, class: 'Admin', parent: :user do
sequence(:email) { |n| "admin_#{n}@example.com" }
terms_of_use_accepted { true }
end

factory :mentor, class: 'Mentor', parent: :user do
Expand All @@ -38,6 +39,7 @@
city { 'city' }
dob { '1.1.1990' }
phone { '123456798' }
terms_of_use_accepted { true }

to_create { |instance| instance.save(validate: false) }
end
Expand All @@ -48,6 +50,7 @@
sequence(:prename) { |n| "Mentor prename#{n}" }
association :school
phone { '123456798' }
terms_of_use_accepted { true }
end

factory :principal, class: 'Principal', parent: :user do
Expand Down
28 changes: 28 additions & 0 deletions spec/requests/site_configuration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,32 @@
expect(page).to have_css('h1', text: 'last1, first1')
expect(page).to have_css('h2', text: 'Gesprächsdokumentationen')
end

Choose a reason for hiding this comment

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

It would be cool to check if

  • After updating the terms, the terms_of_use_accepted gets reset for other users that have previously accepted the terms
  • Checking whether the user's terms_of_use_accepted change from false to true on the database level

Copy link
Author

Choose a reason for hiding this comment

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

I think the first check what you want is test on line 29 - i create admin and teacher (both with akcepted = true) then i login with admin - did some change in terms of use and save. Logout and login with teacher and check if there is showed page with term of use.

Second check - i tried to do new test in same file on line 57 - similar function but at the end i was reload teacher and check terms_of_use_accepted. Did you mean that?

scenario 'After edit terms of use other users must accept it after login' do
@teacher = create(:teacher)
log_in(create(:admin))
visit edit_site_url
fill_in 'Nutzungsbedingungen', with: 'Terms of use'
click_button 'Seitenweite Konfiguration aktualisieren'
click_link 'Abmelden'

visit new_user_session_path
fill_in 'user_email', with: @teacher.email
fill_in 'user_password', with: @teacher.password
click_button 'Anmelden'

expect(page).to have_button('Nutzungsbedingungen Akzeptieren')
end

scenario 'User after login must accept terms of use' do
@teacher = create(:teacher, terms_of_use_accepted: false)

visit new_user_session_path
fill_in 'user_email', with: @teacher.email
fill_in 'user_password', with: @teacher.password
click_button 'Anmelden'

click_button 'Nutzungsbedingungen Akzeptieren'
expect(page).to have_content('Erfolgreich angemeldet')
end
end