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

[WIP] 658 659 move data #685

Open
wants to merge 7 commits into
base: dev
Choose a base branch
from
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
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
8 changes: 5 additions & 3 deletions app/controllers/application_letters_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,13 @@ def new
flash[:event_id] = params[:event_id]
flash.keep(:event_id)
return redirect_to user_session_path, :alert => message
=begin
elsif not current_user.profile.present?
message = I18n.t('application_letters.fill_in_profile_before_creation')
flash[:event_id] = params[:event_id]
flash.keep(:event_id)
return redirect_to new_profile_path, :alert => message
=end
end

@application_letter = ApplicationLetter.new
Expand Down Expand Up @@ -84,10 +86,10 @@ def create
:recipients => current_user.email,
:reply_to => Rails.configuration.reply_to_address,
:subject => I18n.t('controllers.application_letters.confirmation_mail.subject'),
:content => I18n.t("controllers.application_letters.confirmation_mail.content_#{current_user.profile.gender}",
:content => I18n.t("controllers.application_letters.confirmation_mail.content_#{@application_letter.gender}", #TODO: Fall für 'andere'
:seminar_name => seminar_name,
:first_name => current_user.profile.first_name,
:last_name => current_user.profile.last_name,
:first_name => @application_letter.first_name,
:last_name => @application_letter.last_name,
:event_link => application_letters_url)
}
@email = Email.new(email_params)
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: 2 additions & 0 deletions app/controllers/profiles_controller.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
=begin
class ProfilesController < ApplicationController
load_and_authorize_resource

Expand Down Expand Up @@ -65,3 +66,4 @@ def profile_params
params.require(:profile).permit(Profile.allowed_params)
end
end
=end
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
2 changes: 2 additions & 0 deletions app/helpers/application_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,12 @@ def dropdown_items
o = ''
# everyone gets settings
o << (menu_item t(:settings, scope: 'navbar'), edit_user_registration_path)
=begin
# everyone gets their profile, if it exists
unless current_user.profile.present?
o << (menu_item t(:create_profile, scope: 'navbar'), new_profile_path)
end
=end
# pupils get their applications
if current_user.role == "pupil"
o << (menu_item t(:my_events, scope: 'navbar'), application_letters_path)
Expand Down
8 changes: 1 addition & 7 deletions app/models/ability.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,8 @@ def initialize(user)


if user.role? :pupil
# Pupils can only edit their own profiles
can [:new, :create], Profile
can [:index, :show, :edit, :update, :destroy], Profile, user: { id: user.id }
# Pupils can only edit their own applications
if user.profile.present?
can [:new, :create], ApplicationLetter
end
can [:new, :create], ApplicationLetter
can [:index, :show, :edit, :update, :check, :destroy], ApplicationLetter, user: { id: user.id }
# Pupils can upload their letters of agreement
can [:create], AgreementLetter
Expand All @@ -60,7 +55,6 @@ def initialize(user)
cannot :check, ApplicationLetter
end
if user.role? :organizer
can [:index, :show], Profile
can [:index, :show, :view_and_add_notes, :update_status], ApplicationLetter
cannot :update, ApplicationLetter
can [:view_applicants, :edit_applicants, :view_participants, :print_applications,
Expand Down
75 changes: 72 additions & 3 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 @@ -76,7 +129,7 @@ def deadline_cannot_be_in_the_past
end
end

# Since EatingHabits are persited in booleans we need to generate a
# Since EatingHabits are persisted in booleans we need to generate a
# EatingHabitStateCode to allow sorting
# US 28_4.5

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
16 changes: 10 additions & 6 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 @@ -125,8 +130,7 @@ def requires_agreement_letter_for_event?(given_event)
# @param given_event [Event], age [Integer]
# @return [Boolean]
def older_than_required_age_at_start_date_of_event?(given_event, age)
return false unless self.profile
age_at_event_start = self.profile.age_at_time(given_event.start_date)
age_at_event_start = given_event.application_letters.find_by(user: self).age_at_time(given_event.start_date)
return age_at_event_start >= age
end

Expand All @@ -151,7 +155,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 +165,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
20 changes: 9 additions & 11 deletions app/views/application_letters/check.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,13 @@

<h3><%= t('.my_personal_data') %></h3>
<dl>
<dt><%= Profile.human_attribute_name(:name) %>:</dt>
<dd><%= @application_letter.user.profile.name %></dd>
<dt><%= Profile.human_attribute_name(:gender) %>:</dt>
<dd><%= t "profiles.genders.#{@application_letter.user.profile.gender}" %></dd>
<dt><%= Profile.human_attribute_name(:birth_date) %>:</dt>
<dd><%= l(@application_letter.user.profile.birth_date) %></dd>
<dt><%= Profile.human_attribute_name(:address) %>:</dt>
<dd><%= @application_letter.user.profile.address %></dd>
</dl>
<%= link_to t('.edit', :default => t("helpers.links.edit")),
edit_profile_path(@application_letter.user.profile), id: 'edit_profile_link', class: 'btn btn-default' %>
<dt><%= Application_letter.human_attribute_name(:name) %>:</dt>
<dd><%= @application_letter.name %></dd>
<dt><%= Application_letter.human_attribute_name(:gender) %>:</dt>
<dd><%= t "profiles.genders.#{@application_letter.gender}" %></dd>
<dt><%= Application_letter.human_attribute_name(:birth_date) %>:</dt>
<dd><%= l(@application_letter.birth_date) %></dd>
<dt><%= Application_letter.human_attribute_name(:address) %>:</dt>
<dd><%= @application_letter.address %></dd>
</dl> <!-- TODO: search for missing </dl> -->
</div>
18 changes: 9 additions & 9 deletions app/views/application_letters/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,20 @@
<h1><%=t '.title', :default => model_class.model_name.human.titleize %></h1>
</div>

<% if can? :show, @application_letter.user.profile %>
<h3><%= link_to @application_letter.user.profile.name, profile_path(@application_letter.user.profile) %></h3>
<% if can? :show, @application_letter.user.profile %> <!-- TODO: show information that used to be in the profile here if user has the right to see it-->
<h3><%= link_to @application_letter.name, profile_path(@application_letter.user.profile) %></h3>
<% else %>
<h3><%= @application_letter.user.profile.name %></h3>
<h3><%= @application_letter.name %></h3>
<% end %>

<dl class="dl-horizontal" id="applicant_details">
<dt><strong><%= Profile.human_attribute_name(:gender) %>:</strong></dt>
<dd><%= t("profiles.genders.#{@application_letter.user.profile.gender}") %></dd>
<dt><strong><%= ApplicationLetter.human_attribute_name(:gender) %>:</strong></dt>
<dd><%= t("profiles.genders.#{@application_letter.gender}") %></dd>
<dt><strong><%= t('.age_when_event_starts') %>:</strong></dt>
<dd><%= @application_letter.user.profile.age_at_time(@application_letter.event.start_date) %></dd>
<dd><%= @application_letter.age_at_time(@application_letter.event.start_date) %></dd>
<% if can? :view_personal_details, ApplicationLetter %>
<dt><strong><%= Profile.human_attribute_name(:address) %>:</strong></dt>
<dd><%= @application_letter.user.profile.address %></dd>
<dt><strong><%= ApplicationLetter.human_attribute_name(:address) %>:</strong></dt>
<dd><%= @application_letter.address %></dd>
<% end %>
<dt><strong><%= User.human_attribute_name(:accepted_application_count) %>:</strong></dt>
<dd><%= @application_letter.user.accepted_applications_count(@application_letter.event) %></dd>
Expand Down Expand Up @@ -69,7 +69,7 @@
<% if @application_note.errors.any? %>
<div id="error_expl" class="panel panel-danger">
<div class="panel-heading">
<h3 class="panel-title"><%= pluralize(@application_note.errors.count, "error") %> prohibited this application from being saved:</h3>
<h3 class="panel-title"><%= pluralize(@application_note.errors.count, "error") %> prohibited this application from being saved:</h3> <!-- TODO: translation? or is it included in pluralize?-->
</div>
<div class="panel-body">
<ul>
Expand Down
Loading