Skip to content

Commit

Permalink
Merge pull request #411 from DigitalCurationCentre/xsrust/bugfixes
Browse files Browse the repository at this point in the history
Xsrust/bugfixes
  • Loading branch information
vyruss authored Jun 9, 2017
2 parents 4a1b06f + a9e0cc8 commit 2a525bc
Show file tree
Hide file tree
Showing 11 changed files with 132 additions and 33 deletions.
103 changes: 83 additions & 20 deletions app/controllers/templates_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,32 +14,23 @@ def admin_index
funder_templates, org_templates, customizations = [], [], []

# Get all of the unique template family ids (dmptemplate_id) for each funder and the current org
funder_ids = Org.funders.includes(:templates).collect{|f| f.templates.valid.collect{|ft| ft.dmptemplate_id } }.flatten.uniq
org_ids = current_user.org.templates.valid.collect{|t| t.dmptemplate_id }.flatten.uniq
funder_ids = Org.funders.includes(:templates).collect{|f| f.templates.where(published: true).valid.collect{|ft| ft.dmptemplate_id } }.flatten.uniq
org_ids = current_user.org.templates.where(customization_of: nil).valid.collect{|t| t.dmptemplate_id }.flatten.uniq

org_ids.each do |id|
current = Template.current(id)
live = Template.live(id)

# If this isn't a customization of a funder template
if current.customization_of.nil?
org_templates << {current: current, live: live}

# This is a customization of a funder template
else
funder_live = Template.live(current.customization_of)
customizations << current.customization_of
# Mark the customization as stale if the funder has a newer version
funder_templates << {current: current, live: live, stale: funder_live.updated_at > current.created_at}
end
org_templates << {current: current, live: live}
end

# Get the funder templates
funder_ids.each do |id|
# If the org has a customization we don't want to load the funder version
unless customizations.include?(id)
funder_templates << {current: Template.current(id), live: Template.live(id)}
end
funder_live = Template.live(id)
current = Template.org_customizations(id, current_user.org_id)
# if we have a current template, check to see if there is a live version
live = current.nil? ? nil : Template.live(current.dmptemplate_id)
# need a current version, default to funder live if no custs exist
current = funder_live unless current.present?

funder_templates << {current: current, live: live, funder_live: funder_live, stale: funder_live.updated_at > current.created_at}
end

@funder_templates = funder_templates.sort{|x,y|
Expand Down Expand Up @@ -82,6 +73,78 @@ def admin_customize
redirect_to admin_template_template_path(customisation)
end

# GET /org/admin/templates/:id/admin_transfer_customization
# the funder template's id is passed through here
# -----------------------------------------------------
def admin_transfer_customization
@template = Template.includes(:org).find(params[:id])
authorize @template
new_customization = Template.deep_copy(@template)
new_customization.org_id = current_user.org_id
new_customization.published = false
new_customization.customization_of = @template.dmptemplate_id
new_customization.dirty = true
new_customization.phases.includes(sections: :questions).each do |phase|
phase.modifiable = false
phase.save
phase.sections.each do |section|
section.modifiable = false
section.save
section.questions.each do |question|
question.modifiable = false
question.save
end
end
end
customizations = Template.includes(:org, phases:[sections: [questions: :annotations]]).where(org_id: current_user.org_id, customization_of: @template.dmptemplate_id).order(version: :desc)
# existing version to port over
max_version = customizations.first
new_customization.dmptemplate_id = max_version.dmptemplate_id
new_customization.version = max_version.version + 1
# here we rip the customizations out of the old template
# First, we find any customzed phases or sections
max_version.phases.each do |phase|
# check if the phase was added as a customization
if phase.modifiable
# deep copy the phase and add it to the template
phase_copy = Phase.deep_copy(phase)
phase_copy.number = new_customization.phases.length + 1
phase_copy.template_id = new_customization.id
phase_copy.save!
else
# iterate over the sections to see if any of them are customizations
phase.sections.each do |section|
if section.modifiable
# this is a custom section
section_copy = Section.deep_copy(section)
customization_phase = new_customization.phases.includes(:sections.where(number: phase.number).first)
section_copy.phase_id = customization_phase.id
# custom sections get added to the end
section_copy.number = customization_phase.sections.length + 1
# section from phase with corresponding number in the main_template
section_copy.save!
else
# not a customized section, iterate over questions
customization_phase = new_customization.phases.includes(sections: [questions: :annotations]).where(number: phase.number).first
customization_section = customization_phase.sections.where(number: section.number).first
section.questions.each do |question|
# find corresponding question in new template
customization_question = customization_section.questions.where(number: question.number).first
# apply annotations
question.annotations.each do |annotation|
annotation_copy = Annotation.deep_copy(annotation)
annotation_copy.question_id = customization_question.id
annotation_copy.save!
end
end
end
end
end
end
new_customization.save
redirect_to admin_template_template_path(new_customization)
end

# PUT /org/admin/templates/:id/admin_publish
# -----------------------------------------------------
def admin_publish
Expand Down
13 changes: 13 additions & 0 deletions app/models/template.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,19 @@ def self.live(dmptemplate_id)
Template.where(dmptemplate_id: dmptemplate_id, published: true).valid.first
end

##
# Retrieves the most current customization of the template for the
# specified org and dmptemplate_id
# returns nil if no customizations found
#
# @params [integer] dmptemplate_id of the original template
# @params [integer] org_id for the customizing organisation
# @return [nil, Template] the customized template or nil
def self.org_customizations(dmptemplate_id, org_id)
Template.where(customization_of: dmptemplate_id, org_id: org_id).order(version: :desc).valid.first
end


##
# deep copy the given template and all of it's associations
#
Expand Down
4 changes: 4 additions & 0 deletions app/policies/template_policy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ def admin_template_history?
user.can_modify_templates? && (template.org_id == user.org_id)
end

def admin_transfer_customization?
user.can_modify_templates?
end


class Scope < Scope
def resolve
Expand Down
22 changes: 11 additions & 11 deletions app/views/templates/admin_index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -111,25 +111,19 @@
<td class="dmp_td_small">
<!-- If this is a funder template -->
<% if hash[:current].customization_of.nil? %>
<%= _('Published') %>

<!-- This is a customization of a funder template -->
<% else %>
<!-- If the original funder template has been changed -->
<% if hash[:stale] %>
<%= _('Original funder template has changed!')%>

<% elsif hash[:live].nil? %>
<!-- The template does not have a live version -->
<%= b_label = _('Un-published') %>

<% elsif hash[:current].dirty? %>
<% elsif !hash[:current].published? %>
<%= _('You have un-published changes') %>

<% else %>
<%= _('Published') %>
<% end %>

<% end %>
</td>
<td class="dmp_td_small">
Expand All @@ -138,13 +132,19 @@
</td>
<td class="dmp_td_small">
<% if hash[:current].customization_of.nil? %>
<% b_label = _('Customise') %>
<% b_label = _('Customise') %>
<%= link_to b_label, admin_customize_template_path(hash[:current]), method: :get, class: "dmp_table_link" %>
<% else %>
<% b_label = _('Edit customisation') %>
<%= link_to b_label, admin_template_template_path(hash[:current]), class: "dmp_table_link" %>
<% if hash[:stale] %>
<% b_label = _('Transfer customisation') %>
<%= link_to b_label, admin_transfer_customization_template_path(hash[:funder_live]), class: "dmp_table_link" %>
<% else %>
<% b_label = _('Edit customisation') %>
<%= link_to b_label, admin_template_template_path(hash[:current]), class: "dmp_table_link" %>
<% end %>

<% end %>

<% if !hash[:current].customization_of.nil? %>
<% if hash[:live].nil? || hash[:current].dirty? %>
<%= link_to _('Publish'), admin_publish_template_path(hash[:current]), method: :put, class: "dmp_table_link" %>
Expand Down
7 changes: 5 additions & 2 deletions config/locale/app.pot
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ msgid ""
msgstr ""
"Project-Id-Version: app 1.0.0\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2017-06-08 13:02+0100\n"
"PO-Revision-Date: 2017-06-08 13:02+0100\n"
"POT-Creation-Date: 2017-06-08 14:43+0000\n"
"PO-Revision-Date: 2017-06-08 14:43+0000\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <[email protected]>\n"
"Language: \n"
Expand Down Expand Up @@ -1320,6 +1320,9 @@ msgstr ""
msgid "Top banner text"
msgstr ""

msgid "Transfer customisation"
msgstr ""

msgid "Un-published"
msgstr ""

Expand Down
3 changes: 3 additions & 0 deletions config/locale/de/app.po
Original file line number Diff line number Diff line change
Expand Up @@ -1391,6 +1391,9 @@ msgstr "Oben"
msgid "Top banner text"
msgstr ""

msgid "Transfer customisation"
msgstr ""

#, fuzzy
msgid "Un-published"
msgstr "Veröffentlicht"
Expand Down
3 changes: 3 additions & 0 deletions config/locale/en_GB/app.po
Original file line number Diff line number Diff line change
Expand Up @@ -1369,6 +1369,9 @@ msgstr "Top"
msgid "Top banner text"
msgstr "Top banner text"

msgid "Transfer customisation"
msgstr ""

msgid "Un-published"
msgstr "Un-published"

Expand Down
3 changes: 3 additions & 0 deletions config/locale/en_US/app.po
Original file line number Diff line number Diff line change
Expand Up @@ -1369,6 +1369,9 @@ msgstr "Top"
msgid "Top banner text"
msgstr "Top banner text"

msgid "Transfer customisation"
msgstr ""

msgid "Un-published"
msgstr "Un-published"

Expand Down
3 changes: 3 additions & 0 deletions config/locale/es/app.po
Original file line number Diff line number Diff line change
Expand Up @@ -1384,6 +1384,9 @@ msgstr "Superior"
msgid "Top banner text"
msgstr "Texto del banner superior"

msgid "Transfer customisation"
msgstr ""

#, fuzzy
msgid "Un-published"
msgstr "Publicado"
Expand Down
3 changes: 3 additions & 0 deletions config/locale/fr/app.po
Original file line number Diff line number Diff line change
Expand Up @@ -1381,6 +1381,9 @@ msgstr "Haut"
msgid "Top banner text"
msgstr "Texte de la bannière en haut décran"

msgid "Transfer customisation"
msgstr ""

#, fuzzy
msgid "Un-published"
msgstr "Publiée"
Expand Down
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@
put 'admin_update'
put 'admin_publish'
put 'admin_unpublish'
get 'admin_transfer_customization'
end
end

Expand Down

0 comments on commit 2a525bc

Please sign in to comment.