Module: ActsAsBookable::DBUtils

Defined in:
lib/acts_as_bookable/db_utils.rb

Class Method Summary collapse

Class Method Details

.active_record4?Boolean

Returns:

  • (Boolean)


21
22
23
# File 'lib/acts_as_bookable/db_utils.rb', line 21

def active_record4?
  ::ActiveRecord::VERSION::MAJOR == 4
end

.active_record5?Boolean

Returns:

  • (Boolean)


25
26
27
# File 'lib/acts_as_bookable/db_utils.rb', line 25

def active_record5?
  ::ActiveRecord::VERSION::MAJOR == 5
end

.connectionObject



4
5
6
# File 'lib/acts_as_bookable/db_utils.rb', line 4

def connection
  ActsAsBookable::Booking.connection
end

.escape_like(str) ⇒ Object

escape _ and % characters in strings, since these are wildcards in SQL.



47
48
49
# File 'lib/acts_as_bookable/db_utils.rb', line 47

def escape_like(str)
  str.gsub(/[!%_]/) { |x| '!' + x }
end

.like_operatorObject



29
30
31
# File 'lib/acts_as_bookable/db_utils.rb', line 29

def like_operator
  using_postgresql? ? 'ILIKE' : 'LIKE'
end

.time_comparison(query, field, operator, time) ⇒ Object

Compare times according to the DB



36
37
38
39
40
41
42
43
44
# File 'lib/acts_as_bookable/db_utils.rb', line 36

def time_comparison (query, field, operator, time)
  if using_postgresql?
    query.where("#{field}::timestamp #{operator} ?::timestamp", time.to_time.utc.to_s)
  elsif using_sqlite?
    query.where("Datetime(#{field}) #{operator} Datetime('#{time.to_time.utc.iso8601}')")
  else
    query.where("#{field} #{operator} ?", time.to_time)
  end
end

.using_mysql?Boolean

Returns:

  • (Boolean)


12
13
14
15
# File 'lib/acts_as_bookable/db_utils.rb', line 12

def using_mysql?
  #We should probably use regex for mysql to support prehistoric adapters
  connection && connection.adapter_name == 'Mysql2'
end

.using_postgresql?Boolean

Returns:

  • (Boolean)


8
9
10
# File 'lib/acts_as_bookable/db_utils.rb', line 8

def using_postgresql?
  connection && connection.adapter_name == 'PostgreSQL'
end

.using_sqlite?Boolean

Returns:

  • (Boolean)


17
18
19
# File 'lib/acts_as_bookable/db_utils.rb', line 17

def using_sqlite?
  connection && connection.adapter_name == 'SQLite'
end