Module: ActsAsBookable::Bookable::Core::ClassMethods

Defined in:
lib/acts_as_bookable/bookable/core.rb

Instance Method Summary collapse

Instance Method Details

#initialize_acts_as_bookable_coreObject

Initialize the core of Bookable



14
15
16
17
# File 'lib/acts_as_bookable/bookable/core.rb', line 14

def initialize_acts_as_bookable_core
  # Manage the options
  set_options
end

#validate_booking_options!(options) ⇒ Object

Check if options passed for booking this Bookable are valid

Raises:

  • ActsAsBookable::OptionsInvalid if options are not valid



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/acts_as_bookable/bookable/core.rb', line 24

def validate_booking_options!(options)
  unpermitted_params = []
  required_params = {}

  #
  # Set unpermitted parameters and required parameters depending on Bookable options
  #

  # Switch :time_type
  case self.booking_opts[:time_type]
  # when :range, we need :time_start and :time_end
  when :range
    required_params[:time_start] = [Time,Date]
    required_params[:time_end] = [Time,Date]
    unpermitted_params << :time
  when :fixed
    required_params[:time] = [Time,Date]
    unpermitted_params << :time_start
    unpermitted_params << :time_end
  when :none
    unpermitted_params << :time_start
    unpermitted_params << :time_end
    unpermitted_params << :time
  end

  # Switch :capacity_type
  case self.booking_opts[:capacity_type]
  when :closed
    required_params[:amount] = [Integer]
  when :open
    required_params[:amount] = [Integer]
  when :none
    unpermitted_params << :amount
  end

  #
  # Actual validation
  #
  unpermitted_params = unpermitted_params
    .select{ |p| options.has_key?(p) }
    .map{ |p| "'#{p}'"}
  wrong_types = required_params
    .select{ |k,v| options.has_key?(k) && (v.select{|type| options[k].is_a?(type)}.length == 0) }
    .map{ |k,v| "'#{k}' must be a '#{v.join(' or ')}' but '#{options[k].class.to_s}' found" }
  required_params = required_params
    .select{ |k,v| !options.has_key?(k) }
    .map{ |k,v| "'#{k}'" }

  #
  # Raise OptionsInvalid if some invalid parameters were found
  #
  if unpermitted_params.length + required_params.length + wrong_types.length > 0
    message = ""
    message << " unpermitted parameters: #{unpermitted_params.join(',')}." if (unpermitted_params.length > 0)
    message << " missing parameters: #{required_params.join(',')}." if (required_params.length > 0)
    message << " parameters type mismatch: #{wrong_types.join(',')}" if (wrong_types.length > 0)
    raise ActsAsBookable::OptionsInvalid.new(self, message)
  end

  #
  # Convert options (Date to Time)
  #
  options[:time_start] = options[:time_start].to_time if options[:time_start].present?
  options[:time_end] = options[:time_end].to_time if options[:time_end].present?
  options[:time] = options[:time].to_time if options[:time].present?

  # Return true if everything's ok
  true
end