universal_table/app/models/u_table.rb

40 lines
1.2 KiB
Ruby

class UTable
include Mongoid::Document
include Mongoid::Timestamps
include OrbitCategory::Categorizable
include Slug
field :title, as: :slug_title, localize: true
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'
has_many :table_columns, :dependent => :destroy
has_many :table_entries, :dependent => :destroy
accepts_nested_attributes_for :table_columns, :allow_destroy => true
FIELD_TYPES = ["text", "integer", "editor", "image", "date", "period"]
DATE_FORMATS = ["yyyy/MM/dd hh:mm", "yyyy/MM/dd","yyyy/MM", "yyyy"]
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]
end
end