0

我正在编写 Rails 中可数资源租赁的预订系统。资源所有者希望查看一段时间内预订的总资源量百分比。

calendar_for gem 似乎是不错的选择,除了我找不到在一段时间内显示此百分比的方法。

假设总资源为 100:

  • 2013 年 1 月 12 日至 2013 年 2 月 20 日的客户 A 书籍 20 单位。
  • 从 2013 年 1 月 15 日到 2013 年 3 月 1 日的客户 B 书籍 30 单位。

我想查看预订的容量

  • 从 2013 年 1 月 12 日到 2013 年 1 月 14 日 = 20%(仅限客户 A 预订)
  • 从 2013 年 1 月 15 日到 2013 年 2 月 20 日 = 50% (20+30)(从客户 A 和客户 B 预订)
  • 从 2013 年 2 月 21 日到 2013 年 3 月 1 日 = 30%(仅限客户 B 预订)

渐渐地,我收集到了这些宝石

  • gem '事件日历'
  • 宝石'table_builder'
  • 宝石'watu_table_builder'
  • 宝石“日历_日期选择”

下面的代码是著名课程 RailsCast #213 的复制品。

index_calendar.html.erb

    <%=  @date.prev_month.strftime("%B %Y") %>
<%= calendar_for(@bookings, :year => @date.year, :month => @date.prev_month.month) do |calendar| %>

  <%= calendar.head('Sun', 'Mon', 'Tue', 'Wed', 'Thr', 'Fri', 'Sat') %>

  <% calendar.day(:day_method => :from_date) do |date, bookings| %>
    <%= date.day %>
    <ul>
      <% for booking in bookings %>
        <li><%= link_to h(booking.capacity), booking %></li>
      <% end %>
    </ul>
  <% end %>

<% end %>

替代方案是:

<li><%= link_to h(booking.total_capacity(date)), booking %></li>

这我已经尝试结合booking.rb两种方法:

这显示了 from_date(即开始时)预订单位的百分比

def capacity
  @a=Resource.find_by_id(self.resource_id).units
  [(self.units/@a*100).to_s + "%"].join
end

这也仅显示 from_date 的总预订量

def total_capacity(day)
  @wall=Booking.joins(:resource).
    where("#{day}<=till_time AND from_time>#{day}").sum(:units)
end

预订模型具有以下字段:

  • :从日期,
  • :直到日期,
  • :单位

资源模型

  • :单位

谢谢,尼克

4

1 回答 1

0

我的问题的解决方案是对这个问题的回答: Fill object from array

booking_days/index_calendar.html.erb

<%=  @date.prev_month.strftime("%B %Y") %>
<%= calendar_for(@booking_days, :year => @date.year, :month => @date.month) do |calendar| %>

  <%= calendar.head('Sun', 'Mon', 'Tue', 'Wed', 'Thr', 'Fri', 'Sat') %>

  <% calendar.day(:day_method => :day) do |date, booking_days| %>
    <%= date.day %>
    <ul>
      <% for booking_day in booking_days %>
        <li><%= link_to h(booking_day.value), booking_day %></li>
      <% end %>
    </ul>
  <% end %>
<% end %>

这是由booking_days_controller.rb备份的

def index_calendar
    @date = params[:month] ? Date.strptime(params[:month],"%Y-%m") : Date.today
    dstart=@date.prev_month.beginning_of_month
    dend=@date.next_month.end_of_month
    rid=Resource.find_by_user_id(session[:user_id]).id
    sql = ["select @row := @row + 1 as id, ts.dat as day, sum(bookings.value) as value " +
        "from (select adddate('#{dstart}', num1000.i) as dat " + 
        "from num1000 where adddate('#{dstart}', num1000.i) <= '#{dend}') as ts, resources, " + 
        "bookings JOIN  (SELECT @row := 0) r " + 
        "where bookings.resources_id=resources.id AND resources.id=#{rid} AND " + 
        "adddate(bookings.from_time,-1)<=ts.dat AND ts.dat<=bookings.till_time group by ts.dat"].join
    @booking_days = BookingDay.find_by_sql(sql)
    respond_to do |format|
      format.html # index_calendar.html.erb
      format.json { render json: @booking_days }
    end
  end

Booking_days 是“即时”创建的,不会存储到数据库中。

Booking_day 的结构是 {:id, :value, :day}。

预订对象存储在数据库中,具有以下结构 {:user_id, :resource_id, :from_time, :to_time, :value}。它是通过 booking_day 对象显示在日历中的所有信息的基础。

num1000 是 i=0...999 的表

尼克

于 2013-06-25T18:12:05.023 回答