Class: ActiveMerge::SimpleService
- Inherits:
-
ActivePatterns::BaseService
- Object
- ActivePatterns::BaseService
- ActiveMerge::SimpleService
- 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.
Instance Attribute Summary collapse
-
#first ⇒ Object
readonly
Returns the value of attribute first.
-
#klass ⇒ Object
readonly
Returns the value of attribute klass.
-
#links ⇒ Object
readonly
Returns the value of attribute links.
-
#second ⇒ Object
readonly
Returns the value of attribute second.
Instance Method Summary collapse
-
#initialize(first, second) ⇒ SimpleService
constructor
A new instance of SimpleService.
- #provide(validate: true) ⇒ Object
Constructor Details
#initialize(first, second) ⇒ SimpleService
Returns a new instance of SimpleService.
16 17 18 19 20 21 22 |
# File 'lib/active_merge/simple_service.rb', line 16 def initialize(first, second) @klass = first.class return unless klass.ancestors.include?(ActiveRecord::Base) && second.is_a?(klass) && first.persisted? && second.persisted? @first, @second = first, second @links = find_links end |
Instance Attribute Details
#first ⇒ Object (readonly)
Returns the value of attribute first.
23 24 25 |
# File 'lib/active_merge/simple_service.rb', line 23 def first @first end |
#klass ⇒ Object (readonly)
Returns the value of attribute klass.
23 24 25 |
# File 'lib/active_merge/simple_service.rb', line 23 def klass @klass end |
#links ⇒ Object (readonly)
Returns the value of attribute links.
23 24 25 |
# File 'lib/active_merge/simple_service.rb', line 23 def links @links end |
#second ⇒ Object (readonly)
Returns the value of attribute second.
23 24 25 |
# File 'lib/active_merge/simple_service.rb', line 23 def second @second end |
Instance Method Details
#provide(validate: true) ⇒ Object
27 28 29 30 31 32 |
# File 'lib/active_merge/simple_service.rb', line 27 def provide(validate: true) transaction do remove_second! validate save_links! validate end end |