gpt4 book ai didi

ruby-on-rails - 议程日历/花名册 View

转载 作者:太空宇宙 更新时间:2023-11-03 16:20:33 25 4
gpt4 key购买 nike

我想显示一个议程,显示 future 7 天谁可以参加名册,逻辑如下:

循环遍历 7 天中的每一天,对于每一天:

  1. 如果休假从今天开始,显示 Starts %H:%M
  2. 如果今天休假结束,则显示 Finishes %H:%M
  3. 如果今天开始和结束请假,显示 %H:%M - %H:%M
  4. 如果休假不是今天开始或结束,而是跨越今天,则显示 all day
  5. 应包括在 7 天之外开始或结束但跨越显示的 7 天的休假。

我希望有人能为我指明正确的方向,最好使用 Rails 和 Postgres。我不反对每天查询,因为最多只会显示 30 天,但我也在寻找性能合理的代码,因为每天可能有 100 多条休假记录。

第一个不满足上述标准的示例。

<% @dates.each do |d| %>
<h5><%= d.strftime("%A %d %b") %></h5>
<% @leave = Leave.where('(start_at BETWEEN ? AND ?) OR (end_at BETWEEN ? AND ?)', d.beginning_of_day, d.end_of_day, d.beginning_of_day, d.end_of_day) %>
<% @leave = @leave.where.not('end_at < ?', Time.current) %>

<% if @leave.any? %>
<ul>
<% @leave.each do |leave| %>
<% if leave.single_day? && leave.start_at.to_date == d %>
<li>
<label class="label label-danger"><%= leave.start_at.strftime('%H:%M') %> - <%= leave.end_at.strftime('%H:%M') %></label>
<%= leave.user.name %>
</li>
<% elsif !leave.single_day? %>
<% if leave.start_at.to_date == d %>
<li>
<label class="label label-danger">From <%= leave.start_at.strftime('%H:%M') %></label>
<%= leave.user.name %>
</li>
<% elsif leave.end_at.to_date == d %>
<li>
<label class="label label-danger">Until <%= leave.end_at.strftime('%H:%M') %></label>
<%= leave.user.name %>
</li>
<% else %>
<li>
<label class="label label-danger">All Day</label>
<%= leave.user.name %>
</li>
<% end %>
<% end %>
<% end %>
</ul>
<br/>
<span class="label label-success" style="margin-right: 10px;">
<span class="glyphicon glyphicon-ok"></span>
</span>
<%= current_account.users.active.where.not(id: @leave.map(&:user_id)).count %> Available
<% else %>
<span class="label label-success" style="margin-right: 10px;">
<span class="glyphicon glyphicon-ok"></span>
</span>
<%= current_account.users.active.count %> Available
<% end %>

<br />
<br />
<% end %>

最佳答案

我会从这样的事情开始:

在模型中:

def starts_on?(date)
start_at.to_date == date
end

def ends_on?(date)
end_at.to_date = date
end

def covers?(date)
leave.start_at.to_date <= date && leave.end_at.to_date >= date
end

def starts_and_ends_on?(date)
starts_on?(date) && ends_on?(date)
end

在 Controller 中:

@dates = # define the date range like you already do, I assume it is an array
@leaves = Leave.includes(:user).
where('start_at =< ? AND end_at >= ?', dates.max, dates.min)

在助手中:

def relevant_leaves_for_date(date, leaves)
leaves.select { |leave| leave.covers?(date) }
end

def leave_description_for_date(leave, date)
if leave.starts_and_ends_on?(date)
"#{leave.start_at.strftime('%H:%M')} - #{leave.end_at.strftime('%H:%M')}"
elsif leave.starts_on?(date)
"From #{leave.start_at.strftime('%H:%M')}"
elsif leave.ends_on?(date)
"Until #{leave.end_at.strftime('%H:%M')}"
else
'All day'
end
end

def available_users(leaves)
current_account.users.active.count - leaves.size
end

在 View 中:

<% @dates.each do |date| %>
<h5><%= date.strftime("%A %d %b") %></h5>

<% leaves = relevant_leaves_for_date(date, @leaves) %>
<% if leaves.any? %>
<ul>

<% leaves.each do |leave| %>
<li>
<label class="label label-danger">
<%= leave_description_for_date(leave, date) %>
</label>
<%= leave.user.name %>
</li>
<% end %>

</ul>
<% end %>

<span class="label label-success">
<span class="glyphicon glyphicon-ok"></span>
</span>
<%= available_users(leaves) %> Available
<% end %>

您可能会注意到我删除了 <br>style来自html的标签。请不要使用 <br>造型或style html中的属性。在您的 css 文件中添加标签类和样式。

关于ruby-on-rails - 议程日历/花名册 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34249376/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com