Updated. Fix bug.

This commit is contained in:
BoHung Chiu 2022-10-21 15:29:03 +08:00
parent 908a90d467
commit ca3f2f3a10
8 changed files with 110 additions and 29 deletions

File diff suppressed because one or more lines are too long

View File

@ -307,7 +307,7 @@ class Admin::RulingTimersController < OrbitMemberController
@comment = RulingTimerComment.where(:id=>params["id"]).first @comment = RulingTimerComment.where(:id=>params["id"]).first
end end
if @comment.nil? if @comment.nil?
@comment = RulingTimerComment.new(:ruling_timer_task_id=>params["task_id"]) @comment = RulingTimerComment.new(:ruling_timer_task_id=>params["task_id"],:uploader_id=>current_user.id)
i = @task.ruling_timer_comments.count i = @task.ruling_timer_comments.count
else else
i = @task.ruling_timer_comments.find_index{|c| c.id.to_s == params["id"]} i = @task.ruling_timer_comments.find_index{|c| c.id.to_s == params["id"]}

View File

@ -6,7 +6,7 @@ class RulingTimerComment
belongs_to :ruling_timer_task belongs_to :ruling_timer_task
has_many :ruling_timer_notifies, :autosave => true, :dependent => :destroy has_many :ruling_timer_notifies, :autosave => true, :dependent => :destroy
before_create do before_create do
self.uploader_id = self.ruling_timer_task.update_user_id rescue nil self.uploader_id = self.ruling_timer_task.update_user_id rescue nil if self.uploader_id.nil?
end end
after_save do after_save do
all_user_ids = self.ruling_timer_task.get_all_user_ids rescue [] all_user_ids = self.ruling_timer_task.get_all_user_ids rescue []
@ -41,8 +41,8 @@ class RulingTimerComment
formate_date2 = I18n.t("ruling_timer.formate_date2") formate_date2 = I18n.t("ruling_timer.formate_date2")
formate_date3 = I18n.t("ruling_timer.formate_date3") formate_date3 = I18n.t("ruling_timer.formate_date3")
formate_date4 = I18n.t("ruling_timer.formate_date4") formate_date4 = I18n.t("ruling_timer.formate_date4")
diff = time_now.to_time - update_time.to_time diff = time_now.to_time.to_i - update_time.to_time.to_i
diff_time = Time.at(diff) diff_time = Time.at(diff).utc
if diff < 3600 if diff < 3600
diff_time.strftime(formate_date4).gsub(/0+(\d)/){|f| $1} diff_time.strftime(formate_date4).gsub(/0+(\d)/){|f| $1}
elsif diff < 86400 elsif diff < 86400

View File

@ -17,12 +17,27 @@ class RulingTimerTemp
field :date, type: String, default: "" field :date, type: String, default: ""
belongs_to :user belongs_to :user
before_create do |record| before_create do |record|
time_now = DateTime.now.utc time_now = DateTime.now.new_offset(0)
record.date = time_now.new_offset(self.time_offset).strftime("%Y/%m/%d %w") if record.date.blank? record.date = time_now.new_offset(self.time_offset).strftime("%Y/%m/%d %w") if record.date.blank?
end end
after_save do
@work_times_exec = nil
@rest_times_exec = nil
end
def get_attrs(except_fields=[])
attrs = {}
self.fields.except(*(["_id","created_at","updated_at"] + except_fields)).each do |k, v|
if (v.options[:localize] rescue false)
attrs["#{k}_translations"] = self[k]
else
attrs[k] = self[k]
end
end
attrs
end
def update_work_times(work_times,store=true) def update_work_times(work_times,store=true)
work_times = work_times.to_a work_times = work_times.to_a
time_now = DateTime.now.utc time_now = DateTime.now.new_offset(0)
self.date = time_now.new_offset(self.time_offset).strftime("%Y/%m/%d %w") if self.date.blank? self.date = time_now.new_offset(self.time_offset).strftime("%Y/%m/%d %w") if self.date.blank?
date_str = self.date.split(" ")[0] date_str = self.date.split(" ")[0]
work_times = work_times.to_a.map do |t| work_times = work_times.to_a.map do |t|
@ -37,7 +52,7 @@ class RulingTimerTemp
self.work_times self.work_times
end end
def fix_work_times(store=true) def fix_work_times(store=true)
time_now = DateTime.now.utc time_now = DateTime.now.new_offset(0)
work_times_range = self.work_times.each_slice(2).to_a.map{|a,b| convert_datetime(a)..(b.nil? ? time_now : convert_datetime(b))} work_times_range = self.work_times.each_slice(2).to_a.map{|a,b| convert_datetime(a)..(b.nil? ? time_now : convert_datetime(b))}
work_times_range = eliminate_intersection(work_times_range) work_times_range = eliminate_intersection(work_times_range)
new_work_times_range = merge_ranges(work_times_range.uniq).flat_map{|range| [range.first,range.last]} new_work_times_range = merge_ranges(work_times_range.uniq).flat_map{|range| [range.first,range.last]}
@ -49,7 +64,7 @@ class RulingTimerTemp
self.work_times self.work_times
end end
def merge_work_times(new_work_times,store=true) def merge_work_times(new_work_times,store=true)
time_now = DateTime.now.utc time_now = DateTime.now.new_offset(0)
work_times_range = self.work_times.each_slice(2).to_a.map{|a,b| convert_datetime(a)..(b.nil? ? time_now : convert_datetime(b))} work_times_range = self.work_times.each_slice(2).to_a.map{|a,b| convert_datetime(a)..(b.nil? ? time_now : convert_datetime(b))}
new_work_times_range = new_work_times.each_slice(2).to_a.map{|a,b| convert_datetime(a)..(b.nil? ? time_now : convert_datetime(b))} new_work_times_range = new_work_times.each_slice(2).to_a.map{|a,b| convert_datetime(a)..(b.nil? ? time_now : convert_datetime(b))}
work_times_range = eliminate_intersection(work_times_range) work_times_range = eliminate_intersection(work_times_range)
@ -130,7 +145,7 @@ class RulingTimerTemp
def get_last_work_time def get_last_work_time
last_work_time = self.work_times.last last_work_time = self.work_times.last
if last_work_time.nil? if last_work_time.nil?
last_work_time = DateTime.now.utc.new_offset(self.time_offset) last_work_time = DateTime.now.new_offset(0).new_offset(self.time_offset)
else else
last_work_time = convert_datetime(last_work_time).new_offset(self.time_offset) last_work_time = convert_datetime(last_work_time).new_offset(self.time_offset)
end end
@ -139,7 +154,7 @@ class RulingTimerTemp
def get_first_work_time def get_first_work_time
first_work_time = self.work_times.first first_work_time = self.work_times.first
if first_work_time.nil? if first_work_time.nil?
first_work_time = DateTime.now.utc.new_offset(self.time_offset) first_work_time = DateTime.now.new_offset(0).new_offset(self.time_offset)
else else
first_work_time = convert_datetime(first_work_time).new_offset(self.time_offset) first_work_time = convert_datetime(first_work_time).new_offset(self.time_offset)
end end
@ -147,7 +162,7 @@ class RulingTimerTemp
end end
def check_and_store def check_and_store
unless self.new_record? unless self.new_record?
time_now = DateTime.now.utc time_now = DateTime.now.new_offset(0)
date_str = time_now.new_offset(self.time_offset).strftime("%Y/%m/%d %w") date_str = time_now.new_offset(self.time_offset).strftime("%Y/%m/%d %w")
if self.date != date_str if self.date != date_str
self.stop self.stop
@ -159,7 +174,7 @@ class RulingTimerTemp
self.time_offset = offset if offset self.time_offset = offset if offset
self.check_and_store self.check_and_store
self.status = "working" self.status = "working"
time_now = DateTime.now.utc time_now = DateTime.now.new_offset(0)
self.date = time_now.new_offset(self.time_offset).strftime("%Y/%m/%d %w") self.date = time_now.new_offset(self.time_offset).strftime("%Y/%m/%d %w")
if self.work_times.count % 2 == 0 if self.work_times.count % 2 == 0
self.work_times.push(time_now) self.work_times.push(time_now)
@ -200,15 +215,44 @@ class RulingTimerTemp
temp_tasks.each{|task| task.stop} temp_tasks.each{|task| task.stop}
end end
def stop def stop
time_now = DateTime.now.utc time_now = DateTime.now.new_offset(0)
tmp_date = Date.parse(self.date.split(" ")[0])
time_now_date = time_now.new_offset(self.time_offset).to_date
@new_ruling_timer = nil
if tmp_date != time_now_date
push_time = tmp_date.to_datetime.new_offset(self.time_offset).end_of_day.new_offset(0)
need_create_history = true
else
push_time = time_now
need_create_history = false
end
field_name = nil
if self.work_times.count % 2 == 1 if self.work_times.count % 2 == 1
self.work_times.push(time_now) self.work_times.push(push_time)
field_name = "work_times"
end end
if self.rest_times.count % 2 == 1 if self.rest_times.count % 2 == 1
self.rest_times.push(time_now) self.rest_times.push(push_time)
field_name = "rest_times"
end
if need_create_history && field_name
tmp_attrs = self.get_attrs(["work_time_str", "rest_time_str", "all_work_times_seconds", "all_rest_times_seconds", "work_times", "rest_times", "status"])
tmp_attrs["all_#{field_name}_seconds"] = 86400
tmp_attrs["#{field_name.singularize}_str"] = "24:00:00"
self.store
self.send("#{field_name}=", [time_now.new_offset(self.time_offset).beginning_of_day.new_offset(0), time_now])
((tmp_date + 1.day)...time_now_date).each do |d|
tmp_date_str = d.strftime('%Y/%m/%d %w')
begin_of_d = DateTime.parse(d.strftime("%Y/%m/%d 00:00:00#{self.time_offset}"))
end_of_d = begin_of_d.end_of_day
tmp_attrs[field_name] = [begin_of_d.new_offset(0), end_of_d.new_offset(0)]
tmp_attrs["date"] = tmp_date_str
RulingTimerHistory.create(tmp_attrs)
end
end end
self.status = "stop" self.status = "stop"
self.calc("work_times",false) self.calc("work_times",false)
self.calc("rest_times",false)
self.work_time_str = transform_second_to_time(self.all_work_times_seconds) self.work_time_str = transform_second_to_time(self.all_work_times_seconds)
self.rest_time_str = transform_second_to_time(self.all_rest_times_seconds) self.rest_time_str = transform_second_to_time(self.all_rest_times_seconds)
self.save self.save
@ -217,7 +261,7 @@ class RulingTimerTemp
end end
def rest def rest
self.status = "rest" self.status = "rest"
time_now = DateTime.now.utc time_now = DateTime.now.new_offset(0)
if self.work_times.count % 2 == 1 if self.work_times.count % 2 == 1
self.work_times.push(time_now) self.work_times.push(time_now)
end end
@ -299,22 +343,38 @@ class RulingTimerTemp
start_index = 0 start_index = 0
if !recalc if !recalc
all_seconds = tmp_seconds all_seconds = tmp_seconds
start_index = time_infos.count - 1 if self.send("#{field_name}_changed?") || (time_infos.count % 2 == 1)
start_index -= (start_index % 2) instance_variable_name = "@#{field_name}_exec"
time_infos = time_infos[start_index..-1].to_a instance_variable = instance_variable_get(instance_variable_name)
if !(self.send("#{field_name}_changed?")) && time_infos.count != 1 if instance_variable.nil?
instance_variable_set(instance_variable_name, true)
last_val = self.send("#{field_name}_was").to_a.last
if last_val
idx = time_infos.index(last_val)
if idx
start_index = idx + (idx % 2 == 0 ? 0 : 1)
time_infos = time_infos[start_index..-1]
end
end
else
all_seconds = 0 #recalc
end
else
return all_seconds return all_seconds
end end
end end
time_infos.push(DateTime.now.utc) if (padding && time_infos.count % 2 == 1) time_infos.push(DateTime.now.new_offset(0)) if (padding && time_infos.count % 2 == 1)
prev_t = nil
time_infos.each_with_index do |t,i| time_infos.each_with_index do |t,i|
if i % 2 == 0 if i % 2 == 0
next_t = time_infos[i+1] next_t = time_infos[i+1]
if !next_t.nil? if !next_t.nil?
all_seconds += ((convert_datetime(next_t) - convert_datetime(t)) * 1.day).round all_seconds += ((convert_datetime(next_t) - convert_datetime(t)) * 1.day).round + ((prev_t && prev_t == t) ? 0.second : 1.second)
else else
break break
end end
else
prev_t = t
end end
end end
self.send("#{record_field_name}=",all_seconds) self.send("#{record_field_name}=",all_seconds)
@ -346,6 +406,7 @@ class RulingTimerTemp
ruling_timer_history.date = self.get_first_work_time.new_offset(self.time_offset).strftime("%Y/%m/%d %w") ruling_timer_history.date = self.get_first_work_time.new_offset(self.time_offset).strftime("%Y/%m/%d %w")
ruling_timer_history.user = self.user ruling_timer_history.user = self.user
ruling_timer_history.save ruling_timer_history.save
@timer_history = ruling_timer_history
end end
available_locales = I18n.available_locales available_locales = I18n.available_locales
not_reset_fields = ["time_offset","user_id","tasks_finished","sub_task_ids","tasks"] not_reset_fields = ["time_offset","user_id","tasks_finished","sub_task_ids","tasks"]
@ -371,7 +432,7 @@ class RulingTimerTemp
self.tasks = temp_tasks self.tasks = temp_tasks
self.tasks_finished = [] self.tasks_finished = []
end end
time_now = DateTime.now.utc time_now = DateTime.now.new_offset(0)
self.created_at = time_now self.created_at = time_now
self.updated_at = time_now self.updated_at = time_now
self.date = time_now.new_offset(self.time_offset).strftime("%Y/%m/%d %w") self.date = time_now.new_offset(self.time_offset).strftime("%Y/%m/%d %w")

View File

@ -3,7 +3,7 @@
<div> <div>
<span class="user_name"><%= form_comment.uploader.name rescue "" %></span> <span class="user_name"><%= form_comment.uploader.name rescue "" %></span>
<span class="update_time"><%= form_comment.display_updated_at rescue "" %></span> <span class="update_time"><%= form_comment.display_updated_at rescue "" %></span>
<p class="content_text markdown_text"><%= form_comment.content %></p> <p class="content_text markdown_text"><%= form_comment.content.gsub("\n", "<br>").html_safe %></p>
<%= f.text_area :content, :class => "hide content_textarea", :placeholder => t("ruling_timer.comments"), :title => t("ruling_timer.comments") %> <%= f.text_area :content, :class => "hide content_textarea", :placeholder => t("ruling_timer.comments"), :title => t("ruling_timer.comments") %>
</div> </div>
<% end %> <% end %>
@ -19,7 +19,7 @@
<a class="icon-trash"></a> <a class="icon-trash"></a>
</span> </span>
<% else %> <% else %>
<% if @current_user.id == form_comment.uploader.id %> <% if @current_user.id == form_comment.uploader_id %>
<span class="add-on btn edit_comment"><a class="icon-edit" title="<%= t(:edit) %>"></a></span> <span class="add-on btn edit_comment"><a class="icon-edit" title="<%= t(:edit) %>"></a></span>
<% unless @no_comment_save_btn %> <% unless @no_comment_save_btn %>
<span class="cancel_edit_comment add-on btn hide" title="<%= t("ruling_timer.cancel_edit") %>"> <span class="cancel_edit_comment add-on btn hide" title="<%= t("ruling_timer.cancel_edit") %>">

View File

@ -12,6 +12,15 @@
font-weight: bold; font-weight: bold;
} }
</style> </style>
<%
if @timer.class == RulingTimerTemp
@timer.check_and_store
@timer_history = @timer.instance_variable_get(:@timer_history)
if @timer_history
@timer = @timer_history
end
end
%>
<%= form_for @timer, url: update_timer_admin_ruling_timer_path(@timer), html: {class: "form-horizontal main-forms previewable"} do |f| %> <%= form_for @timer, url: update_timer_admin_ruling_timer_path(@timer), html: {class: "form-horizontal main-forms previewable"} do |f| %>
<fieldset> <fieldset>
<h3><%= @timer.date.split(" ")[0] + " " + @weekdays[@timer.date.split(" ")[1].to_i] %></h3> <h3><%= @timer.date.split(" ")[0] + " " + @weekdays[@timer.date.split(" ")[1].to_i] %></h3>

View File

@ -31,7 +31,7 @@
<div class="span20-percent"> <div class="span20-percent">
<% name = user.name %> <% name = user.name %>
<% link = work_history_admin_ruling_timer_path(:id=>user.id) %> <% link = work_history_admin_ruling_timer_path(:id=>user.id) %>
<a href="<%=link%>" title="<%=name%>"><img src="<%=user.member_profile.avatar.url%>"></a> <a href="<%=link%>" title="<%=name%>"><img src="<%=user.get_avatar%>"></a>
<div class="status-tags"> <div class="status-tags">
<% timer_temp = timer_temps[i] %> <% timer_temp = timer_temps[i] %>
<% if timer_temp.status != "stop" %> <% if timer_temp.status != "stop" %>

View File

@ -1,4 +1,4 @@
<script src="https://cdn.jsdelivr.net/npm/marked/marked.min.js"></script> <%= javascript_include_tag "ruling_timers/marked.min.js" %>
<style type="text/css"> <style type="text/css">
.markdown_text:not(.ready){ .markdown_text:not(.ready){
display: none; display: none;
@ -179,6 +179,11 @@
} }
</style> </style>
<script> <script>
var new_renderer = new marked.Renderer();
new_renderer.link = function( href, title, text ) {
return '<a target="_blank" href="'+ href +'" title="' + title + '">' + text + '</a>';
}
marked.setOptions({'renderer': new_renderer});
$(document).on('click', '#add_comment', function(){ $(document).on('click', '#add_comment', function(){
var new_id = $(this).prev().attr('value'); var new_id = $(this).prev().attr('value');
var old_id = new RegExp("new_ruling_timer_files", "g"); var old_id = new RegExp("new_ruling_timer_files", "g");
@ -226,7 +231,7 @@
} }
}); });
$(".markdown_text:not(.ready)").each(function(i,v){ $(".markdown_text:not(.ready)").each(function(i,v){
$(v).html(marked($(v).html())); $(v).html(marked.parse($(v).html()));
$(v).addClass("ready"); $(v).addClass("ready");
}) })
$(document).on("click",".save_comment",function(){ $(document).on("click",".save_comment",function(){
@ -245,7 +250,7 @@
if(data["html"]){ if(data["html"]){
comment_block.replaceWith(data["html"]); comment_block.replaceWith(data["html"]);
$(".markdown_text:not(.ready)").each(function(i,v){ $(".markdown_text:not(.ready)").each(function(i,v){
$(v).html(marked($(v).html())); $(v).html(marked.parse($(v).html()));
$(v).addClass("ready"); $(v).addClass("ready");
}) })
} }