universal_table/app/models/u_table.rb

40 lines
1.2 KiB
Ruby
Raw Normal View History

2015-11-13 13:10:00 +00:00
class UTable
include Mongoid::Document
include Mongoid::Timestamps
include OrbitCategory::Categorizable
include Slug
field :title, as: :slug_title, localize: true
2022-02-25 06:49:00 +00:00
field :ordered_with_sort_number, type: Boolean, default: false
field :sort_number_order_direction, type: String, default: 'desc'
field :ordered_with_created_at, type: Boolean, default: true
field :created_at_order_direction, type: String, default: 'desc'
2015-11-13 13:10:00 +00:00
has_many :table_columns, :dependent => :destroy
has_many :table_entries, :dependent => :destroy
accepts_nested_attributes_for :table_columns, :allow_destroy => true
2022-02-25 06:49:00 +00:00
FIELD_TYPES = ["text", "integer", "editor", "image", "date", "period"]
DATE_FORMATS = ["yyyy/MM/dd hh:mm", "yyyy/MM/dd","yyyy/MM", "yyyy"]
2022-02-25 06:49:00 +00:00
def default_ordered
if self.ordered_with_created_at
sort_column = 'created_at'
direction = self.created_at_order_direction
elsif self.ordered_with_sort_number
sort_column = 'sort_number'
direction = self.sort_number_order_direction
else
sort_column = self.table_columns.where(default_ordered_field: true).first
if sort_column
direction = sort_column.order_direction
else
sort_column = 'created_at'
direction = self.created_at_order_direction
end
end
[sort_column,direction]
2022-02-24 09:31:45 +00:00
end
2015-11-13 13:10:00 +00:00
end