Module: RequestClassDeprecator

Defined in:
lib/request_class_deprecator.rb

Overview

Use in migrations to deprecate request classes Usage: include RequestClassDeprecator deprecate_class('ClassName',options) options an otional hash { new_type: The new request type for affected requests. 'transfer' by default state_change: Hash of from_state => to_state applied to affected requests }

Defined Under Namespace

Classes: Request

Instance Method Summary collapse

Instance Method Details

#deprecate_class(request_class_name, options = {}) ⇒ Object


19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/request_class_deprecator.rb', line 19

def deprecate_class(request_class_name, options = {})
  state_changes = options.fetch(:state_change, {})
  new_request_type = options.fetch(:new_type, transfer_request)
  new_class_name = new_request_type.request_class_name

  ActiveRecord::Base.transaction do
    RequestType.where(request_class_name: request_class_name).each do |rt|
      say "Deprecating: #{rt.name}"
      rt.update!(deprecated: true)

      rt_requests = Request.where(request_type_id: rt.id, sti_type: request_class_name)

      state_changes.each do |from_state, to_state|
        say "Moving #{rt.name} from #{from_state} to #{to_state}", true
        mig = rt_requests.where(state: from_state).update_all(state: to_state) # rubocop:disable Rails/SkipsModelValidations
        say "Moved: #{mig}", true
      end

      say 'Updating requests:'
      mig = rt_requests.update_all(sti_type: new_class_name, request_type_id: new_request_type.id) # rubocop:disable Rails/SkipsModelValidations
      say "Updated: #{mig}", true
      PlatePurpose::Relationship.where(transfer_request_type_id: rt.id).update_all(transfer_request_type_id: new_request_type.id) # rubocop:disable Rails/SkipsModelValidations
    end
  end
end

#transfer_requestObject


15
16
17
# File 'lib/request_class_deprecator.rb', line 15

def transfer_request
  RequestType.find_by!(key: 'transfer')
end