archive/app/models/archive_file.rb

144 lines
3.3 KiB
Ruby
Raw Normal View History

2014-05-14 11:52:06 +00:00
# encoding: utf-8
2014-05-08 06:03:33 +00:00
class ArchiveFile
include Mongoid::Document
2014-05-14 11:52:06 +00:00
include Mongoid::Timestamps
include OrbitCategory::Categorizable
include OrbitModel::Status
include OrbitTag::Taggable
include Slug
# include Tire::Model::Search
# include Tire::Model::Callbacks
# BelongsToCategory = :archive_file_category
# PAYMENT_TYPES = @site_valid_locales
2021-09-17 05:43:32 +00:00
before_save do
cat = self.category rescue nil
if cat && ArchiveCategory.where(:category_id => cat.id).count==0
2021-09-17 06:02:47 +00:00
ArchiveCategory.create(category_id: cat.id.to_s,sort_number: cat.sort_number)
2021-09-17 05:43:32 +00:00
end
end
2014-05-14 11:52:06 +00:00
field :title, as: :slug_title, localize: true
field :description, localize: true
2021-09-29 10:43:34 +00:00
field :urls, localize: true, type: Array
field :url, localize: true #Old field
field :url_texts, localize: true, type: Array, default: []
2014-05-14 11:52:06 +00:00
field :create_user_id
field :update_user_id
field :postdate , :type => DateTime, :default => Time.now
field :deadline , :type => DateTime
field :uid, type: String
field :sort_number, type: Integer
2014-06-06 04:06:34 +00:00
field :rss2_sn
2014-05-14 11:52:06 +00:00
# scope :can_display,where(is_hidden: false)
2016-05-12 11:53:49 +00:00
scope :can_display, ->{where(is_hidden: false).order_by([:is_top, :desc],[:sort_number, :asc])}
2014-05-14 11:52:06 +00:00
# belongs_to :archive_file_category
has_many :archive_file_multiples, :autosave => true, :dependent => :destroy
accepts_nested_attributes_for :archive_file_multiples, :allow_destroy => true
# validates :title, :at_least_one => true
after_save :save_archive_file_multiples
2018-01-25 09:24:39 +00:00
before_save :add_http
2021-09-29 10:43:34 +00:00
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
end
2018-01-25 09:24:39 +00:00
2021-09-29 10:43:34 +00:00
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
2018-01-25 09:24:39 +00:00
def add_http
2021-09-29 10:43:34 +00:00
in_use_locales = Site.first.in_use_locales
temp_urls = {}
in_use_locales.each do |locale|
2018-01-25 09:24:39 +00:00
locale = locale.to_s
2021-09-29 10:43:34 +00:00
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
2018-01-25 09:24:39 +00:00
end
2021-09-29 10:43:34 +00:00
self.urls_translations = temp_urls
2018-01-25 09:24:39 +00:00
end
2014-05-14 11:52:06 +00:00
# def to_indexed_json
# self.to_json
# end
# search_in :title
# searchable do
# text :titles do
# title_translations.to_a.collect{|t| t[1]}
# end
# boolean :frontend_search do
# !is_hidden
# end
# end
# def self.search( category_id = nil )
# if category_id.to_s.size > 0
# find(:all, :conditions => {archive_file_category_id: category_id}).desc( :is_top, :title )
# else
# find(:all).desc( :is_top, :title)
# end
# end
def self.find_by_param(input)
self.find_by(uid: input)
end
def self.widget_datas
where( :is_hidden => false ).desc(:is_top, :title)
end
def get_file_icon( file_data )
file_icon = "<span class=\"o-archives-file-type\">#{file_data.split('.')[-1]}</span>".html_safe
end
def save_archive_file_multiples
2021-09-17 06:02:47 +00:00
self.archive_file_multiples.each do |t|
if t.should_destroy
t.destroy
end
2014-05-14 11:52:06 +00:00
end
end
2014-05-08 06:03:33 +00:00
end