8000 Refactor how section ordering is handled by Bodacious · Pull Request #1837 · DMPRoadmap/roadmap · GitHub
[go: up one dir, main page]

Skip to content

Refactor how section ordering is handled #1837

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

Merged
merged 24 commits into from
Aug 28, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
fa32212
Extract template customizations into own controller
Bodacious Aug 22, 2018
f40be47
Refactor Template model methods
Bodacious Aug 22, 2018
d33c03e
Fix view logic in draggable sections
Bodacious Aug 22, 2018
7b1fe80
Add button for customising phase
Bodacious Aug 22, 2018
19c3ea4
Fix rubocop violations in GuidanceService
Bodacious Aug 22, 2018
a1d727e
Rename GuidanceService to GuidancePresenter
Bodacious Aug 22, 2018
f5bee2f
Fix bug in phase preview order sections
Bodacious Aug 22, 2018
66b0c6a
Refactor preview action/controller code
Bodacious Aug 22, 2018
a6f40bf
Fix Section ordering in various views
Bodacious Aug 22, 2018
a361c1f
Fix rubocop violations
Bodacious Aug 22, 2018
3703bf7
Fix bug forcing Phases to be modifiable
Bodacious Aug 22, 2018
f469e4b
Fix broken tests
Bodacious Aug 23, 2018
525c178
Update method to check if section is draggable
Bodacious Aug 23, 2018
8d1fe15
Extract copy action into its own resource
Bodacious Aug 23, 2018
3d1ea64
Extract customization transfers to own controller
Bodacious Aug 23, 2018
e45f733
Remove redundant routing condition
Bodacious Aug 23, 2018
d824bd1
Fix Rubocop violation issues
Bodacious Aug 23, 2018
a7ff152
Change condition for section draggable
Bodacious Aug 23, 2018
f57d201
Refactor instance variable in PlansController
Bodacious Aug 23, 2018
dd55261
Fix broken tests
Bodacious Aug 27, 2018
1e44d0b
Add test support for 'de' locale
Bodacious Aug 27, 2018
1edf690
Merge branch 'development' into refactor/section-reordering
Bodacious Aug 27, 2018
61000f6
Fix merge issue in plans controller
Bodacious Aug 28, 2018
0a3a200
Fix broken specs in plan#request_feedback
Bodacious Aug 28, 2018
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
13 changes: 13 additions & 0 deletions app/controllers/concerns/template_methods.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# frozen_string_literal: true

# This module holds helper controller methods for controllers that deal with Templates
#
module TemplateMethods

private

def template_type(template)
template.customization_of.present? ? _("customisation") : _("template")
end

end
19 changes: 19 additions & 0 deletions app/controllers/org_admin/phase_versions_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# frozen_string_literal: true

class OrgAdmin::PhaseVersionsController < ApplicationController

include Versionable

def create
@phase = Phase.find(params[:phase_id])
authorize @phase, :create?
@new_phase = get_modifiable(@phase)
flash[:notice] = if @new_phase == @phase
"This template is already a draft"
else
"New version of Template created"
end
redirect_to org_admin_template_phase_url(@new_phase.template, @new_phase)
end

end
14 changes: 6 additions & 8 deletions app/controllers/org_admin/phases_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ def edit
template: phase.template,
phase: phase,
prefix_section: phase.prefix_section,
sections: phase.sections.order(:number).select(:id, :title, :modifiable),
sections: phase.sections.order(:number)
.select(:id, :title, :modifiable, :phase_id),
suffix_sections: phase.suffix_sections.order(:number),
current_section: Section.find_by(id: params[:section], phase_id: phase.id)
})
Expand All @@ -57,13 +58,10 @@ def edit
# preview a phase
# GET /org_admin/phases/[:id]/preview
def preview
phase = Phase.includes(:template).find(params[:id])
authorize phase
render("/org_admin/phases/preview",
locals: {
template: phase.template,
phase: phase
})
@phase = Phase.includes(:template).find(params[:id])
authorize @phase
@template = @phase.template
@guidance_presenter = GuidancePresenter.new(Plan.new(template: @phase.template))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice. I hadn't even heard of presenters in Rails before (learn something new every day) :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not a "native" Rails concept. But a good general principle to call on when you'd like to clean up code in an MVC paradigm :)

end

# add a new phase to a passed template
Expand Down
16 changes: 7 additions & 9 deletions app/controllers/org_admin/sections_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def index
template: phase.template,
phase: phase,
prefix_section: phase.prefix_section,
sections: phase.sections,
sections: phase.sections.order(:number),
suffix_sections: phase.suffix_sections,
current_section: phase.sections.first,
modifiable: edit,
Expand All @@ -31,14 +31,12 @@ def index

# GET /org_admin/templates/[:template_id]/phases/[:phase_id]/sections/[:id]
def show
section = Section.find(params[:id])
authorize section
section = Section.includes(questions: [:annotations, :question_options])
.find(params[:id])
render partial: "show", locals: {
template: Template.find(params[:template_id]),
section: section
}
@section = Section.find(params[:id])
authorize @section
@section = Section.includes(questions: [:annotations, :question_options])
.find(params[:id])
@template = Template.find(params[:template_id])
render partial: "show", locals: { template: @template, section: @section }
end

# GET /org_admin/templates/[:template_id]/phases/[:phase_id]/sections/[:id]/edit
Expand Down
27 changes: 27 additions & 0 deletions app/controllers/org_admin/template_copies_controller.rb F438
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# frozen_string_literal: true

class OrgAdmin::TemplateCopiesController < ApplicationController

include TemplateMethods

after_action :verify_authorized

# POST /org_admin/templates/:id/copy (AJAX)
def create
@template = Template.find(params[:template_id])
authorize @template, :copy?
begin
new_copy = @template.generate_copy!(current_user.org)
flash[:notice] = "#{template_type(@template).capitalize} was successfully copied."
redirect_to edit_org_admin_template_path(new_copy)
rescue StandardError => e
flash[:alert] = failed_create_error(@template, template_type(@template))
if request.referrer.present?
redirect_to :back
else
redirect_to org_admin_templates_path
end
end
end

end
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# frozen_string_literal: true

class OrgAdmin::TemplateCustomizationTransfersController < ApplicationController

after_action :verify_authorized

# POST /org_admin/templates/:id/transfer_customization
# the funder template's id is passed through here
def create
@template = Template.includes(:org).find(params[:template_id])
authorize @template, :transfer_customization?
if @template.upgrade_customization?
begin
new_customization = @template.upgrade_customization!
redirect_to org_admin_template_path(new_customization)
rescue StandardError => e
flash[:alert] = _("Unable to transfer your customizations.")
if request.referrer.present?
redirect_to :back
else
redirect_to org_admin_templates_path
end
end
else
flash[:notice] = _("That template is no longer customizable.")
if request.referrer.present?
redirect_to :back
else
redirect_to org_admin_templates_path
end
end
end

end
27 changes: 27 additions & 0 deletions app/controllers/org_admin/template_customizations_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# frozen_string_literal: true

class OrgAdmin::TemplateCustomizationsController < ApplicationController

include Paginable
include Versionable
after_action :verify_authorized

# POST /org_admin/templates/:id/customize
def create
@template = Template.find(params[:template_id])
authorize(@template, :customize?)
if @template.customize?(current_user.org)
begin
@customisation = @template.customize!(current_user.org)
redirect_to org_admin_template_path(@customisation)
return
rescue ArgumentError => e
flash[:alert] = _("Unable to customize that template.")
end
else
flash[:notice] = _("That template is not customizable.")
end
redirect_to :back
end

end
77 changes: 2 additions & 75 deletions app/controllers/org_admin/templates_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ class TemplatesController < ApplicationController

include Paginable
include Versionable
include TemplateMethods

after_action :verify_authorized

# The root version of index which returns all templates
Expand Down Expand Up @@ -241,77 +243,6 @@ def history
}
end

# POST /org_admin/templates/:id/customize
def customize
template = Template.find(params[:id])
authorize template
if template.customize?(current_user.org)
begin
customisation = template.customize!(current_user.org)
redirect_to org_admin_template_path(customisation)
rescue StandardError => e
flash[:alert] = _("Unable to customize that template.")
if request.referrer.present?
redirect_to request.referrer
else
redirect_to org_admin_templates_path
end
end
else
flash[:notice] = _("That template is not customizable.")
if request.referrer.present?
redirect_to request.referrer
else
redirect_to org_admin_templates_path
end
end
end

# POST /org_admin/templates/:id/transfer_customization
# the funder template's id is passed through here
def transfer_customization
template = Template.includes(:org).find(params[:id])
authorize template
if template.upgrade_customization?
begin
new_customization = template.upgrade_customization!
redirect_to org_admin_template_path(new_customization)
rescue StandardError => e
flash[:alert] = _("Unable to transfer your customizations.")
if request.referrer.present?
redirect_to request.referrer
else
redirect_to org_admin_templates_path
end
end
else
flash[:notice] = _("That template is no longer customizable.")
if request.referrer.present?
redirect_to request.referrer
else
redirect_to org_admin_templates_path
end
end
end

# POST /org_admin/templates/:id/copy (AJAX)
def copy
template = Template.find(params[:id])
authorize template
begin
new_copy = template.generate_copy!(current_user.org)
flash[:notice] = "#{template_type(template).capitalize} was successfully copied."
redirect_to edit_org_admin_template_path(new_copy)
rescue StandardError => e
flash[:alert] = failed_create_error(template, template_type(template))
if request.referrer.present?
redirect_to request.referrer
else
redirect_to org_admin_templates_path
end
end
end

# PATCH /org_admin/templates/:id/publish (AJAX)
def publish
template = Template.find(params[:id])
Expand Down Expand Up @@ -412,10 +343,6 @@ def template_params
params.require(:template).permit(:title, :description, :visibility, :links)
end

def template_type(template)
template.customization_of.present? ? _("customisation") : _("template")
end

def get_referrer(template, referrer)
return org_admin_templates_path unless referrer.present?
if referrer.end_with?(new_org_admin_template_path) ||
Expand Down
33 changes: 15 additions & 18 deletions app/controllers/plans_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@ def new
@funders = Org.funder
.joins(:templates)
.where(templates: { published: true }).uniq.sort(&:name)
@orgs = (Org.organisation + Org.institution + Org.managing_orgs)
.flatten.uniq.sort { |x, y| x.name <=> y.name }
@orgs = (Org.organisation + Org.institution + Org.managing_orgs).flatten
.uniq.sort(&:name)

# Get the current user's org
@default_org = current_user.org if @orgs.include?(current_user.org)

if params[:test]
if params.key?(:test)
flash[:notice] = "#{_('This is a')} <strong>#{_('test plan')}</strong>"
end
@is_test = params[:test] ||= false
Expand Down Expand Up @@ -105,11 +105,13 @@ def create
msg += " #{_('This plan is based on the default template.')}"

elsif !@plan.template.customization_of.nil?
# rubocop:disable Metrics/LineLength
# We used a customized version of the the funder template
# rubocop:disable Metrics/LineLength
msg += " #{_('This plan is based on the')} #{plan_params[:funder_name]}: '#{@plan.template.title}' #{_('template with customisations by the')} #{plan_params[:org_name]}"
# rubocop:enable Metrics/LineLength
else
# rubocop:disable Metrics/LineLength
# We used the specified org's or funder's template
# rubocop:disable Metrics/LineLength
msg += " #{_('This plan is based on the')} #{@plan.template.org.name}: '#{@plan.template.title}' template."
Expand Down Expand Up @@ -175,15 +177,13 @@ def show
@all_ggs_grouped_by_org = @all_ggs_grouped_by_org.sort_by do |org, gg|
(org.nil? ? "" : org.name)
end
@selected_guidance_groups = @selected_guidance_groups.collect do |gg|
gg.id
end
@selected_guidance_groups = @selected_guidance_groups.ids

@based_on = if @plan.template.customization_of.nil?
@plan.template
else
Template.where(family_id: @plan.template.customization_of).first
end

respond_to :html
end

Expand Down Expand Up @@ -213,7 +213,6 @@ def update
end
@plan.guidance_groups = GuidanceGroup.where(id: guidance_group_ids)
@plan.save

if @plan.update_attributes(attrs)
format.html do
redirect_to overview_plan_path(@plan),
Expand Down Expand Up @@ -301,16 +300,12 @@ def download
def export
@plan = Plan.includes(:answers).find(params[:id])
authorize @plan

@selected_phase = @plan.phases.find(params[:phase_id])

@show_coversheet = params[:export][:project_details].present?
@show_sections_questions = params[:export][:question_headings].present?
@show_unanswered = params[:export][:unanswered_questions].present?
@show_custom_sections = params[:export][:custom_sections].present?

@public_plan = false

@hash = @plan.as_pdf(@show_coversheet)
@formatting = params[:export][:formatting] || @plan.settings(:export).formatting
file_name = @plan.title.gsub(/ /, "_")
Expand Down Expand Up @@ -340,7 +335,6 @@ def export
# rubocop:enable Metrics/BlockLength
end


def duplicate
plan = Plan.find(params[:id])
authorize plan
Expand Down Expand Up @@ -379,10 +373,11 @@ def visibility
end
else
# rubocop:disable Metrics/LineLength

render status: :forbidden, json: {
msg: _("Unable to change the plan's status since it is needed at least "\
"%{percentage} percentage responded") % { percentage: Rails.application.config.default_plan_percentage_answered } }
msg: _("Unable to change the plan's status since it is needed at least %{percentage} percentage responded") % {
percentage: Rails.application.config.default_plan_percentage_answered
}
}
# rubocop:enable Metrics/LineLength
end
else
Expand Down Expand Up @@ -419,7 +414,9 @@ def overview
authorize plan
render(:overview, locals: { plan: plan })
rescue ActiveRecord::RecordNotFound
flash[:alert] = _("There is no plan associated with id %{id}") % { id: params[:id] }
flash[:alert] = _("There is no plan associated with id %{id}") % {
id: params[:id]
}
redirect_to(action: :index)
end
end
Expand Down Expand Up @@ -498,7 +495,7 @@ def render_phases_edit(plan, phase, guidance_groups)
readonly: readonly,
guidance_groups: guidance_groups,
answers: answers,
guidance_service: GuidanceService.new(plan)
guidance_presenter: GuidancePresenter.new(plan)
})
end

Expand Down
Loading
0