Class: ActiveMerge::SimpleService

Inherits:
ActivePatterns::BaseService
  • Object
show all
Includes:
ActiveModel::Validations
Defined in:
lib/active_merge/simple_service.rb

Overview

Service Object responds for merging two ActiveRecord instances as a whole transaction.

All objects linked to the second instance via “has_many” association are re-associated to the first one.

Then the second instance removed. All errors collected in the #errors method.

Defined Under Namespace

Classes: Links

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(first, second) ⇒ SimpleService

Returns a new instance of SimpleService.



16
17
18
19
20
21
22
23
24
25
# File 'lib/active_merge/simple_service.rb', line 16

def initialize(first, second)

  if first.class.ancestors.include?(ActiveRecord::Base) && first.persisted?
    @first = first
  end

  if @first && (second.class == @first.class) && second.persisted?
    @second = second
  end
end

Instance Attribute Details

#firstObject (readonly)

Returns the value of attribute first.



26
27
28
# File 'lib/active_merge/simple_service.rb', line 26

def first
  @first
end

#secondObject (readonly)

Returns the value of attribute second.



26
27
28
# File 'lib/active_merge/simple_service.rb', line 26

def second
  @second
end

Instance Method Details

#provideObject



30
31
32
33
34
35
# File 'lib/active_merge/simple_service.rb', line 30

def provide
  transaction do
    Links.new(second).each { |item, key| rebind(item, key) }
    change(second) { second.destroy! }
  end
end