diff --git a/app/controllers/plans_controller.rb b/app/controllers/plans_controller.rb index f98ff45..8bedcbc 100644 --- a/app/controllers/plans_controller.rb +++ b/app/controllers/plans_controller.rb @@ -6,7 +6,7 @@ def index authorize Plan - @plans = current_user.plans + @plans = current_user.active_plans end # GET /plans/public_index @@ -292,49 +292,46 @@ # ------------------------------------------------------------- def public_export @plan = Plan.find(params[:id]) - # If the plan has multiple phases we should export each - @plan.phases.each do |phase| - @exported_plan = ExportedPlan.new.tap do |ep| - ep.plan = @plan - ep.phase_id = phase.id - ep.format = :pdf - plan_settings = @plan.settings(:export) + @exported_plan = ExportedPlan.new.tap do |ep| + ep.plan = @plan + ep.phase_id = @plan.phases.first.id + ep.format = :pdf + plan_settings = @plan.settings(:export) - Settings::Template::DEFAULT_SETTINGS.each do |key, value| - ep.settings(:export).send("#{key}=", plan_settings.send(key)) - end - end - - begin - @exported_plan.save! - # If the template has multiple phases then append the phase title to the filename - if @plan.phases.count > 1 - file_name = "#{@plan.title.gsub(/ /, "_")}-#{phase.title}" - else - file_name = @plan.title.gsub(/ /, "_") - end - - respond_to do |format| - format.pdf do - @formatting = @plan.settings(:export).formatting - render pdf: file_name, - margin: @formatting[:margin], - footer: { - center: _('This document was generated by %{application_name}') % {application_name: Rails.configuration.branding[:application][:name]}, - font_size: 8, - spacing: (@formatting[:margin][:bottom] / 2) - 4, - right: '[page] of [topage]' - } - end - end - rescue ActiveRecord::RecordInvalid => e - @phase_options = @plan.phases.order(:number).pluck(:title,:id) - redirect_to show_export_plan_path(@plan), alert: _('Unable to download the DMP at this time.') + Settings::Template::DEFAULT_SETTINGS.each do |key, value| + ep.settings(:export).send("#{key}=", plan_settings.send(key)) end end + # need to determine which phases to export + @a_q_ids = Answer.where(plan_id: @plan.id).pluck(:question_id).uniq + @a_s_ids = Question.where(id: @a_q_ids).pluck(:section_id).uniq + a_p_ids = Section.where(id: @a_s_ids).pluck(:phase_id).uniq + @phases = Phase.includes(sections: :questions).where(id: a_p_ids).order(:number) + + begin + @exported_plan.save! + file_name = @plan.title.gsub(/ /, "_") + + respond_to do |format| + format.pdf do + @formatting = @plan.settings(:export).formatting + render pdf: file_name, + margin: @formatting[:margin], + footer: { + center: _('This document was generated by %{application_name}') % {application_name: Rails.configuration.branding[:application][:name]}, + font_size: 8, + spacing: (@formatting[:margin][:bottom] / 2) - 4, + right: '[page] of [topage]' + } + end + end + rescue ActiveRecord::RecordInvalid => e + @phase_options = @plan.phases.order(:number).pluck(:title,:id) + redirect_to show_export_plan_path(@plan), alert: _('Unable to download the DMP at this time.') + end end - + def duplicate plan = Plan.find(params[:id]) authorize plan diff --git a/app/controllers/roles_controller.rb b/app/controllers/roles_controller.rb index abe77f2..4b79893 100644 --- a/app/controllers/roles_controller.rb +++ b/app/controllers/roles_controller.rb @@ -65,6 +65,22 @@ UserMailer.project_access_removed_notification(user, plan, current_user).deliver_now redirect_to controller: 'plans', action: 'share', id: @role.plan.id end + + # This function makes user's role on a plan inactive - i.e. "removes" this from their plans + def deactivate + role = Role.find(params[:id]) + authorize role + role.active = false + # if creator, remove from public plans list + if role.creator? && role.plan.publicly_visible? + role.plan.visibility = Plan.visibilities[:privately_visible] + role.plan.save + end + role.save + @plans = current_user.active_plans + flash[:notice] = _('Plan removed') + render "plans/index" + end private diff --git a/app/models/role.rb b/app/models/role.rb index a51177d..588d55c 100644 --- a/app/models/role.rb +++ b/app/models/role.rb @@ -1,4 +1,5 @@ class Role < ActiveRecord::Base + after_initialize :set_defaults include FlagShihTzu ## @@ -37,6 +38,10 @@ end end + def set_defaults + self.active = true if self.new_record? + end + end # ----------------------------------------------------- diff --git a/app/models/user.rb b/app/models/user.rb index 678c8dd..94918ec 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -82,6 +82,19 @@ end ## + # returns all active plans for a user + # + # @return [Plans] + def active_plans + plans = [] + self.roles.includes(:plan).where(active: true).each do |r| + plans << r.plan + end + return plans + end + + + ## # Returns the user's identifier for the specified scheme name # # @param the identifier scheme name (e.g. ORCID) diff --git a/app/policies/plan_policy.rb b/app/policies/plan_policy.rb index 3f8388c..99d9865 100644 --- a/app/policies/plan_policy.rb +++ b/app/policies/plan_policy.rb @@ -9,31 +9,31 @@ end def show? - @plan.readable_by?(@user.id) + @plan.readable_by?(@user.id) && Role.find_by(user_id: @user.id, plan_id: @plan.id).active end def share? - @plan.readable_by?(@user.id) + @plan.readable_by?(@user.id) && Role.find_by(user_id: @user.id, plan_id: @plan.id).active end def export? - @plan.readable_by?(@user.id) + @plan.readable_by?(@user.id) && Role.find_by(user_id: @user.id, plan_id: @plan.id).active end def show_export? - @plan.readable_by?(@user.id) + @plan.readable_by?(@user.id) && Role.find_by(user_id: @user.id, plan_id: @plan.id).active end def update? - @plan.editable_by?(@user.id) + @plan.editable_by?(@user.id) && Role.find_by(user_id: @user.id, plan_id: @plan.id).active end def destroy? - @plan.editable_by?(@user.id) + @plan.editable_by?(@user.id) && Role.find_by(user_id: @user.id, plan_id: @plan.id).active end def status? - @plan.readable_by?(@user.id) + @plan.readable_by?(@user.id) && Role.find_by(user_id: @user.id, plan_id: @plan.id).active end def possible_templates? @@ -41,15 +41,15 @@ end def duplicate? - @plan.editable_by?(@user.id) + @plan.editable_by?(@user.id) && Role.find_by(user_id: @user.id, plan_id: @plan.id).active end def visibility? - @plan.administerable_by?(@user.id) + @plan.administerable_by?(@user.id) && Role.find_by(user_id: @user.id, plan_id: @plan.id).active end def set_test? - @plan.administerable_by?(@user.id) + @plan.administerable_by?(@user.id)&& Role.find_by(user_id: @user.id, plan_id: @plan.id).active end # TODO: These routes are no lonmger used @@ -80,7 +80,7 @@ =end def answer? - @plan.readable_by?(@user.id) + @plan.readable_by?(@user.id) && Role.find_by(user_id: @user.id, plan_id: @plan.id).active end end diff --git a/app/policies/role_policy.rb b/app/policies/role_policy.rb index 00e2b79..c26d77c 100644 --- a/app/policies/role_policy.rb +++ b/app/policies/role_policy.rb @@ -19,4 +19,8 @@ def destroy? @role.plan.owned_by?(@user.id) end + + def archive? + @role.user_id = @user.id + end end \ No newline at end of file diff --git a/app/views/plans/index.html.erb b/app/views/plans/index.html.erb index 7be68aa..8fc703b 100644 --- a/app/views/plans/index.html.erb +++ b/app/views/plans/index.html.erb @@ -8,23 +8,24 @@

<% if @plans.count > 0 %> + <%= _('The table below lists the plans that you have created, and any that have been shared with you by others.') %>
<%= _('These can be edited, shared, exported or deleted at anytime.')%> <% else %> - <%= _("Welcome.") %>
+ <%= _("Welcome.") %>
<%= _("You are now ready to create your first DMP.") %>
<%= _("Click the 'Create plan' button below to begin.")%> <% end %>

- +
- <% if @plans.count > 0 %> + <% if @plans.length > 0 %> - <% if @plans.count > 10 %> + <% if @plans.length > 10 %> @@ -45,7 +46,7 @@ <% @plans.each do |plan| %> @@ -65,34 +66,32 @@ @@ -100,7 +99,7 @@
- <%= render(partial: "shared/table_filter", + <%= render(partial: "shared/table_filter", locals: {path: plans_path, placeholder: _('Filter plans')}) %>
- <%= link_to "#{plan.title.length > 60 ? "#{plan.title[0..59]} ..." : plan.title}", + <%= link_to "#{plan.title.length > 60 ? "#{plan.title[0..59]} ..." : plan.title}", plan_path(plan) %> <%= plan.template.title %>
<% end %> - +
<%= _('Create plan') %>
diff --git a/app/views/plans/public_export.pdf.erb b/app/views/plans/public_export.pdf.erb index f33d1d9..ca7fb83 100644 --- a/app/views/plans/public_export.pdf.erb +++ b/app/views/plans/public_export.pdf.erb @@ -1,70 +1,61 @@ - - - - <%= @plan.title %> - - - - -

<%= @plan.title %>

- <% @exported_plan.admin_details.each do |field| - value = @exported_plan.send(field) - if value.present? %> -

<%= admin_field_t(field.to_s) -%> <%= value -%>

- <% else %> -

<%= admin_field_t(field.to_s) -%> <%= _('-') %>

- <% end %> - <% end %> - -

<%= _("Copyright information: The above plan creator(s) have agreed that others may use as much of the text of this plan as they would like in their own plans, and customise it as necessary. You do not need to credit the creator(s) as the source of the language used, but using any of the plan's text does not imply that the creator(s) endorse, or have any relationship to, your project or proposal") %>

+ + + + <%= @plan.title %> + + + + +

<%= @plan.title %>

- <% @exported_plan.sections.each do |section| %> - <% questions = @exported_plan.questions_for_section(section.id) - if questions.present? - %> -

<%= section.title %>

- <% questions.each do |question| %> -
-

<%= raw question.text %>

- <% answer = @plan.answer(question.id, false) %> - <% if answer.nil? then %> -

<%= _('Question not answered.') -%>

- <% else %> - <% q_format = question.question_format %> - <% if q_format.option_based? %> - - - <% if question.option_comment_display == true then%> - <% if !answer.text.nil? then %> - <%= raw answer.text.gsub(/(\s||<\/td>| )*(<\/tr>|)/,"") %> - <%end%> - <%end%> - <%else%> - - <% if !answer.text.nil? then %> - <%= raw answer.text.gsub(/(\s||<\/td>| )*(<\/tr>|)/,"") %> - <%end%> - <% end %> - <% end %> -
+

<%= _("Copyright information: The above plan creator(s) have agreed that others may use as much of the text of this plan as they would like in their own plans, and customise it as necessary. You do not need to credit the creator(s) as the source of the language used, but using any of the plan's text does not imply that the creator(s) endorse, or have any relationship to, your project or proposal") %>

+ <% @phases.each do |phase| %> +
+

<%= phase.title %>

+ <% Section.where(phase_id: phase.id, id: @a_s_ids).order(:number).each do |section| %> +

<%= section.title %>

+ <% Question.where(section_id: section, id: @a_q_ids).order(:number).each do |question| %> +
+

<%= raw question.text %>

+ <% answer = @plan.answer(question.id, false) %> + <% if answer.nil? %> +

<%= _('Question not answered.') -%>

+ <% else %> + <% q_format = question.question_format %> + <% if q_format.option_based? %> + + + <% if question.option_comment_display == true %> + <% if !answer.text.nil? %> + <%= raw answer.text.gsub(/(\s||<\/td>| )*(<\/tr>|)/,"") %> + <% end %> + <% end %> + <% else %> + + <% if !answer.text.nil? %> + <%= raw answer.text.gsub(/(\s||<\/td>| )*(<\/tr>|)/,"") %> + <% end %> + <% end %> <% end %> +
<% end %> - + <% end %> + <% end %> + \ No newline at end of file diff --git a/app/views/plans/share.html.erb b/app/views/plans/share.html.erb index b331136..7347bb0 100644 --- a/app/views/plans/share.html.erb +++ b/app/views/plans/share.html.erb @@ -1,7 +1,7 @@ <% javascript('views/plans/share.js') %> -

<%= @plan.title %>

+

<%= @plan.title %>

@@ -11,13 +11,13 @@ - + <% @plan.template.phases.each do |phase| %> <% end %> - + @@ -29,31 +29,31 @@
- +

<%= _('Set plan visibility') %>

<%= _('Public or organisational visibility is intended for finished plans. You must answer at least one question to enable these options.') %>

- + <%= form_for @plan, html: {method: :put, class: "roadmap-form"} do |f| %>
<%= f.hidden_field :id %>
<%= f.radio_button :visibility, :privately_visible, disabled: !@allow_visibility %> - <%= f.label :visibility, _('Private: restricted to me and my collaborators'), + <%= f.label :visibility, _('Private: restricted to me and my collaborators'), class: "checkbox-label regular-text#{(@allow_visibility ? '' : ' disabled')}" %>
<%= f.radio_button :visibility, :organisationally_visible, disabled: !@allow_visibility %> - <%= f.label :visibility, _('Organisation: anyone at my organisation can view'), + <%= f.label :visibility, _('Organisation: anyone at my organisation can view'), class: "checkbox-label regular-text#{(@allow_visibility ? '' : ' disabled')}" %>
<%= f.radio_button :visibility, :publicly_visible, disabled: !@allow_visibility %> - <%= f.label :visibility, _('Public: anyone can view'), + <%= f.label :visibility, _('Public: anyone can view'), class: "checkbox-label regular-text#{(@allow_visibility ? '' : ' disabled')}" %>
<% end %> - +

<%= _('Manage collaborators')%>

@@ -69,7 +69,7 @@ - <% plan_roles = @plan.roles.all %> + <% plan_roles = @plan.roles.where(active: true) %> <% plan_roles.each do |role| %> <%= role.user.name %> @@ -97,7 +97,7 @@ <% end %> - +

<%= _('Invite collaborators') %>

<% new_role = Role.new %> <% new_role.plan = @plan %> @@ -128,8 +128,8 @@
<%= render partial: 'shared/accessible_submit_button', - locals: {id: 'add-collaborator-button', - val: _('Add collaborator'), + locals: {id: 'add-collaborator-button', + val: _('Add collaborator'), disabled_initially: true, classes: 'small-input-button left-indent', tooltip: _('Enter a valid email and permission level.')} %> diff --git a/config/routes.rb b/config/routes.rb index 3343ab9..defb261 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -224,7 +224,11 @@ end end - resources :roles, only: [:create, :update, :destroy] + resources :roles, only: [:create, :update, :destroy] do + member do + put :deactivate + end + end namespace :settings do resources :plans, only: [:update] diff --git a/db/migrate/20170719114516_add_active_flag_to_roles.rb b/db/migrate/20170719114516_add_active_flag_to_roles.rb new file mode 100644 index 0000000..c8a7cee --- /dev/null +++ b/db/migrate/20170719114516_add_active_flag_to_roles.rb @@ -0,0 +1,5 @@ +class AddActiveFlagToRoles < ActiveRecord::Migration + def change + add_column :roles, :active, :boolean, default: true + end +end diff --git a/db/schema.rb b/db/schema.rb index 57917c1..1361a05 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -11,7 +11,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20170712084314) do +ActiveRecord::Schema.define(version: 20170719114516) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -280,7 +280,8 @@ t.integer "plan_id" t.datetime "created_at" t.datetime "updated_at" - t.integer "access", default: 0, null: false + t.integer "access", default: 0, null: false + t.boolean "active", default: true end add_index "roles", ["plan_id"], name: "index_roles_on_plan_id", using: :btree