Class: DelayHenka::ScheduledChange
- Inherits:
-
ApplicationRecord
- Object
- ActiveRecord::Base
- ApplicationRecord
- DelayHenka::ScheduledChange
- Defined in:
- app/models/delay_henka/scheduled_change.rb
Constant Summary collapse
- STATES =
{ STAGED: 'staged', REPLACED: 'replaced', COMPLETED: 'completed', ERRORED: 'errored' }
Class Method Summary collapse
Instance Method Summary collapse
Class Method Details
.schedule(record:, changes:, by_id:, schedule_at: Time.current) ⇒ Object
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'app/models/delay_henka/scheduled_change.rb', line 20 def self.schedule(record:, changes:, by_id:, schedule_at: Time.current) Keka.run do service = WhetherSchedule.new(record) new_changes = changes.each_with_object([]) do |(attribute, new_val), accum| old_val = record.public_send(attribute) cleaned_new_val = cleanup_val(new_val) decision = service.make_decision(attribute, cleaned_new_val) if decision.ok? accum << new( changeable: record, submitted_by_id: by_id, attribute_name: attribute, old_value: old_val, new_value: cleaned_new_val, schedule_at: schedule_at ) elsif decision.msg return decision # error present else # otherwise do nothing - maybe no change is made end end transaction do new_changes.each(&:save!) end end end |
Instance Method Details
#apply_change ⇒ Object
49 50 51 52 53 54 55 56 57 58 59 |
# File 'app/models/delay_henka/scheduled_change.rb', line 49 def apply_change if changeable if changeable.update(attribute_name => new_value) update!(state: STATES[:COMPLETED]) else update!(state: STATES[:ERRORED], error_message: changeable.errors..join(', ')) end else update_columns(state: STATES[:ERRORED], error_message: 'Target record cannot be found') end end |
#replace_change ⇒ Object
61 62 63 |
# File 'app/models/delay_henka/scheduled_change.rb', line 61 def replace_change update(state: STATES[:REPLACED]) end |