Class: ActiveMerge::SimpleService
- Inherits:
-
Object
- Object
- ActiveMerge::SimpleService
- Includes:
- ActiveModel::Validations
- Defined in:
- lib/active_merge/simple_service.rb
Overview
Сервисный объект (паттерн Service Object), отвечающий за объединение двух записей ActiveRecord
В ходе объединения все ссылки на вторую запись перепривязываются к первой, затем вторая запись удаляется
В случае любой ошибки вызывается исключение, а ошибка добавляется в массив errors
Instance Attribute Summary collapse
-
#first ⇒ Object
readonly
Returns the value of attribute first.
-
#second ⇒ Object
readonly
Returns the value of attribute second.
Instance Method Summary collapse
-
#initialize(first, second) ⇒ SimpleService
constructor
A new instance of SimpleService.
- #provide ⇒ Object
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
#first ⇒ Object (readonly)
Returns the value of attribute first.
26 27 28 |
# File 'lib/active_merge/simple_service.rb', line 26 def first @first end |
#second ⇒ Object (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
#provide ⇒ Object
30 31 32 33 34 35 36 |
# File 'lib/active_merge/simple_service.rb', line 30 def provide ActiveRecord::Base.transaction requires_new: true do raise unless valid? refs.each{ |item, key| rebind(item, key) } destroy end end |