2015-11-13 13:10:00 +00:00
|
|
|
class TableEntry
|
|
|
|
include Mongoid::Document
|
|
|
|
include Mongoid::Timestamps
|
|
|
|
include Slug
|
|
|
|
|
2022-02-24 09:31:45 +00:00
|
|
|
attr_accessor :sort_value
|
2024-08-11 02:15:35 +00:00
|
|
|
field :have_data, type: Boolean, localize: true
|
2022-02-25 06:49:00 +00:00
|
|
|
field :sort_number, type: Integer
|
2024-07-21 07:23:29 +00:00
|
|
|
field :view_count, type: Integer, default: 0
|
2022-02-24 09:31:45 +00:00
|
|
|
|
2015-11-13 13:10:00 +00:00
|
|
|
has_many :column_entries, :dependent => :destroy
|
2024-07-20 00:45:45 +00:00
|
|
|
belongs_to :u_table, index: true
|
2015-11-13 13:10:00 +00:00
|
|
|
|
|
|
|
accepts_nested_attributes_for :column_entries, :allow_destroy => true
|
2024-08-11 02:15:35 +00:00
|
|
|
|
|
|
|
I18n.available_locales.each do |locale|
|
|
|
|
index({have_data: 1}, { collation: {locale: locale.to_s}, unique: false, background: true })
|
|
|
|
end
|
2022-02-25 06:49:00 +00:00
|
|
|
|
|
|
|
before_save do
|
|
|
|
if self[:sort_number].nil?
|
2024-08-11 02:15:35 +00:00
|
|
|
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
|
2022-02-25 06:49:00 +00:00
|
|
|
end
|
2024-08-11 02:15:35 +00:00
|
|
|
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
|
2022-02-25 06:49:00 +00:00
|
|
|
end
|
2022-02-24 09:31:45 +00:00
|
|
|
|
|
|
|
def self.u_table
|
|
|
|
UTable.find(criteria.selector['u_table_id'])
|
|
|
|
end
|
|
|
|
|
2024-08-08 15:53:10 +00:00
|
|
|
def self.get_sort_field(params: nil, table: nil)
|
|
|
|
field = nil
|
|
|
|
direction = nil
|
2022-02-24 09:31:45 +00:00
|
|
|
if table.nil?
|
2024-08-08 15:53:10 +00:00
|
|
|
table = self.u_table
|
2022-02-24 09:31:45 +00:00
|
|
|
end
|
2024-08-08 15:53:10 +00:00
|
|
|
if params
|
2022-02-24 09:31:45 +00:00
|
|
|
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?
|
2024-08-08 15:53:10 +00:00
|
|
|
field, direction = table.default_ordered
|
2022-02-25 06:49:00 +00:00
|
|
|
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
|
2022-02-24 09:31:45 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2024-08-08 15:53:10 +00:00
|
|
|
[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
|
2022-02-24 09:31:45 +00:00
|
|
|
|
2022-02-25 06:49:00 +00:00
|
|
|
if (field=='created_at' || field == 'sort_number')
|
|
|
|
if column_entries.nil?
|
|
|
|
values = self.order_by({field => direction})
|
|
|
|
else
|
2023-06-12 15:37:32 +00:00
|
|
|
values = column_entries.map{|v| v.table_entry}.compact
|
2022-02-25 06:49:00 +00:00
|
|
|
values = values.sort_by{|v| v.send(field)}
|
|
|
|
if direction == 'desc'
|
|
|
|
values = values.reverse
|
|
|
|
end
|
2022-03-05 13:41:58 +00:00
|
|
|
if paginated || !per.nil?
|
|
|
|
values_count = values.count
|
|
|
|
values_count = 1 if values_count==0
|
|
|
|
values = Kaminari.paginate_array(values,limit: values_count)
|
|
|
|
end
|
2022-03-05 13:26:42 +00:00
|
|
|
end
|
|
|
|
if !per.nil?
|
|
|
|
values = values.page(page_num).per(per)
|
2022-02-24 11:34:53 +00:00
|
|
|
end
|
2022-02-24 09:31:45 +00:00
|
|
|
else
|
2022-02-25 06:49:00 +00:00
|
|
|
column_to_sort = field
|
2022-02-24 11:34:53 +00:00
|
|
|
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))
|
2022-02-24 09:31:45 +00:00
|
|
|
end
|
2022-02-24 11:34:53 +00:00
|
|
|
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
|
2022-02-24 09:31:45 +00:00
|
|
|
end
|
|
|
|
end
|
2022-02-24 11:34:53 +00:00
|
|
|
values
|
2022-02-24 09:31:45 +00:00
|
|
|
end
|
2015-11-13 13:10:00 +00:00
|
|
|
end
|