diff --git a/app/controllers/orgs_controller.rb b/app/controllers/orgs_controller.rb
index c64931b..8a6deae 100644
--- a/app/controllers/orgs_controller.rb
+++ b/app/controllers/orgs_controller.rb
@@ -8,6 +8,7 @@
@org = Org.find(params[:id])
authorize @org
@languages = Language.all.order("name")
+ @org.links = {"org": []} unless @org.links.present?
end
##
@@ -17,26 +18,13 @@
@org = Org.find(params[:id])
authorize @org
@org.logo = attrs[:logo] if attrs[:logo]
- failure = ''
tab = (attrs[:feedback_enabled].present? ? 'feedback' : 'profile')
-
- if attrs[:links].present?
- if is_json_array_of_objects?(attrs[:links])
- json = JSON.parse(attrs[:links])
- # Make sure that the JSON hash is structured as: {"link":"string","text":"string"}
- if json.all?{ |o| o['link'].present? && o['text'].present? }
- @org.links = json
- else
- failure = _('Unable to save your changes. Invalid URLs.')
- end
- else
- failure = _('Unable to save your changes. Invalid URLs.')
- end
- attrs.delete('links')
+ if params[:org_links].present?
+ @org.links = JSON.parse(params[:org_links])
end
begin
- if failure.blank? && @org.update_attributes(attrs)
+ if @org.update_attributes(attrs)
redirect_to "#{admin_edit_org_path(@org)}\##{tab}", notice: success_message(_('organisation'), _('saved'))
else
failure = failed_update_error(@org, _('organisation')) if failure.blank?
@@ -91,7 +79,7 @@
private
def org_params
- params.require(:org).permit(:name, :abbreviation, :logo, :contact_email, :contact_name, :remove_logo, :links,
+ params.require(:org).permit(:name, :abbreviation, :logo, :contact_email, :contact_name, :remove_logo,
:feedback_enabled, :feedback_email_subject, :feedback_email_msg)
end
end
diff --git a/app/models/org.rb b/app/models/org.rb
index a5ee99d..df88458 100644
--- a/app/models/org.rb
+++ b/app/models/org.rb
@@ -2,12 +2,14 @@
include GlobalHelpers
include FlagShihTzu
extend Dragonfly::Model::Validations
-
+ validates_with OrgLinksValidator
+
##
# Sort order: Name ASC
default_scope { order(name: :asc) }
- # Stores links as an JSON array: [{"link":"http://www.myorg.edu","text":"My Org"},...]
+ # Stores links as an JSON object: { org: [{"link":"www.example.com","text":"foo"}, ...] }
+ # The links are validated against custom validator allocated at validators/template_links_validator.rb
serialize :links, JSON
##
@@ -34,7 +36,6 @@
:language, :org_type, :region, :token_permission_types,
:guidance_group_ids, :is_other, :region_id, :logo_uid, :logo_name,
:feedback_enabled, :feedback_email_subject, :feedback_email_msg
-
##
# Validators
validates :contact_email, email: true, allow_nil: true
diff --git a/app/validators/org_links_validator.rb b/app/validators/org_links_validator.rb
new file mode 100644
index 0000000..82ca15d
--- /dev/null
+++ b/app/validators/org_links_validator.rb
@@ -0,0 +1,13 @@
+class OrgLinksValidator < ActiveModel::Validator
+ include JSONLinkValidator
+ def validate(record)
+ links = record.links
+ if links.is_a?(Hash)
+ if !links.has_key?('org')
+ record.errors[:links] << _('A key "org" is expected for links hash') %{ :key => k }
+ end
+ else
+ record.errors[:links] << _('A hash is expected for links')
+ end
+ end
+end
\ No newline at end of file
diff --git a/app/views/layouts/_branding.html.erb b/app/views/layouts/_branding.html.erb
index e59a9b4..6705e94 100644
--- a/app/views/layouts/_branding.html.erb
+++ b/app/views/layouts/_branding.html.erb
@@ -8,25 +8,31 @@
- <% if user_signed_in? && !current_user.org.nil? && current_user.org.logo.present? %>
- <%= link_to(image_tag(current_user.org.logo.thumb('100x100%').url,
- alt: current_user.org.name,
- class: "org-logo",
- title: current_user.org.name),
- current_user.org.target_url) %>
+ <% if user_signed_in? && !current_user.org.nil? %>
+ <% if current_user.org.logo.present? %>
+ <%= link_to(image_tag(current_user.org.logo.thumb('100x100%').url,
+ alt: current_user.org.name,
+ class: "org-logo",
+ title: current_user.org.name),
+ current_user.org.target_url) %>
+ <% else %>
+
- <%= f.hidden_field :links %>
-
-
-
<%= _("Organisation URL") %> (<%= _('Up to ') %>)
-
-
- <% if @org.links.length > 0 %>
- <% @org.links.each do |url| %>
- <%= render partial: 'org_link', locals: {link: url['link'], text: url['text']} %>
- <% end %>
- <% else %>
- <%= render partial: 'org_link', locals: {link: '', text: ''} %>
- <% end %>
-
+
+ <%= render(partial: '/shared/links',
+ locals: {
+ context: 'org',
+ title: _('Organisation URLs'),
+ links: @org.links['org'],
+ max_number_links: MAX_NUMBER_LINKS_FUNDER,
+ tooltip: _('Links will be displayed next to your organisation\'s logo') }) %>
+ <%= hidden_field_tag('org_links', value: @org.links) %>
diff --git a/db/seeds.rb b/db/seeds.rb
index 1c6d553..94f6545 100644
--- a/db/seeds.rb
+++ b/db/seeds.rb
@@ -165,17 +165,16 @@
orgs = [
{name: Rails.configuration.branding[:organisation][:name],
abbreviation: Rails.configuration.branding[:organisation][:abbreviation],
- banner_text: 'This is an example organisation',
- org_type: 3,
+ org_type: 3, links: {"org":[]},
language_id: Language.find_by(abbreviation: 'en_GB'),
token_permission_types: TokenPermissionType.all},
{name: 'Government Agency',
abbreviation: 'GA',
- org_type: 2,
+ org_type: 2, links: {"org":[]},
language: Language.find_by(abbreviation: 'en_GB')},
{name: 'University of Exampleland',
abbreviation: 'UOS',
- org_type: 1,
+ org_type: 1, links: {"org":[]},
language: Language.find_by(abbreviation: 'en_GB')}
]
orgs.map{ |o| Org.create!(o) if Org.find_by(abbreviation: o[:abbreviation]).nil? }
@@ -353,15 +352,19 @@
is_default: true,
version: 0,
migrated: false,
- dmptemplate_id: 1},
+ dmptemplate_id: 1,
+ visibility: Template.visibilities[:publicly_visible],
+ links: '{"funder":[],"sample_plan":[]}'},
{title: "OLD - Department of Testing Award",
- published: false,
- org: Org.find_by(abbreviation: 'GA'),
- is_default: false,
- version: 0,
- migrated: false,
- dmptemplate_id: 2},
+ published: false,
+ org: Org.find_by(abbreviation: 'GA'),
+ is_default: false,
+ version: 0,
+ migrated: false,
+ visibility: Template.visibilities[:organisationally_visible],
+ dmptemplate_id: 2,
+ links: '{"funder":[],"sample_plan":[]}'},
{title: "Department of Testing Award",
published: true,
@@ -369,7 +372,9 @@
is_default: false,
version: 0,
migrated: false,
- dmptemplate_id: 3}
+ visibility: Template.visibilities[:organisationally_visible],
+ dmptemplate_id: 3,
+ links: '{"funder":[],"sample_plan":[]}'}
]
# Template creation calls defaults handler which sets is_default and
# published to false automatically, so update them after creation
@@ -378,6 +383,7 @@
tmplt = Template.create!(t)
tmplt.published = t[:published]
tmplt.is_default = t[:is_default]
+ tmplt.visibility = t[:visibility]
tmplt.save!
end
end
diff --git a/lib/assets/javascripts/views/orgs/admin_edit.js b/lib/assets/javascripts/views/orgs/admin_edit.js
index 64866ff..b0645ae 100644
--- a/lib/assets/javascripts/views/orgs/admin_edit.js
+++ b/lib/assets/javascripts/views/orgs/admin_edit.js
@@ -3,6 +3,7 @@
import { convertToText } from 'number-to-text/index';
import ariatiseForm from '../../utils/ariatiseForm';
import { Tinymce } from '../../utils/tinymce';
+import { eachLinks } from '../../utils/links';
import { MAX_NUMBER_ORG_URLS } from '../../constants';
$(() => {
@@ -53,15 +54,12 @@
// Serialize URLs to JSON for form submission
$('#edit_org_profile_form').submit(() => {
- const json = Array.from($('#org-link-section div.org-link')).map((el) => {
- const link = $(el).find('#org_links_url');
- const text = $(el).find('#org_links_text');
- return {
- link: (link.length > 0 ? $(link).val() : ''),
- text: (text.length > 0 ? $(text).val() : ''),
- };
+ const links = {};
+ eachLinks((ctx, value) => {
+ links[ctx] = value;
+ }).done(() => {
+ $('#org_links').val(JSON.stringify(links));
});
- $('#org_links').val(JSON.stringify(json));
});
// Initialises tinymce for any target element with class tinymce_answer
diff --git a/lib/assets/javascripts/views/shared/my_org.js b/lib/assets/javascripts/views/shared/my_org.js
index 8c1d6a7..535b0d8 100644
--- a/lib/assets/javascripts/views/shared/my_org.js
+++ b/lib/assets/javascripts/views/shared/my_org.js
@@ -28,7 +28,9 @@
});
// Display the other org textbox if the value is filled out and no org id is selected
- if ($(id).val().length <= 0 && isValidText($(text).val())) {
- toggleInputs(false);
+ if ($(id).val() && $(text).val()) {
+ if ($(id).val().length <= 0 && isValidText($(text).val())) {
+ toggleInputs(false);
+ }
}
});
diff --git a/lib/assets/stylesheets/overrides.scss b/lib/assets/stylesheets/overrides.scss
index 425e7fd..1682f64 100644
--- a/lib/assets/stylesheets/overrides.scss
+++ b/lib/assets/stylesheets/overrides.scss
@@ -251,6 +251,10 @@
#org-navbar {
border-radius: 0px;
margin-top: -20px;
+
+ #banner-org-name {
+ margin-top: 40px;
+ }
}
@media (min-width: 768px) {
#org-navbar .navbar-nav li a {
diff --git a/lib/tasks/bugfix.rake b/lib/tasks/bugfix.rake
index d31d86c..ca0b627 100644
--- a/lib/tasks/bugfix.rake
+++ b/lib/tasks/bugfix.rake
@@ -3,6 +3,7 @@
desc "Upgrade to 1.0"
task v1_0_0: :environment do
Rake::Task['bugfix:set_template_visibility'].execute
+ Rake::Task['bugfix:set_org_link_defaults'].execute
end
desc "Bug fixes for version v0.3.3"
@@ -58,4 +59,20 @@
Template.where(org_id: funders).update_all visibility: Template.visibilities[:publicly_visible]
Template.default.update visibility: Template.visibilities[:publicly_visible]
end
+
+ desc "Set all orgs.links defaults"
+ task set_org_link_defaults: :environment do
+ Org.all.each do |org|
+ org.links = '{"org":[]}'
+ org.save!
+ end
+ end
+
+ desc "Set all template.links defaults"
+ task set_org_link_defaults: :environment do
+ Template.all.each do |template|
+ template.links = '{"funder":[],"sample_plan":[]}'
+ template.save!
+ end
+ end
end
\ No newline at end of file
diff --git a/test/functional/orgs_controller_test.rb b/test/functional/orgs_controller_test.rb
index 0dcdedc..a5a2aa2 100644
--- a/test/functional/orgs_controller_test.rb
+++ b/test/functional/orgs_controller_test.rb
@@ -25,7 +25,7 @@
# admin_update_org PUT /org/admin/:id/admin_update orgs#admin_update
setup do
- @org = Org.first
+ @org = Org.create!(name: 'Testing', abbreviation: 'TST', links: {"org":[]})
scaffold_org_admin(@org)
end
@@ -47,7 +47,7 @@
# PUT /org/admin/:id/admin_update (admin_update_org_path)
# ----------------------------------------------------------
test 'update the org' do
- params = {name: 'Testing UPDATE'}
+ params = {name: 'Testing UPDATE', links: {"org": []}}
# Should redirect user to the root path if they are not logged in!
put admin_update_org_path(@org), {org: params}
diff --git a/test/test_helper.rb b/test/test_helper.rb
index e333611..1d8928e 100644
--- a/test/test_helper.rb
+++ b/test/test_helper.rb
@@ -50,7 +50,8 @@
# ----------------------------------------------------------------------
def scaffold_template
template = Template.new(title: 'Test template',
- description: 'My test template',
+ description: 'My test template',
+ links: '{"funder":[],"sample_plan":[]}',
org: Org.first, migrated: false, dmptemplate_id: "0000009999")
template.phases << Phase.new(title: 'Test phase',
diff --git a/test/unit/guidance_test.rb b/test/unit/guidance_test.rb
index 9901ee6..7812a86 100644
--- a/test/unit/guidance_test.rb
+++ b/test/unit/guidance_test.rb
@@ -36,7 +36,7 @@
# ---------------------------------------------------
test "retrieves guidance by org" do
- org = Org.create(name: 'Tester 123', abbreviation: 'TEST', org_type: 1)
+ org = Org.create!(name: 'Tester 123', abbreviation: 'TEST', org_type: 1, links: {"org":[]})
assert Guidance.by_org(org.id).empty?, "expected the newly created org to have no guidance"
assert_not Guidance.by_org(@user.org.id).empty?, "expected the org to have guidance"
diff --git a/test/unit/language_test.rb b/test/unit/language_test.rb
index 8447bbb..c83de7a 100644
--- a/test/unit/language_test.rb
+++ b/test/unit/language_test.rb
@@ -42,7 +42,7 @@
# ---------------------------------------------------
test "can manage has_many relationship with Organisations" do
- org = Org.create(name: 'testing', abbreviation: "TEST")
+ org = Org.create(name: 'testing', abbreviation: "TEST", links: {"org":[]})
verify_has_many_relationship(Language.last, org, Language.last.orgs.count)
end
diff --git a/test/unit/org_test.rb b/test/unit/org_test.rb
index dd0bf5a..28e57cf 100644
--- a/test/unit/org_test.rb
+++ b/test/unit/org_test.rb
@@ -2,7 +2,7 @@
class OrgTest < ActiveSupport::TestCase
setup do
- @org = Org.first
+ @org = Org.create!(name: 'Testing', abbreviation: 'TST', links: {"org":[]})
@language = Language.find_by(abbreviation: I18n.default_locale)
end
@@ -13,6 +13,7 @@
assert_not(org.valid?)
org.name = 'ABCD'
+ org.links = {"org":[]}
assert(org.valid?)
end
@@ -145,7 +146,7 @@
# ---------------------------------------------------
test "can CRUD" do
- org = Org.create(name: 'testing')
+ org = Org.create(name: 'testing', links: {"org":[]})
assert_not org.id.nil?, "was expecting to be able to create a new Org: #{org.errors.map{|f, m| f.to_s + ' ' + m}.join(', ')}"
org.abbreviation = 'TEST'
diff --git a/test/unit/question_test.rb b/test/unit/question_test.rb
index b73f4df..9cd95b2 100644
--- a/test/unit/question_test.rb
+++ b/test/unit/question_test.rb
@@ -63,7 +63,7 @@
assert_equal 'Test 1', @question.annotations.where(org_id: @user.org.id).first.text, "expected the correct annotation"
assert_equal 'Test 2', @question.annotations.where(org_id: Org.first.id).first.text, "expected the correct annotation"
- org = Org.create(name: 'New One')
+ org = Org.create(name: 'New One', links: {"org":[]})
assert_equal nil, @question.get_example_answer(org.id), "expected no annotation for a new org"
end
diff --git a/test/unit/token_permission_type_test.rb b/test/unit/token_permission_type_test.rb
index b71c7ab..13c3346 100644
--- a/test/unit/token_permission_type_test.rb
+++ b/test/unit/token_permission_type_test.rb
@@ -36,7 +36,7 @@
# ---------------------------------------------------
test "can manage has_many relationship with Org" do
- org = Org.new(name: 'Testing')
+ org = Org.new(name: 'Testing', links: {"org":[]})
verify_has_many_relationship(@tpt, org, @tpt.orgs.count)
end
diff --git a/test/unit/user_test.rb b/test/unit/user_test.rb
index 2383f61..9817def 100644
--- a/test/unit/user_test.rb
+++ b/test/unit/user_test.rb
@@ -317,7 +317,7 @@
# ---------------------------------------------------
test "can manage belongs_to relationship with Org" do
- org = Org.new(name: 'Tester', abbreviation: 'TST')
+ org = Org.new(name: 'Tester', abbreviation: 'TST', links: {"org":[]})
verify_belongs_to_relationship(@user, org)
end