Sha256: 408721577dd491774ea00c4a3f64558980d8b08eeac081b97ef70ce01091c84b

Contents?: true

Size: 1.1 KB

Versions: 23

Compression:

Stored size: 1.1 KB

Contents

require_dependency 'flexite/action_service'

module Flexite
  class Entry
    class ArrayUpdateService < ActionService
      include InnerProcessable

      def call
        if @form.invalid?
          return failure
        end

        process_entry
      end

      private

      def process_entry
        Entry.transaction(requires_new: true) do
          update_entries(@form.entries)
          create_entries
        end

        process_result('Entry was updated')
      end

      def update_entries(entries)
        entries.each do |_, entry|
          save_entry(entry)
        end
      end

      def save_entry(entry)
        call_service_for(:update, entry)
      end

      def create_entries
        return if @form.new_entries.blank?

        @form.new_entries.each do |_, entry|
          entry[:parent_id] = @form.id
          entry[:parent_type] = @form.type.constantize.base_class.sti_name
          call_service_for(:create, entry)
        end
      end

      protected

      def failure
        save_errors
        Result.new(success: false, endpoint: { status: 400 })
      end
    end
  end
end

Version data entries

23 entries across 23 versions & 1 rubygems

Version Path
flexite-0.0.25 app/services/flexite/entry/array_update_service.rb
flexite-0.0.24 app/services/flexite/entry/array_update_service.rb
flexite-0.0.23 app/services/flexite/entry/array_update_service.rb
flexite-0.0.22 app/services/flexite/entry/array_update_service.rb
flexite-0.0.21 app/services/flexite/entry/array_update_service.rb
flexite-0.0.20 app/services/flexite/entry/array_update_service.rb
flexite-0.0.19 app/services/flexite/entry/array_update_service.rb
flexite-0.0.18 app/services/flexite/entry/array_update_service.rb
flexite-0.0.17 app/services/flexite/entry/array_update_service.rb
flexite-0.0.16 app/services/flexite/entry/array_update_service.rb
flexite-0.0.15 app/services/flexite/entry/array_update_service.rb
flexite-0.0.14 app/services/flexite/entry/array_update_service.rb
flexite-0.0.13 app/services/flexite/entry/array_update_service.rb
flexite-0.0.12 app/services/flexite/entry/array_update_service.rb
flexite-0.0.11 app/services/flexite/entry/array_update_service.rb
flexite-0.0.10 app/services/flexite/entry/array_update_service.rb
flexite-0.0.9 app/services/flexite/entry/array_update_service.rb
flexite-0.0.8 app/services/flexite/entry/array_update_service.rb
flexite-0.0.7 app/services/flexite/entry/array_update_service.rb
flexite-0.0.6 app/services/flexite/entry/array_update_service.rb