Skip to content

Commit

Permalink
WIP prune profiles
Browse files Browse the repository at this point in the history
  • Loading branch information
bjrne committed Aug 9, 2017
1 parent 6517b9a commit 58634e4
Show file tree
Hide file tree
Showing 49 changed files with 319 additions and 541 deletions.
2 changes: 1 addition & 1 deletion app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def add_missing_permission_flashes
path = check_application_letter_path(application_letter)
flash.now[:warning] << "#{t('agreement_letters.please_upload', event: event.name)} <a class='btn btn-default btn-xs' href='#{path}'>
#{t('agreement_letters.upload')}
</a>".html_safe if current_user.older_than_required_age_at_start_date_of_event?(event, current_user.profile.age) && application_letter.accepted? && event.acceptances_have_been_sent?
</a>".html_safe if current_user.older_than_required_age_at_start_date_of_event?(event, current_user.age) && application_letter.accepted? && event.acceptances_have_been_sent?
end
end
end
Expand Down
6 changes: 3 additions & 3 deletions app/controllers/events_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -221,9 +221,9 @@ def participants_pdf

data = @application_letters.collect do |application_letter|
[
application_letter.user.profile.first_name,
application_letter.user.profile.last_name,
application_letter.user.profile.birth_date,
application_letter.first_name,
application_letter.last_name,
application_letter.birth_date,
application_letter.allergies
]
end
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/users_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ class UsersController < ApplicationController
# GET /users
def index
authorize! :index, User
@users = User.with_profiles.paginate(:page => params[:page], :per_page => 20)
@users = User.paginate(:page => params[:page], :per_page => 20)
if params[:search]
@users = User.search(params[:search]).paginate(:page => params[:page], :per_page => 20)
end
Expand Down
4 changes: 2 additions & 2 deletions app/helpers/applicants_overview_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ def sort_caret(label, attr)
def sort_application_letters

if params[:sort]
unless Profile.allowed_sort_methods.include? params[:sort].to_sym
unless ApplicationLetter.allowed_sort_methods.include? params[:sort].to_sym
raise CanCan::AccessDenied
else
@application_letters.sort_by! {|l| l.user.profile.send(params[:sort]) }
@application_letters.sort_by! {|l| l.send(params[:sort]) }
end
end

Expand Down
73 changes: 71 additions & 2 deletions app/models/application_letter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,18 @@
# status :integer not null
#
class ApplicationLetter < ActiveRecord::Base
POSSIBLE_GENDERS = ['male', 'female', 'other']

belongs_to :user
belongs_to :event

has_many :application_notes
serialize :custom_application_fields, Array

validates_presence_of :user, :event, :motivation, :emergency_number, :organisation
validates_presence_of :user, :event, :motivation, :emergency_number, :organisation, :first_name, :last_name, :gender,
:birth_date, :street_name, :zip_code, :city, :state, :country
validates_inclusion_of :gender, in: POSSIBLE_GENDERS
validate :birthdate_not_in_future
#Use 0 as default for hidden event applications
validates :vegetarian, :vegan, :allergic, inclusion: { in: [true, false] }
validates :vegetarian, :vegan, :allergic, exclusion: { in: [nil] }
Expand All @@ -28,6 +33,54 @@ class ApplicationLetter < ActiveRecord::Base
enum status: {accepted: 1, rejected: 0, pending: 2, alternative: 3, canceled: 4}
validates :status, inclusion: { in: statuses.keys }

# Returns true if the user is 18 years old or older
#
# @param none
# @return [Boolean] whether the user is an adult
def adult?()
self.birth_date.in_time_zone <= 18.years.ago
end

# Returns the age of the user based on the current date
#
# @param none
# @return [Int] for age as number of years
def age
return age_at_time(Time.zone.now)
end

# Returns the age of the user based on the given date
#
# @param none
# @return [Int] for age as number of years
def age_at_time (given_date)
given_date_is_before_birth_date = given_date.month > birth_date.month || (given_date.month == birth_date.month && given_date.day >= birth_date.day)
return given_date.year - birth_date.year - (given_date_is_before_birth_date ? 0 : 1)
end

# Returns the full name of the user
#
# @param none
# @return [String] of name
def name
first_name + " " + last_name
end

# Returns the address of the user
# in format: Street, Zip-Code City, State, Country
#
# @param none
# @return [String] of address
def address
street_name + ", " + zip_code + " " + city + ", " + state + ", " + country
end

private
def birthdate_not_in_future
if birth_date.present? and birth_date.in_time_zone > Date.current
errors.add(:birth_date, I18n.t('profiles.validation.birthday_in_future'))
end
end

# Returns an array of selectable statuses
#
Expand Down Expand Up @@ -132,7 +185,7 @@ def status_notification_sent_cannot_be_changed
# @param none
# @return [Int] for age as number of years
def applicant_age_when_event_starts
user.profile.age_at_time(event.start_date)
age_at_time(event.start_date)
end

# Returns an array of eating habits (including allergies, vegan and vegetarian)
Expand All @@ -150,4 +203,20 @@ def eating_habits
def allergic
not allergies.empty?
end

# Returns a list of allowed parameters.
#
# @param none
# @return [Symbol] List of parameters
def self.allowed_params
[:first_name, :last_name, :gender, :birth_date, :street_name, :zip_code, :city, :state, :country, :discovery_of_site] # TODO:
end

# Returns an array containing the allowed methods to sort by
#
# @param none
# @return [Symbol] List of methods
def self.allowed_sort_methods
ApplicationLetter.allowed_params + [:address, :name, :age]
end
end
6 changes: 3 additions & 3 deletions app/models/event.rb
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ def generate_participants_email(all, groups, users)
#
# @return [Array<Array<String, Int>>]
def participants_with_id
participants.map{|participant| [participant.profile.name, participant.id]}
application_letters.map{|application_letter| [application_letter.name, application_letter.user.id]}
end

# Returns a list of group names and their id
Expand Down Expand Up @@ -315,12 +315,12 @@ def application_letters_ordered(field, order_by)
when "email"
"users.email"
when "birth_date", "first_name", "last_name"
"profiles." + field
field
else
"users.email"
end
order_by = 'asc' unless order_by == 'asc' || order_by == 'desc'
application_letters.joins(user: :profile).order(field + ' ' + order_by)
application_letters.joins(user: :profile).order(field + ' ' + order_by) # TODO: by someone SQL-able
end

# Make sure any assignment coming from the controller
Expand Down
2 changes: 2 additions & 0 deletions app/models/profile.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
# updated_at :datetime not null
#
class Profile < ActiveRecord::Base
=begin
POSSIBLE_GENDERS = ['male', 'female', 'other']
belongs_to :user
Expand Down Expand Up @@ -82,4 +83,5 @@ def birthdate_not_in_future
errors.add(:birth_date, I18n.t('profiles.validation.birthday_in_future'))
end
end
=end
end
13 changes: 9 additions & 4 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,12 @@ class User < ActiveRecord::Base
:recoverable, :rememberable, :trackable, :validatable,
:omniauthable, :omniauth_providers => [:hpiopenid]

has_one :profile
has_many :agreement_letters
has_many :application_letters
has_many :participant_groups

belongs_to :user

before_create :set_default_role

ROLES = %i[pupil coach organizer admin]
Expand Down Expand Up @@ -58,8 +59,12 @@ def self.from_omniauth(auth)
end
end

# Returns the full name of the user if the first and last name exist, otherwise return email
#
# @param none
# @return [String] of name
def name
return profile.name if profile
return first_name + " " + last_name if first_name && last_name
email
end

Expand Down Expand Up @@ -151,7 +156,7 @@ def rejected_applications_count(event)
#
# @param pattern to search for
# @return [Array<User>] all users with pattern in their name
def self.search(pattern)
def self.search(pattern) # TODO: by SQL capable person. BTW, this is the only usage of with_profiles
with_profiles.where("profiles.first_name LIKE ? or profiles.last_name LIKE ?", "%#{pattern}%", "%#{pattern}%")
end

Expand All @@ -161,6 +166,6 @@ def self.search(pattern)
# @return [Array<User>] all users including their profile information
def self.with_profiles()
joins("LEFT JOIN profiles ON users.id = profiles.user_id")
.order('profiles.first_name, profiles.last_name, users.email ASC')
.order('profiles.first_name, profiles.last_name, users.email ASC') # TODO: probably remove this or just order it?
end
end
2 changes: 1 addition & 1 deletion config/locales/de.agreement_letters.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@ de:
corrupt_document: "PDF Dokument ist fehlerhaft"
upload_success: "Datei erfolgreich hochgeladen."
upload_failed: "Fehler beim Hochladen."
already_uploaded: "Einverständnigserklärung hochgeladen"
already_uploaded: "Einverständniserklärung hochgeladen"
write_failed: "Datei konnte nicht gespeichert werden."
please_upload: "Bitte lade deine Einverständniserklärung für \"%{event}\" hoch."
1 change: 0 additions & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
get 'send_participants_email'
end
end
resources :profiles, except: [:index, :destroy]

devise_scope :user do
get "/users/sign_up" => redirect("/users/sign_in")
Expand Down
16 changes: 1 addition & 15 deletions db/sample_data.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
require './db/sample_data/agreement_letters'
require './db/sample_data/application_letters'
require './db/sample_data/events'
require './db/sample_data/profiles'
require './db/sample_data/requests'
require './db/sample_data/users'
require './db/sample_data/email_templates'
Expand Down Expand Up @@ -31,19 +30,6 @@ def add_sample_data
users[:organizer] = user_organizer
users[:hpi_admin] = user_admin

profiles = Hash.new
profiles[:pupil] = profile_pupil(users[:pupil])
profiles[:teacher] = profile_teacher(users[:teacher])
profiles[:applicant] = profile_applicant(users[:applicant])

profiles[:tobi] = profile_tobi(users[:tobi])
profiles[:tobi] = profile_tobi(users[:tobi])
profiles[:lisa] = profile_lisa(users[:lisa])
profiles[:max] = profile_pupil_max(users[:max])
profiles[:organizer] = profile_organizer(users[:organizer])
profiles[:coach] = profile_coach(users[:coach])
profiles[:admin] = profile_admin(users[:hpi_admin])

application_letters = Hash.new
application_letters[:applicant_gongakrobatik] = application_letter_applicant_gongakrobatik(users[:applicant], events[:gongakrobatik])
application_letters[:applicant_gongakrobatik_past_deadline] = application_letter_applicant_gongakrobatik(users[:tobi], events[:past_deadline_event])
Expand Down Expand Up @@ -71,7 +57,7 @@ def add_sample_data
email_templates[:acceptance_template] = email_template_acceptance
email_templates[:rejection_template] = email_template_rejection

[events, users, profiles, application_letters, requests, agreement_letters, email_templates].each do |models|
[events, users, application_letters, requests, agreement_letters, email_templates].each do |models|
save_models(models)
end

Expand Down
Loading

0 comments on commit 58634e4

Please sign in to comment.