diff --git a/.travis.yml b/.travis.yml
index 50299bc..453b68d 100644
--- a/.travis.yml
+++ b/.travis.yml
@@ -5,6 +5,7 @@
before_script:
- cp config/database_example.yml config/database.yml
- cp config/secrets_example.yml config/secrets.yml
+ - cp config/branding_example.yml config/branding.yml
- cp config/initializers/devise.rb.example config/initializers/devise.rb
- cp config/initializers/recaptcha.rb.example config/initializers/recaptcha.rb
- cp config/initializers/wicked_pdf.rb.example config/initializers/wicked_pdf.rb
diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb
index 08d677c..1b13b64 100644
--- a/app/controllers/application_controller.rb
+++ b/app/controllers/application_controller.rb
@@ -12,7 +12,11 @@
rescue_from Pundit::NotAuthorizedError, with: :user_not_authorized
def user_not_authorized
- redirect_to root_url, alert: _('You need to sign in or sign up before continuing.')
+ if user_signed_in?
+ redirect_to plans_url, notice: _('You are not authorized to perform this action.')
+ else
+ redirect_to root_url, alert: _('You need to sign in or sign up before continuing.')
+ end
end
before_filter :set_gettext_locale
@@ -83,6 +87,18 @@
end
end
+ def failed_create_error(obj, obj_name)
+ "#{_('Could not create your %{o}.') % {o: obj_name}} #{errors_to_s(obj)}"
+ end
+
+ def failed_update_error(obj, obj_name)
+ "#{_('Could not update your %{o}.') % {o: obj_name}} #{errors_to_s(obj)}"
+ end
+
+ def failed_destroy_error(obj, obj_name)
+ "#{_('Could not delete the %{o}.') % {o: obj_name}} #{errors_to_s(obj)}"
+ end
+
private
# Override rails default render action to look for a branded version of a
# template instead of using the default one. If no override exists, the
@@ -94,4 +110,10 @@
def prepend_view_paths
prepend_view_path "app/views/branded"
end
+
+ def errors_to_s(obj)
+ if obj.errors.count > 0
+ "
#{obj.errors.collect{|e,m| "#{_(e)} - #{_(m)}"}.join("
")}"
+ end
+ end
end
diff --git a/app/controllers/contacts_controller.rb b/app/controllers/contacts_controller.rb
deleted file mode 100644
index 36fa10f..0000000
--- a/app/controllers/contacts_controller.rb
+++ /dev/null
@@ -1,28 +0,0 @@
-class ContactsController < ContactUs::ContactsController
- respond_to :html
-
- ##
- # create
- #
- # POST - Create a Contact Request
- def create
- @contact = ContactUs::Contact.new(params[:contact_us_contact])
- if (!user_signed_in?)
- if verify_recaptcha(message: "You have not added the validation words correctly") && @contact.save
- flash[:notice] = t('contact_us.notices.success')
- redirect_to(root_path)
- else # recaptcha invalid or contact failed to save
- flash[:alert] = t('contact_us.notices.error')
- render_new_page
- end
- else # no user signed in
- if @contact.save
- flash[:notice] = t('contact_us.notices.success')
- redirect_to :controller => 'projects', :action => 'index'
- else # contact failed to save
- flash[:alert] = t('contact_us.notices.error')
- render_new_page
- end
- end
- end
-end
\ No newline at end of file
diff --git a/app/controllers/guidance_groups_controller.rb b/app/controllers/guidance_groups_controller.rb
index 9d7706f..ce09688 100644
--- a/app/controllers/guidance_groups_controller.rb
+++ b/app/controllers/guidance_groups_controller.rb
@@ -29,7 +29,8 @@
if @guidance_group.save
redirect_to admin_index_guidance_path, notice: _('Guidance group was successfully created.')
else
- render action: "new"
+ flash[:notice] = failed_create_error(@guidance_group, _('guidance group'))
+ render 'admin_new'
end
end
@@ -51,11 +52,12 @@
if @guidance_group.update_attributes(params[:guidance_group])
redirect_to admin_index_guidance_path(params[:guidance_group]), notice: _('Guidance group was successfully updated.')
else
- render action: "edit"
+ flash[:notice] = failed_update_error(@guidance_group, _('guidance group'))
+ render 'admin_edit'
end
end
-
+# TODO: This does not have a route in config/routes.rb and is unreachable!
# PUT /guidance_groups/1
def admin_update_publish
@guidance_group = GuidanceGroup.find(params[:id])
@@ -66,7 +68,7 @@
if @guidance_group.update_attributes(params[:guidance_group])
redirect_to admin_index_guidance_path(params[:guidance_group]), notice: _('Guidance group was successfully updated.')
else
- render action: "edit"
+ redirect_to admin_index_guidance_path(@guidance_group), notice: failed_update_error(@guidance_group, _('guidance group'))
end
end
@@ -76,9 +78,11 @@
def admin_destroy
@guidance_group = GuidanceGroup.find(params[:id])
authorize @guidance_group
- @guidance_group.destroy
-
- redirect_to admin_index_guidance_path, notice: _('Guidance group was successfully deleted.')
+ if @guidance_group.destroy
+ redirect_to admin_index_guidance_path, notice: _('Guidance group was successfully deleted.')
+ else
+ redirect_to admin_index_guidance_path, notice: failed_destroy_error(@guidance_group, _('guidance group'))
+ end
end
end
\ No newline at end of file
diff --git a/app/controllers/guidances_controller.rb b/app/controllers/guidances_controller.rb
index 7180c6a..47c5004 100644
--- a/app/controllers/guidances_controller.rb
+++ b/app/controllers/guidances_controller.rb
@@ -42,7 +42,9 @@
@guidance.question_id = params["question_id"]
@guidance.themes = []
- guidance_params[:theme_ids].map{|t| @guidance.themes << Theme.find(t.to_i) unless t.empty? }
+ if !guidance_params[:theme_ids].nil?
+ guidance_params[:theme_ids].map{|t| @guidance.themes << Theme.find(t.to_i) unless t.empty? }
+ end
if @guidance.published == true then
@gg = GuidanceGroup.find(@guidance.guidance_group_id)
@@ -55,6 +57,7 @@
if @guidance.save
redirect_to admin_show_guidance_path(@guidance), notice: _('Guidance was successfully created.')
else
+ flash[:notice] = failed_create_error(@guidance, _('guidance'))
@themes = Theme.all.order('title')
@guidance_groups = GuidanceGroup.where(org_id: current_user.org_id).order('name ASC')
render action: "admin_new"
@@ -68,15 +71,14 @@
authorize @guidance
@guidance.text = params["guidance-text"]
@guidance.question_id = params["question_id"]
-
- @guidance.themes = []
- guidance_params[:theme_ids].map{|t| @guidance.themes << Theme.find(t.to_i) unless t.empty? }
-
- if @guidance.update_attributes(guidance_params)
- redirect_to admin_show_guidance_path(guidance_params), notice: _('Guidance was successfully updated.')
+
+ if @guidance.save(guidance_params)
+ redirect_to admin_show_guidance_path(params[:guidance]), notice: _('Guidance was successfully updated.')
else
+ flash[:notice] = failed_update_error(@guidance, _('guidance'))
@themes = Theme.all.order('title')
@guidance_groups = GuidanceGroup.where(org_id: current_user.org_id).order('name ASC')
+
render action: "admin_edit"
end
end
@@ -86,9 +88,11 @@
def admin_destroy
@guidance = Guidance.find(params[:id])
authorize @guidance
- @guidance.destroy
-
- redirect_to admin_index_guidance_path
+ if @guidance.destroy
+ redirect_to admin_index_guidance_path, notice: _('Guidance was successfully deleted.')
+ else
+ redirect_to admin_index_guidance_path, notice: failed_destroy_error(@guidance, _('guidance'))
+ end
end
diff --git a/app/controllers/home_controller.rb b/app/controllers/home_controller.rb
index a311c09..7a75e3c 100644
--- a/app/controllers/home_controller.rb
+++ b/app/controllers/home_controller.rb
@@ -11,6 +11,8 @@
def index
if user_signed_in?
name = current_user.name(false)
+# TODO: Investigate if this is even relevant anymore. The name var will never be blank here because the logic in
+# User says to return the email if the firstname and surname are empty regardless of the flag passed in
if name.blank?
redirect_to edit_user_registration_path
else
diff --git a/app/controllers/notes_controller.rb b/app/controllers/notes_controller.rb
index 3653f4b..474281a 100644
--- a/app/controllers/notes_controller.rb
+++ b/app/controllers/notes_controller.rb
@@ -29,13 +29,15 @@
authorize @note
@plan = answer.plan
- @notice = "Save failed."
@answer = answer
@question = Question.find(question_id)
if @note.save
@status = true
@notice = _('Comment was successfully created.')
+ else
+ @status = false
+ @notice = failed_create_error(@note, _('note'))
end
notes = answer.notes.all
@num_notes = notes.count
@@ -54,6 +56,8 @@
if @note.update_attributes(params[:note])
@notice = _('Comment was successfully saved.')
+ else
+ @notice = failed_update_error(@note, _('note'))
end
end
@@ -71,6 +75,8 @@
if @note.update_attributes(params[:note])
@notice = _('Comment removed.')
+ else
+ @notice = failed_destroy_error(@note, _('note'))
end
end
end
diff --git a/app/controllers/orgs_controller.rb b/app/controllers/orgs_controller.rb
index 91b8ded..a8f9835 100644
--- a/app/controllers/orgs_controller.rb
+++ b/app/controllers/orgs_controller.rb
@@ -20,10 +20,11 @@
##
# PUT /organisations/1
def admin_update
+ attrs = org_params
@org = Org.find(params[:id])
authorize @org
@org.banner_text = params["org_banner_text"]
- @org.logo = params[:org][:logo] if params[:org][:logo]
+ @org.logo = org_params[:logo] if org_params[:logo]
begin
if @org.update_attributes(org_params)
@@ -34,7 +35,7 @@
# its unclear why its doing this. Placing a check here for the data type. We should reasses though
# when doing a broader eval of the look/feel of the site and we come up with a standardized way of
# displaying errors
- flash[:notice] = @org.errors.collect{|a, e| "#{a} - #{(e.instance_of?(String) ? e : e.message)}"}.join('
').html_safe
+ flash[:notice] = failed_update_error(@org, _('organisation'))
render action: "admin_edit"
end
rescue Dragonfly::Job::Fetch::NotFound => dflye
@@ -44,8 +45,8 @@
end
private
-
- def org_params
- params.require(:org).permit(:name, :abbreviation, :target_url)
- end
+ def org_params
+ params.require(:org).permit(:name, :abbreviation, :target_url, :is_other, :banner_text, :language_id,
+ :region_id, :logo, :contact_email)
+ end
end
diff --git a/app/controllers/phases_controller.rb b/app/controllers/phases_controller.rb
index f1890ca..9f9163d 100644
--- a/app/controllers/phases_controller.rb
+++ b/app/controllers/phases_controller.rb
@@ -158,12 +158,15 @@
def admin_create
@phase = Phase.new(params[:phase])
authorize @phase
+
@phase.description = params["phase-desc"]
@phase.modifiable = true
if @phase.save
redirect_to admin_show_phase_path(id: @phase.id, edit: 'true'), notice: _('Information was successfully created.')
else
- render action: "admin_show"
+ flash[:notice] = failed_create_error(@phase, _('phase'))
+ @template = @phase.template
+ render "admin_add"
end
end
@@ -176,7 +179,16 @@
if @phase.update_attributes(params[:phase])
redirect_to admin_show_phase_path(@phase), notice: _('Information was successfully updated.')
else
- render action: "admin_show"
+ @sections = @phase.sections
+ @template = @phase.template
+ # These params may not be available in this context so they may need
+ # to be set to true without the check
+ @edit = true
+ @open = !params[:section_id].nil?
+ @section_id = (params[:section_id].nil? ? nil : params[:section_id].to_i)
+ @question_id = (params[:question_id].nil? ? nil : params[:question_id].to_i)
+ flash[:notice] = failed_update_error(@phase, _('phase'))
+ render 'admin_show'
end
end
@@ -185,10 +197,20 @@
@phase = Phase.find(params[:phase_id])
authorize @phase
@template = @phase.template
- @phase.destroy
- redirect_to admin_template_template_path(@template), notice: _('Information was successfully deleted.')
+ if @phase.destroy
+ redirect_to admin_template_template_path(@template), notice: _('Information was successfully deleted.')
+ else
+ @sections = @phase.sections
+
+ # These params may not be available in this context so they may need
+ # to be set to true without the check
+ @edit = true
+ @open = !params[:section_id].nil?
+ @section_id = (params[:section_id].nil? ? nil : params[:section_id].to_i)
+ @question_id = (params[:question_id].nil? ? nil : params[:question_id].to_i)
+ flash[:notice] = failed_destroy_error(@phase, _('phase'))
+ render 'admin_show'
+ end
end
-
-
end
diff --git a/app/controllers/plans_controller.rb b/app/controllers/plans_controller.rb
index 25216b7..1408264 100644
--- a/app/controllers/plans_controller.rb
+++ b/app/controllers/plans_controller.rb
@@ -17,91 +17,81 @@
# GET /plans/new
def new
- if user_signed_in? then
- @plan = Plan.new
- authorize @plan
- @funders = Org.funders.all
+ @plan = Plan.new
+ authorize @plan
+ @funders = Org.funders.all
- respond_to do |format|
- format.html # new.html.erb
- end
- else
- respond_to do |format|
- format.html { redirect_to edit_user_registration_path }
- end
+ respond_to do |format|
+ format.html # new.html.erb
end
end
def create
- if user_signed_in? then
- @plan = Plan.new
- @plan.save
- authorize @plan
+ @plan = Plan.new
+ authorize @plan
+ @plan.save
- if params[:template_id]
- @templates = [ Template.find(params[:template_id] ) ]
+ if params[:template_id]
+ @templates = [ Template.find(params[:template_id] ) ]
+ else
+
+ funder_id = params[:plan][:funder_id]
+ if !funder_id.blank?
+ # get all funder @templates
+ funder = Org.find(params[:plan][:funder_id])
+ @templates = get_most_recent( funder.templates.where("published = ?", true).all )
+
+ orgtemplates = current_user.org.templates.all
+ replacements = []
+
+ # replace any that are customised by the org
+ orgtemplates.each do |orgt|
+ base_template = orgt.customization_of
+ @templates.delete(base_template)
+ replacements << orgt
+ end
+ @templates + replacements
+
else
+ # get all org @templates which are not customisations
+ @templates = current_user.org.templates.where(customization_of: nil)
- funder_id = params[:plan][:funder_id]
- if !funder_id.blank?
- # get all funder @templates
- funder = Org.find(params[:plan][:funder_id])
- @templates = get_most_recent( funder.templates.where("published = ?", true).all )
-
- orgtemplates = current_user.org.templates.all
- replacements = []
-
- # replace any that are customised by the org
- orgtemplates.each do |orgt|
- base_template = orgt.customization_of
- @templates.delete(base_template)
- replacements << orgt
- end
- @templates + replacements
-
- else
- # get all org @templates which are not customisations
- @templates = current_user.org.templates.where(customization_of: nil)
-
- # if none of these get the basic dcc template
- if @templates.blank?
- @templates = Template.find_by_is_default(true)
- end
- end
- end
-
- # if we have more than one template then back to the user
- # using the 'create' template
- # to choose otherwise just create the plan
- # and go to the plan/show template
- if @templates.length > 1
- return
- end
-
- @plan.template = @templates[0]
-
- @plan.principal_investigator = current_user.name
-
- @plan.title = _('My plan')+' ('+@plan.template.title+')' # We should use interpolated string since the order of the words from this message could vary among languages
-
- @plan.assign_creator(current_user.id)
-
- @plan.set_possible_guidance_groups
-
- @selected_guidance_groups = @plan.guidance_groups.map{ |pgg| [pgg.name, pgg.id, :checked => false] }
- @selected_guidance_groups.sort!
-
- respond_to do |format|
- if @plan.save
- format.html { redirect_to({:action => "show", :id => @plan.id, :editing => true }, {:notice => _('Plan was successfully created.')}) }
- else
- @error = "Something went wrong"
- format.html { render action: "new" }
+ # if none of these get the basic dcc template
+ if @templates.blank?
+ @templates = Template.find_by_is_default(true)
end
end
- else
- render(:file => File.join(Rails.root, 'public/403.html'), :status => 403, :layout => false)
+ end
+
+ # if we have more than one template then back to the user
+ # using the 'create' template
+ # to choose otherwise just create the plan
+ # and go to the plan/show template
+ if @templates.length > 1
+ return
+ end
+
+ @plan.template = @templates[0]
+
+ @plan.principal_investigator = current_user.name
+
+ @plan.title = _('My plan')+' ('+@plan.template.title+')' # We should use interpolated string since the order of the words from this message could vary among languages
+
+ @plan.assign_creator(current_user.id)
+
+ @plan.set_possible_guidance_groups
+
+ @selected_guidance_groups = @plan.guidance_groups.map{ |pgg| [pgg.name, pgg.id, :checked => false] }
+ @selected_guidance_groups.sort!
+
+ respond_to do |format|
+ if @plan.save
+ format.html { redirect_to({:action => "show", :id => @plan.id, :editing => true }, {:notice => _('Plan was successfully created.')}) }
+ else
+ flash[:notice] = failed_create_error(@plan, _('plan'))
+ format.html { render action: "new" }
+ end
end
end
@@ -109,29 +99,14 @@
# GET /plans/show
def show
- puts 'plans#show'
@plan = Plan.eager_load(params[:id])
authorize @plan
-
- @editing = params[:editing] && @plan.administerable_by?(current_user.id)
+ @editing = (!params[:editing].nil? && @plan.administerable_by?(current_user.id))
@selected_guidance_groups = []
all_guidance_groups = @plan.plan_guidance_groups
@selected_guidance_groups = all_guidance_groups.map{ |pgg| [ pgg.guidance_group.name, pgg.guidance_group.id, :checked => pgg.selected ] }
@selected_guidance_groups.sort!
-
- if user_signed_in? && @plan.readable_by?(current_user.id) then
- respond_to do |format|
- format.html # show.html.erb
- end
- elsif user_signed_in? then
- respond_to do |format|
- format.html { redirect_to projects_url, notice: _('This account does not have access to that plan.') }
- end
- else
- respond_to do |format|
- format.html { redirect_to edit_user_registration_path }
- end
- end
+ respond_to :html
end
@@ -146,43 +121,29 @@
#
# GET /plans/1/edit
def edit
-
@plan = Plan.find(params[:id])
-
- @phase = nil
- if params[:phase]
- @phase = Phase.find(params[:phase])
- end
-
authorize @plan
+ # If there was no phase specified use the template's 1st phase
+ @phase = (params[:phase].nil? ? @plan.template.phases.first : Phase.find(params[:phase]))
@readonly = @plan.editable_by?(current_user.id)
- if !user_signed_in? then
- respond_to do |format|
- format.html { redirect_to edit_user_registration_path }
- end
- elsif !@plan.readable_by?(current_user.id) then
- respond_to do |format|
- format.html { redirect_to projects_url, notice: _('This account does not have access to that plan.') }
- end
- end
+ respond_to :html
end
+
# PUT /plans/1
# PUT /plans/1.json
def update
@plan = Plan.find(params[:id])
authorize @plan
- if user_signed_in? && @plan.editable_by?(current_user.id) then
- respond_to do |format|
- if @plan.update_attributes(params[:plan])
- format.html { redirect_to @plan, :editing => false, notice: _('Plan was successfully updated.') }
- format.json { head :no_content }
- else
- format.html { render action: "edit" }
- end
+
+ respond_to do |format|
+ if @plan.update_attributes(params[:plan])
+ format.html { redirect_to @plan, :editing => false, notice: _('Plan was successfully updated.') }
+ format.json { head :no_content }
+ else
+ flash[:notice] = failed_update_error(@plan, _('plan'))
+ format.html { render action: "edit" }
end
- else
- render(:file => File.join(Rails.root, 'public/403.html'), :status => 403, :layout => false)
end
end
@@ -191,19 +152,17 @@
def update_guidance_choices
@plan = Plan.find(params[:id])
authorize @plan
- if user_signed_in? && @plan.editable_by?(current_user.id) then
- guidance_ids = params[:plan][:plan_guidance_group_ids]
- @plan.plan_guidance_groups.each do |pgg|
- pgg.selected = guidance_ids.include?(pgg.guidance_group_id.to_s)
- pgg.save!
- end
- @plan.save!
+ guidance_ids = params[:plan][:plan_guidance_group_ids]
+
+# TODO: This always appears to be empty for a new plan. What SHOULD it contain, all guidance_groups?
+ @plan.plan_guidance_groups.each do |pgg|
+ pgg.selected = guidance_ids.include?(pgg.guidance_group_id.to_s)
+ pgg.save!
+ end
+ @plan.save!
- respond_to do |format|
- format.json { head :no_content }
- end
- else
- render(:file => File.join(Rails.root, 'public/403.html'), :status => 403, :layout => false)
+ respond_to do |format|
+ format.json { head :no_content }
end
end
@@ -211,29 +170,21 @@
@plan = Plan.find(params[:id])
authorize @plan
@plan_data = @plan.to_hash
- if !user_signed_in? then
- respond_to do |format|
- format.html { redirect_to edit_user_registration_path }
- end
- elsif !@plan.editable_by?(current_user.id) then
- respond_to do |format|
- format.html { redirect_to plans_url, notice: _('This account does not have access to that plan.') }
- end
- end
end
def destroy
@plan = Plan.find(params[:id])
authorize @plan
- if user_signed_in? && @plan.editable_by?(current_user.id) then
- @plan.destroy
-
+ if @plan.destroy
respond_to do |format|
- format.html { redirect_to plans_url }
+ format.html { redirect_to plans_url, notice: _('Plan was successfully deleted.') }
end
else
- render(:file => File.join(Rails.root, 'public/403.html'), :status => 403, :layout => false)
+ respond_to do |format|
+ flash[:notice] = failed_create_error(@plan, _('plan'))
+ format.html { render action: "edit" }
+ end
end
end
@@ -242,24 +193,19 @@
def status
@plan = Plan.find(params[:id])
authorize @plan
- if user_signed_in? && @plan.readable_by(current_user.id) then
- respond_to do |format|
- format.json { render json: @plan.status }
- end
- else
- render(:file => File.join(Rails.root, 'public/403.html'), :status => 403, :layout => false)
+ respond_to do |format|
+ format.json { render json: @plan.status }
end
end
+
+# TODO: Remove these endpoints now that we're no longer using them
+=begin
def section_answers
@plan = Plan.find(params[:id])
authorize @plan
- if user_signed_in? && @plan.readable_by(current_user.id) then
- respond_to do |format|
- format.json { render json: @plan.section_answers(params[:section_id]) }
- end
- else
- render(:file => File.join(Rails.root, 'public/403.html'), :status => 403, :layout => false)
+ respond_to do |format|
+ format.json { render json: @plan.section_answers(params[:section_id]) }
end
end
@@ -340,16 +286,19 @@
render(:file => File.join(Rails.root, 'public/403.html'), :status => 403, :layout => false)
end
end
+=end
def answer
@plan = Plan.find(params[:id])
authorize @plan
- if user_signed_in? && @plan.readable_by(current_user.id) then
+ if !params[:q_id].nil?
respond_to do |format|
format.json { render json: @plan.answer(params[:q_id], false).to_json(:include => :options) }
end
else
- render(:file => File.join(Rails.root, 'public/403.html'), :status => 403, :layout => false)
+ respond_to do |format|
+ format.json { render json: {} }
+ end
end
end
@@ -358,56 +307,49 @@
authorize @plan
render 'show_export'
end
-
+
def export
@plan = Plan.find(params[:id])
authorize @plan
- if user_signed_in? && @plan.readable_by?(current_user.id) then
- @exported_plan = ExportedPlan.new.tap do |ep|
- ep.plan = @plan
- ep.user = current_user
- ep.format = params[:format].to_sym
- plan_settings = @plan.settings(:export)
+ # If no format is specified, default to PDF
+ params[:format] = 'pdf' if params[:format].nil?
- Settings::Template::DEFAULT_SETTINGS.each do |key, value|
- ep.settings(:export).send("#{key}=", plan_settings.send(key))
+ @exported_plan = ExportedPlan.new.tap do |ep|
+ ep.plan = @plan
+ ep.user = current_user
+ ep.format = params[:format].to_sym
+ 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!
+ file_name = @exported_plan.project_name
+
+ respond_to do |format|
+ format.html
+ format.csv { send_data @exported_plan.as_csv, filename: "#{file_name}.csv" }
+ format.text { send_data @exported_plan.as_txt, filename: "#{file_name}.txt" }
+ format.docx { headers["Content-Disposition"] = "attachment; filename=\"#{file_name}.docx\""}
+ 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
-
- begin
- @exported_plan.save!
- file_name = @exported_plan.project_name
-
- respond_to do |format|
- format.html
- format.csv { send_data @exported_plan.as_csv, filename: "#{file_name}.csv" }
- format.text { send_data @exported_plan.as_txt, filename: "#{file_name}.txt" }
- format.docx { headers["Content-Disposition"] = "attachment; filename=\"#{file_name}.docx\""}
- 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
- redirect_to show_export_plan_path(@plan), notice: _('%{format} is not a valid exporting format. Available formats to export are %{available_formats}.') %
- {format: params[:format], available_formats: ExportedPlan::VALID_FORMATS.to_s}
- end
- elsif !user_signed_in? then
- respond_to do |format|
- format.html { redirect_to edit_user_registration_path }
- end
- elsif !@plan.editable_by(current_user.id) then
- respond_to do |format|
- format.html { redirect_to plans_path, notice: _('This account does not have access to that plan.') }
- end
+ rescue ActiveRecord::RecordInvalid => e
+ redirect_to show_export_plan_path(@plan), notice: _('%{format} is not a valid exporting format. Available formats to export are %{available_formats}.') %
+ {format: params[:format], available_formats: ExportedPlan::VALID_FORMATS.to_s}
end
end
diff --git a/app/controllers/questions_controller.rb b/app/controllers/questions_controller.rb
index 803479f..47f9a11 100644
--- a/app/controllers/questions_controller.rb
+++ b/app/controllers/questions_controller.rb
@@ -8,10 +8,19 @@
authorize @question
@question.guidance = params["new-question-guidance"]
@question.default_value = params["new-question-default-value"]
- if @question.save!
+ if @question.save
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.')
else
- render action: "phases/admin_show"
+ @edit = (@question.section.phase.template.org == current_user.org)
+ @open = true
+ @phase = @question.section.phase
+ @section = @question.section
+ @sections = @phase.sections
+ @section_id = @question.section.id
+ @question_id = @question.id
+
+ flash[:notice] = failed_create_error(@question, _('question'))
+ render template: 'phases/admin_show'
end
end
@@ -26,7 +35,14 @@
if @question.update_attributes(params[:question])
redirect_to admin_show_phase_path(id: @phase.id, section_id: @section.id, question_id: @question.id, edit: 'true'), notice: _('Information was successfully updated.')
else
- render action: "phases/admin_show"
+ @edit = (@phase.template.org == current_user.org)
+ @open = true
+ @sections = @phase.sections
+ @section_id = @section.id
+ @question_id = @question.id
+
+ flash[:notice] = failed_update_error(@question, _('question'))
+ render template: 'phases/admin_show'
end
end
@@ -36,8 +52,11 @@
authorize @question
@section = @question.section
@phase = @section.phase
- @question.destroy
- redirect_to admin_show_phase_path(id: @phase.id, section_id: @section.id, edit: 'true'), notice: _('Information was successfully deleted.')
+ if @question.destroy
+ redirect_to admin_show_phase_path(id: @phase.id, section_id: @section.id, edit: 'true'), notice: _('Information was successfully deleted.')
+ else
+ redirect_to admin_show_phase_path(id: @phase.id, section_id: @section.id, edit: 'true'), notice: failed_destroy_error(@question, 'question')
+ end
end
end
\ No newline at end of file
diff --git a/app/controllers/registrations_controller.rb b/app/controllers/registrations_controller.rb
index 8d3153d..8f12488 100644
--- a/app/controllers/registrations_controller.rb
+++ b/app/controllers/registrations_controller.rb
@@ -93,12 +93,12 @@
if current_user.email != params[:user][:email] # if user changing email
if params[:user][:current_password].blank? # password needs to be present
message = _('Please enter your password to change email address.')
- succesfully_updated = false
+ successfully_updated = false
else
- succesfully_updated = current_user.update_with_password(password_update)
+ successfully_updated = current_user.update_with_password(password_update)
end
elsif params[:user][:password].present? # user is changing password
- succesfully_updated = false # shared across first 3 conditions
+ successfully_updated = false # shared across first 3 conditions
if params[:user][:current_password].blank?
message = _('Please enter your current password')
elsif params[:user][:password_confirmation].blank?
@@ -106,13 +106,13 @@
elsif params[:user][:password] != params[:user][:password_confirmation]
message = _('Password and comfirmation must match')
else
- succesfully_updated = current_user.update_with_password(password_update)
+ successfully_updated = current_user.update_with_password(password_update)
end
else # potentially unreachable... but I dont like to leave off the else
- succesfully_updated = current_user.update_with_password(password_update)
+ successfully_updated = current_user.update_with_password(password_update)
end
else # password not required
- current_user.update_without_password(update_params)
+ successfully_updated = current_user.update_without_password(update_params)
end
#unlink shibboleth from user's details
@@ -121,24 +121,25 @@
end
#render the correct page
- if succesfully_updated
+ if successfully_updated
if confirm
current_user.skip_confirmation!
current_user.save!
end
session[:locale] = current_user.get_locale unless current_user.get_locale.nil?
set_gettext_locale #Method defined at controllers/application_controller.rb
- set_flash_message :notice, :updated
+ set_flash_message :notice, _('Details successfully updated.')
sign_in current_user, bypass_sign_in: true # Sign in the user bypassing validation in case his password changed
- redirect_to({:controller => "registrations", :action => "edit"}, {:notice => _('Details successfully updated.')})
+ redirect_to edit_user_registration_path, notice: _('Details successfully updated.')
+
else
- flash[:notice] = message.blank? ? _('Update unsucessful, changes not saved') : messages
+ flash[:notice] = message.blank? ? failed_update_error(current_user, _('profile')) : message
render "edit"
end
end
def sign_up_params
- params.require(:user).permit(:email, :password, :password_confirmation,
+ params.require(:user).permit(:email, :password, :password_confirmation, :firstname, :surname,
:accept_terms, :org_id, :other_organisation)
end
diff --git a/app/controllers/roles_controller.rb b/app/controllers/roles_controller.rb
index 35c95d6..c909016 100644
--- a/app/controllers/roles_controller.rb
+++ b/app/controllers/roles_controller.rb
@@ -20,7 +20,7 @@
UserMailer.sharing_notification(@role).deliver
flash[:notice] = message
else
- flash[:notice] = @role.errors
+ flash[:notice] = generate_error_notice(@role, _('role'))
end
else
flash[:notice] = _('Please enter an email address')
@@ -39,6 +39,7 @@
UserMailer.permissions_change_notification(@role).deliver
redirect_to controller: 'plans', action: 'share', id: @role.plan.id
else
+ flash[:notice] = generate_error_notice(@role, _('role'))
render action: "edit"
end
end
@@ -49,10 +50,9 @@
user = @role.user
plan = @role.plan
@role.destroy
-
flash[:notice] = _('Access removed')
UserMailer.project_access_removed_notification(user, plan).deliver
- redirect_to controller: 'plans', action: 'share', id: @role.plan.slug
+ redirect_to controller: 'plans', action: 'share', id: @role.plan.id
end
private
diff --git a/app/controllers/sections_controller.rb b/app/controllers/sections_controller.rb
index 6df6210..e05418c 100644
--- a/app/controllers/sections_controller.rb
+++ b/app/controllers/sections_controller.rb
@@ -13,7 +13,13 @@
redirect_to admin_show_phase_path(id: @section.phase_id,
:section_id => @section.id, edit: 'true'), notice: _('Information was successfully created.')
else
- render action: "phases/admin_show"
+ @edit = (@phase.template.org == current_user.org)
+ @open = true
+ @sections = @phase.sections
+ @section_id = @section.id
+ @question_id = nil
+ flash[:notice] = failed_create_error(@section, _('section'))
+ render template: 'phases/admin_show'
end
end
@@ -27,7 +33,13 @@
if @section.update_attributes(params[:section])
redirect_to admin_show_phase_path(id: @phase.id, section_id: @section.id , edit: 'true'), notice: _('Information was successfully updated.')
else
- render action: "phases/admin_show"
+ @edit = (@phase.template.org == current_user.org)
+ @open = true
+ @sections = @phase.sections
+ @section_id = @section.id
+ @question_id = nil
+ flash[:notice] = failed_update_error(@section, _('section'))
+ render template: 'phases/admin_show'
end
end
@@ -37,8 +49,18 @@
@section = Section.includes(phase: :template).find(params[:section_id])
authorize @section
@phase = @section.phase
- @section.destroy
- redirect_to admin_show_phase_path(id: @phase.id, edit: 'true' ), notice: _('Information was successfully deleted.')
+ if @section.destroy
+ redirect_to admin_show_phase_path(id: @phase.id, edit: 'true' ), notice: _('Information was successfully deleted.')
+ else
+ @edit = (@phase.template.org == current_user.org)
+ @open = true
+ @sections = @phase.sections
+ @section_id = @section.id
+ @question_id = nil
+
+ flash[:notice] = failed_destroy_error(@section, _('section'))
+ render template: 'phases/admin_show'
+ end
end
end
\ No newline at end of file
diff --git a/app/controllers/templates_controller.rb b/app/controllers/templates_controller.rb
index 4f5e453..c150673 100644
--- a/app/controllers/templates_controller.rb
+++ b/app/controllers/templates_controller.rb
@@ -54,6 +54,8 @@
@template = Template.includes(:org, phases: [sections: [questions: [:question_options, :question_format,
:suggested_answers]]]).find(params[:id])
# check to see if this is a funder template needing customized
+
+ authorize @template
if @template.org_id != current_user.org_id
# definitely need to deep_copy the given template
new_customization = Template.deep_copy(@template)
@@ -145,7 +147,7 @@
new_version.save!
@template = new_version
end
- authorize @template
+
# once the correct template has been generated, we convert it to hash
@hash = @template.to_hash
end
@@ -170,7 +172,9 @@
end
redirect_to admin_template_template_path(), notice: _('Information was successfully updated.')
else
- render action: "edit"
+ @hash = @template.to_hash
+ flash[:notice] = failed_update_error(@template, _('template'))
+ render 'admin_template'
end
end
@@ -185,6 +189,8 @@
# creates a new template with version 0 and new dmptemplate_id
def admin_create
@template = Template.new(params[:template])
+ authorize @template
+
@template.org_id = current_user.org_id
@template.description = params['template-desc']
@template.published = false
@@ -196,10 +202,12 @@
random = rand 2147483647
break random unless Template.exists?(dmptemplate_id: random)
end
- authorize @template
- if @template.save!
+
+ if @template.save
redirect_to admin_template_template_path(@template), notice: _('Information was successfully created.')
else
+ @hash = @template.to_hash
+ flash[:notice] = failed_create_error(@template, _('template'))
render action: "admin_new"
end
end
@@ -209,8 +217,13 @@
def admin_destroy
@template = Template.find(params[:id])
authorize @template
- @template.destroy
- redirect_to admin_index_template_path
+ if @template.destroy
+ redirect_to admin_index_template_path
+ else
+ @hash = @template.to_hash
+ flash[:notice] = failed_destroy_error(@template, _('template'))
+ render admin_template_template_path(@template)
+ end
end
# GET /templates/1
diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb
index 98b9c59..13f857c 100644
--- a/app/controllers/users_controller.rb
+++ b/app/controllers/users_controller.rb
@@ -18,7 +18,7 @@
@user = User.includes(:perms).find(params[:id])
authorize @user
user_perms = current_user.perms
- @perms = user_perms & [Perm::GRANT_PERMISSIONS, Perm::MODIFY_TEMPLATES, Perm::MODIFY_GUIDANCE, Perm::USE_API, Perm::CHANGE_ORG_DETAILS]
+ @perms = user_perms & [Perm.grant_permissions, Perm.modify_templates, Perm.modify_guidance, Perm.use_api, Perm.change_org_details]
end
##
@@ -34,14 +34,14 @@
if @user.perms.include? perm
if ! perms.include? perm
@user.perms.delete(perm)
- if perm.id == Perm::USE_API.id
+ if perm.id == Perm.use_api.id
@user.remove_token!
end
end
else
if perms.include? perm
@user.perms << perm
- if perm.name == Perm::USE_API.id
+ if perm.name == Perm.use_api.id
@user.keep_or_generate_token!
end
end
diff --git a/app/models/guidance.rb b/app/models/guidance.rb
index 00b912c..f22cf3b 100644
--- a/app/models/guidance.rb
+++ b/app/models/guidance.rb
@@ -13,12 +13,9 @@
##
# Associations
belongs_to :guidance_group
-# belongs_to :question
has_and_belongs_to_many :themes, join_table: "themes_in_guidance"
# depricated, but required for migration "single_group_for_guidance"
-# has_and_belongs_to_many :guidance_groups, join_table: "guidance_in_group"
-
-
+ #has_and_belongs_to_many :guidance_groups, join_table: "guidance_in_group"
# EVALUATE CLASS AND INSTANCE METHODS BELOW
diff --git a/app/models/perm.rb b/app/models/perm.rb
index 5efd21f..2bc5aad 100644
--- a/app/models/perm.rb
+++ b/app/models/perm.rb
@@ -12,12 +12,21 @@
##
# Constant perms
- ADD_ORGS = Perm.where(name: 'add_organisations').first.freeze
- CHANGE_AFFILIATION = Perm.where(name: 'change_org_affiliation').first.freeze
- GRANT_PERMISSIONS = Perm.where(name: 'grant_permissions').first.freeze
- MODIFY_TEMPLATES = Perm.where(name: 'modify_templates').first.freeze
- MODIFY_GUIDANCE = Perm.where(name: 'modify_guidance').first.freeze
- USE_API = Perm.where(name: 'use_api').first.freeze
- CHANGE_ORG_DETAILS = Perm.where(name: 'change_org_details').first.freeze
- GRANT_API = Perm.where(name: 'grant_api_to_orgs').first.freeze
+ #ADD_ORGS = Perm.where(name: 'add_organisations').first.freeze
+ #CHANGE_AFFILIATION = Perm.where(name: 'change_org_affiliation').first.freeze
+ #GRANT_PERMISSIONS = Perm.where(name: 'grant_permissions').first.freeze
+ #MODIFY_TEMPLATES = Perm.where(name: 'modify_templates').first.freeze
+ #MODIFY_GUIDANCE = Perm.where(name: 'modify_guidance').first.freeze
+ #USE_API = Perm.where(name: 'use_api').first.freeze
+ #CHANGE_ORG_DETAILS = Perm.where(name: 'change_org_details').first.freeze
+ #GRANT_API = Perm.where(name: 'grant_api_to_orgs').first.freeze
+
+ scope :add_orgs, -> {Perm.find_by(name: 'add_organisations')}
+ scope :change_affiliation, -> {Perm.find_by(name: 'change_org_affiliation')}
+ scope :grant_permissions, -> {Perm.find_by(name: 'grant_permissions')}
+ scope :modify_templates, -> {Perm.find_by(name: 'modify_templates')}
+ scope :modify_guidance, -> {Perm.find_by(name: 'modify_guidance')}
+ scope :use_api, -> {Perm.find_by(name: 'use_api')}
+ scope :change_org_details, -> {Perm.find_by(name: 'change_org_details')}
+ scope :grant_api, -> {Perm.find_by(name: 'grant_api_to_orgs')}
end
diff --git a/app/models/role.rb b/app/models/role.rb
index cee961b..75ad26c 100644
--- a/app/models/role.rb
+++ b/app/models/role.rb
@@ -2,17 +2,18 @@
include FlagShihTzu
##
- # Associations
+ # Associationsrequire "role"
+
belongs_to :user
belongs_to :plan
##
# Define Bit Field Values
# Column access
- has_flags 1 => :creator,
- 2 => :administrator,
- 3 => :editor,
- 4 => :commenter,
+ has_flags 1 => :creator, # 1
+ 2 => :administrator, # 2
+ 3 => :editor, # 4
+ 4 => :commenter, # 8
column: 'access'
validates :user, :plan, :access, presence: true
@@ -37,3 +38,22 @@
end
end
+
+# -----------------------------------------------------
+# Bitwise key
+# -----------------------------------------------------
+# 01 - creator
+# 02 - administrator
+# 03 - creator + administrator
+# 04 - editor
+# 05 - creator + editor
+# 06 - administraor + editor
+# 07 - creator + editor + administrator
+# 08 - commenter
+# 09 - creator + commenter
+# 10 - administrator + commenter
+# 11 - creator + administrator + commenter
+# 12 - editor + commenter
+# 13 - creator + editor + commenter
+# 14 - administrator + editor + commenter
+# 15 - creator + administrator + editor + commenter
\ No newline at end of file
diff --git a/app/models/user.rb b/app/models/user.rb
index ace73b5..dc53469 100644
--- a/app/models/user.rb
+++ b/app/models/user.rb
@@ -163,7 +163,7 @@
#
# @return [Boolean] true if the user can add new organisations
def can_add_orgs?
- perms.include? Perm::ADD_ORGS
+ perms.include? Perm.add_orgs
end
##
@@ -171,7 +171,7 @@
#
# @return [Boolean] true if the user can change their organisation affiliations
def can_change_org?
- perms.include? Perm::CHANGE_AFFILIATION
+ perms.include? Perm.change_affiliation
end
##
@@ -179,7 +179,7 @@
#
# @return [Boolean] true if the user can grant their permissions to others
def can_grant_permissions?
- perms.include? Perm::GRANT_PERMISSIONS
+ perms.include? Perm.grant_permissions
end
##
@@ -187,7 +187,7 @@
#
# @return [Boolean] true if the user can modify organisation templates
def can_modify_templates?
- perms.include? Perm::MODIFY_TEMPLATES
+ self.perms.include? Perm.modify_templates
end
##
@@ -195,7 +195,7 @@
#
# @return [Boolean] true if the user can modify organistion guidance
def can_modify_guidance?
- perms.include? Perm::MODIFY_GUIDANCE
+ perms.include? Perm.modify_guidance
end
##
@@ -203,7 +203,7 @@
#
# @return [Boolean] true if the user can use the api
def can_use_api?
- perms.include? Perm::USE_API
+ perms.include? Perm.use_api
end
##
@@ -211,7 +211,7 @@
#
# @return [Boolean] true if the user can modify the org's details
def can_modify_org_details?
- perms.include? Perm::CHANGE_ORG_DETAILS
+ perms.include? Perm.change_org_details
end
@@ -220,7 +220,7 @@
#
# @return [Boolean] true if the user can grant api permissions to organisations
def can_grant_api_to_orgs?
- perms.include? Perm::GRANT_API
+ perms.include? Perm.grant_api
end
##
diff --git a/app/policies/plan_policy.rb b/app/policies/plan_policy.rb
index 98256fe..318608d 100644
--- a/app/policies/plan_policy.rb
+++ b/app/policies/plan_policy.rb
@@ -31,14 +31,21 @@
def show_export?
@plan.readable_by?(@user.id)
end
+
def update?
@plan.editable_by?(@user.id)
end
+ def destroy?
+ @plan.editable_by?(@user.id)
+ end
+
def status?
@plan.readable_by?(@user.id)
end
+# TODO: These routes are no lonmger used
+=begin
def section_answers?
@plan.readable_by?(@user.id)
end
@@ -62,6 +69,7 @@
def unlock_section?
@plan.editable_by?(@user.id)
end
+=end
def answer?
@plan.readable_by?(@user.id)
diff --git a/app/views/phases/admin_show.html.erb b/app/views/phases/admin_show.html.erb
index 8763623..cd02a2d 100644
--- a/app/views/phases/admin_show.html.erb
+++ b/app/views/phases/admin_show.html.erb
@@ -29,11 +29,11 @@