Class: Shiftable::ShiftingPolymorphicRelation

Inherits:
Shifting
  • Object
show all
Includes:
Enumerable
Defined in:
lib/shiftable/shifting_polymorphic_relation.rb

Overview

Gets data to be shifted

Instance Attribute Summary

Attributes inherited from Shifting

#bang, #base, #column, #from, #result, #run_save, #shift_all_wrapper, #shift_each_wrapper, #to

Instance Method Summary collapse

Methods inherited from Shifting

#initialize

Constructor Details

This class inherits a constructor from Shiftable::Shifting

Instance Method Details

#each(&block) ⇒ Object



20
21
22
# File 'lib/shiftable/shifting_polymorphic_relation.rb', line 20

def each(&block)
  result.each(&block)
end

#found?Boolean

Returns:

  • (Boolean)


16
17
18
# File 'lib/shiftable/shifting_polymorphic_relation.rb', line 16

def found?
  result.any?
end

#polymorphic_id_columnObject



8
9
10
# File 'lib/shiftable/shifting_polymorphic_relation.rb', line 8

def polymorphic_id_column
  column[:id_column]
end

#polymorphic_type_columnObject



12
13
14
# File 'lib/shiftable/shifting_polymorphic_relation.rb', line 12

def polymorphic_type_column
  "#{column[:as]}_type"
end

#shiftObject



25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/shiftable/shifting_polymorphic_relation.rb', line 25

def shift
  return false unless found?

  each do |record|
    record.send("#{polymorphic_id_column}=", to.id)
  end
  @run_save = yield if block_given?
  return result unless run_save

  run_save!
  result
end