我想展示一个议程,显示谁可以在未来七天的名册,逻辑如下:
每7天循环,每天:
如果今天开始休假,则显示Starts %H:%M
如果假期今天结束,则显示Finishes %H:%M
如果假期今天开始并结束,则显示%H:%M - %H:%M
如果假期不是今天开始或结束,而是跨越了今天,则显示all day
应包括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
在控制器中:
@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
在视图中:
<% @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 %>
您可能会注意到我从html中删除了
<br>
和style
标记请不要在html中使用<br>
设置样式或style
属性。添加类标记并在css文件中设置它们的样式。关于ruby-on-rails - 议程日历/花名册 View ,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/34249376/