diff --git a/app/controllers/guidances_controller.rb b/app/controllers/guidances_controller.rb
index eeb25dd..90a7051 100644
--- a/app/controllers/guidances_controller.rb
+++ b/app/controllers/guidances_controller.rb
@@ -42,9 +42,18 @@
guidance_params[:theme_ids].map{|t| guidance.themes << Theme.find(t.to_i) unless t.empty? }
end
+ guidance_group = GuidanceGroup.where(id: guidance.guidance_group_id).first
+ unless guidance_group.nil?
+ if !guidance_group.published? || guidance_group.published.nil?
+ guidance_group.published = true
+ guidance_group.save
+ end
+ end
+
if guidance.save
flash[:notice] = success_message(_('guidance'), _('created'))
redirect_to(action: :admin_index)
+
else
flash[:alert] = failed_create_error(guidance, _('guidance'))
redirect_to(action: :admin_index)
@@ -57,7 +66,15 @@
guidance = Guidance.find(params[:id])
authorize guidance
guidance.text = params["guidance-text"]
-
+
+ guidance_group = GuidanceGroup.where(id: guidance.guidance_group_id).first
+ unless guidance_group.nil?
+ if !guidance_group.published? || guidance_group.published.nil?
+ guidance_group.published = true
+ guidance_group.save
+ end
+ end
+
if guidance.update_attributes(guidance_params)
flash[:notice] = success_message(_('guidance'), _('saved'))
redirect_to(action: :admin_index)
diff --git a/app/models/org.rb b/app/models/org.rb
index a9f0e3c..1a41c1e 100644
--- a/app/models/org.rb
+++ b/app/models/org.rb
@@ -61,6 +61,8 @@
# Predefined queries for retrieving the managain organisation and funders
scope :managing_orgs, -> { where(abbreviation: Rails.configuration.branding[:organisation][:abbreviation]) }
+ after_create :create_guidance_group
+
# EVALUATE CLASS AND INSTANCE METHODS BELOW
#
# What do they do? do they do it efficiently, and do we need them?
@@ -161,4 +163,9 @@
end
end
end
+
+ # creates a dfefault Guidance Group on create on the Org
+ def create_guidance_group
+ GuidanceGroup.create(name: self.abbreviation? ? self.abbreviation : self.name , org_id: self.id)
+ end
end
diff --git a/app/views/guidance_groups/admin_new.html.erb b/app/views/guidance_groups/admin_new.html.erb
index 5ca69d9..0cbb485 100644
--- a/app/views/guidance_groups/admin_new.html.erb
+++ b/app/views/guidance_groups/admin_new.html.erb
@@ -13,6 +13,11 @@
<%= f.text_field :name, as: :string, class: "form-control", 'data-toggle': 'tooltip', title: _('Add an appropriate name for your guidance group. This name will be used to tell the end user where the guidance has come from. It will be appended to text identifying the theme e.g. "[guidance group name]: guidance on data sharing" so we suggest you just use the institution or department name.') %>
+
+ <%= f.check_box :published, 'data-toggle': 'tooltip', title: _("Check this box when you are ready for guidance associated with this group to appear on user's plans.") %>
+ <%= f.label _('Published'), for: :published, class: "control-label" %>
+
+
<%= f.check_box :optional_subset, 'data-toggle': 'tooltip', title: _("If the guidance is only meant for a subset of users e.g. those in a specific college or institute, check this box. Users will be able to select to display this subset guidance when answering questions in the 'create plan' wizard.") %>
<%= f.label _('Optional Subset'), for: :optional_subset, class: "control-label" %><%= _(' (e.g. School/ Department) ') %>
diff --git a/test/functional/plans_controller_test.rb b/test/functional/plans_controller_test.rb
index 82c545f..3d771e2 100644
--- a/test/functional/plans_controller_test.rb
+++ b/test/functional/plans_controller_test.rb
@@ -1,5 +1,5 @@
require 'test_helper'
-
+require 'byebug'
class PlansControllerTest < ActionDispatch::IntegrationTest
include Devise::Test::IntegrationHelpers
@@ -35,6 +35,14 @@
# DELETE /plans/:id plans#destroy
setup do
+ # First clear out any existing templates
+ GuidanceGroup.delete_all
+ GuidanceGroup.create!(name: "Generic Guidance (provided by the example curation...", org_id: 1,
+ created_at: "2018-01-03 21:02:14", updated_at: "2018-01-03 21:02:14",
+ optional_subset: true, published: true)
+ GuidanceGroup.create!(name: "Government Agency Advice (Funder specific guidance...", org_id: 2,
+ created_at: "2018-01-03 21:02:14", updated_at: "2018-01-03 21:02:14",
+ optional_subset: false, published: true)
@org = Org.first
scaffold_plan
@user = @plan.owner
@@ -201,6 +209,11 @@
# Make sure the guidance is attached to the template first so that its a valid selection!
q = @template.phases.first.sections.first.questions.first
+
+ #this tricky bit is needed to set guidances to newly created Guidance Groups
+ Guidance.update_all( guidance_group_id: GuidanceGroup.first.id)
+ Guidance.last.update!(guidance_group_id: GuidanceGroup.last.id)
+
q.themes << GuidanceGroup.first.guidances.first.themes.first
q.themes << GuidanceGroup.last.guidances.first.themes.first
q.save
diff --git a/test/unit/guidance_group_test.rb b/test/unit/guidance_group_test.rb
index 542bf46..47c1ce5 100644
--- a/test/unit/guidance_group_test.rb
+++ b/test/unit/guidance_group_test.rb
@@ -6,7 +6,10 @@
setup do
@user = User.first
@org = Org.last
-
+ # First clear out any existing templates
+ GuidanceGroup.all.each do |gg|
+ gg.destroy!
+ end
@guidance_group = GuidanceGroup.create(name: 'Test Guidance Group', org: @org,
optional_subset: false, published: true)
end
diff --git a/test/unit/org_test.rb b/test/unit/org_test.rb
index 28e57cf..90a10fb 100644
--- a/test/unit/org_test.rb
+++ b/test/unit/org_test.rb
@@ -192,5 +192,11 @@
tpt = TokenPermissionType.new(token_type: 'testing')
verify_has_many_relationship(@org, tpt, @org.token_permission_types.count)
end
-
+
+ # ---------------------------------------------------
+ test "Guidance Group should be created after_create of the Org" do
+ org = Org.create!(name: 'Testing Guidance Group for Org', abbreviation: 'TGG', links: {"org":[]})
+ assert_equal org.guidance_groups.count, 1
+ org.destroy!
+ end
end
diff --git a/test/unit/plan_test.rb b/test/unit/plan_test.rb
index d092536..667b649 100644
--- a/test/unit/plan_test.rb
+++ b/test/unit/plan_test.rb
@@ -3,6 +3,7 @@
class PlanTest < ActiveSupport::TestCase
setup do
+
@org = Org.first
@template = Template.first
@@ -74,7 +75,13 @@
# Create a new theme and attach it to our template's question and a guidance group
t = Theme.create!(title: 'Test A')
q = @template.phases.first.sections.first.questions.first
- g = GuidanceGroup.first.guidances.first
+ # This is to make the default guidance group created in callback to be published.
+ # This ensures the selected gudiance group test passes with appropriate GuidanceGroup.
+ gug = @org.guidance_groups.first
+ gug.published = true
+ gug.save!
+ g = gug.guidances.first
+
g.themes << t
g.save
q.themes << t
@@ -86,7 +93,7 @@
g = Guidance.create!(text: 'Testing guidance', guidance_group: gg, themes: [t])
pggs = @plan.get_guidance_group_options
- assert pggs.include?(GuidanceGroup.first)
+ assert pggs.include?(gug)
assert_not pggs.include?(gg)
end