RAILS: Availability of Resource after booking over period of time with calendar_for

此生再无相见时 提交于 2019-12-25 07:35:48

问题


I am writing booking system for rentals of countable resource in Rails. Owner of resource wants to see total resource volume booked over the time as percentage.

calendar_for gem seems to be nice choice, except that I fail to find the way to show this percentage over period of time.

Suppose, total resource is 100:

  • client-A books from 12-jan-2013 till 20-Feb-2013 20 units.
  • client-B books from 15-jan-2013 till 1-Mar-2013 30 units.

I want to see booked capacity

  • from 12-jan-2013 to 14-jan-2013 = 20% (booking from Client-A only)
  • from 15-jan-2013 to 20-Feb-2013 = 50% (20+30) (booking from Client-A and Client-B)
  • from 21-Feb-2013 to 1-Mar-2013 = 30% (booking from Client-B only)

gradually, I got collected these gems

  • gem 'event-calendar'
  • gem 'table_builder'
  • gem 'watu_table_builder'
  • gem 'calendar_date_select'

the code below is replica from the famous lesson 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 %>

Alternate is:

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

This I have tried in combination with booking.rb two methods:

this shows percentage of booked units at from_date (i.e. at start)

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

this shows only total booking at from_date too

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

Booking model has these fields:

  • :from_date,
  • :till_date,
  • :units

Resource model has

  • :units

Thanks, Nik


回答1:


The solution to my question came as answer to this question: 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 %>

This was backed up with 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 is created "on-fly" and is not stored into DB.

Structure of Booking_day is {:id, :value, :day}.

Booking object is stored into DB and has this structure {:user_id, :resource_id, :from_time, :to_time, :value}. It is the base for all information shown in the calendar via booking_day object.

num1000 is a table of i=0...999

Nik



来源:https://stackoverflow.com/questions/17222612/rails-availability-of-resource-after-booking-over-period-of-time-with-calendar

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!