fix error

This commit is contained in:
邱博亞 2022-02-24 19:34:53 +08:00
parent 14038cf90c
commit aeffcf2630
5 changed files with 54 additions and 77 deletions

View File

@ -17,9 +17,8 @@ class Admin::UniversalTablesController < OrbitAdminController
if params[:q].present?
@entries = search_data(@table)
else
@entries = @table.table_entries.criteria
@entries = @table.table_entries.criteria.sorting(params: params,table: @table,page_num: params[:page],per: 10)
end
@entries = @entries.sorting(params: params,table: @table).page(params[:page]).per(10)
end
end
@ -243,12 +242,8 @@ class Admin::UniversalTablesController < OrbitAdminController
column.each do |c|
columns = (columns | c.column_entries.any_of(:"text.en" => regex).or(:"text.zh_tw" => regex).or(:"content.en" => regex).or(:"content.zh_tw" => regex))
end
entry_ids = []
columns.each do |column|
entry_ids << column.table_entry_id
end
entry_ids = entry_ids.uniq
entries = TableEntry.where(:id.in=> entry_ids)
columns = Kaminari.paginate_array(columns,limit: columns.count)
entries = TableEntry.where(:u_table_id=>table.id).sorting(params: params,table: table,column_entries: columns,page_num: params[:page],per: 10)
end
def table_params

View File

@ -188,54 +188,17 @@ class UniversalTablesController < ApplicationController
regexes = keywords.split(/\s+(?=(?:[^"]*"[^"]*")*[^"]*$)/)
regex = Regexp.union(regexes.map{|word| Regexp.new(".*"+word+".*", "i")})
end
if params["sort"].present?
column_to_sort = table.table_columns.where(:key => params["sortcolumn"]).first
case column_to_sort.type
when "text"
field_name = :text
when "editor"
field_name = :content
when "date"
field_name = :date
end
if params["column"] == params["sortcolumn"]
columns = column.column_entries.any_of(:"text.en" => regex).or(:"text.zh_tw" => regex).or(:"content.en" => regex).or(:"content.zh_tw" => regex).order_by(field_name => params["sort"])
else
temp = column.column_entries.any_of(:"text.en" => regex).or(:"text.zh_tw" => regex).or(:"content.en" => regex).or(:"content.zh_tw" => regex)
columns = []
temp.each do |c|
columns << c.table_entry.column_entries.where(:table_column_id => column_to_sort.id).first
end
sorted_columns = column_to_sort.column_entries.order_by(field_name => params["sort"]).to_a
columns = sorted_columns & columns
end
else
columns = column.column_entries.any_of(:"text.en" => regex).or(:"text.zh_tw" => regex).or(:"content.en" => regex).or(:"content.zh_tw" => regex)
end
columns.each do |column|
entries << column.table_entry
end
entries = entries.sort{|k,v| v["created_at"] <=> k["created_at"]} if !params["sort"].present?
entries = Kaminari.paginate_array(entries).page(params["page_no"]).per(OrbitHelper.page_data_count) if(paginated)
if paginated
entries = TableEntry.where(:u_table_id=>table.id).sorting(params: params,table: table,column_entries: columns,page_num: params["page_no"],per: OrbitHelper.page_data_count)
else
if params["sort"].present?
column_to_sort = table.table_columns.where(:key => params["sortcolumn"]).first
case column_to_sort.type
when "text"
field_name = :text
when "editor"
field_name = :content
when "date"
field_name = :date
entries = TableEntry.where(:u_table_id=>table.id).sorting(params: params,table: table,column_entries: columns,paginated: false)
end
columns = column_to_sort.column_entries.order_by(field_name => params["sort"])
columns.each do |column|
entries << column.table_entry
end
entries = Kaminari.paginate_array(entries).page(params["page_no"]).per(OrbitHelper.page_data_count) if(paginated)
else
entries = table.table_entries.sorting(params: params,table: table).page(params["page_no"]).per(OrbitHelper.page_data_count) if(paginated)
if paginated
entries = TableEntry.where(:u_table_id=>table.id).sorting(params: params,table: table,page_num: params["page_no"],per: OrbitHelper.page_data_count)
else
entries = TableEntry.where(:u_table_id=>table.id).sorting(params: params,table: table,paginated: false)
end
end
entries

View File

@ -17,18 +17,4 @@ class ColumnEntry
def type
self.table_column.type
end
def sort_value
case self.type
when "text"
self.text
when "editor"
self.content
when "image"
self[:image]
when "date"
self.date
when "period"
[self.period_from,ce.period_to]
end
end
end

View File

@ -15,4 +15,18 @@ class TableColumn
belongs_to :u_table
has_many :column_entries, :dependent => :destroy
def sort_hash(direction)
case self.type
when "text"
{text: direction}
when "editor"
{content: direction}
when "image"
{image: direction}
when "date"
{date: direction}
when "period"
{period_from: direction,period_to: direction}
end
end
end

View File

@ -15,7 +15,9 @@ class TableEntry
UTable.find(criteria.selector['u_table_id'])
end
def self.sorting(params: nil,table: nil,field: nil,direction: nil)
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 table.nil?
table = u_table
end
@ -38,19 +40,36 @@ class TableEntry
end
field = field.to_s
if field=='created_at' || field == 'sort_number'
self.order_by({field => direction})
if (field=='created_at' || field == 'sort_number') && column_entries.nil?
values = self.order_by({field => direction})
if !per.nil?
values = values.page(page_num).per(per)
end
else
values = criteria.to_a
field_id = table.table_columns.where(key: field).first || table.table_columns.where(title: field).first
values.each do |v|
v.sort_value = v.column_entries.where(:table_column_id=>field_id).first.sort_value
column_to_sort = table.table_columns.where(key: field).first || table.table_columns.where(title: field).first
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
values = values.sort_by{|v| v.sort_value}
if direction=='desc'
values = values.reverse
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
Kaminari.paginate_array(values)
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