Module: TableSync::EventActions
- Included in:
- ConfigDecorator
- Defined in:
- lib/table_sync/event_actions.rb
Instance Method Summary collapse
- #correct_keys?(query_results) ⇒ Boolean
- #destroy(data) ⇒ Object
-
#update(data) ⇒ Object
rubocop:disable Metrics/MethodLength.
Instance Method Details
#correct_keys?(query_results) ⇒ Boolean
59 60 61 |
# File 'lib/table_sync/event_actions.rb', line 59 def correct_keys?(query_results) query_results.uniq { |d| d.slice(*target_keys) }.size == query_results.size end |
#destroy(data) ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/table_sync/event_actions.rb', line 34 def destroy(data) attributes = data.first || {} target_attributes = attributes.select { |key| target_keys.include?(key) } 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 |
#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 |
# File 'lib/table_sync/event_actions.rb', line 5 def update(data) # rubocop:disable Metrics/MethodLength 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 correct_keys?(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 |