Module: TableSync::EventActions
- Included in:
- ConfigDecorator
- Defined in:
- lib/table_sync/event_actions.rb
Instance Method Summary collapse
- #destroy(data) ⇒ Object
- #expected_update_result?(query_results) ⇒ Boolean
- #prevent_incomplete_event!(attributes) ⇒ Object
-
#update(data) ⇒ Object
rubocop:disable Metrics/MethodLength.
Instance Method Details
#destroy(data) ⇒ Object
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/table_sync/event_actions.rb', line 40 def destroy(data) attributes = data.first || {} target_attributes = attributes.select { |key, _value| target_keys.include?(key) } prevent_incomplete_event!(target_attributes) model.transaction do @config.callback_registry.get_callbacks(kind: :before_commit, event: :destroy).each do |cb| cb[attributes] end if on_destroy results = on_destroy.call(attributes: attributes, target_keys: target_keys) else results = model.destroy(target_attributes) return if results.empty? raise TableSync::DestroyError.new(target_attributes) if results.size != 1 end @config.model.after_commit do @config.callback_registry.get_callbacks(kind: :after_commit, event: :destroy).each do |cb| cb[results] end end end end |
#expected_update_result?(query_results) ⇒ Boolean
66 67 68 |
# File 'lib/table_sync/event_actions.rb', line 66 def expected_update_result?(query_results) query_results.uniq { |d| d.slice(*target_keys) }.size == query_results.size end |
#prevent_incomplete_event!(attributes) ⇒ Object
70 71 72 73 74 |
# File 'lib/table_sync/event_actions.rb', line 70 def prevent_incomplete_event!(attributes) unless target_keys.all?(&attributes.keys.method(:include?)) raise TableSync::UnprovidedEventTargetKeysError.new(target_keys, attributes) end end |
#update(data) ⇒ Object
rubocop:disable Metrics/MethodLength
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/table_sync/event_actions.rb', line 5 def update(data) # rubocop:disable Metrics/MethodLength data.each_value do |attribute_set| attribute_set.each do |attributes| prevent_incomplete_event!(attributes) end end model.transaction do args = { data: data, target_keys: target_keys, version_key: version_key, first_sync_time_key: first_sync_time_key, default_values: default_values, } @config.callback_registry.get_callbacks(kind: :before_commit, event: :update).each do |cb| cb[data.values.flatten] end results = data.reduce([]) do |upserts, (part_model, part_data)| upserts + part_model.upsert(**args, data: part_data) end return if results.empty? raise TableSync::UpsertError.new(**args) unless expected_update_result?(results) @config.model.after_commit do @config.callback_registry.get_callbacks(kind: :after_commit, event: :update).each do |cb| cb[results] end end end end |