Module: ActsAsBookable::DBUtils
- Defined in:
- lib/acts_as_bookable/db_utils.rb
Class Method Summary collapse
- .active_record4? ⇒ Boolean
- .active_record5? ⇒ Boolean
- .connection ⇒ Object
-
.escape_like(str) ⇒ Object
escape _ and % characters in strings, since these are wildcards in SQL.
- .like_operator ⇒ Object
-
.time_comparison(query, field, operator, time) ⇒ Object
Compare times according to the DB.
- .using_mysql? ⇒ Boolean
- .using_postgresql? ⇒ Boolean
- .using_sqlite? ⇒ Boolean
Class Method Details
.active_record4? ⇒ 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
25 26 27 |
# File 'lib/acts_as_bookable/db_utils.rb', line 25 def active_record5? ::ActiveRecord::VERSION::MAJOR == 5 end |
.connection ⇒ Object
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_operator ⇒ Object
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
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
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
17 18 19 |
# File 'lib/acts_as_bookable/db_utils.rb', line 17 def using_sqlite? connection && connection.adapter_name == 'SQLite' end |