fix error

This commit is contained in:
邱博亞 2022-02-25 14:49:00 +08:00
parent aeffcf2630
commit 0d8972ea29
22 changed files with 394 additions and 93 deletions

View File

@ -2,12 +2,38 @@ class Admin::UniversalTablesController < OrbitAdminController
def index
@table_fields = ["universal_table.table_name","universal_table.created_time","universal_table.total_no_of_entries", "universal_table.import_from_excel"]
@tables = UTable.where(:title.ne => "")
.order_by(sort)
.with_categories(filters("category"))
@tables = UTable.where(:title.ne => "")
.order_by(sort)
.with_categories(filters("category"))
end
def update_sort
uid = params[:universal_table_id].split("-").last
@table = UTable.where(:uid => uid).first rescue nil
if !@table.nil?
ids = params[:ids]
ids = ids.reverse if @table.sort_number_order_direction=='desc'
ids.each_with_index do |id,i|
TableEntry.where(id: id).update(sort_number: i)
end
@entries = TableEntry.where(u_table_id: @table.id).sorting(params: params,table: @table)
@columns = @table.table_columns.asc(:order)
@table_fields = ['universal_table.sort_number']+@columns.collect{|tc| tc.title} + ['universal_table.created_at']
render 'update_sort',layout: false
end
end
def edit_sort
uid = params[:universal_table_id].split("-").last
@table = UTable.where(:uid => uid).first rescue nil
if !@table.nil?
@entries = TableEntry.where(u_table_id: @table.id).sorting(params: params,table: @table)
@columns = @table.table_columns.asc(:order)
@table_fields = ['universal_table.sort_number']+@columns.collect{|tc| tc.title} + ['universal_table.created_at']
end
end
def show
uid = params[:id].split("-").last
@table = UTable.where(:uid => uid).first rescue nil
@ -17,7 +43,7 @@ class Admin::UniversalTablesController < OrbitAdminController
if params[:q].present?
@entries = search_data(@table)
else
@entries = @table.table_entries.criteria.sorting(params: params,table: @table,page_num: params[:page],per: 10)
@entries = TableEntry.where(u_table_id: @table.id).sorting(params: params,table: @table,page_num: params[:page],per: 10)
end
end
end
@ -88,6 +114,8 @@ class Admin::UniversalTablesController < OrbitAdminController
skip = skip + 1
end
ce.text_translations = v
when "integer"
ce.number = (val.blank? ? nil : val)
when "editor"
v = {}
@site_in_use_locales.sort.each_with_index do |locale,x|
@ -110,6 +138,7 @@ class Admin::UniversalTablesController < OrbitAdminController
ce.period_to = val
end
ce.table_column_id = tc.id
ce.u_table_id = table.id
ce.save
te.column_entries << ce
end

View File

@ -34,13 +34,22 @@ class UniversalTablesController < ApplicationController
sort_class = "sort hide"
when "text"
if tc.make_categorizable
select_values = tc.column_entries.distinct(:text).uniq
select_values = tc.column_entries.distinct(:text)
form_field = "<input type=\"hidden\" name=\"authenticity_token\" value=\"#{csrf_value}\"><select class='form-control' name='q'>"
select_values.each do |sv|
form_field = form_field + "<option value='#{sv[I18n.locale.to_s]}'>#{sv[I18n.locale.to_s]}</option>"
end
form_field = form_field + "</select>"
end
when "integer"
if tc.make_categorizable
select_values = tc.column_entries.distinct(:number)
form_field = "<input type=\"hidden\" name=\"authenticity_token\" value=\"#{csrf_value}\"><select class='form-control' name='q'>"
select_values.each do |sv|
form_field = form_field + "<option value='#{sv}'>#{sv}</option>"
end
form_field = form_field + "</select>"
end
end
if params["sortcolumn"] == tc.key
@ -77,6 +86,8 @@ class UniversalTablesController < ApplicationController
case ce.type
when "text"
text = ce.text
when "integer"
text = ce.number
when "editor"
text = ce.content
when "date"
@ -146,6 +157,8 @@ class UniversalTablesController < ApplicationController
case ce.type
when "text"
text = ce.text
when "integer"
text = ce.number
when "editor"
text = ce.content
when "date"
@ -215,6 +228,8 @@ class UniversalTablesController < ApplicationController
case ce.type
when "text"
text = ce.text
when "integer"
text = ce.number
when "editor"
text = ce.content
when "date"

View File

@ -27,5 +27,17 @@ module Admin::UniversalTablesHelper
select = select + "</select>"
"<div class='select-holder'> <span>Or</span> " + select + "</div>"
end
def render_unique_number(f,column,i)
select_values = column.column_entries.distinct(:number).uniq
select = "<select id='#{column.key}_#{i}'>"
s = ""
select = select + "<option class='muted' value='#{s}'>---------------Select---------------</option>"
select_values.each do |sv|
select = select + "<option value='#{sv}'>#{sv}</option>"
end
select = select + "</select>"
"<div class='select-holder'> <span>Or</span> " + select + "</div>"
end
end

View File

@ -7,6 +7,7 @@ class ColumnEntry
field :date, type: DateTime
field :period_from, type: DateTime
field :period_to, type: DateTime
field :number, type: Integer
mount_uploader :image, ImageUploader
@ -16,5 +17,5 @@ class ColumnEntry
def type
self.table_column.type
end
end
end

View File

@ -19,6 +19,8 @@ class TableColumn
case self.type
when "text"
{text: direction}
when "integer"
{number: direction}
when "editor"
{content: direction}
when "image"

View File

@ -4,12 +4,19 @@ class TableEntry
include Slug
attr_accessor :sort_value
field :sort_number, type: Integer, default: 0
field :sort_number, type: Integer
has_many :column_entries, :dependent => :destroy
belongs_to :u_table
accepts_nested_attributes_for :column_entries, :allow_destroy => true
before_save do
if self[:sort_number].nil?
sort_number_to_set = self.class.where(u_table_id: self.u_table_id).order_by(sort_number: :desc).first.sort_number
self.sort_number = sort_number_to_set + 1
end
end
def self.u_table
UTable.find(criteria.selector['u_table_id'])
@ -30,23 +37,30 @@ class TableEntry
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
field,direction = table.default_ordered
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
end
end
end
field = field.to_s
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)
if (field=='created_at' || field == 'sort_number')
if column_entries.nil?
values = self.order_by({field => direction})
if !per.nil?
values = values.page(page_num).per(per)
end
else
values = column_entries.map{|v| v.table_entry}
values = values.sort_by{|v| v.send(field)}
if direction == 'desc'
values = values.reverse
end
end
else
column_to_sort = table.table_columns.where(key: field).first || table.table_columns.where(title: field).first
column_to_sort = field
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))

View File

@ -5,16 +5,36 @@ class UTable
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", "editor", "image", "date", "period"]
FIELD_TYPES = ["text", "integer", "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'
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

View File

@ -1,7 +1,7 @@
<% if !defined?(i) %>
<div class="attributes">
<% end %>
<div class="attributes-header clearfix">
<% if !defined?(i) %>
<div class="attributes">
<% end %>
<div class="attributes-header clearfix">
<a class="btn btn-mini pull-right btn-danger delete" href="#"><i class="icon-trash"></i> Delete</a>
<% if defined?(i) %>
<%= f.hidden_field :_destroy, :value => "false", :class => "attribute_field_to_delete" %>
@ -64,13 +64,13 @@
<%= f.select :order_direction,['desc','asc'].map{|v| [t("universal_table.#{v}"),v]} %>
</div>
</div>
</div>
</div>
<div class="control-group">
<label class="control-label muted" for="">Type</label>
<div class="controls">
<% select_values = UTable::FIELD_TYPES.collect{|ft| [ft.capitalize,ft]} %>
<%= f.select :type, select_values, {}, {class: "type-selector"} %>
<span class="link_to_show <%= !defined?(i) || column.type == "text" ? "" : "hide" %>">
<span class="link_to_show <%= (!defined?(i) || column.type == "text" || column.type == "integer") ? "" : "hide" %>">
<label class="checkbox inline attributes-checkbox ">
<%= f.check_box :is_link_to_show %> Link to show
</label>

View File

@ -0,0 +1,79 @@
<div id="data-table" class="ut-table">
<table class="table main-list">
<thead>
<tr class="sort-header">
<% @table_fields.each do |field| %>
<%
field_text = field.to_s.include?('.') ? t(field.to_s) : field.to_s
sort = field.to_s.split('.')[-1]
active = params[:sort].eql? sort
order = active ? (["asc", "desc"]-[params[:order]]).first : "asc"
arrow = (order.eql? "desc") ? "<b class='icons-arrow-up-3'></b>" : "<b class='icons-arrow-down-4'></b>"
klass = field.eql?(:title) ? "span5" : "span2"
th_data = "<a href='?sort=#{sort}&order=#{order}'>#{field_text} #{active ? arrow : ""}</a>"
%>
<th class='<%= klass %> <%= active ? "active" : "" %>'><%= th_data.html_safe %></th>
<% end %>
</tr>
</thead>
<tbody id="sortable">
<% can_edit = can_edit_or_delete?(@entries.first.u_table) if !(@entries.first.nil?) %>
<% @entries.each do |entry| %>
<tr data-id="<%= entry.id %>">
<td>
<%= entry.sort_number %>
</td>
<% @columns.each_with_index do |column, index| %>
<% ce = entry.column_entries.where(:table_column_id => column.id).first rescue nil %>
<% if !ce.nil? %>
<td>
<% case ce.type %>
<% when "text" %>
<%= ce.text %>
<% when "integer" %>
<%= ce.number %>
<% when "editor" %>
<%= ce.content.html_safe rescue "" %>
<% when "image" %>
<div class="image-expander">
<% if !ce.image.nil? %>
<a href="<%= ce.image.url %>" target="_blank"><img src="<%= ce.image.thumb.url %>" class="image-preview" /></a>
<% end %>
</div>
<% when "date" %>
<%= format_date(ce.date, column.date_format) %>
<% when "period" %>
<% if !ce.period_from.nil? %>
<%= format_date(ce.period_from, column.date_format) %> ~ <%= format_date(ce.period_to, column.date_format) %>
<% end %>
<% end %>
<% if index == 0 && can_edit %>
<div class="quick-edit">
<ul class="nav nav-pills">
<li><a href="<%= admin_universal_table_edit_entry_path(entry) %>"><%= t(:edit) %></a></li>
<li><a href="<%= admin_universal_table_delete_entry_path(entry.id) %>" class="delete text-error" data-method="delete" data-confirm="Are you sure?"><%= t(:delete_) %></a></li>
</ul>
</div>
<% end %>
</td>
<% else %>
<td>
&nbsp;
</td>
<% end %>
<% end %>
<td>
<%= entry.created_at %>
</td>
</tr>
<% end %>
</tbody>
</table>
</div>

View File

@ -24,18 +24,7 @@
%>
<% end %>
<%= f.fields_for :column_entries, object, :child_index => index do |f| %>
<% case column.type %>
<% when "text" %>
<%= render :partial => "text_field", :object => object, :locals => {:f => f, :column => column, :i => index} %>
<% when "editor" %>
<%= render :partial => "editor_field", :object => object, :locals => {:f => f, :column => column, :i => index} %>
<% when "image" %>
<%= render :partial => "image_field", :object => object, :locals => {:f => f, :column => column, :i => index} %>
<% when "date" %>
<%= render :partial => "date_field", :object => object, :locals => {:f => f, :column => column, :i => index} %>
<% when "period" %>
<%= render :partial => "period_field", :object => object, :locals => {:f => f, :column => column, :i => index} %>
<% end %>
<%= render :partial => "#{column.type}_field", :object => object, :locals => {:f => f, :column => column, :i => index} %>
<% end %>
<% end %>
</div>

View File

@ -12,15 +12,18 @@
<tr id="table_<%= table.id.to_s %>">
<td>
<a href="<%= admin_universal_table_path(table) %>"><%= table.title %></a>
<div class="quick-edit">
<ul class="nav nav-pills">
<% if can_edit %>
<li><a href="<%= edit_admin_universal_table_path(table) %>"><%= t(:edit) %></a></li>
<li><a href="/admin/universal_tables/<%=table.id.to_s%>/export_data?format=xlsx" data-table-id="<%= table.id.to_s %>" class="export-xls"><%= t('universal_table.export_xls') %></a></li>
<li><a href="<%= admin_universal_table_path(table) %>" class="delete text-error" data-method="delete" data-confirm="Are you sure?"><%= t(:delete_) %></a></li>
<% end %>
</ul>
</div>
<div class="quick-edit">
<ul class="nav nav-pills">
<% if can_edit %>
<li><a href="<%= edit_admin_universal_table_path(table) %>"><%= t(:edit) %></a></li>
<% if table.ordered_with_sort_number %>
<li><a href="<%= admin_universal_table_edit_sort_path(table) %>"><%= t('universal_table.edit_sort') %></a></li>
<% end %>
<li><a href="/admin/universal_tables/<%=table.id.to_s%>/export_data?format=xlsx" data-table-id="<%= table.id.to_s %>" class="export-xls"><%= t('universal_table.export_xls') %></a></li>
<li><a href="<%= admin_universal_table_path(table) %>" class="delete text-error" data-method="delete" data-confirm="Are you sure?"><%= t(:delete_) %></a></li>
<% end %>
</ul>
</div>
</td>
<td>
<%= table.created_at.strftime("%Y-%m-%d") %>

View File

@ -0,0 +1,25 @@
<div class="control-group">
<%= f.label :number, column.title, :class => "control-label" %>
<div class="controls">
<div class="input-append">
<%= f.number_field :number, :value => integer_field.number %>
</div>
<% if column.make_categorizable %>
<%= render_unique_number(f,column,i).html_safe %>
<script type="text/javascript">
$("select#<%= column.key + "_" + i.to_s %>").on("change",function(){
var el = $(this),
value = el.val(),
inputs = el.parent().parent().find("input[type=number]");
inputs.val(value);
})
</script>
<% end %>
</div>
<% if !integer_field.new_record? %>
<%= f.hidden_field :id %>
<% else %>
<%= f.hidden_field :table_column_id, :value => column.id %>
<% end %>
</div>

View File

@ -1,55 +1,83 @@
<% content_for :page_specific_css do %>
<%= stylesheet_link_tag "universal_table/universal-table" %>
<% end %>
<% content_for :page_specific_javascript do %>
<% content_for :page_specific_javascript do %>
<%= javascript_include_tag "universal_table/jquery-ui.min" %>
<% end %>
<fieldset class="utable-heading-wrap">
<div class="utable-heading-header">
<h4><%= t("universal_table.table_name") %></h4>
</div>
<div class="control-group">
<div class="controls">
<div class="input-append">
<div class="tab-content">
<% @site_in_use_locales.each do |locale| %>
<% active = (locale == @site_in_use_locales.first ? "active in" : "") %>
<div class="tab-pane fade <%= active %>" id="table_name_<%= locale.to_s %>">
<%= f.fields_for :title_translations do |f| %>
<%= f.text_field locale, :placeholder => "Title", :value => @table.title_translations[locale] %>
<% end %>
</div>
<% end %>
</div>
<div class="btn-group" data-toggle="buttons-radio">
<div class="utable-heading-header">
<h4><%= t("universal_table.table_name") %></h4>
</div>
<div class="control-group">
<div class="controls">
<div class="input-append">
<div class="tab-content">
<% @site_in_use_locales.each do |locale| %>
<% active = (locale == @site_in_use_locales.first ? "active" : "") %>
<%= link_to t(locale).to_s,"#table_name_#{locale.to_s}",:class=>"btn #{active}",:data=>{:toggle=>"tab"}%>
<% active = (locale == @site_in_use_locales.first ? "active in" : "") %>
<div class="tab-pane fade <%= active %>" id="table_name_<%= locale.to_s %>">
<%= f.fields_for :title_translations do |f| %>
<%= f.text_field locale, :placeholder => "Title", :value => @table.title_translations[locale] %>
<% end %>
</div>
<% end %>
</div>
</div>
</div>
</div>
</fieldset>
</div>
<fieldset class="utable-content">
<div id="attributes-area" class="input-area">
<% @table.table_columns.asc(:order).each_with_index do |table_column, index| %>
<div class="attributes default ">
<%= f.fields_for :table_columns, table_column, :child_index => index.to_s do |f| %>
<%= render :partial => "column", :object => table_column, :locals => {:f => f, :i => index} %>
<%= f.hidden_field :id %>
<% end %>
<div class="btn-group" data-toggle="buttons-radio">
<% @site_in_use_locales.each do |locale| %>
<% active = (locale == @site_in_use_locales.first ? "active" : "") %>
<%= link_to t(locale).to_s,"#table_name_#{locale.to_s}",:class=>"btn #{active}",:data=>{:toggle=>"tab"}%>
<% end %>
</div>
</div>
</div>
</div>
</fieldset>
<fieldset>
<div class="utable-heading-header">
<h4><%= t("universal_table.default_ordered_field") %></h4>
</div>
<div class="control-group">
<label class="control-label muted" for=""><%= t('universal_table.created_at') %></label>
<div class="controls">
<div>
<%= f.check_box :ordered_with_created_at, class: 'default_ordered_field ordered_with_created_at' %>
</div>
<div class="order_direction<%= ' hidden' if !f.object.ordered_with_created_at %>">
<%= f.select :created_at_order_direction,['desc','asc'].map{|v| [t("universal_table.#{v}"),v]} %>
</div>
<% end %>
</div>
<div class="form-actions">
<button type="button" class="btn btn-success add-attributes"><%= t("universal_table.add_column") %></button>
<input class="btn btn-primary" name="commit" type="submit" value="<%= t("save") %>">
</div>
<div class="control-group">
<label class="control-label muted" for=""><%= t('universal_table.sort_number') %></label>
<div class="controls">
<div>
<%= f.check_box :ordered_with_sort_number, class: 'default_ordered_field' %>
</div>
<div class="order_direction<%= ' hidden' if !f.object.ordered_with_sort_number %>">
<%= f.select :sort_number_order_direction,['desc','asc'].map{|v| [t("universal_table.#{v}"),v]} %>
</div>
</div>
</fieldset>
</div>
</fieldset>
<fieldset class="utable-content">
<div id="attributes-area" class="input-area">
<% @table.table_columns.asc(:order).each_with_index do |table_column, index| %>
<div class="attributes default ">
<%= f.fields_for :table_columns, table_column, :child_index => index.to_s do |f| %>
<%= render :partial => "column", :object => table_column, :locals => {:f => f, :i => index} %>
<%= f.hidden_field :id %>
<% end %>
</div>
<% end %>
</div>
<div class="form-actions">
<button type="button" class="btn btn-success add-attributes"><%= t("universal_table.add_column") %></button>
<input class="btn btn-primary" name="commit" type="submit" value="<%= t("save") %>">
</div>
</fieldset>
<script type="text/javascript">
@ -87,7 +115,7 @@
$(document.body).on("change","select.type-selector",function(){
var el = $(this),
label = el.parent().find("span.link_to_show");
if(el.val() == "text"){
if(el.val() == "text" || el.val() == "integer"){
label.removeClass("hide");
}else{
label.addClass("hide");
@ -107,6 +135,12 @@
$(this).parents('.controls').eq(0).find('.order_direction').removeClass('hidden');
}
});
$(document).ready(function(){
if ($('.ordered_with_created_at').prop('checked')){
$('.default_ordered_field').not($('.ordered_with_created_at')[0]).prop('checked',false);
$('.order_direction').not($('.ordered_with_created_at').eq(0).parents('.controls').eq(0).find('.order_direction')[0]).addClass('hidden');
}
})
function key_on_blur() {
$('input[data-type=key]').on('blur',function() {
var index_this = $(this).parents('.attributes').index()

View File

@ -0,0 +1,40 @@
<% content_for :page_specific_css do %>
<%= stylesheet_link_tag "universal_table/universal-table" %>
<% end %>
<div style="margin-bottom: 1em;">
<button type="button" class="btn btn-primary" id="update_sort_button"><%= t('universal_table.manual_update_sort') %></button>
</div>
<%= render partial: 'edit_sort' %>
<script type="text/javascript">
function update_sort(){
var ids = $.map($('#sortable>tr'),function(v){return $(v).data('id')});
$.ajax({
url: "<%= admin_universal_table_update_sort_path(@table) %>",
type: 'POST',
dataType: 'text',
data: {ids: ids},
success: function(data){
$('#data-table').replaceWith(data);
sortable();
}
});
}
function sortable(){
$( "#sortable" ).sortable({
update: function( event, ui ) {
update_sort();
}
});
}
$(document).ready(function(){
$('#update_sort_button').click(update_sort);
sortable();
});
</script>

View File

@ -18,6 +18,10 @@ wb.add_worksheet(name: "Structure") do |sheet|
row1 << column.key
row2 << column.type + "-#{locale}"
end
when "integer"
row << column.title
row1 << column.key
row2 << column.type
when "editor"
@site_in_use_locales.sort.each do |locale|
row << column.title + " - " + t(locale.to_s)

View File

@ -37,6 +37,8 @@
<% case ce.type %>
<% when "text" %>
<%= ce.text %>
<% when "integer" %>
<%= ce.number %>
<% when "editor" %>
<%= ce.content.html_safe rescue "" %>
<% when "image" %>

View File

@ -0,0 +1 @@
<%= render partial: 'edit_sort' %>

View File

@ -18,6 +18,10 @@ wb.add_worksheet(name: "Structure") do |sheet|
row1 << column.key
row2 << column.type + "-#{locale}"
end
when "integer"
row << column.title
row1 << column.key
row2 << column.type
when "editor"
site_in_use_locales.sort.each do |locale|
row << column.title + " - " + t(locale.to_s)
@ -52,6 +56,8 @@ wb.add_worksheet(name: "Structure") do |sheet|
site_in_use_locales.sort.each do |locale|
row << (column.text_translations[locale.to_s] rescue "")
end
when "integer"
row << column.number
when "editor"
site_in_use_locales.sort.each do |locale|
row << (column.content_translations[locale.to_s] rescue "")

View File

@ -13,4 +13,8 @@ en:
add_column: Add Column
default_ordered_field: Default Ordered Field
asc: asc
desc: desc
desc: desc
sort_number: Sort Number
created_at: Created Time
edit_sort: Edit Sorting
manual_update_sort: Manually Update Sorting

View File

@ -13,4 +13,8 @@ zh_tw:
add_column: 新增欄位
default_ordered_field: 預設排序欄位
asc: 升序
desc: 降序
desc: 降序
sort_number: 排序數
created_at: 創建時間
edit_sort: 編輯排序
manual_update_sort: 手動更新排序

View File

@ -11,7 +11,9 @@ Rails.application.routes.draw do
resources :universal_tables do
get "new_entry"
delete "delete_entry"
get "edit_entry"
get "edit_entry"
get "edit_sort"
post "update_sort", to: 'universal_tables#update_sort'
get "export_structure"
member do
get "export_data"

View File

@ -1,6 +1,21 @@
module UniversalTable
class Engine < ::Rails::Engine
initializer "universal_table" do
initializer "universal_table" do
if ENV['worker_num']=='0' && File.basename($0) != 'rake' && !Rails.const_defined?('Console')
require File.expand_path('../../../app/models/table_entry', __FILE__)
require File.expand_path('../../../app/models/u_table', __FILE__)
if defined?(TableEntry) && defined?(UTable)
if TableEntry.where(sort_number: nil).count>0
UTable.all.pluck(:id).each do |u_table_id|
table_entries = TableEntry.where(u_table_id: u_table_id).order_by(id: 1)
table_entry_ids = table_entries.pluck(:id)
table_entry_ids.each_with_index do |id,i|
TableEntry.where(id: id).update(sort_number: i)
end
end
end
end
end
OrbitApp.registration "UniversalTable", :type => "ModuleApp" do
module_label "universal_table.universal_table"
base_url File.expand_path File.dirname(__FILE__)