Class: Draft::OverwriteService

Inherits:
Object
  • Object
show all
Defined in:
lib/rails_admin_draft/services/draft/overwrite_service.rb

Constant Summary collapse

DRAFT_ASSOCIATIONS =
[:draft, :draft_source]

Instance Method Summary collapse

Constructor Details

#initialize(source:, destination:) ⇒ OverwriteService



7
8
9
10
11
12
13
14
# File 'lib/rails_admin_draft/services/draft/overwrite_service.rb', line 7

def initialize(source:, destination:)
  raise "Source must be an ActiveRecord::Base" if !source.is_a?(ActiveRecord::Base)
  raise "Destination must be an ActiveRecord::Base" if !destination.is_a?(ActiveRecord::Base)
  raise "Source and destination must be of the same type" if source.class != destination.class

  @source = source
  @destination = destination
end

Instance Method Details

#performObject

Performs the overwrite. Handles attributes, associations and uploads.



17
18
19
20
21
22
23
24
25
26
# File 'lib/rails_admin_draft/services/draft/overwrite_service.rb', line 17

def perform
  @destination.assign_attributes(@source.attributes.except("id", "draft_source_id", *upload_attributes.map(&:to_s)))
  handle_associations(:has_one)
  handle_associations(:has_many)
  handle_associations(:has_and_belongs_to_many)
  handle_uploads

  @destination.save!
  return @destination.reload
end