Sha256: 58aafc2e470ebdf87fe73876467c3688b3913bee76ec42331d1de9593795030a

Contents?: true

Size: 929 Bytes

Versions: 36

Compression:

Stored size: 929 Bytes

Contents

module ForestLiana
  class HasManyDissociator
    def initialize(resource, association, params)
      @resource = resource
      @association = association
      @params = params
      @with_deletion = @params[:delete].to_s == 'true'
      @data = params['data']
    end

    def perform
      @record = @resource.find(@params[:id])
      associated_records = @resource.find(@params[:id]).send(@association.name)

      remove_association = !@with_deletion || @association.macro == :has_and_belongs_to_many

      if remove_association
        if @data.is_a?(Array)
          @data.each do |record_deleted|
            associated_records.delete(@association.klass.find(record_deleted[:id]))
          end
        end
      end

      if @with_deletion
        if @data.is_a?(Array)
          record_ids = @data.map { |record| record[:id] }
          @association.klass.destroy(record_ids)
        end
      end
    end
  end
end

Version data entries

36 entries across 36 versions & 1 rubygems

Version Path
forest_liana-2.11.10 app/services/forest_liana/has_many_dissociator.rb
forest_liana-2.11.9 app/services/forest_liana/has_many_dissociator.rb
forest_liana-2.11.8 app/services/forest_liana/has_many_dissociator.rb
forest_liana-2.11.7 app/services/forest_liana/has_many_dissociator.rb
forest_liana-2.11.6 app/services/forest_liana/has_many_dissociator.rb
forest_liana-2.11.5 app/services/forest_liana/has_many_dissociator.rb
forest_liana-2.11.4 app/services/forest_liana/has_many_dissociator.rb
forest_liana-2.11.3 app/services/forest_liana/has_many_dissociator.rb
forest_liana-2.11.2 app/services/forest_liana/has_many_dissociator.rb
forest_liana-2.11.1 app/services/forest_liana/has_many_dissociator.rb
forest_liana-2.11.0 app/services/forest_liana/has_many_dissociator.rb
forest_liana-2.10.5 app/services/forest_liana/has_many_dissociator.rb
forest_liana-2.10.4 app/services/forest_liana/has_many_dissociator.rb
forest_liana-2.10.3 app/services/forest_liana/has_many_dissociator.rb
forest_liana-2.10.2 app/services/forest_liana/has_many_dissociator.rb
forest_liana-2.10.1 app/services/forest_liana/has_many_dissociator.rb
forest_liana-2.10.0 app/services/forest_liana/has_many_dissociator.rb
forest_liana-2.9.2 app/services/forest_liana/has_many_dissociator.rb
forest_liana-2.9.1 app/services/forest_liana/has_many_dissociator.rb
forest_liana-2.9.0 app/services/forest_liana/has_many_dissociator.rb