Skip to content

Commit 12e94f4

Browse files
authored
Merge pull request #36 from DMPRoadmap/development
Development
2 parents ab274f0 + 5e20286 commit 12e94f4

File tree

80 files changed

+1250
-1269
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

80 files changed

+1250
-1269
lines changed

app/controllers/admin/suggested_answers_controller.rb renamed to app/controllers/admin/annotations_controller.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
module Admin
2-
class SuggestedAnswersController < Admin::ApplicationController
2+
class AnnotationsController < Admin::ApplicationController
33
# To customize the behavior of this controller,
44
# you can overwrite any of the RESTful actions. For example:
55
#
66
# def index
77
# super
8-
# @resources = SuggestedAnswer.
8+
# @resources = Annotation.
99
# page(params[:page]).
1010
# per(10)
1111
# end
1212

1313
# Define a custom finder by overriding the `find_resource` method:
1414
# def find_resource(param)
15-
# SuggestedAnswer.find_by!(slug: param)
15+
# Annotation.find_by!(slug: param)
1616
# end
1717

1818
# See https://administrate-prototype.herokuapp.com/customizing_controller_actions
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
class AnnotationsController < ApplicationController
2+
respond_to :html
3+
after_action :verify_authorized
4+
5+
#create annotations
6+
def admin_create
7+
@example_answer = Annotation.new(params[:annotation])
8+
authorize @example_answer
9+
if @example_answer.save
10+
redirect_to admin_show_phase_path(id: @example_answer.question.section.phase_id, section_id: @example_answer.question.section_id, question_id: @example_answer.question.id, edit: 'true'), notice: _('Information was successfully created.')
11+
else
12+
@section = @example_answer.question.section
13+
@phase = @section.phase
14+
@open = true
15+
@sections = @phase.sections
16+
@section_id = @section.id
17+
@question_id = @example_answer.question
18+
flash[:notice] = failed_create_error(@example_answer, _('example answer'))
19+
render "phases/admin_show"
20+
end
21+
end
22+
23+
24+
#update a example answer of a template
25+
def admin_update
26+
@example_answer = Annotation.includes(question: { section: {phase: :template}}).find(params[:id])
27+
authorize @example_answer #.question.section.phase.template
28+
@question = @example_answer.question
29+
@section = @question.section
30+
@phase = @section.phase
31+
if @example_answer.update_attributes(params[:annotation])
32+
redirect_to admin_show_phase_path(id: @phase.id, section_id: @section.id, question_id: @question.id, edit: 'true'), notice: _('Information was successfully updated.')
33+
else
34+
flash[:notice] = failed_update_error(@example_answer, _('example answer'))
35+
render action: "phases/admin_show"
36+
end
37+
end
38+
39+
#delete an annotation
40+
def admin_destroy
41+
@example_answer = Annotation.includes(question: { section: {phase: :template}}).find(params[:id])
42+
authorize @example_answer
43+
@question = @example_answer.question
44+
@section = @question.section
45+
@phase = @section.phase
46+
if @example_answer.destroy
47+
redirect_to admin_show_phase_path(id: @phase.id, section_id: @section.id, edit: 'true'), notice: _('Information was successfully deleted.')
48+
else
49+
redirect_to admin_show_phase_path(id: @phase.id, section_id: @section.id, edit: 'true'), notice: flash[:notice] = failed_destroy_error(@example_answer, _('example answer'))
50+
end
51+
end
52+
53+
end

app/controllers/phases_controller.rb

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,16 @@ class PhasesController < ApplicationController
44
after_action :verify_authorized
55

66

7-
# GET /plans/:plan_id/phases/:id/edit
8-
def edit
7+
# GET /plans/:plan_id/phases/:id/edit
8+
def edit
99

1010
@plan = Plan.eager_load2(params[:plan_id])
11+
# authorization done on plan so found in plan_policy
1112
authorize @plan
1213

1314
phase_id = params[:id].to_i
1415
@phase = @plan.template.phases.select {|p| p.id == phase_id}.first
16+
@readonly = !@plan.editable_by?(current_user.id)
1517

1618
# the eager_load pulls in ALL answers
1719
# need to restrict to just ones for this plan
@@ -43,7 +45,7 @@ def edit
4345

4446
# create a map from theme to array of guidances
4547
# where guidance is a hash with the text and the org name
46-
theme_guidance = {}
48+
theme_guidance = {}
4749

4850
guidance_groups.each do |guidance_group|
4951
guidance_group.guidances.each do |guidance|
@@ -83,14 +85,14 @@ def edit
8385

8486
if !user_signed_in? then
8587
respond_to do |format|
86-
format.html { redirect_to edit_user_registration_path }
87-
end
88-
end
88+
format.html { redirect_to edit_user_registration_path }
89+
end
90+
end
8991

90-
end
92+
end
9193

9294

93-
# GET /plans/PLANID/phases/PHASEID/status.json
95+
# GET /plans/PLANID/phases/PHASEID/status.json
9496
def status
9597
@plan = Plan.eager_load(params[:plan_id])
9698
authorize @plan
@@ -110,9 +112,10 @@ def admin_show
110112
@phase = Phase.eager_load(:sections).find_by('phases.id = ?', params[:id])
111113
authorize @phase
112114

113-
@edit = (@phase.template.org == current_user.org)
115+
@current = Template.current(@phase.template.dmptemplate_id)
116+
@edit = (@phase.template.org == current_user.org) && (@phase.template == @current)
114117
#@edit = params[:edit] == "true" ? true : false
115-
118+
116119
#verify if there are any sections if not create one
117120
@sections = @phase.sections
118121
if !@sections.any?() || @sections.count == 0
@@ -158,13 +161,13 @@ def admin_add
158161
def admin_create
159162
@phase = Phase.new(params[:phase])
160163
authorize @phase
161-
164+
162165
@phase.description = params["phase-desc"]
163166
@phase.modifiable = true
164167
if @phase.save
165168
@phase.template.dirty = true
166169
@phase.template.save!
167-
170+
168171
redirect_to admin_show_phase_path(id: @phase.id, edit: 'true'), notice: _('Information was successfully created.')
169172
else
170173
flash[:notice] = failed_create_error(@phase, _('phase'))
@@ -182,15 +185,15 @@ def admin_update
182185
if @phase.update_attributes(params[:phase])
183186
@phase.template.dirty = true
184187
@phase.template.save!
185-
188+
186189
redirect_to admin_show_phase_path(@phase), notice: _('Information was successfully updated.')
187190
else
188191
@sections = @phase.sections
189192
@template = @phase.template
190193
# These params may not be available in this context so they may need
191194
# to be set to true without the check
192195
@edit = true
193-
@open = !params[:section_id].nil?
196+
@open = !params[:section_id].nil?
194197
@section_id = (params[:section_id].nil? ? nil : params[:section_id].to_i)
195198
@question_id = (params[:question_id].nil? ? nil : params[:question_id].to_i)
196199
flash[:notice] = failed_update_error(@phase, _('phase'))
@@ -206,15 +209,15 @@ def admin_destroy
206209
if @phase.destroy
207210
@template.dirty = true
208211
@template.save!
209-
212+
210213
redirect_to admin_template_template_path(@template), notice: _('Information was successfully deleted.')
211214
else
212215
@sections = @phase.sections
213-
216+
214217
# These params may not be available in this context so they may need
215218
# to be set to true without the check
216219
@edit = true
217-
@open = !params[:section_id].nil?
220+
@open = !params[:section_id].nil?
218221
@section_id = (params[:section_id].nil? ? nil : params[:section_id].to_i)
219222
@question_id = (params[:question_id].nil? ? nil : params[:question_id].to_i)
220223
flash[:notice] = failed_destroy_error(@phase, _('phase'))

app/controllers/plans_controller.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ def edit
140140
authorize @plan
141141
# If there was no phase specified use the template's 1st phase
142142
@phase = (params[:phase].nil? ? @plan.template.phases.first : Phase.find(params[:phase]))
143-
@readonly = @plan.editable_by?(current_user.id)
143+
@readonly = !@plan.editable_by?(current_user.id)
144144
respond_to :html
145145
end
146146

app/controllers/questions_controller.rb

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,24 @@ class QuestionsController < ApplicationController
66
def admin_create
77
@question = Question.new(params[:question])
88
authorize @question
9-
@question.guidance = params["new-question-guidance"]
9+
example = @question.annotations.first
10+
if example.present?
11+
example.org_id = current_user.org_id
12+
example.example_answer!
13+
end
14+
if params["new-question-guidance"].present?
15+
guidance = @question.annotations.build
16+
guidance.text = params["new-question-guidance"]
17+
guidance.org_id = current_user.org_id
18+
guidance.guidance!
19+
guidance.save
20+
end
1021
@question.default_value = params["new-question-default-value"]
22+
@question.modifiable = true
1123
if @question.save
1224
@question.section.phase.template.dirty = true
1325
@question.section.phase.template.save!
14-
26+
1527
redirect_to admin_show_phase_path(id: @question.section.phase_id, section_id: @question.section_id, question_id: @question.id, edit: 'true'), notice: _('Information was successfully created.')
1628
else
1729
@edit = (@question.section.phase.template.org == current_user.org)
@@ -21,7 +33,7 @@ def admin_create
2133
@sections = @phase.sections
2234
@section_id = @question.section.id
2335
@question_id = @question.id
24-
36+
2537
flash[:notice] = failed_create_error(@question, _('question'))
2638
render template: 'phases/admin_show'
2739
end
@@ -31,22 +43,31 @@ def admin_create
3143
def admin_update
3244
@question = Question.find(params[:id])
3345
authorize @question
34-
@question.guidance = params["question-guidance-#{params[:id]}"]
46+
guidance = @question.get_guidance_annotation(current_user.org_id)
47+
if params["question-guidance-#{params[:id]}"].present?
48+
if guidance.blank?
49+
guidance = @question.annotations.build
50+
guidance.type = :guidance
51+
end
52+
guidance.text = params["question-guidance-#{params[:id]}"]
53+
guidance.save
54+
end
3555
@question.default_value = params["question-default-value-#{params[:id]}"]
3656
@section = @question.section
3757
@phase = @section.phase
58+
template = @phase.template
3859
if @question.update_attributes(params[:question])
39-
@question.section.phase.template.dirty = true
40-
@question.section.phase.template.save!
41-
60+
@phase.template.dirty = true
61+
@phase.template.save!
62+
4263
redirect_to admin_show_phase_path(id: @phase.id, section_id: @section.id, question_id: @question.id, edit: 'true'), notice: _('Information was successfully updated.')
4364
else
4465
@edit = (@phase.template.org == current_user.org)
4566
@open = true
4667
@sections = @phase.sections
4768
@section_id = @section.id
4869
@question_id = @question.id
49-
70+
5071
flash[:notice] = failed_update_error(@question, _('question'))
5172
render template: 'phases/admin_show'
5273
end

app/controllers/registrations_controller.rb

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,10 @@ def create
4040
redirect_to after_sign_up_error_path_for(resource), alert: _('You must accept the terms and conditions to register.')
4141
else
4242
existing_user = User.find_by_email(sign_up_params[:email])
43-
if !existing_user.nil? then
44-
if (existing_user.password == "" || existing_user.password.nil?) && existing_user.confirmed_at.nil? then
45-
@user = existing_user
46-
do_update(false, true)
43+
if !existing_user.nil? # If email exists
44+
if (existing_user.password == "" || existing_user.password.nil?) && existing_user.confirmed_at.nil? # If user has not accepted invitation yet
45+
existing_user.destroy # Only solution for now
46+
super
4747
else
4848
redirect_to after_sign_up_error_path_for(resource), alert: _('That email address is already registered.')
4949
end
@@ -91,16 +91,16 @@ def needs_password?(user, params)
9191
end
9292

9393
def do_update(require_password = true, confirm = false)
94-
if require_password # user is changing email or password
95-
if current_user.email != params[:user][:email] # if user changing email
96-
if params[:user][:current_password].blank? # password needs to be present
94+
if require_password # user is changing email or password
95+
if current_user.email != params[:user][:email] # if user is changing email
96+
if params[:user][:current_password].blank? # password needs to be present
9797
message = _('Please enter your password to change email address.')
9898
successfully_updated = false
9999
else
100100
successfully_updated = current_user.update_with_password(password_update)
101101
end
102-
elsif params[:user][:password].present? # user is changing password
103-
successfully_updated = false # shared across first 3 conditions
102+
elsif params[:user][:password].present? # if user is changing password
103+
successfully_updated = false # shared across first 3 conditions
104104
if params[:user][:current_password].blank?
105105
message = _('Please enter your current password')
106106
elsif params[:user][:password_confirmation].blank?
@@ -110,10 +110,10 @@ def do_update(require_password = true, confirm = false)
110110
else
111111
successfully_updated = current_user.update_with_password(password_update)
112112
end
113-
else # potentially unreachable... but I dont like to leave off the else
113+
else # potentially unreachable... but I dont like to leave off the else
114114
successfully_updated = current_user.update_with_password(password_update)
115115
end
116-
else # password not required
116+
else # password not required
117117
successfully_updated = current_user.update_without_password(update_params)
118118
end
119119

@@ -125,7 +125,7 @@ def do_update(require_password = true, confirm = false)
125125
#render the correct page
126126
if successfully_updated
127127
if confirm
128-
current_user.skip_confirmation!
128+
current_user.skip_confirmation! # will error out if confirmable is turned off in user model
129129
current_user.save!
130130
end
131131
session[:locale] = current_user.get_locale unless current_user.get_locale.nil?

app/controllers/suggested_answers_controller.rb

Lines changed: 0 additions & 65 deletions
This file was deleted.

0 commit comments

Comments
 (0)