Class: NtiReceiptBuilder::SetDefault

Inherits:
Object
  • Object
show all
Defined in:
lib/nti_receipt_builder/set_default.rb

Overview

Makes one template the owner's default, clearing any previous default in a single UPDATE rather than loading the collection.

Instance Method Summary collapse

Constructor Details

#initialize(template:) ⇒ SetDefault

Returns a new instance of SetDefault.



7
8
9
# File 'lib/nti_receipt_builder/set_default.rb', line 7

def initialize(template:)
  @template = template
end

Instance Method Details

#callObject



11
12
13
14
15
16
17
18
# File 'lib/nti_receipt_builder/set_default.rb', line 11

def call
  template.class
          .where(owner_type: template.owner_type, owner_id: template.owner_id, is_default: true)
          .where.not(id: template.id)
          .update_all(is_default: false, updated_at: Time.current)

  template.update!(is_default: true) unless template.is_default?
end