universal_table/app/models/column_entry.rb

71 lines
2.3 KiB
Ruby
Raw Normal View History

2015-11-13 13:10:00 +00:00
class ColumnEntry
include Mongoid::Document
include Mongoid::Timestamps
2024-07-21 07:26:48 +00:00
include Admin::UniversalTablesHelper
include ActionView::Helpers::NumberHelper
2015-11-13 13:10:00 +00:00
field :text, :localize => true
field :content, :localize => true
field :date, type: DateTime
field :period_from, type: DateTime
field :period_to, type: DateTime
2022-02-25 06:49:00 +00:00
field :number, type: Integer
2015-11-13 13:10:00 +00:00
mount_uploader :image, ImageUploader
2024-07-21 07:26:48 +00:00
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
2024-07-20 00:45:45 +00:00
belongs_to :table_entry, index: true
belongs_to :table_column, index: true
2024-07-14 14:57:37 +00:00
I18n.available_locales.each do |locale|
index({text: 1}, { collation: {locale: locale.to_s}, unique: false, background: true })
index({content: 1}, { collation: {locale: locale.to_s}, unique: false, background: true })
end
2015-11-13 13:10:00 +00:00
def type
self.table_column.type
2022-02-25 06:49:00 +00:00
end
2024-07-21 07:26:48 +00:00
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 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
2015-11-13 13:10:00 +00:00
end