universal_table/app/models/table_entry.rb

149 lines
5.1 KiB
Ruby

class TableEntry
include Mongoid::Document
include Mongoid::Timestamps
include Slug
attr_accessor :sort_value
field :have_data, type: Boolean, localize: true
field :sort_number, type: Integer
field :view_count, type: Integer, default: 0
has_many :column_entries, :dependent => :destroy
belongs_to :u_table, index: true
accepts_nested_attributes_for :column_entries, :allow_destroy => true
I18n.available_locales.each do |locale|
index({have_data: 1}, { collation: {locale: locale.to_s}, unique: false, background: true })
end
before_save do
if self[:sort_number].nil?
other_record = self.class.where(:u_table_id=> self.u_table_id, :id.ne=> self.id).order_by(sort_number: :desc).first
sort_number_to_set = other_record ? other_record.sort_number : 0
self.sort_number = sort_number_to_set.to_i + 1
end
self.get_have_data
end
def fix_have_data
have_data_translations = self.get_have_data
self.class.where(:id=> self.id).update_all(have_data_translations.map{|l, v| ["have_data.#{l}", v]}.to_h)
end
def get_have_data
searchable_field_ids = TableColumn.filter_searchable.where(u_table_id: self.u_table_id).pluck(:id)
searchable_column_entries = self.column_entries.where(:table_column_id.in=> searchable_field_ids).to_a
self.have_data_translations = I18n.available_locales.map do |locale|
flag = searchable_column_entries.detect{|ce| ce.have_data(locale)}.present?
[locale.to_s, flag]
end.to_h
end
def self.u_table
UTable.find(criteria.selector['u_table_id'])
end
def self.get_sort_field(params: nil, table: nil)
field = nil
direction = nil
if table.nil?
table = self.u_table
end
if params
if !params[:sortcolumn].blank?
field = params[:sortcolumn]
direction = params[:sort]
else
field = params[:sort].blank? ? nil : params[:sort]
direction = params[:order].blank? ? 'desc' : params[:order]
end
if field.nil?
field, direction = table.default_ordered
else
field = field.to_s
if !(field=='created_at' || field == 'sort_number')
field = table.table_columns.where(key: field).first || table.table_columns.where(title: field).first
end
end
end
[table, field, direction]
end
def self.sorted(entries: nil, params: nil, table: nil, field: nil, direction: nil, paginated: true)
if field.nil? || direction.nil?
table, field, direction = self.get_sort_field(params: params, table: table)
end
if entries.nil?
entries = table.table_entries
end
if (field=='created_at' || field == 'sort_number')
values = entries.order_by({field => direction})
else
column_to_sort = field
if entries.selector.present?
column_entries = ColumnEntry.where(:table_column_id=>column_to_sort.id,:table_entry_id.in => entries.pluck(:id)).order_by(column_to_sort.sort_hash(direction))
else
column_entries = ColumnEntry.where(:table_column_id=>column_to_sort.id).order_by(column_to_sort.sort_hash(direction))
end
values = column_entries.map{|v| v.table_entry}
if paginated
values = Kaminari.paginate_array(values)
end
end
values
end
def self.sorting(params: nil,table: nil,field: nil,direction: nil,page_num: nil,per: nil,column_entries: nil,paginated: true)
page_num = 1 if page_num.blank?
page_num = page_num.to_i
if field.nil? || direction.nil?
table, field, direction = self.get_sort_field(params: params, table: table)
end
if (field=='created_at' || field == 'sort_number')
if column_entries.nil?
values = self.order_by({field => direction})
else
values = column_entries.map{|v| v.table_entry}.compact
values = values.sort_by{|v| v.send(field)}
if direction == 'desc'
values = values.reverse
end
if paginated || !per.nil?
values_count = values.count
values_count = 1 if values_count==0
values = Kaminari.paginate_array(values,limit: values_count)
end
end
if !per.nil?
values = values.page(page_num).per(per)
end
else
column_to_sort = field
if column_entries.nil?
if criteria.selector.keys != ['u_table_id']
column_entries = ColumnEntry.where(:table_column_id=>column_to_sort.id,:table_entry_id.in => criteria.pluck(:id)).order_by(column_to_sort.sort_hash(direction))
else
column_entries = ColumnEntry.where(:table_column_id=>column_to_sort.id).order_by(column_to_sort.sort_hash(direction))
end
else
column_entries = ColumnEntry.where(:table_column_id=>column_to_sort.id,:table_entry_id.in => (column_entries.class==Kaminari::PaginatableArray ? column_entries.map(&:table_entry_id) : column_entries.pluck(:table_entry_id))).order_by(column_to_sort.sort_hash(direction))
end
if !per.nil?
total_count = column_entries.count
column_entries = column_entries.page(page_num).per(per)
offset = page_num==0 ? 0 : (page_num-1)*per
end_offset = (total_count-offset-per)
end_offset = 0 if end_offset<0
values = Kaminari.paginate_array([nil]*offset+column_entries.map{|v| v.table_entry}+[nil]*end_offset).page(page_num).per(per)
else
values = column_entries.map{|v| v.table_entry}
if paginated
values = Kaminari.paginate_array(values)
end
end
end
values
end
end