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 %> + + <% end %> <% end %> -