Module: Shamu::Services::ActiveRecord
- Extended by:
- ActiveSupport::Concern
- Defined in:
- lib/shamu/services/active_record.rb
Overview
Helper methods useful for services that interact with ActiveRecord::Base models.
Instance Method Summary collapse
-
#scope_relation(relation, list_scope) ⇒ ActiveRecord::Relation
Apply the filters specified in
list_scope
to therelation
. -
#with_request ⇒ Object
Override to make sure we always catch ActiveRecord not found errors.
-
#with_transaction(options = {}, &block) ⇒ Result
Wrap all the changes to any ActiveRecord resource in a transaction.
-
#wrap_not_found(&block) ⇒ Object
Watch for ActiveRecord::RecordNotFound errors and rethrow as a NotFoundError.
Instance Method Details
#scope_relation(relation, list_scope) ⇒ ActiveRecord::Relation
Apply the filters specified in list_scope
to the relation
.
58 59 60 61 62 63 64 65 66 |
# File 'lib/shamu/services/active_record.rb', line 58 def scope_relation( relation, list_scope ) return unless relation if relation.respond_to?( :by_list_scope ) relation.by_list_scope( list_scope ) else fail "Can't scope a #{ relation.klass }. Add `scope :by_list_scope, ->(list_scope) { ... }` or extend Shamu::Entities::ActiveRecord." # rubocop:disable Metrics/LineLength end end |
#with_request ⇒ Object
Override to make sure we always catch ActiveRecord not found errors.
11 12 13 14 15 |
# File 'lib/shamu/services/active_record.rb', line 11 def with_request( * ) wrap_not_found do super end end |
#with_transaction(options = {}, &block) ⇒ Result
Wrap all the changes to any ActiveRecord resource in a transaction.
39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/shamu/services/active_record.rb', line 39 def with_transaction( = {}, &block ) result = nil ::ActiveRecord::Base.transaction do result = yield success = result && ( result.respond_to?( :valid? ) ? result.valid? : true ) raise ::ActiveRecord::Rollback unless success end result end |
#wrap_not_found(&block) ⇒ Object
Watch for ActiveRecord::RecordNotFound errors and rethrow as a NotFoundError.
25 26 27 28 29 |
# File 'lib/shamu/services/active_record.rb', line 25 def wrap_not_found( &block ) yield rescue ::ActiveRecord::RecordNotFound raise Shamu::NotFoundError end |