<commit_before>module Reservation
  class Reservation < ActiveRecord::Base
    NAME_MAX_LENGTH = 24
    self.table_name = "reservations"

    attr_accessor :guidelines_ok

    belongs_to :reserver, class_name: "User"
    belongs_to :sponsor, class_name: "User"
    belongs_to :resource
    belongs_to :meal

    delegate :household, to: :reserver
    delegate :name, :community, to: :reserver, prefix: true
    delegate :name, to: :reserver_community, prefix: true
    delegate :community, to: :sponsor, prefix: true, allow_nil: true
    delegate :community_id, to: :resource
    delegate :name, :full_name, to: :resource, prefix: true
    delegate :title, to: :meal, prefix: true
    delegate :access_level, :fixed_start_time?, :fixed_end_time?, :requires_kind?, to: :rule_set

    validates :name, presence: true, length: { maximum: NAME_MAX_LENGTH }
    validates :resource_id, :reserver_id, :starts_at, :ends_at, presence: true
    validate :guidelines_accepted
    validate :start_before_end
    validate :no_overlap
    validate :apply_rules

    normalize_attributes :kind, :note

    # Counts the number of seconds or days booked for the given resources by the given
    # household over the given period.
    # The number of days is rounded up for each event.
    # i.e., a 1-hour event and a 10-hour event both counts as 1 day, while a 36-hour event
    # counts as 2 days.
    def self.booked_time_for(resources:, household:, period:, unit:)
      where(resource: resources, reserver: household.users, starts_at: period).to_a.sum(&unit)
    end

    def self.new_with_defaults(attribs)
      reservation = new(attribs)

      reservation.starts_at ||= Time.zone.now.midnight + 1.week + 17.hours
      reservation.ends_at ||= Time.zone.now.midnight + 1.week + 18.hours

      # Set fixed start/end time
      rule_set = reservation.rule_set
      if fst = rule_set[:fixed_start_time].try(:value)
        reservation.starts_at = reservation.starts_at.change(hour: fst.hour, min: fst.min)
      end
      if fet = rule_set[:fixed_end_time].try(:value)
        reservation.ends_at = reservation.ends_at.change(hour: fet.hour, min: fet.min)
      end
      if reservation.starts_at >= reservation.ends_at
        reservation.ends_at = reservation.ends_at.change(day: reservation.ends_at.day + 1)
      end

      reservation
    end

    def seconds
      ends_at - starts_at
    end

    def days<commit_msg>Fixed bug in reservation time calculations<commit_after>module Reservation
  class Reservation < ActiveRecord::Base
    NAME_MAX_LENGTH = 24
    self.table_name = "reservations"

    attr_accessor :guidelines_ok

    belongs_to :reserver, class_name: "User"
    belongs_to :sponsor, class_name: "User"
    belongs_to :resource
    belongs_to :meal

    delegate :household, to: :reserver
    delegate :name, :community, to: :reserver, prefix: true
    delegate :name, to: :reserver_community, prefix: true
    delegate :community, to: :sponsor, prefix: true, allow_nil: true
    delegate :community_id, to: :resource
    delegate :name, :full_name, to: :resource, prefix: true
    delegate :title, to: :meal, prefix: true
    delegate :access_level, :fixed_start_time?, :fixed_end_time?, :requires_kind?, to: :rule_set

    validates :name, presence: true, length: { maximum: NAME_MAX_LENGTH }
    validates :resource_id, :reserver_id, :starts_at, :ends_at, presence: true
    validate :guidelines_accepted
    validate :start_before_end
    validate :no_overlap
    validate :apply_rules

    normalize_attributes :kind, :note

    # Counts the number of seconds or days booked for the given resources by the given
    # household over the given period.
    # The number of days is rounded up for each event.
    # i.e., a 1-hour event and a 10-hour event both counts as 1 day, while a 36-hour event
    # counts as 2 days.
    def self.booked_time_for(resources:, household:, period:, unit:)
      where(resource: resources, reserver: household.users, starts_at: period).to_a.sum(&unit)
    end

    def self.new_with_defaults(attribs)
      reservation = new(attribs)

      reservation.starts_at ||= Time.zone.now.midnight + 1.week + 17.hours
      reservation.ends_at ||= Time.zone.now.midnight + 1.week + 18.hours

      # Set fixed start/end time
      rule_set = reservation.rule_set
      if fst = rule_set[:fixed_start_time].try(:value)
        reservation.starts_at = reservation.starts_at.change(hour: fst.hour, min: fst.min)
      end
      if fet = rule_set[:fixed_end_time].try(:value)
        reservation.ends_at = reservation.ends_at.change(hour: fet.hour, min: fet.min)
      end
      if reservation.starts_at >= reservation.ends_at
        reservation.ends_at += 1.day
      end

      reservation
    end

    def seconds
      ends_at - starts_at
    end

    def days
{'fraction_non_alphanumeric': 0.09365244536940687, 'fraction_numerical': 0.006659729448491155, 'mean_word_length': 3.4091743119266056, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4537940', 'n_tokens_mistral': 1610, 'n_tokens_neox': 1506, 'n_words': 428}