diff --git a/app/controllers/admin/universal_tables_controller.rb b/app/controllers/admin/universal_tables_controller.rb index 9c10f7a..0dc6b52 100644 --- a/app/controllers/admin/universal_tables_controller.rb +++ b/app/controllers/admin/universal_tables_controller.rb @@ -15,10 +15,11 @@ class Admin::UniversalTablesController < OrbitAdminController @columns = @table.table_columns.asc(:order) @table_fields = @columns.collect{|tc| tc.title} if params[:q].present? - @entries = Kaminari.paginate_array(search_data(@table)).page(params[:page]).per(10) + @entries = search_data(@table) else - @entries = @table.table_entries.desc(:created_at).page(params[:page]).per(10) + @entries = @table.table_entries.criteria end + @entries = @entries.sorting(params: params,table: @table).page(params[:page]).per(10) end end @@ -242,11 +243,12 @@ 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 - entries = [] + entry_ids = [] columns.each do |column| - entries << column.table_entry + entry_ids << column.table_entry_id end - entries = entries.sort{|k,v| v["created_at"] <=> k["created_at"]} + entry_ids = entry_ids.uniq + entries = TableEntry.where(:id.in=> entry_ids) end def table_params diff --git a/app/controllers/universal_tables_controller.rb b/app/controllers/universal_tables_controller.rb index 292d332..94763ae 100644 --- a/app/controllers/universal_tables_controller.rb +++ b/app/controllers/universal_tables_controller.rb @@ -179,66 +179,66 @@ class UniversalTablesController < ApplicationController def get_entries(params, table, page, paginated=true) entries = [] if params["column"].present? - keywords = params["q"] - keywords = keywords.strip.nil? ? keywords : keywords.strip - column = table.table_columns.where(:key => params["column"]).first - if column.make_categorizable - regex = Regexp.new(".*"+keywords+".*", "i") - else - regexes = keywords.split(/\s+(?=(?:[^"]*"[^"]*")*[^"]*$)/) - regex = Regexp.union(regexes.map{|word| Regexp.new(".*"+word+".*", "i")}) + keywords = params["q"] + keywords = keywords.strip.nil? ? keywords : keywords.strip + column = table.table_columns.where(:key => params["column"]).first + if column.make_categorizable + regex = Regexp.new(".*"+keywords+".*", "i") + else + 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["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 + 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 - columns = column.column_entries.any_of(:"text.en" => regex).or(:"text.zh_tw" => regex).or(:"content.en" => regex).or(:"content.zh_tw" => regex) + 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) + 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 + end + columns = column_to_sort.column_entries.order_by(field_name => params["sort"]) 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) 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 - 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.desc(:created_at).page(params["page_no"]).per(OrbitHelper.page_data_count) if(paginated) - end + entries = table.table_entries.sorting(params: params,table: table).page(params["page_no"]).per(OrbitHelper.page_data_count) if(paginated) end - entries + end + entries end def show diff --git a/app/models/column_entry.rb b/app/models/column_entry.rb index 3ccd0a7..06de2bf 100644 --- a/app/models/column_entry.rb +++ b/app/models/column_entry.rb @@ -17,4 +17,18 @@ 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 \ No newline at end of file diff --git a/app/models/table_column.rb b/app/models/table_column.rb index 6d017b7..8194f63 100644 --- a/app/models/table_column.rb +++ b/app/models/table_column.rb @@ -10,7 +10,8 @@ class TableColumn field :is_link_to_show, type: Boolean, default: false field :order, type: Integer field :make_categorizable, type: Boolean, default: false - + field :default_ordered_field, type: Boolean, default: false + field :order_direction,type: String,default: 'desc' belongs_to :u_table has_many :column_entries, :dependent => :destroy diff --git a/app/models/table_entry.rb b/app/models/table_entry.rb index 453e12a..b23a990 100644 --- a/app/models/table_entry.rb +++ b/app/models/table_entry.rb @@ -3,8 +3,54 @@ class TableEntry include Mongoid::Timestamps include Slug + attr_accessor :sort_value + field :sort_number, type: Integer, default: 0 + has_many :column_entries, :dependent => :destroy belongs_to :u_table accepts_nested_attributes_for :column_entries, :allow_destroy => true + + def self.u_table + UTable.find(criteria.selector['u_table_id']) + end + + def self.sorting(params: nil,table: nil,field: nil,direction: nil) + if table.nil? + table = u_table + end + if field.nil? || direction.nil? + 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 = table.default_ordered_field + if field != 'created_at' + columns = table.table_columns + sort_column = columns.where(:key=>field).first || columns.where(:title=>field).first + direction = sort_column.order_direction + end + end + end + + field = field.to_s + if field=='created_at' || field == 'sort_number' + self.order_by({field => direction}) + 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 + end + values = values.sort_by{|v| v.sort_value} + if direction=='desc' + values = values.reverse + end + Kaminari.paginate_array(values) + end + end end \ No newline at end of file diff --git a/app/models/u_table.rb b/app/models/u_table.rb index 51e10fc..6b97bc5 100644 --- a/app/models/u_table.rb +++ b/app/models/u_table.rb @@ -13,4 +13,8 @@ class UTable FIELD_TYPES = ["text", "editor", "image", "date", "period"] DATE_FORMATS = ["yyyy/MM/dd hh:mm", "yyyy/MM/dd","yyyy/MM", "yyyy"] + def default_ordered_field + f = self.table_columns.where(default_ordered_field: true).first + f ? (f.key||f.title) : 'created_at' + end end \ No newline at end of file diff --git a/app/views/admin/universal_tables/_column.html.erb b/app/views/admin/universal_tables/_column.html.erb index bdd1632..d4f424c 100644 --- a/app/views/admin/universal_tables/_column.html.erb +++ b/app/views/admin/universal_tables/_column.html.erb @@ -14,46 +14,57 @@