universal_table/app/models/column_entry.rb

98 lines
2.9 KiB
Ruby

class ColumnEntry
include Mongoid::Document
include Mongoid::Timestamps
include Admin::UniversalTablesHelper
include ActionView::Helpers::NumberHelper
field :text, :localize => true
field :content, :localize => true
field :date, type: DateTime
field :period_from, type: DateTime
field :period_to, type: DateTime
field :number, type: Integer
mount_uploader :image, ImageUploader
has_many :column_entry_files, :autosave => true, :dependent => :destroy
accepts_nested_attributes_for :column_entry_files, :allow_destroy => true
after_save :save_column_entry_files
belongs_to :table_entry, index: true
belongs_to :table_column, index: true
I18n.available_locales.each do |locale|
index({"text.#{locale}" => 1}, { unique: false, background: true })
index({"content.#{locale}" => 1}, { unique: false, background: true })
end
def type
self.table_column.type
end
def save_column_entry_files
return if @skip_callback
self.column_entry_files.each do |t|
if t.should_destroy
t.destroy
end
end
end
def have_data(locale)
flag = nil
case self.type
when "text"
flag = self.text_translations[locale].present?
when "integer"
flag = true
when "editor"
flag = self.content_translations[locale].present?
when "date"
flag = self.date.present?
when "period"
flag = self.period_from.present? || self.period_to.present?
when "image"
flag = self.image.present?
when "file"
flag = false
self.column_entry_files.each do |entry_file|
next unless entry_file.choose_lang_display(locale) && entry_file.file.present?
flag = true
end
else
flag = true
end
flag
end
def get_frontend_text(column)
text = ""
case self.type
when "text"
text = self.text
when "integer"
text = self.number
when "editor"
text = self.content
when "date"
text = format_date(self.date, column.date_format)
when "period"
text = format_date(self.period_from, column.date_format) + " ~ " + format_date(self.period_to, column.date_format)
text = "" if text.starts_with?(" ~")
when "image"
text = "<img src='#{self.image.thumb.url}' class='image-preview' />"
when "file"
locale = I18n.locale.to_s
text = "<ul class=\"column_entry_files\">"
self.column_entry_files.desc(:sort_number).each do |entry_file|
next unless entry_file.choose_lang_display(locale)
file_title = entry_file.get_file_title
text += "<li class=\"column_entry_file\"><a class=\"column_entry_file_link\" href=\"#{entry_file.get_link}\" title=\"#{file_title}\" target=\"_blank\">#{file_title}</a><span class=\"file_size\">(#{number_to_human_size(entry_file.file.size)})</span><span class=\"view_count\"><i class=\"fa fa-eye\" title=\"#{I18n.t("universal_table.downloaded_times")}\"></i><span class=\"view-count\">#{entry_file.download_count}</span></span></li>"
end
text += "</ul>"
end
text
end
end