archive/app/models/archive_file.rb

120 lines
2.7 KiB
Ruby

# encoding: utf-8
class ArchiveFile
include Mongoid::Document
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
before_save do
cat = self.category rescue nil
if cat && ArchiveCategory.where(:category_id => cat.id).count==0
ArchiveCategory.create(category_id: cat.id.to_s,sort_number: cat.sort_number)
end
end
field :title, as: :slug_title, localize: true
field :description, localize: true
field :url, localize: true
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
field :rss2_sn
# scope :can_display,where(is_hidden: false)
scope :can_display, ->{where(is_hidden: false).order_by([:is_top, :desc],[:sort_number, :asc])}
# 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
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]
end
end
self.url_translations = temp_url
end
# 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
self.archive_file_multiples.each do |t|
if t.should_destroy
t.destroy
end
end
end
end