Module: YiffSpace::Concerns::ActiveRecordExtensions::ClassMethods
- Defined in:
- lib/yiffspace/concerns/active_record_extensions.rb
Instance Method Summary collapse
- #statement_timeout ⇒ Object
-
#unnest(column, name: column.singularize, type: Arel::Nodes::LeftJoinLateral) ⇒ Object
CrossJoinLateral, LeftJoinLateral, nil.
- #with_timeout(time, default_value = nil) ⇒ Object
- #without_timeout ⇒ Object
Instance Method Details
#statement_timeout ⇒ Object
28 29 30 |
# File 'lib/yiffspace/concerns/active_record_extensions.rb', line 28 def statement_timeout ApplicationRecord.connection.select_one("SELECT setting FROM pg_settings WHERE name = 'statement_timeout'")["setting"].to_i end |
#unnest(column, name: column.singularize, type: Arel::Nodes::LeftJoinLateral) ⇒ Object
CrossJoinLateral, LeftJoinLateral, nil
33 34 35 36 37 38 39 40 41 |
# File 'lib/yiffspace/concerns/active_record_extensions.rb', line 33 def unnest(column, name: column.singularize, type: Arel::Nodes::LeftJoinLateral) function = Arel::Nodes::NamedFunction.new("unnest", [arel(column)], name) return function if type.nil? joins(type.new( function, Arel::Nodes::On.new(Arel.sql("TRUE")), )) end |
#with_timeout(time, default_value = nil) ⇒ Object
17 18 19 20 21 22 23 24 25 26 |
# File 'lib/yiffspace/concerns/active_record_extensions.rb', line 17 def with_timeout(time, default_value = nil) original = statement_timeout connection.execute("SET STATEMENT_TIMEOUT = #{time}") unless Rails.env.test? yield rescue ::ActiveRecord::StatementInvalid => e Rails.logger.warn("Statement timeout exceeded: #{e.}") default_value ensure connection.execute("SET STATEMENT_TIMEOUT = #{original}") unless Rails.env.test? end |
#without_timeout ⇒ Object
9 10 11 12 13 14 15 |
# File 'lib/yiffspace/concerns/active_record_extensions.rb', line 9 def without_timeout original = statement_timeout connection.execute("SET STATEMENT_TIMEOUT = 0") unless Rails.env.test? yield ensure connection.execute("SET STATEMENT_TIMEOUT = #{original}") unless Rails.env.test? end |