diff --git a/app/controllers/archives_controller.rb b/app/controllers/archives_controller.rb index 0133d81..e4af582 100644 --- a/app/controllers/archives_controller.rb +++ b/app/controllers/archives_controller.rb @@ -41,13 +41,15 @@ class ArchivesController < ApplicationController } end end - if archive.url.present? - files << { - "file-name" => archive.title, - "file-type" => "link", - "file-url" => archive.url - } - end + if archive.urls.present? + archive.urls.each_with_index do |url,i| + files << { + "file-name" => archive.title, + "file-type" => archive.get_url_text(i), + "file-url" => url + } + end + end archives << { "archive-title" => archive.title, "description" => archive.description, @@ -127,13 +129,15 @@ class ArchivesController < ApplicationController "file-url" => "/xhr/archive/download?file=#{file.id}" } end - if archive.url.present? - files << { - "file-name" => archive.title, - "file-type" => "link", - "file-url" => archive.url - } - end + if archive.urls.present? + archive.urls.each_with_index do |url,i| + files << { + "file-name" => archive.title, + "file-type" => archive.get_url_text(i), + "file-url" => url + } + end + end end { "archive-title" => archive.title, @@ -184,12 +188,14 @@ class ArchivesController < ApplicationController } end end - if archive.url.present? - files << { - "file-name" => archive.title, - "file-type" => "link", - "file-url" => archive.url - } + if archive.urls.present? + archive.urls.each_with_index do |url,i| + files << { + "file-name" => archive.title, + "file-type" => archive.get_url_text(i), + "file-url" => url + } + end end { "archive-title" => archive.title, diff --git a/app/models/archive_file.rb b/app/models/archive_file.rb index eb19fc5..f3417c3 100644 --- a/app/models/archive_file.rb +++ b/app/models/archive_file.rb @@ -24,7 +24,9 @@ class ArchiveFile field :title, as: :slug_title, localize: true field :description, localize: true - field :url, localize: true + field :urls, localize: true, type: Array + field :url, localize: true #Old field + field :url_texts, localize: true, type: Array, default: [] field :create_user_id field :update_user_id field :postdate , :type => DateTime, :default => Time.now @@ -48,18 +50,41 @@ class ArchiveFile before_save :add_http - protected - - def add_http - temp_url = {} - Site.first.in_use_locales.each do |locale| - locale = locale.to_s - temp_url[locale] = self.url_translations[locale] - unless /https?:\/\/[\S]+/.match(self.url_translations[locale]) || self.url_translations[locale].blank? - temp_url[locale] = 'http://' + self.url_translations[locale] + after_initialize do + unless self.new_record? + if self.urls.nil? && self.url.present? + self.urls_translations = self.url_translations.map{|k,v| [k,[v]]}.to_h + self.save end end - self.url_translations = temp_url + end + + def get_url_text(idx=0,org=false) + url_text = self.url_texts[idx] rescue nil + if org + url_text + else + url_text.present? ? url_text : "link" + end + end + protected + def add_http + in_use_locales = Site.first.in_use_locales + temp_urls = {} + in_use_locales.each do |locale| + locale = locale.to_s + temp_urls[locale] = [] + self.urls_translations[locale].each do |tmp_url| + tmp = tmp_url + if tmp.present? + unless /^(http|https|ftp):\/\/[\S]+/.match(tmp_url) || url.include?("mailto:") + tmp = 'http://' + tmp_url + end + temp_urls[locale] << tmp + end + end rescue nil + end + self.urls_translations = temp_urls end # def to_indexed_json @@ -115,5 +140,4 @@ class ArchiveFile end end end - end diff --git a/app/views/admin/archive_files/_form.html.erb b/app/views/admin/archive_files/_form.html.erb index 99e7fcd..ecb7cf2 100644 --- a/app/views/admin/archive_files/_form.html.erb +++ b/app/views/admin/archive_files/_form.html.erb @@ -161,17 +161,21 @@ <% end %> - +