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

Finishes up final updates for template customization bug #1352

Merged
merged 10 commits into from
Apr 6, 2018
2 changes: 1 addition & 1 deletion app/controllers/answers_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ def create_or_update
"locking" => @stale_answer ?
render_to_string(partial: 'answers/locking', locals: { question: @question, answer: @stale_answer, user: @answer.user }, formats: [:html]) :
nil,
"form" => render_to_string(partial: 'answers/new_edit', locals: { template: template, question: @question, answer: @answer, readonly: false, locking: false }, formats: [:html]),
"form" => render_to_string(partial: 'answers/new_edit', locals: { template: template, question: @question, answer: @answer, readonly: false, locking: false, base_template_org: template.base_org }, formats: [:html]),
"answer_status" => render_to_string(partial: 'answers/status', locals: { answer: @answer}, formats: [:html])
},
"section" => {
Expand Down
2 changes: 2 additions & 0 deletions app/controllers/phases_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ def edit
answers = plan.answers.reduce({}){ |m, a| m[a.question_id] = a; m }

render('/phases/edit', locals: {
base_template_org: phase.template.base_org,
plan: plan, phase: phase, readonly: readonly,
question_guidance: plan.guidance_by_question_as_hash,
guidance_groups: guidance_groups,
Expand Down Expand Up @@ -80,6 +81,7 @@ def admin_preview
authorize @phase
@template = @phase.template
@current_tab = params[:r] || 'all-templates'
@base_template_org = @phase.template.base_org
end


Expand Down
2 changes: 1 addition & 1 deletion app/controllers/questions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ def admin_update
# The user cleared out the guidance value so delete the record
guidance.destroy! if guidance.present?
end
example_answer = @question.get_example_answer(current_user.org_id)
example_answer = @question.get_example_answers(current_user.org_id).first
if params["question"]["annotations_attributes"].present? && params["question"]["annotations_attributes"]["0"]["id"].present?
unless example_answer.present?
example_answer = Annotation.new(type: :example_answer, org_id: current_user.org_id, question_id: @question.id)
Expand Down
10 changes: 5 additions & 5 deletions app/models/question.rb
Original file line number Diff line number Diff line change
Expand Up @@ -108,11 +108,11 @@ def guidance_for_org(org)
##
# get example answer belonging to the currents user for this question
#
# @param org_id [Integer] the id for the organisation
# @return [String] the example answer for this question for the specified org
def get_example_answer(org_id)
example_answer = self.annotations.where(org_id: org_id).where(type: Annotation.types[:example_answer]).order(:created_at)
return example_answer.first
# @param org_ids [Array<Integer>] the ids for the organisations
# @return [Array<Annotation>] the example answers for this question for the specified orgs
def get_example_answers(org_ids)
org_ids = [org_ids] unless org_ids.is_a?(Array)
self.annotations.where(org_id: [org_ids], type: Annotation.types[:example_answer]).order(:created_at)
end

def first_example_answer
Expand Down
10 changes: 10 additions & 0 deletions app/models/template.rb
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,16 @@ def template_type
self.customization_of.present? ? _('customisation') : _('template')
end

# Retrieves the template's org or the org of the template this one is derived
# from of it is a customization
def base_org
if self.customization_of.present?
base_template_org = Template.where(dmptemplate_id: self.customization_of).first.org
else
base_template_org = self.org
end
end

# --------------------------------------------------------
private
# Initialize the published and dirty flags for new templates
Expand Down
13 changes: 9 additions & 4 deletions app/views/annotations/_show.html.erb
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
<% org = template.org.abbreviation.present? ? template.org.abbreviation : template.org.name %>
<% if guidance.present? %>
<% if example_answer.present? || guidance.present? %>
<dl<%= for_plan ? ' class="dl-horizontal"' : '' %>>
<% if example_answer.present? %>
<% label = (for_plan && template.customization_of.present?) ? _('%{org} Example Answer') % { org: example_answer.org.short_name } : _('Example Answer') %>
<dt><%= label %></dt>
<dd><%= raw example_answer.text %><dd>
<% end %>
<% if guidance.present? %>
<dt><%= template.customization_of.nil? ? _('Guidance') : _('%{org} Guidance') % { org: guidance.org.short_name } %></dt>
<% label = (for_plan && template.customization_of.present?) ? _('%{org} Guidance') % { org: guidance.org.short_name } : _('Guidance') %>
<dt><%= label %></dt>
<dd><%= raw guidance.text %></dd>
<% end %>
</dl>
<% end %>
<% end %>
22 changes: 11 additions & 11 deletions app/views/answers/_new_edit.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -64,18 +64,18 @@
<%= f.button(_('Save'), class: "btn btn-default", type: "submit") %>
</fieldset>
<!--Example Answer area -->
<% annotations = [question.first_example_answer] %>
<% annotations << question.get_example_answer(template.org.id) if template.present? && template.org.present? %>
<% annotations.each do |annotation| %>
<% if annotation.present? && annotation.org.present? %>
<div class="panel panel-default">
<span class="label label-default">
<%="#{annotation.org.abbreviation} "%> <%=_('example answer')%>
</span>
<div class="panel-body">
<%= raw annotation.text %>
<% if template.present? && template.org.present? %>
<% question.get_example_answers([base_template_org.id, template.org.id]).each do |annotation| %>
<% if annotation.present? && annotation.org.present? %>
<div class="panel panel-default">
<span class="label label-default">
<%="#{annotation.org.abbreviation} "%> <%=_('example answer')%>
</span>
<div class="panel-body">
<%= raw annotation.text %>
</div>
</div>
</div>
<% end %>
<% end %>
<% end %>
<% end %>
4 changes: 2 additions & 2 deletions app/views/phases/_edit_plan_answers.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@
<div class="question-form">
<div id="<%= "answer-locking-#{question.id}" %>" class="answer-locking"></div>
<div id="<%= "answer-form-#{question.id}" %>" class="answer-form">
<%= render(partial: '/answers/new_edit', locals: { template: phase.template, question: question, answer: answer, readonly: readonly, locking: false }) %>
<%= render(partial: '/answers/new_edit', locals: { template: phase.template, question: question, answer: answer, readonly: readonly, locking: false, base_template_org: base_template_org }) %>
</div>
<div id="<%= "answer-status-#{question.id}" %>" class="mt-10">
<%= render(partial: '/answers/status', locals: { answer: answer }) %>
Expand All @@ -72,7 +72,7 @@
</div>
<div class="col-md-4">
<!-- Guidances and notes partial view -->
<%= render partial: '/phases/guidances_notes', locals: { plan: plan, template: phase.template, question: question, answer: answer, question_guidance: question_guidance, guidance_groups: guidance_groups } %>
<%= render partial: '/phases/guidances_notes', locals: { plan: plan, template: phase.template, question: question, answer: answer, question_guidance: question_guidance, guidance_groups: guidance_groups, base_template_org: base_template_org } %>
</div>
</div>
<%= raw('<hr />') if i != section.questions.length - 1 %>
Expand Down
2 changes: 1 addition & 1 deletion app/views/phases/_guidances_notes.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
<li role="presentation" class="active">
<a data-target="#annotations-<%= question.id %>" aria-controls="annotations-<%= question.id %>" role="tab" data-toggle="tab"
class="view-plan-guidance">
<%= template.org.short_name %>
<%= base_template_org.short_name %>
</a>
</li>
<% end %>
Expand Down
2 changes: 1 addition & 1 deletion app/views/phases/admin_preview.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
<div role="tabpanel" class="tab-pane active">
<div class="panel panel-default">
<div class="panel-body">
<%= render partial: '/phases/edit_plan_answers', locals: { plan: nil, phase: @phase, readonly: true, question_guidance: {}, edit: false, guidance_groups: [] } %>
<%= render partial: '/phases/edit_plan_answers', locals: { plan: nil, phase: @phase, readonly: true, question_guidance: {}, edit: false, guidance_groups: [], base_template_org: @base_template_org } %>
</div>
</div>
</div>
Expand Down
2 changes: 1 addition & 1 deletion app/views/questions/_edit_question.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ in the admin interface.
<div class="form-group col-md-10">
<%= f.label(:example_answer, _('Example Answer'), class: "control-label") %>
<div class="" data-toggle="tooltip" title="<%= ('You can add an example answer to help users respond. These will be presented above the answer box and can be copied/ pasted.') %>">
<% example_answer = question.get_example_answer(current_user.org.id) %>
<% example_answer = question.get_example_answers(current_user.org.id).first %>
<% if example_answer.nil? %>
<% example_answer = question.annotations.build %>
<% example_answer.type = :example_answer %>
Expand Down
4 changes: 2 additions & 2 deletions app/views/questions/_show_question.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
</dd>
<!-- Suggested answer or Example-->
<% if !question.section.phase.template.org.funder? %>
<% example_answer = question.get_example_answer(@original_org.id) %>
<% example_answer = question.get_example_answers(@original_org.id).first %>
<% if example_answer.present? && example_answer.text.present? %>
<dt><%= _('example answer')%></dt>
<dd><%= raw example_answer.text %></dd>
Expand All @@ -60,7 +60,7 @@
<div class="row">
<div class="col-md-12">
<!-- Add suggested or example answers to a funders template-->
<% example_answer = question.get_example_answer(current_user.org_id) %>
<% example_answer = question.get_example_answers(current_user.org_id).first %>
<% guidance = question.get_guidance_annotation(current_user.org_id) %>
<% editing = (example_answer.present? || guidance.present?) %>
<% if !question.modifiable %>
Expand Down
2 changes: 1 addition & 1 deletion test/unit/question_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ class QuestionTest < ActiveSupport::TestCase
assert_equal 'Test 2', @question.annotations.where(org_id: Org.first.id).first.text, "expected the correct annotation"

org = Org.create(name: 'New One', links: {"org":[]})
assert_equal nil, @question.get_example_answer(org.id), "expected no annotation for a new org"
assert_equal 0, @question.get_example_answers(org.id).length, "expected no annotation for a new org"
end

# ---------------------------------------------------
Expand Down