Class: OnlineMigrations::BackgroundDataMigrations::DeleteAssociatedRecords
- Inherits:
-
DataMigration
- Object
- DataMigration
- OnlineMigrations::BackgroundDataMigrations::DeleteAssociatedRecords
- Defined in:
- lib/online_migrations/background_data_migrations/delete_associated_records.rb
Instance Attribute Summary collapse
-
#association ⇒ Object
readonly
Returns the value of attribute association.
-
#record ⇒ Object
readonly
Returns the value of attribute record.
Instance Method Summary collapse
- #collection ⇒ Object
- #count ⇒ Object
-
#initialize(model_name, record_id, association, _options = {}) ⇒ DeleteAssociatedRecords
constructor
A new instance of DeleteAssociatedRecords.
- #process(relation) ⇒ Object
Methods inherited from DataMigration
#after_cancel, #after_complete, #after_pause, #after_resume, #after_start, #after_stop, #around_process, #build_enumerator, collection_batch_size, named
Constructor Details
#initialize(model_name, record_id, association, _options = {}) ⇒ DeleteAssociatedRecords
Returns a new instance of DeleteAssociatedRecords.
9 10 11 12 13 |
# File 'lib/online_migrations/background_data_migrations/delete_associated_records.rb', line 9 def initialize(model_name, record_id, association, = {}) model = Object.const_get(model_name, false) @record = model.find(record_id) @association = association end |
Instance Attribute Details
#association ⇒ Object (readonly)
Returns the value of attribute association.
7 8 9 |
# File 'lib/online_migrations/background_data_migrations/delete_associated_records.rb', line 7 def association @association end |
#record ⇒ Object (readonly)
Returns the value of attribute record.
7 8 9 |
# File 'lib/online_migrations/background_data_migrations/delete_associated_records.rb', line 7 def record @record end |
Instance Method Details
#collection ⇒ Object
15 16 17 18 19 20 21 |
# File 'lib/online_migrations/background_data_migrations/delete_associated_records.rb', line 15 def collection if !@record.respond_to?(association) raise ArgumentError, "'#{@record.class.name}' has no association called '#{association}'" end record.public_send(association).in_batches(of: 100) end |
#count ⇒ Object
27 28 29 |
# File 'lib/online_migrations/background_data_migrations/delete_associated_records.rb', line 27 def count record.public_send(association).count end |
#process(relation) ⇒ Object
23 24 25 |
# File 'lib/online_migrations/background_data_migrations/delete_associated_records.rb', line 23 def process(relation) relation.delete_all end |