Skip to content

Commit f525c72

Browse files
authored
Merge pull request #669 from DMPRoadmap/development
v0.3.4
2 parents d9085e7 + cbe3cf0 commit f525c72

File tree

7 files changed

+95
-76
lines changed

7 files changed

+95
-76
lines changed

app/controllers/phases_controller.rb

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@ class PhasesController < ApplicationController
77
# GET /plans/:plan_id/phases/:id/edit
88
def edit
99

10-
@plan = Plan.eager_load2(params[:plan_id])
10+
@plan = Plan.load_for_phase(params[:plan_id], params[:id])
1111
# authorization done on plan so found in plan_policy
1212
authorize @plan
1313

1414
phase_id = params[:id].to_i
15-
@phase = @plan.template.phases.select {|p| p.id == phase_id}.first
15+
@phase = @plan.template.phases.first
1616
@readonly = !@plan.editable_by?(current_user.id)
1717

1818
# Now we need to get all the themed guidance for the plan.
@@ -46,35 +46,35 @@ def edit
4646
end
4747
end
4848

49-
# create hash from question id to theme to guidance array
50-
# so when we arerendering a question we can grab the guidance out of this
51-
#
52-
# question_guidance = {
53-
# question.id => {
54-
# theme => [ {text: "......", org: "....."} ]
55-
# }
56-
# }
49+
questions = []
50+
# Appends all the questions for a given phase into questions Array.
51+
@phase.sections.each do |section|
52+
section.questions.each do |question|
53+
questions.push(question)
54+
end
55+
end
5756
@question_guidance = {}
58-
@plan.questions.each do |question|
57+
# Puts in question_guidance (key/value) entries where key is the question id and value is a hash.
58+
# Each question id hash has (key/value) entries where key is a theme and value is an Array of {text, org} objects
59+
# Example hash
60+
# question_guidance = { question.id =>
61+
# { theme => [ {text: "......", org: "....."} ] }
62+
# }
63+
questions.each do |question|
5964
qg = {}
6065
question.themes.each do |t|
6166
title = t.title
6267
qg[title] = theme_guidance[title] if theme_guidance.has_key?(title)
6368
end
64-
if !@question_guidance.has_key?(question.id)
65-
@question_guidance[question.id] = Array.new
66-
end
6769
@question_guidance[question.id] = qg
6870
end
6971

7072
if !user_signed_in? then
7173
respond_to do |format|
7274
format.html { redirect_to edit_user_registration_path }
73-
end
74-
end
75-
75+
end
7676
end
77-
77+
end
7878

7979
# GET /plans/PLANID/phases/PHASEID/status.json
8080
def status

app/controllers/roles_controller.rb

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,21 +8,22 @@ def create
88
authorize @role
99
access_level = params[:role][:access_level].to_i
1010
set_access_level(access_level)
11+
message = ''
1112
if params[:user].present?
1213
if @role.plan.owner.present? && @role.plan.owner.email == params[:user]
1314
flash[:notice] = _('Cannot share plan with %{email} since that email matches with the owner of the plan.') % {email: params[:user]}
1415
else
15-
if Role.find_by(plan: @role.plan, user: User.find_by(email: params[:user])) # role already exists
16+
user = User.where_case_insensitive('email',params[:user]).first
17+
if Role.find_by(plan: @role.plan, user: user) # role already exists
1618
flash[:notice] = _('Plan is already shared with %{email}.') % {email: params[:user]}
17-
else
18-
message = _('Plan shared with %{email}.') % {email: params[:user]}
19-
user = User.find_by(email: params[:user])
19+
else
2020
if user.nil?
2121
registered = false
2222
User.invite!(email: params[:user])
23-
message = _('Invitation to %{email} issued successfully.') % {email: params[:user]}
23+
message = _('Invitation to %{email} issued successfully. \n') % {email: params[:user]}
2424
user = User.find_by(email: params[:user])
2525
end
26+
message += _('Plan shared with %{email}.') % {email: user.email}
2627
@role.user = user
2728
if @role.save
2829
if registered then UserMailer.sharing_notification(@role, current_user).deliver_now end

app/models/plan.rb

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -990,16 +990,15 @@ def self.eager_load(id)
990990
]).find(id)
991991
end
992992

993-
def self.eager_load2(id)
993+
def self.load_for_phase(id, phase_id)
994994
Plan.includes(
995-
[{template: [
995+
[template: [
996996
{phases: {sections: {questions: [{answers: :notes}, :annotations, :question_format, :themes]}}},
997997
{customizations: :org},
998998
:org
999-
]},
1000-
{plans_guidance_groups: {guidance_group: {guidances: :themes}}},
1001-
{questions: :themes}
1002-
]).find(id)
999+
],
1000+
plans_guidance_groups: {guidance_group: {guidances: :themes}}
1001+
]).where(id: id, phases: { id: phase_id }).first
10031002
end
10041003

10051004

app/models/settings/template.rb

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
module Settings
22
class Template < RailsSettings::SettingObject
3-
3+
44
#attr_accessible :var, :target, :target_id, :target_type
55

66
VALID_FONT_FACES = [
7-
'Arial, Helvetica, Sans-Serif',
8-
'"Times New Roman", Times, Serif'
7+
'"Times New Roman", Times, Serif',
8+
'Arial, Helvetica, Sans-Serif'
99
]
1010

1111
VALID_FONT_SIZE_RANGE = (8..14)
@@ -17,13 +17,13 @@ class Template < RailsSettings::SettingObject
1717
DEFAULT_SETTINGS = {
1818
formatting: {
1919
margin: { # in millimeters
20-
top: 20,
21-
bottom: 20,
22-
left: 20,
23-
right: 20
20+
top: 10,
21+
bottom: 10,
22+
left: 10,
23+
right: 10
2424
},
2525
font_face: VALID_FONT_FACES.first,
26-
font_size: 12 # pt
26+
font_size: 10 # pt
2727
},
2828
max_pages: 3,
2929
fields: {

app/models/user.rb

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ class User < ActiveRecord::Base
66
# Include default devise modules. Others available are:
77
# :token_authenticatable, :confirmable,
88
# :lockable, :timeoutable and :omniauthable
9-
devise :invitable, :database_authenticatable, :registerable, :recoverable,
10-
:rememberable, :trackable, :validatable, :omniauthable,
9+
devise :invitable, :database_authenticatable, :registerable, :recoverable,
10+
:rememberable, :trackable, :validatable, :omniauthable,
1111
:omniauth_providers => [:shibboleth, :orcid]
1212

1313
##
@@ -26,27 +26,27 @@ def filter(query)
2626
q = "%#{query}%"
2727
conditions = t[:title].matches(q)
2828
columns = %i(
29-
grant_number identifier description principal_investigator data_contact
29+
grant_number identifier description principal_investigator data_contact
3030
)
3131
columns = ['grant_number', 'identifier', 'description', 'principal_investigator', 'data_contact']
3232
columns.each {|col| conditions = conditions.or(t[col].matches(q)) }
3333
self.where(conditions)
3434
end
3535
end
36-
36+
3737
has_many :user_identifiers
3838
has_many :identifier_schemes, through: :user_identifiers
3939

4040
##
4141
# Possibly needed for active_admin
4242
# -relies on protected_attributes gem as syntax depricated in rails 4.2
4343
#accepts_nested_attributes_for :roles
44-
#attr_accessible :password_confirmation, :encrypted_password, :remember_me,
45-
# :id, :email, :firstname, :last_login,:login_count, :orcid_id,
46-
# :password, :shibboleth_id, :user_status_id, :surname,
47-
# :user_type_id, :org_id, :skip_invitation, :other_organisation,
44+
#attr_accessible :password_confirmation, :encrypted_password, :remember_me,
45+
# :id, :email, :firstname, :last_login,:login_count, :orcid_id,
46+
# :password, :shibboleth_id, :user_status_id, :surname,
47+
# :user_type_id, :org_id, :skip_invitation, :other_organisation,
4848
# :accept_terms, :role_ids, :dmponline3, :api_token,
49-
# :organisation, :language, :language_id, :org, :perms,
49+
# :organisation, :language, :language_id, :org, :perms,
5050
# :confirmed_at, :org_id
5151

5252
validates :email, email: true, allow_nil: true, uniqueness: {message: _("must be unique")}
@@ -62,13 +62,13 @@ def filter(query)
6262
# What do they do? do they do it efficiently, and do we need them?
6363

6464
# Determines the locale set for the user or the organisation he/she belongs
65-
# @return String or nil
65+
# @return String or nil
6666
def get_locale
6767
if !self.language.nil?
6868
return self.language.abbreviation
6969
elsif !self.org.nil?
7070
return self.org.get_locale
71-
else
71+
else
7272
return nil
7373
end
7474
end
@@ -126,7 +126,7 @@ def org_id=(new_org_id)
126126
def organisation=(new_org)
127127
org_id = new_org.id unless new_org.nil?
128128
end
129-
129+
130130
##
131131
# checks if the user is a super admin
132132
# if the user has any privelege which requires them to see the super admin page
@@ -144,7 +144,7 @@ def can_super_admin?
144144
#
145145
# @return [Boolean] true if the user is an organisation admin
146146
def can_org_admin?
147-
return self.can_grant_permissions? || self.can_modify_guidance? ||
147+
return self.can_grant_permissions? || self.can_modify_guidance? ||
148148
self.can_modify_templates? || self.can_modify_org_details?
149149
end
150150

@@ -223,7 +223,7 @@ def org_type
223223
return org_type
224224
end
225225
=end
226-
226+
227227
##
228228
# removes the api_token from the user
229229
# modifies the user model
@@ -254,11 +254,11 @@ def keep_or_generate_token!
254254
# --------------------------------------------------------------
255255
def self.from_omniauth(auth)
256256
scheme = IdentifierScheme.find_by(name: auth.provider.downcase)
257-
257+
258258
if scheme.nil?
259259
throw Exception.new('Unknown OAuth provider: ' + auth.provider)
260260
else
261-
joins(:user_identifiers).where('user_identifiers.identifier': auth.uid,
261+
joins(:user_identifiers).where('user_identifiers.identifier': auth.uid,
262262
'user_identifiers.identifier_scheme_id': scheme.id).first
263263
end
264264
end
@@ -269,7 +269,14 @@ def self.from_omniauth(auth)
269269
def deliver_invitation(options = {})
270270
super(options.merge(subject: _('A Data Management Plan in %{application_name} has been shared with you') % {application_name: Rails.configuration.branding[:application][:name]}))
271271
end
272-
272+
##
273+
# Case insensitive search over User model
274+
# @param field [string] The name of the field being queried
275+
# @param val [string] The string to search for, case insensitive
276+
# @return [ActiveRecord::Relation] The result of the search
277+
def self.where_case_insensitive(field, val)
278+
User.where("lower(#{field}) = ?", val.downcase)
279+
end
273280

274281
# TODO: Remove this, its never called.
275282
# this generates a reset password link for a given user
@@ -278,12 +285,12 @@ def deliver_invitation(options = {})
278285
=begin
279286
def reset_password_link
280287
raw, enc = Devise.token_generator.generate(self.class, :reset_password_token)
281-
self.reset_password_token = enc
288+
self.reset_password_token = enc
282289
self.reset_password_sent_at = Time.now.utc
283290
save(validate: false)
284291
285292
edit_user_password_path + '?reset_password_token=' + raw
286293
end
287294
=end
288-
295+
289296
end

db/migrate/20170702012742_ensure_indexes_in_place.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
class EnsureIndexesInPlace < ActiveRecord::Migration
22
def change
33
#users_perms
4+
remove_foreign_key :users_perms, :perms
5+
remove_foreign_key :users_perms, :users
46
remove_index :users_perms, name: 'index_users_perms_on_user_id_and_perm_id'
57
add_index :users_perms, :user_id
8+
add_foreign_key :users_perms, :perms
9+
add_foreign_key :users_perms, :users
610
#user_identifiers
711
add_index :user_identifiers, :user_id
812
#roles
@@ -27,14 +31,22 @@ def change
2731
#annotations
2832
add_index :annotations, :question_id
2933
#question_themes
34+
remove_foreign_key :questions_themes, :questions
35+
remove_foreign_key :questions_themes, :themes
3036
remove_index :questions_themes, name: 'question_theme_index'
3137
remove_index :questions_themes, name: 'theme_question_index'
3238
add_index :questions_themes, :question_id
39+
add_foreign_key :questions_themes, :questions
40+
add_foreign_key :questions_themes, :themes
3341
#question_options
3442
add_index :question_options, :question_id
3543
#answers_question_options
44+
remove_foreign_key :answers_question_options, :answers
45+
remove_foreign_key :answers_question_options, :question_options
3646
remove_index :answers_question_options, name: 'answer_question_option_index'
3747
remove_index :answers_question_options, name: 'question_option_answer_index'
3848
add_index :answers_question_options, :answer_id
49+
add_foreign_key :answers_question_options, :answers
50+
add_foreign_key :answers_question_options, :question_options
3951
end
4052
end

0 commit comments

Comments
 (0)